use of org.janusgraph.core.JanusGraphVertexProperty in project janusgraph by JanusGraph.
the class JanusGraphTest method testMultivaluedVertexProperty.
/**
* Tests multi-valued properties with special focus on indexing and incident unidirectional edges
* which is not tested in {@link #testSchemaTypes()}
* <p>
* --> TODO: split and move this into other test cases: ordering to query, indexing to index
*/
@Test
public <V> void testMultivaluedVertexProperty() {
/*
* Constant test data
*
* The values list below must have at least two elements. The string
* literals were chosen arbitrarily and have no special significance.
*/
final String foo = "foo", bar = "bar", weight = "weight";
final List<String> values = ImmutableList.of("four", "score", "and", "seven");
assertTrue(2 <= values.size(), "Values list must have multiple elements for this test to make sense");
// Create property with name pname and a vertex
PropertyKey w = makeKey(weight, Integer.class);
PropertyKey f = ((StandardPropertyKeyMaker) mgmt.makePropertyKey(foo)).dataType(String.class).cardinality(Cardinality.LIST).sortKey(w).sortOrder(Order.DESC).make();
mgmt.buildIndex(foo, Vertex.class).addKey(f).buildCompositeIndex();
PropertyKey b = mgmt.makePropertyKey(bar).dataType(String.class).cardinality(Cardinality.LIST).make();
mgmt.buildIndex(bar, Vertex.class).addKey(b).buildCompositeIndex();
finishSchema();
JanusGraphVertex v = tx.addVertex();
// Insert prop values
int i = 0;
for (String s : values) {
v.property(foo, s, weight, ++i);
v.property(bar, s, weight, i);
}
// Verify correct number of properties
assertCount(values.size(), v.properties(foo));
assertCount(values.size(), v.properties(bar));
// Verify order
for (String prop : new String[] { foo, bar }) {
int sum = 0;
int index = values.size();
for (Object o : v.query().labels(foo).properties()) {
JanusGraphVertexProperty<String> p = (JanusGraphVertexProperty<String>) o;
assertTrue(values.contains(p.value()));
int weightAsInteger = p.value(weight);
sum += weightAsInteger;
if (prop.equals(foo))
assertEquals(index, weightAsInteger);
index--;
}
assertEquals(values.size() * (values.size() + 1) / 2, sum);
}
assertCount(1, tx.query().has(foo, values.get(1)).vertices());
assertCount(1, tx.query().has(foo, values.get(3)).vertices());
assertCount(1, tx.query().has(bar, values.get(1)).vertices());
assertCount(1, tx.query().has(bar, values.get(3)).vertices());
// Check that removing properties works
asStream(v.properties(foo)).forEach(Property::remove);
// Check that the properties were actually deleted from v
assertEmpty(v.properties(foo));
// Reopen database
clopen();
assertCount(0, tx.query().has(foo, values.get(1)).vertices());
assertCount(0, tx.query().has(foo, values.get(3)).vertices());
assertCount(1, tx.query().has(bar, values.get(1)).vertices());
assertCount(1, tx.query().has(bar, values.get(3)).vertices());
// Retrieve and check our test vertex
v = getV(tx, v);
assertEmpty(v.properties(foo));
assertCount(values.size(), v.properties(bar));
// Reinsert prop values
for (String s : values) {
v.property(foo, s);
}
assertCount(values.size(), v.properties(foo));
// Check that removing properties works
asStream(v.properties(foo)).forEach(Property::remove);
// Check that the properties were actually deleted from v
assertEmpty(v.properties(foo));
}
use of org.janusgraph.core.JanusGraphVertexProperty in project janusgraph by JanusGraph.
the class JanusGraphTest method testSchemaTypes.
/* ==================================================================================
SCHEMA TESTS
==================================================================================*/
/**
* Test the definition and inspection of various schema types and ensure their correct interpretation
* within the graph
*/
@SuppressWarnings("unchecked")
@Test
public void testSchemaTypes() {
// ---------- PROPERTY KEYS ----------------
// Normal single-valued property key
final PropertyKey weight = makeKey("weight", Float.class);
// Indexed unique property key
PropertyKey uid = makeVertexIndexedUniqueKey("uid", String.class);
// Indexed but not unique
final PropertyKey someId = makeVertexIndexedKey("someid", Object.class);
// Set-valued property key
PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).cardinality(Cardinality.SET).make();
// List-valued property key with signature
PropertyKey value = mgmt.makePropertyKey("value").dataType(Double.class).signature(weight).cardinality(Cardinality.LIST).make();
// ---------- EDGE LABELS ----------------
// Standard edge label
EdgeLabel friend = mgmt.makeEdgeLabel("friend").make();
// Unidirected
EdgeLabel link = mgmt.makeEdgeLabel("link").unidirected().multiplicity(Multiplicity.MANY2ONE).make();
// Signature label
EdgeLabel connect = mgmt.makeEdgeLabel("connect").signature(uid).multiplicity(Multiplicity.SIMPLE).make();
// Edge labels with different cardinalities
EdgeLabel parent = mgmt.makeEdgeLabel("parent").multiplicity(Multiplicity.MANY2ONE).make();
EdgeLabel child = mgmt.makeEdgeLabel("child").multiplicity(Multiplicity.ONE2MANY).make();
EdgeLabel spouse = mgmt.makeEdgeLabel("spouse").multiplicity(Multiplicity.ONE2ONE).make();
// ---------- VERTEX LABELS ----------------
VertexLabel person = mgmt.makeVertexLabel("person").make();
VertexLabel tag = mgmt.makeVertexLabel("tag").make();
VertexLabel tweet = mgmt.makeVertexLabel("tweet").setStatic().make();
long[] sig;
// ######### INSPECTION & FAILURE ############
assertTrue(mgmt.isOpen());
assertEquals("weight", weight.toString());
assertTrue(mgmt.containsRelationType("weight"));
assertTrue(mgmt.containsPropertyKey("weight"));
assertFalse(mgmt.containsEdgeLabel("weight"));
assertTrue(mgmt.containsEdgeLabel("connect"));
assertFalse(mgmt.containsPropertyKey("connect"));
assertFalse(mgmt.containsRelationType("bla"));
assertNull(mgmt.getPropertyKey("bla"));
assertNull(mgmt.getEdgeLabel("bla"));
assertNotNull(mgmt.getPropertyKey("weight"));
assertNotNull(mgmt.getEdgeLabel("connect"));
assertTrue(weight.isPropertyKey());
assertFalse(weight.isEdgeLabel());
assertEquals(Cardinality.SINGLE, weight.cardinality());
assertEquals(Cardinality.SINGLE, someId.cardinality());
assertEquals(Cardinality.SET, name.cardinality());
assertEquals(Cardinality.LIST, value.cardinality());
assertEquals(Object.class, someId.dataType());
assertEquals(Float.class, weight.dataType());
sig = ((InternalRelationType) value).getSignature();
assertEquals(1, sig.length);
assertEquals(weight.longId(), sig[0]);
assertTrue(mgmt.getGraphIndex(uid.name()).isUnique());
assertFalse(mgmt.getGraphIndex(someId.name()).isUnique());
assertEquals("friend", friend.name());
assertTrue(friend.isEdgeLabel());
assertFalse(friend.isPropertyKey());
assertEquals(Multiplicity.ONE2ONE, spouse.multiplicity());
assertEquals(Multiplicity.ONE2MANY, child.multiplicity());
assertEquals(Multiplicity.MANY2ONE, parent.multiplicity());
assertEquals(Multiplicity.MULTI, friend.multiplicity());
assertEquals(Multiplicity.SIMPLE, connect.multiplicity());
assertTrue(link.isUnidirected());
assertFalse(link.isDirected());
assertFalse(child.isUnidirected());
assertTrue(spouse.isDirected());
assertFalse(((InternalRelationType) friend).isInvisibleType());
assertTrue(((InternalRelationType) friend).isInvisible());
assertEquals(0, ((InternalRelationType) friend).getSignature().length);
sig = ((InternalRelationType) connect).getSignature();
assertEquals(1, sig.length);
assertEquals(uid.longId(), sig[0]);
assertEquals(0, ((InternalRelationType) friend).getSortKey().length);
assertEquals(Order.DEFAULT, ((InternalRelationType) friend).getSortOrder());
assertEquals(SchemaStatus.ENABLED, ((InternalRelationType) friend).getStatus());
assertEquals(5, Iterables.size(mgmt.getRelationTypes(PropertyKey.class)));
assertEquals(6, Iterables.size(mgmt.getRelationTypes(EdgeLabel.class)));
assertEquals(11, Iterables.size(mgmt.getRelationTypes(RelationType.class)));
assertEquals(3, Iterables.size(mgmt.getVertexLabels()));
assertEquals("tweet", tweet.name());
assertTrue(mgmt.containsVertexLabel("person"));
assertFalse(mgmt.containsVertexLabel("bla"));
assertFalse(person.isPartitioned());
assertFalse(person.isStatic());
assertFalse(tag.isPartitioned());
assertTrue(tweet.isStatic());
// ------ TRY INVALID STUFF --------
// Failures
// No data type
assertThrows(IllegalArgumentException.class, () -> mgmt.makePropertyKey("fid").make());
// Already exists
assertThrows(SchemaViolationException.class, () -> mgmt.makeEdgeLabel("link").unidirected().make());
// signature and sort-key collide
assertThrows(IllegalArgumentException.class, () -> ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).sortKey(someId, weight).signature(someId).make());
// sort key requires the label to be non-constrained
assertThrows(IllegalArgumentException.class, () -> ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).multiplicity(Multiplicity.SIMPLE).sortKey(weight).make());
// sort key requires the label to be non-constrained
assertThrows(IllegalArgumentException.class, () -> ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).multiplicity(Multiplicity.MANY2ONE).sortKey(weight).make());
// Already exists
assertThrows(SchemaViolationException.class, () -> mgmt.makeVertexLabel("tweet").make());
// signature key must have non-generic data type
assertThrows(IllegalArgumentException.class, () -> mgmt.makeEdgeLabel("test").signature(someId).make());
// try {
// //keys must be single-valued
// ((StandardEdgeLabelMaker)mgmt.makeEdgeLabel("other")).
// sortKey(name, weight).make();
// fail();
// ######### END INSPECTION ############
finishSchema();
clopen();
// Load schema types into current transaction
final PropertyKey weight2 = mgmt.getPropertyKey("weight");
uid = mgmt.getPropertyKey("uid");
final PropertyKey someId2 = mgmt.getPropertyKey("someid");
name = mgmt.getPropertyKey("name");
value = mgmt.getPropertyKey("value");
friend = mgmt.getEdgeLabel("friend");
link = mgmt.getEdgeLabel("link");
connect = mgmt.getEdgeLabel("connect");
parent = mgmt.getEdgeLabel("parent");
child = mgmt.getEdgeLabel("child");
spouse = mgmt.getEdgeLabel("spouse");
person = mgmt.getVertexLabel("person");
tag = mgmt.getVertexLabel("tag");
tweet = mgmt.getVertexLabel("tweet");
// ######### INSPECTION & FAILURE (COPIED FROM ABOVE) ############
assertTrue(mgmt.isOpen());
assertEquals("weight", weight2.toString());
assertTrue(mgmt.containsRelationType("weight"));
assertTrue(mgmt.containsPropertyKey("weight"));
assertFalse(mgmt.containsEdgeLabel("weight"));
assertTrue(mgmt.containsEdgeLabel("connect"));
assertFalse(mgmt.containsPropertyKey("connect"));
assertFalse(mgmt.containsRelationType("bla"));
assertNull(mgmt.getPropertyKey("bla"));
assertNull(mgmt.getEdgeLabel("bla"));
assertNotNull(mgmt.getPropertyKey("weight"));
assertNotNull(mgmt.getEdgeLabel("connect"));
assertTrue(weight2.isPropertyKey());
assertFalse(weight2.isEdgeLabel());
assertEquals(Cardinality.SINGLE, weight2.cardinality());
assertEquals(Cardinality.SINGLE, someId2.cardinality());
assertEquals(Cardinality.SET, name.cardinality());
assertEquals(Cardinality.LIST, value.cardinality());
assertEquals(Object.class, someId2.dataType());
assertEquals(Float.class, weight2.dataType());
sig = ((InternalRelationType) value).getSignature();
assertEquals(1, sig.length);
assertEquals(weight2.longId(), sig[0]);
assertTrue(mgmt.getGraphIndex(uid.name()).isUnique());
assertFalse(mgmt.getGraphIndex(someId2.name()).isUnique());
assertEquals("friend", friend.name());
assertTrue(friend.isEdgeLabel());
assertFalse(friend.isPropertyKey());
assertEquals(Multiplicity.ONE2ONE, spouse.multiplicity());
assertEquals(Multiplicity.ONE2MANY, child.multiplicity());
assertEquals(Multiplicity.MANY2ONE, parent.multiplicity());
assertEquals(Multiplicity.MULTI, friend.multiplicity());
assertEquals(Multiplicity.SIMPLE, connect.multiplicity());
assertTrue(link.isUnidirected());
assertFalse(link.isDirected());
assertFalse(child.isUnidirected());
assertTrue(spouse.isDirected());
assertFalse(((InternalRelationType) friend).isInvisibleType());
assertTrue(((InternalRelationType) friend).isInvisible());
assertEquals(0, ((InternalRelationType) friend).getSignature().length);
sig = ((InternalRelationType) connect).getSignature();
assertEquals(1, sig.length);
assertEquals(uid.longId(), sig[0]);
assertEquals(0, ((InternalRelationType) friend).getSortKey().length);
assertEquals(Order.DEFAULT, ((InternalRelationType) friend).getSortOrder());
assertEquals(SchemaStatus.ENABLED, ((InternalRelationType) friend).getStatus());
assertEquals(5, Iterables.size(mgmt.getRelationTypes(PropertyKey.class)));
assertEquals(6, Iterables.size(mgmt.getRelationTypes(EdgeLabel.class)));
assertEquals(11, Iterables.size(mgmt.getRelationTypes(RelationType.class)));
assertEquals(3, Iterables.size(mgmt.getVertexLabels()));
assertEquals("tweet", tweet.name());
assertTrue(mgmt.containsVertexLabel("person"));
assertFalse(mgmt.containsVertexLabel("bla"));
assertFalse(person.isPartitioned());
assertFalse(person.isStatic());
assertFalse(tag.isPartitioned());
assertTrue(tweet.isStatic());
// ------ TRY INVALID STUFF --------
// Failures
assertThrows(IllegalArgumentException.class, () -> mgmt.makePropertyKey("fid").make());
// Already exists
assertThrows(SchemaViolationException.class, () -> mgmt.makeEdgeLabel("link").unidirected().make());
// signature and sort-key collide
assertThrows(IllegalArgumentException.class, () -> ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).sortKey(someId2, weight2).signature(someId2).make());
// sort key requires the label to be non-constrained
assertThrows(IllegalArgumentException.class, () -> ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).multiplicity(Multiplicity.SIMPLE).sortKey(weight2).make());
// sort key requires the label to be non-constrained
assertThrows(IllegalArgumentException.class, () -> ((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("other")).multiplicity(Multiplicity.MANY2ONE).sortKey(weight2).make());
// Already exists
assertThrows(SchemaViolationException.class, () -> mgmt.makeVertexLabel("tweet").make());
// signature key must have non-generic data type
assertThrows(IllegalArgumentException.class, () -> mgmt.makeEdgeLabel("test").signature(someId2).make());
// ######### END INSPECTION ############
/*
####### Make sure schema semantics are honored in transactions ######
*/
clopen();
JanusGraphTransaction tx2;
// shouldn't exist
assertEmpty(tx.query().has("uid", "v1").vertices());
JanusGraphVertex v = tx.addVertex();
// test property keys
v.property("uid", "v1");
v.property("weight", 1.5);
v.property("someid", "Hello");
v.property("name", "Bob");
v.property("name", "John");
VertexProperty p = v.property("value", 11);
p.property("weight", 22);
v.property("value", 33.3, "weight", 66.6);
// same values are supported for list-properties
v.property("value", 11, "weight", 22);
// test edges
JanusGraphVertex v12 = tx.addVertex("person"), v13 = tx.addVertex("person");
v12.property("uid", "v12");
v13.property("uid", "v13");
v12.addEdge("parent", v, "weight", 4.5);
v13.addEdge("parent", v, "weight", 4.5);
v.addEdge("child", v12);
v.addEdge("child", v13);
v.addEdge("spouse", v12);
v.addEdge("friend", v12);
// supports multi edges
v.addEdge("friend", v12);
v.addEdge("connect", v12, "uid", "e1");
v.addEdge("link", v13);
JanusGraphVertex v2 = tx.addVertex("tweet");
v2.addEdge("link", v13);
v12.addEdge("connect", v2);
JanusGraphEdge edge;
// ######### INSPECTION & FAILURE ############
assertEquals(v, getOnlyElement(tx.query().has("uid", Cmp.EQUAL, "v1").vertices()));
v = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v1"));
v12 = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v12"));
v13 = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v13"));
// Invalid data type
JanusGraphVertex finalV = v;
assertThrows(SchemaViolationException.class, () -> finalV.property("weight", "x"));
// Only one "John" should be allowed
assertThrows(SchemaViolationException.class, () -> finalV.property(VertexProperty.Cardinality.list, "name", "John"));
// Cannot set a property as edge
assertThrows(IllegalArgumentException.class, () -> finalV.property("link", finalV));
// Only one property for weight allowed
v.property(VertexProperty.Cardinality.single, "weight", 1.0);
assertCount(1, v.properties("weight"));
v.property(VertexProperty.Cardinality.single, "weight", 0.5);
assertEquals(0.5, v.<Float>value("weight").doubleValue(), 0.00001);
assertEquals("v1", v.value("uid"));
assertCount(2, v.properties("name"));
for (Object prop : v.query().labels("name").properties()) {
String nameString = ((JanusGraphVertexProperty<String>) prop).value();
assertTrue(nameString.equals("Bob") || nameString.equals("John"));
}
assertTrue(Iterators.size(v.properties("value")) >= 3);
for (Object o : v.query().labels("value").properties()) {
JanusGraphVertexProperty<Double> prop = (JanusGraphVertexProperty<Double>) o;
double prec = prop.value();
assertEquals(prec * 2, prop.<Number>value("weight").doubleValue(), 0.00001);
}
// Ensure we can add additional values
p = v.property("value", 44.4, "weight", 88.8);
assertEquals(v, getOnlyElement(tx.query().has("someid", Cmp.EQUAL, "Hello").vertices()));
// ------- EDGES -------
try {
// multiplicity violation
v12.addEdge("parent", v13);
fail();
} catch (SchemaViolationException ignored) {
}
try {
// multiplicity violation
v13.addEdge("child", v12);
fail();
} catch (SchemaViolationException ignored) {
}
try {
// multiplicity violation
v13.addEdge("spouse", v12);
fail();
} catch (SchemaViolationException ignored) {
}
try {
// multiplicity violation
v.addEdge("spouse", v13);
fail();
} catch (SchemaViolationException ignored) {
}
assertCount(2, v.query().direction(Direction.IN).labels("parent").edges());
assertCount(1, v12.query().direction(Direction.OUT).labels("parent").has("weight").edges());
assertCount(1, v13.query().direction(Direction.OUT).labels("parent").has("weight").edges());
assertEquals(v12, getOnlyElement(v.query().direction(Direction.OUT).labels("spouse").vertices()));
edge = Iterables.getOnlyElement(v.query().direction(Direction.BOTH).labels("connect").edges());
assertEquals(1, edge.keys().size());
assertEquals("e1", edge.value("uid"));
try {
// connect is simple
v.addEdge("connect", v12);
fail();
} catch (SchemaViolationException ignored) {
}
// Make sure "link" is unidirected
assertCount(1, v.query().direction(Direction.BOTH).labels("link").edges());
assertCount(0, v13.query().direction(Direction.BOTH).labels("link").edges());
// Assert we can add more friendships
v.addEdge("friend", v12);
v2 = Iterables.getOnlyElement(v12.query().direction(Direction.OUT).labels("connect").vertices());
assertEquals(v13, getOnlyElement(v2.query().direction(Direction.OUT).labels("link").vertices()));
assertEquals(BaseVertexLabel.DEFAULT_VERTEXLABEL.name(), v.label());
assertEquals("person", v12.label());
assertEquals("person", v13.label());
assertCount(4, tx.query().vertices());
// ######### END INSPECTION & FAILURE ############
clopen();
// ######### INSPECTION & FAILURE (copied from above) ############
assertEquals(v, getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v1")));
v = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v1"));
v12 = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v12"));
v13 = getOnlyVertex(tx.query().has("uid", Cmp.EQUAL, "v13"));
JanusGraphVertex finalV1 = v;
assertThrows(SchemaViolationException.class, () -> finalV1.property("weight", "x"));
// Only one "John" should be allowed
assertThrows(SchemaViolationException.class, () -> finalV1.property(VertexProperty.Cardinality.list, "name", "John"));
// Cannot set a property as edge
assertThrows(IllegalArgumentException.class, () -> finalV1.property("link", finalV1));
// Only one property for weight allowed
v.property(VertexProperty.Cardinality.single, "weight", 1.0);
assertCount(1, v.properties("weight"));
v.property(VertexProperty.Cardinality.single, "weight", 0.5);
assertEquals(0.5, v.<Float>value("weight").doubleValue(), 0.00001);
assertEquals("v1", v.value("uid"));
assertCount(2, v.properties("name"));
for (Object o : v.query().labels("name").properties()) {
JanusGraphVertexProperty<String> prop = (JanusGraphVertexProperty<String>) o;
String nameString = prop.value();
assertTrue(nameString.equals("Bob") || nameString.equals("John"));
}
assertTrue(Iterables.size(v.query().labels("value").properties()) >= 3);
for (Object o : v.query().labels("value").properties()) {
JanusGraphVertexProperty<Double> prop = (JanusGraphVertexProperty<Double>) o;
double prec = prop.value();
assertEquals(prec * 2, prop.<Number>value("weight").doubleValue(), 0.00001);
}
// Ensure we can add additional values
p = v.property("value", 44.4, "weight", 88.8);
assertEquals(v, getOnlyElement(tx.query().has("someid", Cmp.EQUAL, "Hello").vertices()));
// ------- EDGES -------
try {
// multiplicity violation
v12.addEdge("parent", v13);
fail();
} catch (SchemaViolationException ignored) {
}
try {
// multiplicity violation
v13.addEdge("child", v12);
fail();
} catch (SchemaViolationException ignored) {
}
try {
// multiplicity violation
v13.addEdge("spouse", v12);
fail();
} catch (SchemaViolationException ignored) {
}
try {
// multiplicity violation
v.addEdge("spouse", v13);
fail();
} catch (SchemaViolationException ignored) {
}
assertCount(2, v.query().direction(Direction.IN).labels("parent").edges());
assertCount(1, v12.query().direction(Direction.OUT).labels("parent").has("weight").edges());
assertCount(1, v13.query().direction(Direction.OUT).labels("parent").has("weight").edges());
assertEquals(v12, getOnlyElement(v.query().direction(Direction.OUT).labels("spouse").vertices()));
edge = Iterables.getOnlyElement(v.query().direction(Direction.BOTH).labels("connect").edges());
assertEquals(1, edge.keys().size());
assertEquals("e1", edge.value("uid"));
try {
// connect is simple
v.addEdge("connect", v12);
fail();
} catch (SchemaViolationException ignored) {
}
// Make sure "link" is unidirected
assertCount(1, v.query().direction(Direction.BOTH).labels("link").edges());
assertCount(0, v13.query().direction(Direction.BOTH).labels("link").edges());
// Assert we can add more friendships
v.addEdge("friend", v12);
v2 = Iterables.getOnlyElement(v12.query().direction(Direction.OUT).labels("connect").vertices());
assertEquals(v13, getOnlyElement(v2.query().direction(Direction.OUT).labels("link").vertices()));
assertEquals(BaseVertexLabel.DEFAULT_VERTEXLABEL.name(), v.label());
assertEquals("person", v12.label());
assertEquals("person", v13.label());
assertCount(4, tx.query().vertices());
// ######### END INSPECTION & FAILURE ############
// Ensure index uniqueness enforcement
tx2 = graph.newTransaction();
try {
JanusGraphVertex vx = tx2.addVertex();
try {
// property is unique
vx.property(VertexProperty.Cardinality.single, "uid", "v1");
fail();
} catch (SchemaViolationException ignored) {
}
vx.property(VertexProperty.Cardinality.single, "uid", "unique");
JanusGraphVertex vx2 = tx2.addVertex();
try {
// property unique
vx2.property(VertexProperty.Cardinality.single, "uid", "unique");
fail();
} catch (SchemaViolationException ignored) {
}
} finally {
tx2.rollback();
}
// Ensure that v2 is really static
v2 = getV(tx, v2);
assertEquals("tweet", v2.label());
try {
v2.property(VertexProperty.Cardinality.single, "weight", 11);
fail();
} catch (SchemaViolationException ignored) {
}
try {
v2.addEdge("friend", v12);
fail();
} catch (SchemaViolationException ignored) {
}
// Ensure that unidirected edges keep pointing to deleted vertices
getV(tx, v13).remove();
assertCount(1, v.query().direction(Direction.BOTH).labels("link").edges());
}
use of org.janusgraph.core.JanusGraphVertexProperty 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.janusgraph.core.JanusGraphVertexProperty 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();
}
}
Aggregations