44import org .dizitart .no2 .collection .Document ;
55import org .dizitart .no2 .collection .NitriteId ;
66import org .dizitart .no2 .collection .operation .FindOptions ;
7- import org .dizitart .no2 .common .FieldValues ;
8- import org .dizitart .no2 .common .Fields ;
9- import org .dizitart .no2 .common .RecordStream ;
10- import org .dizitart .no2 .common .SortOrder ;
7+ import org .dizitart .no2 .common .*;
118import org .dizitart .no2 .common .tuples .Pair ;
129import org .dizitart .no2 .index .IndexDescriptor ;
1310import org .dizitart .no2 .index .IndexType ;
@@ -26,27 +23,27 @@ public class QueryOptimizer {
2623 private final NitriteConfig nitriteConfig ;
2724
2825 /*
29- * 1. If And filter
30- * 1.1 flatten the filter using depth first traversal
31- * 1.2 check each filter, group OR & AND, single field filters
32- * 1.3 scan through single field filter or and filter if there is any match for composite index
33- * 1.3.1 if matching composite index found, get index stream
34- * 1.3.2 group remaining filters as a new and filter and apply on indexed stream from 1.3.1
35- * 1.4 if no matching composite index found, scan for simple index
36- * 1.4.1 if found, get indexed stream
37- * 1.4.2 group remaining filter as a new and filter and apply on index stream from 1.4.1
38- * 1.5 if no matching index found, collscan and apply filter
39- *
40- * 2. If OR filter
41- * 1.1 If every simple field is indexed or and filter composite indexed
42- * 1.1.1
43- * 1.2 If one of the fields is not indexed, get collscan and apply filter
44- *
45- * 3. If simple filter
46- * 3.1 Check if index exists, send indexed stream
47- * 3.2 If no index found, collscan
48- *
49- * */
26+ * 1. If And filter
27+ * 1.1 flatten the filter using depth first traversal
28+ * 1.2 check each filter, group OR & AND, single field filters
29+ * 1.3 scan through single field filter or and filter if there is any match for composite index
30+ * 1.3.1 if matching composite index found, get index stream
31+ * 1.3.2 group remaining filters as a new and filter and apply on indexed stream from 1.3.1
32+ * 1.4 if no matching composite index found, scan for simple index
33+ * 1.4.1 if found, get indexed stream
34+ * 1.4.2 group remaining filter as a new and filter and apply on index stream from 1.4.1
35+ * 1.5 if no matching index found, collscan and apply filter
36+ *
37+ * 2. If OR filter
38+ * 1.1 If every simple field is indexed or and filter composite indexed
39+ * 1.1.1
40+ * 1.2 If one of the fields is not indexed, get collscan and apply filter
41+ *
42+ * 3. If simple filter
43+ * 3.1 Check if index exists, send indexed stream
44+ * 3.2 If no index found, collscan
45+ *
46+ * */
5047
5148 public QueryOptimizer (NitriteConfig nitriteConfig ) {
5249 this .nitriteConfig = nitriteConfig ;
@@ -55,115 +52,29 @@ public QueryOptimizer(NitriteConfig nitriteConfig) {
5552 public FilterStep optimizeFilter (NitriteMap <NitriteId , Document > primaryCollection ,
5653 Filter filter ,
5754 FindOptions findOptions ) {
58-
55+ return null ;
5956 }
6057
6158 private void flattenFilter (String collectionName , Filter filter ) {
6259 if (filter instanceof AndFilter ) {
6360 List <Filter > flattenedFilter = ((AndFilter ) filter ).getFilters ();
64- Pair <Filter , Filter > optimizedAnd = optimize (collectionName , flattenedFilter );
61+ Pair <Filter , Filter > optimizedAnd = optimizeAnd (collectionName , flattenedFilter );
6562 }
6663
6764 }
6865
69- private Pair <Filter , Filter > optimize (String collectionName , List <Filter > flattenedFilter ) {
66+ private Pair <Filter , Filter > optimizeAnd (String collectionName , List <Filter > flattenedFilter ) {
7067 IndexCatalog indexCatalog = nitriteConfig .getNitriteStore ().getIndexCatalog ();
71- for (int i = 0 ; i < flattenedFilter .size (); i ++) {
72- List <Filter > filters = flattenedFilter .subList (0 , i + 1 );
73- Fields fields = getFields (filters );
74- }
75-
76-
77- for (Filter filter : flattenedFilter ) {
78- if (filter instanceof FieldBasedFilter ) {
79- FieldBasedFilter fieldBasedFilter = (FieldBasedFilter ) filter ;
80- Fields fields = Fields .single (fieldBasedFilter .getField ());
81- Set <IndexDescriptor > descriptors = indexCatalog .findMatchingIndexDescriptor (collectionName , fields );
82- if (descriptors != null ) {
83-
84- }
85- }
86- if (filter instanceof AndFilter ) {
87-
88- }
89- }
90-
91- // TODO: use fieldNames as object to create cache and compare
68+ Set <IndexedFieldNames > indexedFieldNames = indexCatalog .findIndexSupportedFields (collectionName );
69+ Set <FilterFieldNames > filterFieldNames = getFieldNames (flattenedFilter );
9270
9371
9472 return null ;
9573 }
9674
97- private Fields getFields (List <Filter > filters ) {
98- Fields fields = new Fields ();
99- for (Filter filter : filters ) {
100- if (filter instanceof FieldBasedFilter ) {
101- FieldBasedFilter fieldBasedFilter = (FieldBasedFilter ) filter ;
102- fields .getSortSpecs ().add (new Pair <>(fieldBasedFilter .getField (), SortOrder .Ascending ));
103- }
104- }
75+ private Set <FilterFieldNames > getFieldNames (List <Filter > filters ) {
10576 return null ;
10677 }
107-
108- private void optimize2 (String collectionName , List <Filter > flattenedFilter ) {
109- IndexCatalog indexCatalog = nitriteConfig .getNitriteStore ().getIndexCatalog ();
110- Map <IndexDescriptor , List <Filter >> group = new HashMap <>();
111-
112- for (int i = 0 ; i < flattenedFilter .size (); i ++) {
113- Filter filter = flattenedFilter .get (0 );
114- if (filter instanceof FieldBasedFilter ) {
115- FieldBasedFilter fieldBasedFilter = (FieldBasedFilter ) filter ;
116- Fields fields = Fields .single (fieldBasedFilter .getField ());
117- IndexDescriptor indexDescriptor = indexCatalog .findIndexDescriptorExact (collectionName , fields );
118- if (indexDescriptor != null ) {
119- List <Filter > filters = group .get (indexDescriptor );
120- if (filters == null ) {
121- filters = new ArrayList <>();
122- }
123- filters .add (filter );
124- group .put (indexDescriptor , filters );
125- }
126- }
127- }
128- }
129-
130-
131-
132-
133-
134-
135-
136-
137-
138-
139-
140-
141-
142-
143-
144-
145-
146-
147-
148-
149-
150-
151-
152-
153-
154-
155-
156-
157-
158-
159-
160-
161-
162-
163-
164-
165-
166-
16778
16879
16980
0 commit comments