Skip to content

Commit

Permalink
Combine two methods into doIndexQuery
Browse files Browse the repository at this point in the history
Change-Id: Iba5c32d39de390374a5492c8c15089c0f530a16a
  • Loading branch information
Linary committed Mar 27, 2019
1 parent a565f26 commit 7d49b57
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.query.ConditionQuery;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.InsertionOrderUtil;
import com.google.common.collect.ImmutableSet;

public final class IdHolder {

Expand All @@ -35,23 +37,26 @@ public final class IdHolder {
private Set<Id> ids;

/**
* For non-paged situation
* For non-paging situation
*/
public IdHolder(Set<Id> ids) {
this.query = null;
this.idsFetcher = null;
this.exhausted = false;
this.ids = ids;
if (ids instanceof ImmutableSet) {
this.ids = InsertionOrderUtil.newSet(ids);
} else {
this.ids = ids;
}
}

/**
* For paged situation
* For paging situation
*/
public IdHolder(ConditionQuery query,
Function<ConditionQuery, PageIds> idsFetcher) {
E.checkArgument(query.paging(),
"Query '%s' must carry the page info in paged mode",
query);
"Query '%s' must include page info", query);
this.query = query.copy();
this.idsFetcher = idsFetcher;
this.exhausted = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public boolean paging() {
public boolean add(IdHolder holder) {
E.checkArgument(this.paging == holder.paging(),
"The IdHolder to be linked must be " +
"IdHolder in same paging mode");
"in same paging mode");
if (this.paging || this.isEmpty()) {
super.add(holder);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ private static Set<Id> limit(Set<Id> ids, Query query) {
private interface QueryHolder {

/**
* For non-paged situation
* For non-paging situation
* @return BackendEntry iterator
*/
public Iterator<BackendEntry> iterator();

/**
* For paged situation
* For paging situation
* @param index position IdHolder(Query)
* @param page set query page
* @param pageSize set query page size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,7 @@ private List<IdHolder> queryByLabel(ConditionQuery query) {
indexQuery.offset(query.offset());
indexQuery.capacity(query.capacity());

IdHolder idHolder;
if (query.paging()) {
idHolder = this.doIndexQueryInPage(il, indexQuery);
} else {
idHolder = this.doIndexQuery(il, indexQuery);
}
IdHolder idHolder = this.doIndexQuery(il, indexQuery);
List<IdHolder> holders = new IdHolderList(query.paging());
holders.add(idHolder);
return holders;
Expand All @@ -321,7 +316,7 @@ private List<IdHolder> queryByUserprop(ConditionQuery query) {
long idsSize = 0;
for (MatchedIndex index : indexes) {
if (paging && index.indexLabels().size() > 1) {
throw new NotSupportException("joint index query in page mode");
throw new NotSupportException("joint index query in paging");
}

if (index.containsSearchIndex()) {
Expand Down Expand Up @@ -351,7 +346,7 @@ private List<IdHolder> doSearchIndex(ConditionQuery query,
IndexQueries queries = index.constructIndexQueries(q);
assert !query.paging() || queries.size() <= 1;
IdHolder holder = this.doIndexQueries(queries);
// NOTE: ids will be merged into one IdHolder if not in paging mode
// NOTE: ids will be merged into one IdHolder if not in paging
holders.add(holder);
}
return holders;
Expand All @@ -372,11 +367,7 @@ private IdHolder doSingleOrCompositeIndex(IndexQueries queries) {
Map.Entry<IndexLabel, ConditionQuery> e = queries.one();
IndexLabel indexLabel = e.getKey();
ConditionQuery query = e.getValue();
if (query.paging()) {
return this.doIndexQueryInPage(indexLabel, query);
} else {
return this.doIndexQuery(indexLabel, query);
}
return this.doIndexQuery(indexLabel, query);
}

@Watched(prefix = "index")
Expand Down Expand Up @@ -454,8 +445,20 @@ private boolean needIndexForLabel() {
}

@Watched(prefix = "index")
private IdHolder doIndexQuery(IndexLabel indexLabel,
ConditionQuery query) {
private IdHolder doIndexQuery(IndexLabel indexLabel, ConditionQuery query) {
if (!query.paging()) {
PageIds pageIds = this.doIndexQueryOnce(indexLabel, query);
return new IdHolder(pageIds.ids());
} else {
return new IdHolder(query, (q) -> {
return this.doIndexQueryOnce(indexLabel, q);
});
}
}

@Watched(prefix = "index")
private PageIds doIndexQueryOnce(IndexLabel indexLabel,
ConditionQuery query) {
LockUtil.Locks locks = new LockUtil.Locks(this.graph().name());
try {
locks.lockReads(LockUtil.INDEX_LABEL_DELETE, indexLabel.id());
Expand All @@ -471,45 +474,25 @@ private IdHolder doIndexQuery(IndexLabel indexLabel,
break;
}
}
return new IdHolder(ids);
// If there is no data, the entries is not a Metadatable object
if (ids.isEmpty()) {
return PageIds.EMPTY;
}
// NOTE: Memory backend's iterator is not Metadatable
if (!query.paging()) {
return new PageIds(ids, null);
}
E.checkState(entries instanceof Metadatable,
"The entries must be Metadatable when query " +
"in paging, but got '%s'",
entries.getClass().getName());
Object page = ((Metadatable) entries).metadata("page");
return new PageIds(ids, (String) page);
} finally {
locks.unlock();
}
}

@Watched(prefix = "index")
private IdHolder doIndexQueryInPage(IndexLabel indexLabel,
ConditionQuery query) {
return new IdHolder(query, (q) -> {
LockUtil.Locks locks = new LockUtil.Locks(this.graph().name());
try {
locks.lockReads(LockUtil.INDEX_LABEL_DELETE, indexLabel.id());
locks.lockReads(LockUtil.INDEX_LABEL_REBUILD, indexLabel.id());

Set<Id> ids = InsertionOrderUtil.newSet();
Iterator<BackendEntry> entries = super.query(q);
while (entries.hasNext()) {
HugeIndex index = this.serializer.readIndex(graph(), q,
entries.next());
ids.addAll(index.elementIds());
}
// If there is no data, the entries is not a Metadatable object
if (ids.isEmpty()) {
return PageIds.EMPTY;
}

E.checkState(entries instanceof Metadatable,
"The entries must be Metadatable when query " +
"in paging, but got '%s'",
entries.getClass().getName());
Object page = ((Metadatable) entries).metadata("page");
return new PageIds(ids, (String) page);
} finally {
locks.unlock();
}
});
}

@Watched(prefix = "index")
private Set<MatchedIndex> collectMatchedIndexes(ConditionQuery query) {
SchemaTransaction schema = this.graph().schemaTransaction();
Expand Down

0 comments on commit 7d49b57

Please sign in to comment.