Skip to content

Commit c02c3bb

Browse files
committed
work in progress
1 parent c021c64 commit c02c3bb

File tree

3 files changed

+48
-124
lines changed

3 files changed

+48
-124
lines changed

nitrite-mvstore-adapter/src/test/java/org/dizitart/no2/NitriteTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import static org.dizitart.no2.common.Constants.INTERNAL_NAME_SEPARATOR;
6363
import static org.dizitart.no2.common.Constants.META_MAP_NAME;
6464
import static org.dizitart.no2.filters.Filter.ALL;
65+
import static org.dizitart.no2.filters.Filter.and;
6566
import static org.dizitart.no2.filters.FluentFilter.where;
6667
import static org.junit.Assert.*;
6768

@@ -465,21 +466,21 @@ public void testReadCompatibility() throws IOException {
465466
NitriteCollection collection = db.getCollection("test");
466467

467468
// text filter has be the first filter in and clause
468-
List<Document> cursor = collection.find(where("second_key").text("fox")
469-
.and(where("first_key").eq(1))).toList();
469+
List<Document> cursor = collection.find(
470+
and(where("second_key").text("fox"), where("first_key").eq(1))).toList();
470471
assertEquals(cursor.size(), 1);
471472
assertEquals(cursor.get(0).get("third_key"), 0.5);
472473

473474
ObjectRepository<Receipt> repository = db.getRepository(Receipt.class);
474475
ObjectRepository<Receipt> orangeRepository = db.getRepository(Receipt.class, "orange");
475476

476-
List<Receipt> list = repository.find(where("synced").eq(true)
477-
.and(where("status").eq(Receipt.Status.PREPARING.toString()))).toList();
477+
List<Receipt> list = repository.find(
478+
and(where("synced").eq(true), where("status").eq(Receipt.Status.PREPARING.toString()))).toList();
478479
assertEquals(list.size(), 1);
479480
assertEquals(list.get(0).clientRef, "1");
480481

481-
list = orangeRepository.find(where("synced").eq(false)
482-
.and(where("status").eq(Receipt.Status.PREPARING.toString()))).toList();
482+
list = orangeRepository.find(
483+
and(where("synced").eq(false), where("status").eq(Receipt.Status.PREPARING.toString()))).toList();
483484
assertEquals(list.size(), 0);
484485
assertNotNull(repository.getAttributes());
485486

@@ -501,7 +502,7 @@ public void testIssue212() {
501502
}
502503

503504
collection.insert(doc1, doc2);
504-
collection.update(where("key").eq("key").and(where("second_key").eq("second_key")),
505+
collection.update(and(where("key").eq("key"), where("second_key").eq("second_key")),
505506
doc, UpdateOptions.updateOptions(true));
506507

507508
for (Document document : collection.find()) {

nitrite/src/main/java/org/dizitart/no2/filters/QueryOptimizer.java

Lines changed: 28 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
import org.dizitart.no2.collection.Document;
55
import org.dizitart.no2.collection.NitriteId;
66
import 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.*;
118
import org.dizitart.no2.common.tuples.Pair;
129
import org.dizitart.no2.index.IndexDescriptor;
1310
import 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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.dizitart.no2.filters;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* @author Anindya Chatterjee
7+
*/
8+
@Data
9+
public class QueryPlan {
10+
private Filter indexedFilter;
11+
private Filter nonIndexedFilter;
12+
}

0 commit comments

Comments
 (0)