Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug: query vertices by ids with hasId() filter when using number id #304

Merged
merged 1 commit into from
Dec 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ public int hashCode() {

@Override
public boolean equals(Object other) {
if (!(other instanceof LongId)) {
if (!(other instanceof Number)) {
return false;
}
return this.id == ((LongId) other).id;
return this.id == ((Number) other).longValue();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,25 +362,31 @@ public static Condition convIn2Relation(HugeGraph graph,
assert bp instanceof Contains;
List<?> values = (List<?>) has.getValue();

String originKey = has.getKey();
if (values.size() > 1) {
E.checkArgument(!originKey.equals(T.key) &&
!originKey.equals(T.value),
"Not support hasKey() or hasValue() with " +
"multiple values");
}

HugeKeys hugeKey = null;
try {
String originKey = has.getKey();
if (values.size() > 1) {
E.checkArgument(!originKey.equals(T.key) &&
!originKey.equals(T.value),
"Not support hasKey() or hasValue() with " +
"multiple values");
}
HugeKeys key = string2HugeKey(originKey);
values = convSysListValueIfNeeded(graph, type, key, values);
hugeKey = string2HugeKey(originKey);
} catch (IllegalArgumentException ignored) {
// Ignore
}

if (hugeKey != null) {
values = convSysListValueIfNeeded(graph, type, hugeKey, values);

switch ((Contains) bp) {
case within:
return Condition.in(key, values);
return Condition.in(hugeKey, values);
case without:
return Condition.nin(key, values);
return Condition.nin(hugeKey, values);
}
} catch (IllegalArgumentException e) {
} else {
String key = has.getKey();
PropertyKey pkey = graph.propertyKey(key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,11 +479,14 @@ public RowIterator(ResultScanner resultScanner) {

public RowIterator(Result... results) {
this.resultScanner = null;
if (results.length == 1 && results[0].isEmpty()) {
this.results = Collections.emptyIterator();
} else {
this.results = Arrays.asList(results).iterator();
List<Result> rs = new ArrayList<>(results.length);
for (Result result : results) {
// Get by Ids may return empty result
if (!result.isEmpty()) {
rs.add(result);
}
}
this.results = rs.iterator();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3302,6 +3302,101 @@ public void testAddCustomizedIdVerticesContainsExisted() {
});
}

@Test
public void testQueryVerticesByIdsWithHasIdFilterAndNumberId() {
HugeGraph graph = graph();
SchemaManager schema = graph.schema();

schema.vertexLabel("user").useCustomizeNumberId().create();

graph.addVertex(T.label, "user", T.id, 123);
graph.addVertex(T.label, "user", T.id, 456);
graph.addVertex(T.label, "user", T.id, 789);
graph.tx().commit();

GraphTraversalSource g = graph.traversal();
List<Vertex> vertices;

vertices = g.V().hasId(P.within(123)).toList();
Assert.assertEquals(1, vertices.size());

vertices = g.V(123, 456).hasId(P.within(123)).toList();
Assert.assertEquals(1, vertices.size());

vertices = g.V(123, 456).hasId(123).toList();
Assert.assertEquals(1, vertices.size());

vertices = g.V(123, 456, 789).hasId(P.within(123, 456)).toList();
Assert.assertEquals(2, vertices.size());

vertices = g.V(123, 456, 789).hasId(456, 789).toList();
Assert.assertEquals(2, vertices.size());

vertices = g.V(123, 456, 789).hasId(P.within(123, 456, 789)).toList();
Assert.assertEquals(3, vertices.size());
}

@Test
public void testQueryVerticesByLabelsWithOneLabelNotExist() {
HugeGraph graph = graph();
SchemaManager schema = graph.schema();

schema.vertexLabel("user1").useCustomizeNumberId().create();
schema.vertexLabel("user2").useCustomizeNumberId().create();

graph.addVertex(T.label, "user1", T.id, 123);
graph.addVertex(T.label, "user2", T.id, 456);
graph.addVertex(T.label, "user2", T.id, 789);
graph.tx().commit();

GraphTraversalSource g = graph.traversal();
List<Vertex> vertices;

vertices = g.V().hasLabel("user1").toList();
Assert.assertEquals(1, vertices.size());

vertices = g.V().hasLabel("user2").toList();
Assert.assertEquals(2, vertices.size());

vertices = g.V().hasLabel("user1", "user2").toList();
Assert.assertEquals(3, vertices.size());

Assert.assertThrows(IllegalArgumentException.class, () -> {
g.V().hasLabel("user3").toList();
}, e -> {
Assert.assertEquals("Undefined vertex label: 'user3'",
e.getMessage());
});

Assert.assertThrows(IllegalArgumentException.class, () -> {
g.V().hasLabel("user1", "user3").toList();
}, e -> {
Assert.assertEquals("Undefined vertex label: 'user3'",
e.getMessage());
});

Assert.assertThrows(IllegalArgumentException.class, () -> {
g.V().hasLabel("user3", "user1").toList();
}, e -> {
Assert.assertEquals("Undefined vertex label: 'user3'",
e.getMessage());
});

Assert.assertThrows(IllegalArgumentException.class, () -> {
g.V().hasLabel("user3", "user4").toList();
}, e -> {
Assert.assertEquals("Undefined vertex label: 'user3'",
e.getMessage());
});

Assert.assertThrows(IllegalArgumentException.class, () -> {
g.V().hasLabel("user4", "user3").toList();
}, e -> {
Assert.assertEquals("Undefined vertex label: 'user4'",
e.getMessage());
});
}

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

Expand Down