use of org.apache.tinkerpop.gremlin.structure.VertexProperty in project janusgraph by JanusGraph.
the class JanusGraphTest method testThreadBoundTx.
/**
* Tests that elements can be accessed beyond their transactional boundaries if they
* are bound to single-threaded graph transactions
*/
@Test
public void testThreadBoundTx() {
PropertyKey t = mgmt.makePropertyKey("type").dataType(Integer.class).make();
mgmt.buildIndex("etype", Edge.class).addKey(t).buildCompositeIndex();
((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("friend")).sortKey(t).make();
finishSchema();
JanusGraphVertex v1 = graph.addVertex("name", "Vertex1", "age", 35);
JanusGraphVertex v2 = graph.addVertex("name", "Vertex2", "age", 45);
JanusGraphVertex v3 = graph.addVertex("name", "Vertex3", "age", 55);
Edge e1 = v1.addEdge("knows", v2, "time", 5);
Edge e2 = v2.addEdge("knows", v3, "time", 15);
Edge e3 = v3.addEdge("knows", v1, "time", 25);
Edge e4 = v2.addEdge("friend", v2, "type", 1);
for (JanusGraphVertex v : new JanusGraphVertex[] { v1, v2, v3 }) {
assertCount(2, v.query().direction(Direction.BOTH).labels("knows").edges());
assertCount(1, v.query().direction(Direction.OUT).labels("knows").edges());
final JanusGraphEdge tmpE = Iterables.getOnlyElement(v.query().direction(Direction.OUT).labels("knows").edges());
assertEquals(5, tmpE.<Integer>value("time") % 10);
}
e3.property("time", 35);
assertEquals(35, e3.<Integer>value("time").intValue());
v1.addEdge("friend", v2, "type", 0);
graph.tx().commit();
e4.property("type", 2);
final JanusGraphEdge ef = Iterables.getOnlyElement(v1.query().direction(Direction.OUT).labels("friend").edges());
assertEquals(ef, getOnlyElement(graph.query().has("type", 0).edges()));
ef.property("type", 1);
graph.tx().commit();
assertEquals(35, e3.<Integer>value("time").intValue());
e3 = getE(graph, e3);
e3.property("time", 45);
assertEquals(45, e3.<Integer>value("time").intValue());
assertEquals(15, e2.<Integer>value("time").intValue());
e2.property("time", 25);
assertEquals(25, e2.<Integer>value("time").intValue());
assertEquals(35, v1.<Integer>value("age").intValue());
assertEquals(55, v3.<Integer>value("age").intValue());
v3.property("age", 65);
assertEquals(65, v3.<Integer>value("age").intValue());
e1 = getE(graph, e1);
for (final JanusGraphVertex v : new JanusGraphVertex[] { v1, v2, v3 }) {
assertCount(2, v.query().direction(Direction.BOTH).labels("knows").edges());
assertCount(1, v.query().direction(Direction.OUT).labels("knows").edges());
assertEquals(5, getOnlyElement(v.query().direction(Direction.OUT).labels("knows").edges()).<Integer>value("time") % 10);
}
graph.tx().commit();
VertexProperty prop = v1.properties().next();
assertTrue(getId(prop) > 0);
prop = (VertexProperty) ((Iterable) graph.multiQuery(v1).properties().values().iterator().next()).iterator().next();
assertTrue(getId(prop) > 0);
assertEquals(45, e3.<Integer>value("time").intValue());
assertEquals(5, e1.<Integer>value("time").intValue());
assertEquals(35, v1.<Integer>value("age").intValue());
assertEquals(65, v3.<Integer>value("age").intValue());
for (JanusGraphVertex v : new JanusGraphVertex[] { v1, v2, v3 }) {
assertCount(2, v.query().direction(Direction.BOTH).labels("knows").edges());
assertCount(1, v.query().direction(Direction.OUT).labels("knows").edges());
assertEquals(5, getOnlyElement(v.query().direction(Direction.OUT).labels("knows").edges()).<Integer>value("time") % 10);
}
graph.tx().commit();
v1 = graph.addVertex();
v2 = graph.addVertex();
v1.addEdge("knows", v2);
graph.tx().commit();
v3 = graph.addVertex();
Edge e = v1.addEdge("knows", v3);
assertFalse(e.property("age").isPresent());
}
use of org.apache.tinkerpop.gremlin.structure.VertexProperty in project janusgraph by JanusGraph.
the class JanusGraphEventualGraphTest method testTimestampSetting.
/**
* Tests the correct interpretation of the commit time and that timestamps can be read
*/
@Test
public void testTimestampSetting() {
clopen(option(GraphDatabaseConfiguration.STORE_META_TIMESTAMPS, "edgestore"), true, option(GraphDatabaseConfiguration.STORE_META_TTL, "edgestore"), true);
// Transaction 1: Init graph with two vertices, having set "name" and "age" properties
JanusGraphTransaction tx1 = graph.buildTransaction().commitTime(Instant.ofEpochSecond(100)).start();
String name = "name";
String age = "age";
String address = "address";
JanusGraphVertex v1 = tx1.addVertex(name, "a");
JanusGraphVertex v2 = tx1.addVertex(age, "14", name, "b", age, "42");
tx1.commit();
// Fetch vertex ids
long id1 = getId(v1);
long id2 = getId(v2);
// Transaction 2: Remove "name" property from v1, set "address" property; create
// an edge v2 -> v1
JanusGraphTransaction tx2 = graph.buildTransaction().commitTime(Instant.ofEpochSecond(1000)).start();
v1 = getV(tx2, id1);
v2 = getV(tx2, id2);
for (Iterator<VertexProperty<Object>> propertyIterator = v1.properties(name); propertyIterator.hasNext(); ) {
VertexProperty prop = propertyIterator.next();
if (features.hasTimestamps()) {
Instant t = prop.value("~timestamp");
assertEquals(100, t.getEpochSecond());
assertEquals(Instant.ofEpochSecond(0, 1000).getNano(), t.getNano());
}
if (features.hasCellTTL()) {
Duration d = prop.value("~ttl");
assertEquals(0L, d.getSeconds());
assertTrue(d.isZero());
}
}
assertEquals(1, v1.query().propertyCount());
assertEquals(1, v1.query().has("~timestamp", Cmp.GREATER_THAN, Instant.ofEpochSecond(10)).propertyCount());
assertEquals(1, v1.query().has("~timestamp", Instant.ofEpochSecond(100, 1000)).propertyCount());
v1.property(name).remove();
v1.property(VertexProperty.Cardinality.single, address, "xyz");
Edge edge = v2.addEdge("parent", v1);
tx2.commit();
Object edgeId = edge.id();
JanusGraphVertex afterTx2 = getV(graph, id1);
// Verify that "name" property is gone
assertFalse(afterTx2.keys().contains(name));
// Verify that "address" property is set
assertEquals("xyz", afterTx2.value(address));
// Verify that the edge is properly registered with the endpoint vertex
assertCount(1, afterTx2.query().direction(IN).labels("parent").edges());
// Verify that edge is registered under the id
assertNotNull(getE(graph, edgeId));
graph.tx().commit();
// Transaction 3: Remove "address" property from v1 with earlier timestamp than
// when the value was set
JanusGraphTransaction tx3 = graph.buildTransaction().commitTime(Instant.ofEpochSecond(200)).start();
v1 = getV(tx3, id1);
v1.property(address).remove();
tx3.commit();
JanusGraphVertex afterTx3 = getV(graph, id1);
graph.tx().commit();
// Verify that "address" is still set
assertEquals("xyz", afterTx3.value(address));
// Transaction 4: Modify "age" property on v2, remove edge between v2 and v1
JanusGraphTransaction tx4 = graph.buildTransaction().commitTime(Instant.ofEpochSecond(2000)).start();
v2 = getV(tx4, id2);
v2.property(VertexProperty.Cardinality.single, age, "15");
getE(tx4, edgeId).remove();
tx4.commit();
JanusGraphVertex afterTx4 = getV(graph, id2);
// Verify that "age" property is modified
assertEquals("15", afterTx4.value(age));
// Verify that edge is no longer registered with the endpoint vertex
assertCount(0, afterTx4.query().direction(OUT).labels("parent").edges());
// Verify that edge entry disappeared from id registry
assertNull(getE(graph, edgeId));
// Transaction 5: Modify "age" property on v2 with earlier timestamp
JanusGraphTransaction tx5 = graph.buildTransaction().commitTime(Instant.ofEpochSecond(1500)).start();
v2 = getV(tx5, id2);
v2.property(VertexProperty.Cardinality.single, age, "16");
tx5.commit();
JanusGraphVertex afterTx5 = getV(graph, id2);
// Verify that the property value is unchanged
assertEquals("15", afterTx5.value(age));
}
use of org.apache.tinkerpop.gremlin.structure.VertexProperty in project janusgraph by JanusGraph.
the class JanusGraphEventualGraphTest method processTx.
private void processTx(JanusGraphTransaction tx, int transactionId, long vid, long uid) {
JanusGraphVertex v = getV(tx, vid);
JanusGraphVertex u = getV(tx, uid);
assertEquals(5.0, v.<Double>value("weight"), 0.00001);
VertexProperty p = getOnlyElement(v.properties("weight"));
assertEquals(1, p.<Integer>value("sig").intValue());
sign(v.property("weight", 6.0), transactionId);
p = getOnlyElement(v.properties("name"));
assertEquals(1, p.<Integer>value("sig").intValue());
assertEquals("John", p.value());
p.remove();
sign(v.property("name", "Bob"), transactionId);
for (String pkey : new String[] { "value", "valuef" }) {
p = getOnlyElement(v.properties(pkey));
assertEquals(1, p.<Integer>value("sig").intValue());
assertEquals(2, p.value());
sign((JanusGraphVertexProperty) p, transactionId);
}
JanusGraphEdge e = Iterables.getOnlyElement(v.query().direction(OUT).labels("es").edges());
assertEquals(1, e.<Integer>value("sig").intValue());
e.remove();
sign(v.addEdge("es", u), transactionId);
e = Iterables.getOnlyElement(v.query().direction(OUT).labels("o2o").edges());
assertEquals(1, e.<Integer>value("sig").intValue());
sign(e, transactionId);
e = Iterables.getOnlyElement(v.query().direction(OUT).labels("o2m").edges());
assertEquals(1, e.<Integer>value("sig").intValue());
e.remove();
sign(v.addEdge("o2m", u), transactionId);
for (String label : new String[] { "em", "emf" }) {
e = Iterables.getOnlyElement(v.query().direction(OUT).labels(label).edges());
assertEquals(1, e.<Integer>value("sig").intValue());
sign(e, transactionId);
}
}
use of org.apache.tinkerpop.gremlin.structure.VertexProperty in project janusgraph by JanusGraph.
the class JanusGraphPartitionGraphTest method testVertexPartitioning.
@Test
public void testVertexPartitioning() {
Object[] options = { option(GraphDatabaseConfiguration.IDS_FLUSH), false };
clopen(options);
makeVertexIndexedUniqueKey("gid", Integer.class);
makeKey("sig", Integer.class);
mgmt.makePropertyKey("name").cardinality(Cardinality.LIST).dataType(String.class).make();
makeLabel("knows");
makeLabel("base");
mgmt.makeEdgeLabel("one").multiplicity(Multiplicity.ONE2ONE).make();
mgmt.makeVertexLabel("person").make();
mgmt.makeVertexLabel("group").partition().make();
finishSchema();
final Set<String> names = ImmutableSet.of("Marko", "Dan", "Stephen", "Daniel", "Josh", "Thad", "Pavel", "Matthias");
final int numG = 10;
final long[] gids = new long[numG];
for (int i = 0; i < numG; i++) {
JanusGraphVertex g = tx.addVertex("group");
g.property(VertexProperty.Cardinality.single, "gid", i);
g.property(VertexProperty.Cardinality.single, "sig", 0);
for (String n : names) {
g.property("name", n);
}
assertEquals(i, g.<Integer>value("gid").intValue());
assertEquals(0, g.<Integer>value("sig").intValue());
assertEquals("group", g.label());
assertCount(names.size(), g.properties("name"));
assertTrue(getId(g) > 0);
gids[i] = getId(g);
if (i > 0) {
g.addEdge("base", getV(tx, gids[0]));
}
if (i % 2 == 1) {
g.addEdge("one", getV(tx, gids[i - 1]));
}
}
for (int i = 0; i < numG; i++) {
JanusGraphVertex g = getV(tx, gids[i]);
assertCount(1, g.query().direction(Direction.BOTH).labels("one").edges());
assertCount(1, g.query().direction(i % 2 == 0 ? Direction.IN : Direction.OUT).labels("one").edges());
assertCount(0, g.query().direction(i % 2 == 1 ? Direction.IN : Direction.OUT).labels("one").edges());
if (i > 0) {
assertCount(1, g.query().direction(Direction.OUT).labels("base").edges());
} else {
assertCount(numG - 1, g.query().direction(Direction.IN).labels("base").edges());
}
}
newTx();
for (int i = 0; i < numG; i++) {
long gId = gids[i];
assertTrue(idManager.isPartitionedVertex(gId));
assertEquals(idManager.getCanonicalVertexId(gId), gId);
JanusGraphVertex g = getV(tx, gId);
final int canonicalPartition = getPartitionID(g);
assertEquals(g, getOnlyElement(tx.query().has("gid", i).vertices()));
assertEquals(i, g.<Integer>value("gid").intValue());
assertCount(names.size(), g.properties("name"));
// Verify that properties are distributed correctly
JanusGraphVertexProperty p = (JanusGraphVertexProperty) getOnlyElement(g.properties("gid"));
assertEquals(canonicalPartition, getPartitionID(p));
for (Iterator<VertexProperty<Object>> niter = g.properties("name"); niter.hasNext(); ) {
assertEquals(canonicalPartition, getPartitionID((JanusGraphVertex) niter.next().element()));
}
// Copied from above
assertCount(1, g.query().direction(Direction.BOTH).labels("one").edges());
assertCount(1, g.query().direction(i % 2 == 0 ? Direction.IN : Direction.OUT).labels("one").edges());
assertCount(0, g.query().direction(i % 2 == 1 ? Direction.IN : Direction.OUT).labels("one").edges());
if (i > 0) {
assertCount(1, g.query().direction(Direction.OUT).labels("base").edges());
} else {
assertCount(numG - 1, g.query().direction(Direction.IN).labels("base").edges());
}
}
clopen(options);
final int numTx = 100;
final int vPerTx = 10;
Multiset<Integer> partitions = HashMultiset.create();
for (int t = 1; t <= numTx; t++) {
JanusGraphVertex g1 = getV(tx, gids[0]), g2 = getV(tx, gids[1]);
assertNotNull(g1);
JanusGraphVertex[] vs = new JanusGraphVertex[vPerTx];
for (int vi = 0; vi < vPerTx; vi++) {
vs[vi] = tx.addVertex("person");
vs[vi].property(VertexProperty.Cardinality.single, "sig", t);
Edge e = vs[vi].addEdge("knows", g1);
e.property("sig", t);
e = g1.addEdge("knows", vs[vi]);
e.property("sig", t);
if (vi % 2 == 0) {
e = vs[vi].addEdge("knows", g2);
e.property("sig", t);
}
}
newTx();
// Verify that all elements are in the same partition
JanusGraphTransaction txx = graph.buildTransaction().readOnly().start();
g1 = getV(tx, gids[0]);
g2 = getV(tx, gids[1]);
int partition = -1;
for (int vi = 0; vi < vPerTx; vi++) {
assertTrue(vs[vi].hasId());
int pid = getPartitionID(vs[vi]);
if (partition < 0)
partition = pid;
else
assertEquals(partition, pid);
int numRelations = 0;
JanusGraphVertex v = getV(txx, vs[vi].longId());
for (JanusGraphRelation r : v.query().relations()) {
numRelations++;
assertEquals(partition, getPartitionID(r));
if (r instanceof JanusGraphEdge) {
JanusGraphVertex o = ((JanusGraphEdge) r).otherVertex(v);
assertTrue(o.equals(g1) || o.equals(g2));
}
}
assertEquals(3 + (vi % 2 == 0 ? 1 : 0), numRelations);
}
partitions.add(partition);
txx.commit();
}
// Verify spread across partitions; this number is a pessimistic lower bound but might fail since it is probabilistic
//
assertTrue(partitions.elementSet().size() >= 3);
newTx();
// Verify edge querying across partitions
JanusGraphVertex g1 = getV(tx, gids[0]);
assertEquals(0, g1.<Integer>value("gid").intValue());
assertEquals("group", g1.label());
assertCount(names.size(), g1.properties("name"));
assertCount(numTx * vPerTx, g1.query().direction(Direction.OUT).labels("knows").edges());
assertCount(numTx * vPerTx, g1.query().direction(Direction.IN).labels("knows").edges());
assertCount(numTx * vPerTx * 2, g1.query().direction(Direction.BOTH).labels("knows").edges());
assertCount(numTx * vPerTx + numG, tx.query().vertices());
newTx();
// Restrict to partitions
for (int t = 0; t < 10; t++) {
int numP = random.nextInt(3) + 1;
Set<Integer> parts = Sets.newHashSet();
int numV = 0;
while (parts.size() < numP) {
int part = Iterables.get(partitions.elementSet(), random.nextInt(partitions.elementSet().size()));
if (parts.add(part))
numV += partitions.count(part);
}
numV *= vPerTx;
int[] partitionArray = new int[numP];
int i = 0;
for (Integer part : parts) partitionArray[i++] = part;
JanusGraphTransaction tx2 = graph.buildTransaction().restrictedPartitions(partitionArray).readOnly().start();
// Copied from above
g1 = getV(tx2, gids[0]);
assertEquals(0, g1.<Integer>value("gid").intValue());
assertEquals("group", g1.label());
assertTrue(names.size() >= Iterators.size(g1.properties("name")));
assertCount(numV, g1.query().direction(Direction.OUT).labels("knows").edges());
assertCount(numV, g1.query().direction(Direction.IN).labels("knows").edges());
assertCount(numV * 2, g1.query().direction(Direction.BOTH).labels("knows").edges());
// Test local intersection
JanusGraphVertex g2 = getV(tx2, gids[1]);
VertexList v1 = g1.query().direction(Direction.IN).labels("knows").vertexIds();
VertexList v2 = g2.query().direction(Direction.IN).labels("knows").vertexIds();
assertEquals(numV, v1.size());
assertEquals(numV / 2, v2.size());
v1.sort();
v2.sort();
LongArrayList al1 = v1.getIDs();
LongArrayList al2 = v2.getIDs();
assertTrue(AbstractLongListUtil.isSorted(al1));
assertTrue(AbstractLongListUtil.isSorted(al2));
LongArrayList alr = AbstractLongListUtil.mergeJoin(al1, al2, false);
assertEquals(numV / 2, alr.size());
tx2.commit();
}
}
use of org.apache.tinkerpop.gremlin.structure.VertexProperty in project janusgraph by JanusGraph.
the class JanusGraphVertexDeserializer method readHadoopVertex.
// Read a single row from the edgestore and create a TinkerVertex corresponding to the row
// The neighboring vertices are represented by DetachedVertex instances
public TinkerVertex readHadoopVertex(final StaticBuffer key, Iterable<Entry> entries) {
// Convert key to a vertex ID
final long vertexId = idManager.getKeyID(key);
Preconditions.checkArgument(vertexId > 0);
// Partitioned vertex handling
if (idManager.isPartitionedVertex(vertexId)) {
Preconditions.checkState(setup.getFilterPartitionedVertices(), "Read partitioned vertex (ID=%s), but partitioned vertex filtering is disabled.", vertexId);
log.debug("Skipping partitioned vertex with ID {}", vertexId);
return null;
}
// Create TinkerVertex
TinkerGraph tg = TinkerGraph.open();
TinkerVertex tv = null;
// Iterate over edgestore columns to find the vertex's label relation
for (final Entry data : entries) {
RelationReader relationReader = setup.getRelationReader();
final RelationCache relation = relationReader.parseRelation(data, false, typeManager);
if (systemTypes.isVertexLabelSystemType(relation.typeId)) {
// Found vertex Label
long vertexLabelId = relation.getOtherVertexId();
VertexLabel vl = typeManager.getExistingVertexLabel(vertexLabelId);
// Create TinkerVertex with this label
tv = getOrCreateVertex(vertexId, vl.name(), tg);
} else if (systemTypes.isTypeSystemType(relation.typeId)) {
log.trace("Vertex {} is a system vertex", vertexId);
return null;
}
}
// Added this following testing
if (null == tv) {
tv = getOrCreateVertex(vertexId, null, tg);
}
Preconditions.checkNotNull(tv, "Unable to determine vertex label for vertex with ID %s", vertexId);
// Iterate over and decode edgestore columns (relations) on this vertex
for (final Entry data : entries) {
try {
RelationReader relationReader = setup.getRelationReader();
final RelationCache relation = relationReader.parseRelation(data, false, typeManager);
// Ignore system types
if (systemTypes.isSystemType(relation.typeId))
continue;
final RelationType type = typeManager.getExistingRelationType(relation.typeId);
// Ignore hidden types
if (((InternalRelationType) type).isInvisibleType())
continue;
// Decode and create the relation (edge or property)
if (type.isPropertyKey()) {
// Decode property
Object value = relation.getValue();
Preconditions.checkNotNull(value);
VertexProperty.Cardinality card = getPropertyKeyCardinality(type.name());
VertexProperty<Object> vp = tv.property(card, type.name(), value, T.id, relation.relationId);
// Decode meta properties
decodeProperties(relation, vp);
} else {
assert type.isEdgeLabel();
// Partitioned vertex handling
if (idManager.isPartitionedVertex(relation.getOtherVertexId())) {
Preconditions.checkState(setup.getFilterPartitionedVertices(), "Read edge incident on a partitioned vertex, but partitioned vertex filtering is disabled. " + "Relation ID: %s. This vertex ID: %s. Other vertex ID: %s. Edge label: %s.", relation.relationId, vertexId, relation.getOtherVertexId(), type.name());
log.debug("Skipping edge with ID {} incident on partitioned vertex with ID {} (and nonpartitioned vertex with ID {})", relation.relationId, relation.getOtherVertexId(), vertexId);
continue;
}
// Decode edge
TinkerEdge te;
// We don't know the label of the other vertex, but one must be provided
TinkerVertex adjacentVertex = getOrCreateVertex(relation.getOtherVertexId(), null, tg);
// skip self-loop edges that were already processed, but from a different direction
if (tv.equals(adjacentVertex) && edgeExists(tv, type, relation)) {
continue;
}
if (relation.direction.equals(Direction.IN)) {
te = (TinkerEdge) adjacentVertex.addEdge(type.name(), tv, T.id, relation.relationId);
} else if (relation.direction.equals(Direction.OUT)) {
te = (TinkerEdge) tv.addEdge(type.name(), adjacentVertex, T.id, relation.relationId);
} else {
throw new RuntimeException("Direction.BOTH is not supported");
}
decodeProperties(relation, te);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return tv;
}
Aggregations