use of com.carrotsearch.hppc.LongArrayList in project titan by thinkaurelius.
the class TitanPartitionGraphTest method testVertexPartitioning.
@Test
public void testVertexPartitioning() throws Exception {
Object[] options = { option(GraphDatabaseConfiguration.IDS_FLUSH), false };
clopen(options);
PropertyKey gid = makeVertexIndexedUniqueKey("gid", Integer.class);
PropertyKey sig = makeKey("sig", Integer.class);
PropertyKey name = mgmt.makePropertyKey("name").cardinality(Cardinality.LIST).dataType(String.class).make();
EdgeLabel knows = makeLabel("knows");
EdgeLabel base = makeLabel("base");
EdgeLabel one = mgmt.makeEdgeLabel("one").multiplicity(Multiplicity.ONE2ONE).make();
VertexLabel person = mgmt.makeVertexLabel("person").make();
VertexLabel group = 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++) {
TitanVertex 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++) {
TitanVertex 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);
TitanVertex g = getV(tx, gId);
final int canonicalPartition = getPartitionID(g);
assertEquals(g, (Vertex) 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
TitanVertexProperty p = (TitanVertexProperty) getOnlyElement(g.properties("gid"));
assertEquals(canonicalPartition, getPartitionID(p));
for (Iterator<VertexProperty<Object>> niter = g.properties("name"); niter.hasNext(); ) {
assertEquals(canonicalPartition, getPartitionID((TitanVertex) 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++) {
TitanVertex g1 = getV(tx, gids[0]), g2 = getV(tx, gids[1]);
assertNotNull(g1);
TitanVertex[] vs = new TitanVertex[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
TitanTransaction 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 numRels = 0;
TitanVertex v = getV(txx, vs[vi].longId());
for (TitanRelation r : v.query().relations()) {
numRels++;
assertEquals(partition, getPartitionID(r));
if (r instanceof TitanEdge) {
TitanVertex o = ((TitanEdge) r).otherVertex(v);
assertTrue(o.equals(g1) || o.equals(g2));
}
}
assertEquals(3 + (vi % 2 == 0 ? 1 : 0), numRels);
}
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
TitanVertex 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[] partarr = new int[numP];
int i = 0;
for (Integer part : parts) partarr[i++] = part;
TitanTransaction tx2 = graph.buildTransaction().restrictedPartitions(partarr).readOnly().start();
//Copied from above
g1 = getV(tx2, gids[0]);
assertEquals(0, g1.<Integer>value("gid").intValue());
assertEquals("group", g1.label());
assertTrue(names.size() >= 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
TitanVertex 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