use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class JsonUtilTest method testSerializeVertexWithNumberId.
@Test
public void testSerializeVertexWithNumberId() {
FakeObjects fakeObject = new FakeObjects();
PropertyKey name = fakeObject.newPropertyKey(IdGenerator.of(1), "name");
PropertyKey age = fakeObject.newPropertyKey(IdGenerator.of(2), "age", DataType.INT, Cardinality.SINGLE);
PropertyKey city = fakeObject.newPropertyKey(IdGenerator.of(3), "city");
VertexLabel vl = fakeObject.newVertexLabel(IdGenerator.of(1), "person", IdStrategy.CUSTOMIZE_NUMBER, name.id(), age.id(), city.id());
Id id = IdGenerator.of(123456L);
HugeVertex vertex = new HugeVertex(fakeObject.graph(), id, vl);
MutableIntObjectMap<HugeProperty<?>> properties = CollectionFactory.newIntObjectMap(name.id(), new HugeVertexProperty<>(vertex, name, "marko"), age.id(), new HugeVertexProperty<>(vertex, age, 29), city.id(), new HugeVertexProperty<>(vertex, city, "Beijing"));
Whitebox.setInternalState(vertex, "properties", properties);
String json = JsonUtil.toJson(vertex);
Assert.assertEquals("{\"id\":123456,\"label\":\"person\"," + "\"type\":\"vertex\",\"properties\":{\"" + "name\":\"marko\",\"age\":29," + "\"city\":\"Beijing\"}}", json);
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class SplicingIdGeneratorTest method testGenerate.
@Test
public void testGenerate() {
FakeObjects fakeObjects = new FakeObjects();
PropertyKey name = fakeObjects.newPropertyKey(IdGenerator.of(1), "name");
VertexLabel vertexLabel = fakeObjects.newVertexLabel(IdGenerator.of(1L), "fake", IdStrategy.PRIMARY_KEY, name.id());
HugeVertex vertex = Mockito.mock(HugeVertex.class);
Mockito.when(vertex.schemaLabel()).thenReturn(vertexLabel);
Mockito.when(vertex.name()).thenReturn("marko");
Id vid = SplicingIdGenerator.instance().generate(vertex);
Assert.assertEquals(IdGenerator.of("1:marko"), vid);
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class GraphTransaction method joinTxEdges.
private Iterator<?> joinTxEdges(Query query, Iterator<HugeEdge> edges, Map<Id, HugeVertex> removingVertices) {
assert query.resultType().isEdge();
BiFunction<Query, HugeEdge, HugeEdge> matchTxFunc = (q, e) -> {
assert q.resultType() == HugeType.EDGE;
if (e.expired() && !q.showExpired()) {
// Filter expired edges with TTL
return null;
}
// Filter edges matched conditions
return q.test(e) ? e : q.test(e = e.switchOwner()) ? e : null;
};
edges = this.joinTxRecords(query, edges, matchTxFunc, this.addedEdges, this.removedEdges, this.updatedEdges);
if (removingVertices.isEmpty()) {
return edges;
}
// Filter edges that belong to deleted vertex
return new FilterIterator<HugeEdge>(edges, edge -> {
for (HugeVertex v : removingVertices.values()) {
if (edge.belongToVertex(v)) {
return false;
}
}
return true;
});
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class GraphTransaction method prepareDeletions.
protected void prepareDeletions(Map<Id, HugeVertex> removedVertices, Map<Id, HugeEdge> removedEdges) {
// Remove related edges of each vertex
for (HugeVertex v : removedVertices.values()) {
if (!v.schemaLabel().existsLinkLabel()) {
continue;
}
// Query all edges of the vertex and remove them
Query query = constructEdgesQuery(v.id(), Directions.BOTH);
Iterator<HugeEdge> vedges = this.queryEdgesFromBackend(query);
try {
while (vedges.hasNext()) {
this.checkTxEdgesCapacity();
HugeEdge edge = vedges.next();
// NOTE: will change the input parameter
removedEdges.put(edge.id(), edge);
// Commit first if enabled commit-part mode
if (this.commitPartOfAdjacentEdges > 0 && removedEdges.size() >= this.commitPartOfAdjacentEdges) {
this.commitPartOfEdgeDeletions(removedEdges);
}
}
} finally {
CloseableIterator.closeIterator(vedges);
}
}
// Remove vertices
for (HugeVertex v : removedVertices.values()) {
this.checkAggregateProperty(v);
/*
* If the backend stores vertex together with edges, it's edges
* would be removed after removing vertex. Otherwise, if the
* backend stores vertex which is separated from edges, it's
* edges should be removed manually when removing vertex.
*/
this.doRemove(this.serializer.writeVertex(v.prepareRemoved()));
this.indexTx.updateVertexIndex(v, true);
this.indexTx.updateLabelIndex(v, true);
}
// Remove edges
this.prepareDeletions(removedEdges);
}
use of com.baidu.hugegraph.structure.HugeVertex in project incubator-hugegraph by apache.
the class GraphTransaction method checkVertexExistIfCustomizedId.
private void checkVertexExistIfCustomizedId(Map<Id, HugeVertex> vertices) {
Set<Id> ids = new HashSet<>();
for (HugeVertex vertex : vertices.values()) {
VertexLabel vl = vertex.schemaLabel();
if (!vl.hidden() && vl.idStrategy().isCustomized()) {
ids.add(vertex.id());
}
}
if (ids.isEmpty()) {
return;
}
IdQuery idQuery = new IdQuery(HugeType.VERTEX, ids);
Iterator<HugeVertex> results = this.queryVerticesFromBackend(idQuery);
try {
if (!results.hasNext()) {
return;
}
HugeVertex existedVertex = results.next();
HugeVertex newVertex = vertices.get(existedVertex.id());
if (!existedVertex.label().equals(newVertex.label())) {
throw new HugeException("The newly added vertex with id:'%s' label:'%s' " + "is not allowed to insert, because already exist " + "a vertex with same id and different label:'%s'", newVertex.id(), newVertex.label(), existedVertex.label());
}
} finally {
CloseableIterator.closeIterator(results);
}
}
Aggregations