|
16 | 16 | */
|
17 | 17 | package software.amazon.documentdb.jdbc.calcite.adapter;
|
18 | 18 |
|
19 |
| -import lombok.SneakyThrows; |
20 | 19 | import org.apache.calcite.linq4j.Enumerator;
|
21 |
| -import org.apache.calcite.linq4j.function.Function1; |
22 |
| -import org.apache.calcite.linq4j.tree.Primitive; |
23 |
| -import org.bson.Document; |
24 |
| -import org.bson.types.Binary; |
25 |
| -import software.amazon.documentdb.jdbc.metadata.DocumentDbSchemaTable; |
26 | 20 |
|
27 |
| -import java.util.Date; |
28 |
| -import java.util.Iterator; |
29 |
| -import java.util.List; |
30 |
| -import java.util.Map; |
31 |
| -import java.util.Map.Entry; |
32 |
| -import java.util.stream.Collectors; |
33 |
| - |
34 |
| -/** Enumerator that reads from a MongoDB collection. */ |
| 21 | +/** Implements the enumerator interface but does not return data. */ |
35 | 22 | class DocumentDbEnumerator implements Enumerator<Object> {
|
36 |
| - private final Iterator<Document> cursor; |
37 |
| - private final Function1<Document, Object> getter; |
38 |
| - private Object current; |
39 | 23 |
|
40 |
| - /** Creates a DocumentDbEnumerator. |
41 |
| - * |
42 |
| - * @param cursor Mongo iterator (usually a {@link com.mongodb.client.MongoCursor}) |
43 |
| - * @param getter Converts an object into a list of fields |
44 |
| - */ |
45 |
| - DocumentDbEnumerator(final Iterator<Document> cursor, |
46 |
| - final Function1<Document, Object> getter) { |
47 |
| - this.cursor = cursor; |
48 |
| - this.getter = getter; |
49 |
| - } |
| 24 | + /** Creates a DocumentDbEnumerator. */ |
| 25 | + DocumentDbEnumerator() { } |
50 | 26 |
|
51 | 27 | @Override public Object current() {
|
52 |
| - return current; |
| 28 | + return null; |
53 | 29 | }
|
54 | 30 |
|
55 | 31 | @Override public boolean moveNext() {
|
56 |
| - try { |
57 |
| - if (cursor.hasNext()) { |
58 |
| - final Document map = cursor.next(); |
59 |
| - current = getter.apply(map); |
60 |
| - return true; |
61 |
| - } else { |
62 |
| - current = null; |
63 |
| - return false; |
64 |
| - } |
65 |
| - } catch (Exception e) { |
66 |
| - throw new RuntimeException(e); |
67 |
| - } |
| 32 | + return false; |
68 | 33 | }
|
69 | 34 |
|
70 | 35 | @Override public void reset() {
|
71 | 36 | throw new UnsupportedOperationException();
|
72 | 37 | }
|
73 | 38 |
|
74 |
| - @SneakyThrows |
75 |
| - @Override public void close() { |
76 |
| - if (cursor instanceof AutoCloseable) { |
77 |
| - ((AutoCloseable) cursor).close(); |
78 |
| - } |
79 |
| - // AggregationOutput implements Iterator but not DBCursor. There is no |
80 |
| - // available close() method -- apparently there is no open resource. |
81 |
| - } |
82 |
| - |
83 |
| - static Function1<Document, Map> mapGetter() { |
84 |
| - return a0 -> (Map) a0; |
85 |
| - } |
86 |
| - |
87 |
| - /** Returns a function that projects a single field. */ |
88 |
| - static Function1<Document, Object> singletonGetter(final String fieldPath, |
89 |
| - final Class fieldClass, |
90 |
| - final DocumentDbSchemaTable tableMetadata) { |
91 |
| - // DocumentDB: modified - start |
92 |
| - return a0 -> getField(a0, fieldPath, fieldClass); |
93 |
| - // DocumentDB: modified - end |
94 |
| - } |
95 |
| - |
96 |
| - /** Returns a function that projects fields. |
97 |
| - * |
98 |
| - * @param fields List of fields to project; or null to return map |
99 |
| - */ |
100 |
| - static Function1<Document, Object[]> listGetter( |
101 |
| - final List<Entry<String, Class<?>>> fields, |
102 |
| - final DocumentDbSchemaTable tableMetadata) { |
103 |
| - return a0 -> { |
104 |
| - // DocumentDB: modified - start |
105 |
| - return fields |
106 |
| - .stream() |
107 |
| - .map(field -> { |
108 |
| - final String path = field.getKey(); |
109 |
| - return getField(a0, path, field.getValue()); |
110 |
| - }) |
111 |
| - .toArray(); |
112 |
| - // DocumentDB: modified - end |
113 |
| - }; |
114 |
| - } |
115 |
| - |
116 |
| - @SuppressWarnings("unchecked") |
117 |
| - static Function1<Document, Object> getter( |
118 |
| - final List<Entry<String, Class<?>>> fields, |
119 |
| - final DocumentDbSchemaTable tableMetadata) { |
120 |
| - //noinspection unchecked |
121 |
| - // DocumentDB: modified - start |
122 |
| - return fields == null |
123 |
| - ? (Function1) mapGetter() |
124 |
| - : fields.size() == 1 |
125 |
| - ? singletonGetter(fields.get(0).getKey(), fields.get(0).getValue(), |
126 |
| - tableMetadata) |
127 |
| - : (Function1) listGetter(fields, tableMetadata); |
128 |
| - // DocumentDB: modified - end |
129 |
| - } |
130 |
| - |
131 |
| - @SuppressWarnings("JdkObsolete") |
132 |
| - private static Object convert(final Object o, final Class clazz) { |
133 |
| - // DocumentDB: modified - start |
134 |
| - Object sourceObject = o; |
135 |
| - // DocumentDB: modified - end |
136 |
| - Class sourceClazz = clazz; |
137 |
| - if (sourceObject == null) { |
138 |
| - return null; |
139 |
| - } |
140 |
| - Primitive primitive = Primitive.of(sourceClazz); |
141 |
| - if (primitive != null) { |
142 |
| - sourceClazz = primitive.boxClass; |
143 |
| - } else { |
144 |
| - primitive = Primitive.ofBox(sourceClazz); |
145 |
| - } |
146 |
| - if (sourceClazz.isInstance(sourceObject)) { |
147 |
| - return sourceObject; |
148 |
| - } |
149 |
| - if (sourceObject instanceof Date && primitive != null) { |
150 |
| - // DocumentDB: modified - begin |
151 |
| - sourceObject = ((Date) sourceObject).getTime(); |
152 |
| - // DocumentDB: modified - end |
153 |
| - } |
154 |
| - if (sourceObject instanceof Number && primitive != null) { |
155 |
| - return primitive.number((Number) sourceObject); |
156 |
| - } |
157 |
| - // DocumentDB: modified - begin |
158 |
| - if (sourceObject instanceof Binary) { |
159 |
| - return ((Binary) sourceObject).getData(); |
160 |
| - } |
161 |
| - if (sourceObject instanceof Document) { |
162 |
| - return ((Document) sourceObject).toJson(); |
163 |
| - } |
164 |
| - if (sourceObject instanceof List) { |
165 |
| - return ((List<?>) sourceObject) |
166 |
| - .stream() |
167 |
| - .map(o1 -> o1 instanceof Document ? ((Document) o1).toJson() : o1) |
168 |
| - .collect(Collectors.toList()); |
169 |
| - } |
170 |
| - // DocumentDB: modified - end |
171 |
| - return sourceObject; |
172 |
| - } |
173 |
| - |
174 |
| - private static Object getField( |
175 |
| - final Document a0, |
176 |
| - final String path, |
177 |
| - final Class<?> fieldClass) |
178 |
| - throws UnsupportedOperationException { |
179 |
| - final String[] segmentedPath = path.split("\\."); |
180 |
| - int j = 0; |
181 |
| - Object segmentValue = a0.get(segmentedPath[j]); |
182 |
| - while (segmentValue instanceof Document) { |
183 |
| - final Document document = (Document) segmentValue; |
184 |
| - j++; |
185 |
| - if (j >= segmentedPath.length) { |
186 |
| - break; |
187 |
| - } |
188 |
| - segmentValue = document.get(segmentedPath[j]); |
189 |
| - } |
190 |
| - return convert(segmentValue, fieldClass); |
191 |
| - } |
| 39 | + @Override public void close() { } |
192 | 40 | }
|
0 commit comments