use of com.vaticle.typedb.core.common.parameters.Arguments.Transaction.Type.WRITE in project grakn by graknlabs.
the class RocksIteratorTest method attributesRetrievedAscending.
@Test
public void attributesRetrievedAscending() {
List<String> strings = new ArrayList<>();
for (int i = 0; i < 10_000; i++) {
strings.add(UUID.randomUUID().toString());
}
try (TypeDB.Transaction transaction = session.transaction(WRITE)) {
AttributeType.String stringValueType = transaction.concepts().getAttributeType("string-value").asString();
for (String string : strings) {
stringValueType.put(string);
}
transaction.commit();
}
// test ascending order
strings.sort(Comparator.naturalOrder());
try (CoreTransaction transaction = session.transaction(READ)) {
Storage.Data storage = transaction.graphMgr.data().storage();
AttributeType.String stringValueType = transaction.concepts().getAttributeType("string-value").asString();
VertexIID.Type iid = ((AttributeTypeImpl) stringValueType).vertex.iid();
Storage.Key.Prefix<VertexIID.Thing> iteratePrefix = VertexIID.Thing.Attribute.String.prefix(iid);
List<String> values = storage.iterate(iteratePrefix, ASC).map(kv -> kv.key().asAttribute().asString().value()).toList();
assertEquals(strings, values);
}
// test descending order
strings.sort(Comparator.reverseOrder());
try (CoreTransaction transaction = session.transaction(READ)) {
Storage.Data storage = transaction.graphMgr.data().storage();
AttributeType.String stringValueType = transaction.concepts().getAttributeType("string-value").asString();
VertexIID.Type iid = ((AttributeTypeImpl) stringValueType).vertex.iid();
Storage.Key.Prefix<VertexIID.Thing> iteratePrefix = VertexIID.Thing.Attribute.String.prefix(iid);
List<String> values = storage.iterate(iteratePrefix, DESC).map(kv -> kv.key().asAttribute().asString().value()).toList();
assertEquals(strings, values);
}
}
use of com.vaticle.typedb.core.common.parameters.Arguments.Transaction.Type.WRITE in project grakn by graknlabs.
the class EncodingTest method encoded_keys_are_correct.
@Test
public void encoded_keys_are_correct() {
try (CoreTransaction transaction = session.transaction(WRITE)) {
Storage.Data storage = transaction.traversal().graph().data().storage();
assertEquals(0, vertexElements(storage).size());
assertEquals(0, variableStartEdgeElements(storage).size());
assertEquals(0, fixedStartEdgeElements(storage).size());
assertEquals(0, optimisationEdgeElements(storage).size());
transaction.query().insert(TypeQL.parseQuery("insert " + "$p isa person, has name 'Alice';" + "$c isa company, has company-id 10;" + "(employer: $c, employee: $p) isa employment;").asInsert());
transaction.commit();
}
try (CoreTransaction transaction = session.transaction(WRITE)) {
GraphManager graph = transaction.traversal().graph();
Storage.Data storage = graph.data().storage();
List<KeyValue<RawKey, ByteArray>> vertexKVs = vertexElements(storage);
assertTrue(iterate(vertexKVs).allMatch(kv -> kv.value().isEmpty()));
List<ByteArray> vertices = iterate(vertexKVs).map(kv -> kv.key().bytes()).toList();
// we must have exactly the expected set of vertex bytes
assertEquals(7, vertices.size());
ByteArray personIID = expectedIID(graph, Label.of("person"), 0);
assertTrue(vertices.contains(personIID));
ByteArray companyIID = expectedIID(graph, Label.of("company"), 0);
assertTrue(vertices.contains(companyIID));
ByteArray employmentIID = expectedIID(graph, Label.of("employment"), 0);
assertTrue(vertices.contains(employmentIID));
ByteArray employeeRoleIID = expectedIID(graph, Label.of("employee", "employment"), 0);
assertTrue(vertices.contains(employeeRoleIID));
ByteArray employerRoleIID = expectedIID(graph, Label.of("employer", "employment"), 0);
assertTrue(vertices.contains(employerRoleIID));
ByteArray nameAliceIID = expectedAttributeIID(graph, Label.of("name"), "Alice");
assertTrue(vertices.contains(nameAliceIID));
ByteArray companyId10IID = expectedAttributeIID(graph, Label.of("company-id"), 10);
assertTrue(vertices.contains(companyId10IID));
// we must have exactly the right set of fixed length edges: relates, plays, and forward has
List<KeyValue<RawKey, ByteArray>> fixedKVs = fixedStartEdgeElements(storage);
assertTrue(iterate(fixedKVs).allMatch(kv -> kv.value().isEmpty()));
List<ByteArray> fixed = iterate(fixedKVs).map(kv -> kv.key().bytes()).toList();
assertEquals(10, fixed.size());
assertTrue(fixed.contains(join(personIID, HAS.forward().bytes(), nameAliceIID)));
assertTrue(fixed.contains(join(personIID, PLAYING.forward().bytes(), employeeRoleIID)));
assertTrue(fixed.contains(join(employeeRoleIID, PLAYING.backward().bytes(), personIID)));
assertTrue(fixed.contains(join(employeeRoleIID, RELATING.backward().bytes(), employmentIID)));
assertTrue(fixed.contains(join(employmentIID, RELATING.forward().bytes(), employeeRoleIID)));
assertTrue(fixed.contains(join(employmentIID, RELATING.forward().bytes(), employerRoleIID)));
assertTrue(fixed.contains(join(employerRoleIID, RELATING.backward().bytes(), employmentIID)));
assertTrue(fixed.contains(join(employerRoleIID, PLAYING.backward().bytes(), companyIID)));
assertTrue(fixed.contains(join(companyIID, PLAYING.forward().bytes(), employerRoleIID)));
assertTrue(fixed.contains(join(companyIID, HAS.forward().bytes(), companyId10IID)));
// we must have exactly the right set of variable length edges: backward has
List<KeyValue<RawKey, ByteArray>> variableKVs = variableStartEdgeElements(storage);
assertTrue(iterate(variableKVs).allMatch(kv -> kv.value().isEmpty()));
List<ByteArray> variable = iterate(variableKVs).map(kv -> kv.key().bytes()).toList();
assertEquals(2, variable.size());
assertTrue(variable.contains(join(nameAliceIID, HAS.backward().bytes(), personIID)));
assertTrue(variable.contains(join(companyId10IID, HAS.backward().bytes(), companyIID)));
// we must have exactly the right set of optimisation edges: role player
List<KeyValue<RawKey, ByteArray>> optimisationKVs = optimisationEdgeElements(storage);
assertTrue(iterate(optimisationKVs).allMatch(kv -> kv.value().isEmpty()));
List<ByteArray> optimisation = iterate(optimisationKVs).map(kv -> kv.key().bytes()).toList();
assertEquals(4, optimisation.size());
TypeVertex employerRoleType = graph.schema().getType(Label.of("employer", "employment"));
TypeVertex employeeRoleType = graph.schema().getType(Label.of("employee", "employment"));
assertTrue(optimisation.contains(join(personIID, ROLEPLAYER.backward().bytes(), employeeRoleType.iid().bytes(), employmentIID, employeeRoleIID.view(1 + employeeRoleType.iid().bytes().length()))));
assertTrue(optimisation.contains(join(employmentIID, ROLEPLAYER.forward().bytes(), employeeRoleType.iid().bytes(), personIID, employeeRoleIID.view(1 + employeeRoleType.iid().bytes().length()))));
assertTrue(optimisation.contains(join(companyIID, ROLEPLAYER.backward().bytes(), employerRoleType.iid().bytes(), employmentIID, employerRoleIID.view(1 + employerRoleType.iid().bytes().length()))));
assertTrue(optimisation.contains(join(employmentIID, ROLEPLAYER.forward().bytes(), employerRoleType.iid().bytes(), companyIID, employerRoleIID.view(1 + employerRoleType.iid().bytes().length()))));
}
}
Aggregations