Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
jadepeng committed Jul 5, 2021
1 parent 30ea45d commit 29bb384
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,15 @@ public void resetConditions() {
}

public void recordIndexValue(Id propertyId, Id id, Object indexValue) {
if (this.element2IndexValueMap == null) {
this.element2IndexValueMap = new Element2IndexValueMap();
}
this.ensureElement2IndexValueMap();
this.element2IndexValueMap.addIndexValue(propertyId, id, indexValue);
}

public void selectedIndexField(Id indexField) {
this.ensureElement2IndexValueMap();
this.element2IndexValueMap.selectedIndexField(indexField);
}

public Set<LeftIndex> getElementLeftIndex(Id elementId) {
if (this.element2IndexValueMap == null) {
return null;
Expand Down Expand Up @@ -525,6 +528,24 @@ public void registerResultsFilter(Function<HugeElement, Boolean> filter) {
}
}

public ConditionQuery originConditionQuery() {
Query originQuery = this.originQuery();
if (!(originQuery instanceof ConditionQuery)) {
return null;
}

while (originQuery.originQuery() instanceof ConditionQuery) {
originQuery = originQuery.originQuery();
}
return (ConditionQuery) originQuery;
}

private void ensureElement2IndexValueMap() {
if (this.element2IndexValueMap == null) {
this.element2IndexValueMap = new Element2IndexValueMap();
}
}

public static String concatValues(List<Object> values) {
List<Object> newValues = new ArrayList<>(values.size());
for (Object v : values) {
Expand Down Expand Up @@ -553,7 +574,7 @@ public static final class Element2IndexValueMap {

private final Map<Id, Set<LeftIndex>> leftIndexMap;
private final Map<Id, Map<Id, Set<Object>>> filed2IndexValues;
private Id mainIndexField;
private Id selectedIndexField;

public Element2IndexValueMap() {
this.filed2IndexValues = new HashMap<>();
Expand All @@ -575,6 +596,10 @@ public void addIndexValue(Id indexField, Id elementId,
}
}

public void selectedIndexField(Id indexField) {
this.selectedIndexField = indexField;
}

public Set<Object> getIndexValues(Id indexField, Id elementId) {
if (!this.filed2IndexValues.containsKey(indexField)) {
return null;
Expand Down Expand Up @@ -628,27 +653,17 @@ public boolean validRangeIndex(HugeElement element, Condition cond) {
this.addLeftIndex(propId, fieldValues, element.id());
}

if (this.mainIndexField == null) {
this.initMainIndexFiled();
}

/*
* NOTE: If we query by more than one range index field,
* actually read data from one index, so the left index field's
* index not read completely, so can't judgment correct or not
* NOTE: When query by more than one range index field,
* if current field is not the selected one, it can only be used to
* determine whether the index values matched, can't determine
* the element is valid or not
*/
return !propId.equals(this.mainIndexField) || hasRightValue;
}

private void initMainIndexFiled() {
int maxElementSize = 0;
for (Id indexField : this.filed2IndexValues.keySet()) {
int elementSize = this.filed2IndexValues.get(indexField).size();
if (elementSize > maxElementSize) {
this.mainIndexField = indexField;
maxElementSize = elementSize;
}
if (this.selectedIndexField != null) {
return !propId.equals(this.selectedIndexField) || hasRightValue;
}

return hasRightValue;
}

private static boolean removeValue(Set<Object> values, Object value){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ private IdHolder doJointIndex(IndexQueries queries) {
IdHolder holder = this.doIndexQuery(indexLabel, query);
if (resultHolder == null) {
resultHolder = holder;
this.storeSelectedIndexField(indexLabel, query);
}
assert this.indexIntersectThresh > 0; // default value is 1000
Set<Id> ids = ((BatchIdHolder) holder).peekNext(
Expand All @@ -554,6 +555,7 @@ private IdHolder doJointIndex(IndexQueries queries) {
} else if (filtering) {
assert ids.size() < this.indexIntersectThresh;
resultHolder = holder;
this.storeSelectedIndexField(indexLabel, query);
break;
} else {
if (intersectIds == null) {
Expand All @@ -575,6 +577,20 @@ private IdHolder doJointIndex(IndexQueries queries) {
}
}

private void storeSelectedIndexField(IndexLabel indexLabel,
ConditionQuery query) {
// Only store range index field
if (!indexLabel.indexType().isRange()) {
return;
}

ConditionQuery originConditionQuery =
query.originConditionQuery();
if (originConditionQuery != null) {
originConditionQuery.selectedIndexField(indexLabel.indexField());
}
}

@Watched(prefix = "index")
private IdHolder doIndexQuery(IndexLabel indexLabel, ConditionQuery query) {
if (!query.paging()) {
Expand Down Expand Up @@ -628,22 +644,14 @@ private void recordIndexValue(ConditionQuery query, HugeIndex index) {
return;
}

ConditionQuery originQuery = this.getOriginConditionQuery(query);
ConditionQuery originQuery = query.originConditionQuery();
Id fieldId = index.indexLabel().indexField();
for (Id id : index.elementIds()) {
Object value = index.indexLabel().validValue(index.fieldValues());
originQuery.recordIndexValue(fieldId, id, value);
}
}

private ConditionQuery getOriginConditionQuery(ConditionQuery query) {
Query originQuery = query.originQuery();
while (originQuery.originQuery() instanceof ConditionQuery) {
originQuery = originQuery.originQuery();
}
return (ConditionQuery) originQuery;
}

@Watched(prefix = "index")
private PageIds doIndexQueryOnce(IndexLabel indexLabel,
ConditionQuery query) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ public void indexField(Id id) {
}

public Id indexField() {
E.checkState(this.indexType.isRange() || this.indexType.isSearch(),
"Can't call indexField() for %s index label",
this.indexType.string());
E.checkState(this.indexFields.size() == 1,
"There should be only one field in %s index label, " +
"but got: %s", this.indexType.string(), this.indexFields);
Expand Down

0 comments on commit 29bb384

Please sign in to comment.