Skip to content

Commit

Permalink
hugegraph-1329 fix bug label index can't work with offset and limit
Browse files Browse the repository at this point in the history
Change-Id: I388bb75140a6fb75f8c98545a3114af14da3c1b4
  • Loading branch information
zhoney authored and Linary committed Aug 9, 2018
1 parent b7f2bdc commit f49d72e
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ public void offset(long offset) {
this.offset = offset;
}

public long total() {
if (this.limit == NO_LIMIT) {
return NO_LIMIT;
} else {
return this.offset + this.limit;
}
}

public long limit() {
return this.limit;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,10 @@ private Query writeSecondaryIndexQuery(ConditionQuery query) {
E.checkArgument(key != null, "Please specify the index key");

Id id = formatIndexId(query.resultType(), index, key);
return new IdQuery(query, id);
IdQuery idQuery = new IdQuery(query, id);
idQuery.limit(query.limit());
idQuery.offset(query.offset());
return idQuery;
}

private Query writeRangeIndexQuery(ConditionQuery query) {
Expand Down Expand Up @@ -586,7 +589,10 @@ private Query writeRangeIndexQuery(ConditionQuery query) {
HugeType type = query.resultType();
if (keyEq != null) {
Id id = formatIndexId(type, index, keyEq);
return new IdQuery(query, id);
IdQuery idQuery = new IdQuery(query, id);
idQuery.limit(query.limit());
idQuery.offset(query.offset());
return idQuery;
}

if (keyMin == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.type.define.HugeKeys;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.InsertionOrderUtil;

public class InMemoryDBTable extends BackendTable<BackendSession,
TextBackendEntry> {
Expand Down Expand Up @@ -159,7 +160,7 @@ public Iterator<BackendEntry> query(BackendSession session, Query query) {
protected Map<Id, BackendEntry> queryById(Set<Id> ids,
Map<Id, BackendEntry> entries) {
assert ids.size() > 0;
Map<Id, BackendEntry> rs = new HashMap<>();
Map<Id, BackendEntry> rs = InsertionOrderUtil.newMap();

for (Id id : ids) {
assert !id.number();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ public Iterator<BackendEntry> query(BackendSession session, Query query) {
assert fieldValue != null && indexLabelId != null;
Id id = SplicingIdGenerator.splicing(fieldValue, indexLabelId);
IdQuery q = new IdQuery(query, id);
q.offset(query.offset());
q.limit(query.limit());
return super.query(session, q);
}
}
Expand Down Expand Up @@ -257,6 +259,8 @@ public Iterator<BackendEntry> query(BackendSession session, Query query) {
Id id = HugeIndex.formatIndexId(HugeType.RANGE_INDEX,
indexLabelId, keyEq);
IdQuery q = new IdQuery(query, id);
q.offset(query.offset());
q.limit(query.limit());
return super.query(session, q);
} else {
if (keyMin == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -939,9 +939,11 @@ public IndexQueries constructQueries(ConditionQuery query) {
assert indexQuery != null;
/*
* Set limit for single index or composite index
* to avoid redundant element ids
* to avoid redundant element ids.
* Not set offset because this query might be a sub-query,
* see queryByUserprop()
*/
indexQuery.limit(query.limit());
indexQuery.limit(query.total());
IndexQueries indexQueries = new IndexQueries();
indexQueries.put(il, indexQuery);
return indexQueries;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2762,6 +2762,73 @@ public void testQueryByPageWithOffset() {
});
}

@Test
public void testQueryBySecondaryIndexWithLimitAndOffset() {
init5Persons();

List<Vertex> vertices = graph().traversal().V()
.has("city", "Beijing").toList();
Assert.assertEquals(3, vertices.size());

List<Vertex> vertices1 = graph().traversal().V()
.has("city", "Beijing")
.range(0, 2).toList();
Assert.assertEquals(2, vertices1.size());

List<Vertex> vertices2 = graph().traversal().V()
.has("city", "Beijing")
.range(2, 3).toList();
Assert.assertEquals(1, vertices2.size());

vertices1.addAll(vertices2);
Assert.assertEquals(vertices.size(), vertices1.size());
Assert.assertTrue(vertices.containsAll(vertices1));
}

@Test
public void testQueryByRangeIndexWithLimitAndOffset() {
init5Persons();

List<Vertex> vertices = graph().traversal().V()
.has("age", P.between(5, 22)).toList();
Assert.assertEquals(4, vertices.size());
List<Vertex> vertices1 = graph().traversal().V()
.has("age", P.between(5, 22))
.range(0, 3).toList();
Assert.assertEquals(3, vertices1.size());
List<Vertex> vertices2 = graph().traversal().V()
.has("age", P.between(5, 22))
.range(3, 4).toList();
Assert.assertEquals(1, vertices2.size());

vertices1.addAll(vertices2);
Assert.assertEquals(vertices.size(), vertices1.size());
Assert.assertTrue(vertices.containsAll(vertices1));
}

@Test
public void testQueryByLabelIndexWithLimitAndOffset() {
init5Persons();

List<Vertex> vertices = graph().traversal().V().hasLabel("person")
.toList();
Assert.assertEquals(5, vertices.size());

List<Vertex> vertices1 = graph().traversal().V()
.hasLabel("person")
.range(0, 3).toList();
Assert.assertEquals(3, vertices1.size());

List<Vertex> vertices2 = graph().traversal().V()
.hasLabel("person")
.range(3, 5).toList();
Assert.assertEquals(2, vertices2.size());

vertices1.addAll(vertices2);
Assert.assertEquals(vertices.size(), vertices1.size());
Assert.assertTrue(vertices.containsAll(vertices1));
}

private void init10Vertices() {
HugeGraph graph = graph();

Expand Down

0 comments on commit f49d72e

Please sign in to comment.