Skip to content

Commit

Permalink
unify action of special-symbol in primary-key and sort-key
Browse files Browse the repository at this point in the history
Change-Id: I1cb380396588dbe6aea3d4a8b68a9cdf675bebd7
  • Loading branch information
javeme committed Dec 9, 2021
1 parent 83180fb commit f6d6427
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,12 @@ public <V> Object serialValue(V value, boolean encodeNumber) {
E.checkArgument(this.cardinality.single(),
"The cardinality can't be '%s' for navigation key '%s'",
this.cardinality, this.name());
if (encodeNumber &&
(this.dataType.isNumber() || this.dataType.isDate())) {
return LongEncoding.encodeNumber(validValue);
if (this.dataType.isNumber() || this.dataType.isDate()) {
if (encodeNumber) {
return LongEncoding.encodeNumber(validValue);
} else {
return validValue.toString();
}
}
return validValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Iterator;
import java.util.List;

import org.apache.logging.log4j.util.Strings;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Property;
Expand All @@ -33,7 +34,7 @@
import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.backend.id.EdgeId;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.id.SplicingIdGenerator;
import com.baidu.hugegraph.backend.query.ConditionQuery;
import com.baidu.hugegraph.backend.query.QueryResults;
import com.baidu.hugegraph.backend.serializer.BytesBuffer;
import com.baidu.hugegraph.backend.tx.GraphTransaction;
Expand Down Expand Up @@ -98,7 +99,12 @@ public EdgeLabel schemaLabel() {
@Override
public String name() {
if (this.name == null) {
this.name = SplicingIdGenerator.concatValues(sortValues());
List<Object> sortValues = this.sortValues();
if (sortValues.isEmpty()) {
this.name = Strings.EMPTY;
} else {
this.name = ConditionQuery.concatValues(sortValues);
}
}
return this.name;
}
Expand Down Expand Up @@ -154,7 +160,7 @@ public EdgeId idWithDirection() {
}

@Watched(prefix = "edge")
public List<Object> sortValues() {
protected List<Object> sortValues() {
List<Id> sortKeys = this.schemaLabel().sortKeys();
if (sortKeys.isEmpty()) {
return ImmutableList.of();
Expand All @@ -164,7 +170,11 @@ public List<Object> sortValues() {
HugeProperty<?> property = this.getProperty(sk);
E.checkState(property != null,
"The value of sort key '%s' can't be null", sk);
propValues.add(property.serialValue(true));
Object propValue = property.serialValue(true);
if (Strings.EMPTY.equals(propValue)) {
propValue = ConditionQuery.INDEX_VALUE_EMPTY;
}
propValues.add(propValue);
}
return propValues;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.util.Strings;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.T;
Expand All @@ -42,6 +43,7 @@
import com.baidu.hugegraph.backend.id.IdGenerator;
import com.baidu.hugegraph.backend.id.SnowflakeIdGenerator;
import com.baidu.hugegraph.backend.id.SplicingIdGenerator;
import com.baidu.hugegraph.backend.query.ConditionQuery;
import com.baidu.hugegraph.backend.query.Query;
import com.baidu.hugegraph.backend.query.QueryResults;
import com.baidu.hugegraph.backend.serializer.BytesBuffer;
Expand Down Expand Up @@ -121,7 +123,7 @@ public String name() {
E.checkState(!propValues.isEmpty(),
"Primary values must not be empty " +
"(has properties %s)", hasProperties());
name = SplicingIdGenerator.concatValues(propValues);
name = ConditionQuery.concatValues(propValues);
E.checkArgument(!name.isEmpty(),
"The value of primary key can't be empty");
}
Expand Down Expand Up @@ -193,7 +195,7 @@ public void correctVertexLabel(VertexLabel correctLabel) {
}

@Watched(prefix = "vertex")
public List<Object> primaryValues() {
protected List<Object> primaryValues() {
E.checkArgument(this.label.idStrategy() == IdStrategy.PRIMARY_KEY,
"The id strategy '%s' don't have primary keys",
this.label.idStrategy());
Expand All @@ -210,7 +212,11 @@ public List<Object> primaryValues() {
E.checkState(property != null,
"The value of primary key '%s' can't be null",
this.graph().propertyKey(pk).name());
propValues.add(property.serialValue(encodeNumber));
Object propValue = property.serialValue(encodeNumber);
if (Strings.EMPTY.equals(propValue)) {
propValue = ConditionQuery.INDEX_VALUE_EMPTY;
}
propValues.add(propValue);
}
return propValues;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public void testAddEdgeWithLargeSortkey() {

Assert.assertThrows(IllegalArgumentException.class, () -> {
final int LEN = BytesBuffer.BIG_ID_LEN_MAX;
String largeTime = new String(new byte[LEN]) + "{large-time}";
String largeTime = "{large-time}" + new String(new byte[LEN]);
james.addEdge("write", book, "time", largeTime);
graph.tx().commit();
}, e -> {
Expand All @@ -490,6 +490,14 @@ public void testAddEdgeWithInvalidSortkey() {
Vertex book = graph.addVertex(T.label, "book", "name", "Test-Book-1");
graph.tx().commit();

Assert.assertThrows(IllegalArgumentException.class, () -> {
james.addEdge("write", book, "time", "\u00002017-5-27");
graph.tx().commit();
}, e -> {
Assert.assertContains("Illegal leading char '\\u0' in index",
e.getMessage());
});

String backend = graph.backend();
if (backend.equals("postgresql")) {
Assert.assertThrows(BackendException.class, () -> {
Expand Down Expand Up @@ -2620,6 +2628,9 @@ public void testQueryEdgesByIdWithGraphAPI() {
Object id = graph.traversal().E().toList().get(0).id();
List<Edge> edges = ImmutableList.copyOf(graph.edges(id));
Assert.assertEquals(1, edges.size());

edges = ImmutableList.copyOf(graph.edges(id, id));
Assert.assertEquals(2, edges.size());
}

@Test
Expand All @@ -2630,28 +2641,41 @@ public void testQueryEdgesByIdWithGraphAPIAndNotCommitedUpdate() {
Edge edge = graph.traversal().E().hasLabel("look").toList().get(0);
Object id = edge.id();
Assert.assertTrue(graph.edges(id).hasNext());
Assert.assertTrue(graph.edges(id, id).hasNext());

edge.property("score", 101);

List<Edge> edges = ImmutableList.copyOf(graph.edges(id));
Assert.assertEquals(1, edges.size());
Assert.assertEquals(101, (int) edges.get(0).value("score"));

edges = ImmutableList.copyOf(graph.edges(id, id));
Assert.assertEquals(2, edges.size());
Assert.assertEquals(101, (int) edges.get(1).value("score"));
}

@Test
public void testQueryEdgesByIdWithGraphAPIAndNotCommitedRemoved() {
HugeGraph graph = graph();
init18Edges();

Edge edge = graph.traversal().E().toList().get(0);
Object id = edge.id();
Assert.assertTrue(graph.edges(id).hasNext());
List<Edge> edges = graph.traversal().E().toList();

edge.remove();
Assert.assertFalse(graph.edges(id).hasNext());
Edge edge1 = edges.get(0);
Edge edge2 = edges.get(1);
Assert.assertTrue(graph.edges(edge1.id()).hasNext());
Assert.assertTrue(graph.edges(edge2.id()).hasNext());

edge1.remove();
edge2.remove();
Assert.assertFalse(graph.edges(edge1.id()).hasNext());
Assert.assertFalse(graph.edges(edge2.id()).hasNext());
Assert.assertFalse(graph.edges(edge1.id(), edge2.id()).hasNext());

graph.tx().rollback();
Assert.assertTrue(graph.edges(id).hasNext());
Assert.assertTrue(graph.edges(edge1.id()).hasNext());
Assert.assertTrue(graph.edges(edge2.id()).hasNext());
Assert.assertTrue(graph.edges(edge1.id(), edge2.id()).hasNext());
}

@Test
Expand Down
Loading

0 comments on commit f6d6427

Please sign in to comment.