use of org.janusgraph.core.JanusGraph in project grakn by graknlabs.
the class TxFactoryJanusTest method testSingleton.
@Test
public void testSingleton() {
when(session.keyspace()).thenReturn(Keyspace.of("anothertest"));
TxFactoryJanus factory = new TxFactoryJanus(session);
GraknTxJanus mg1 = factory.open(GraknTxType.BATCH);
JanusGraph tinkerGraphMg1 = mg1.getTinkerPopGraph();
mg1.close();
GraknTxJanus mg2 = factory.open(GraknTxType.WRITE);
JanusGraph tinkerGraphMg2 = mg2.getTinkerPopGraph();
mg2.close();
GraknTxJanus mg3 = factory.open(GraknTxType.BATCH);
assertEquals(mg1, mg3);
assertEquals(tinkerGraphMg1, mg3.getTinkerPopGraph());
assertTrue(mg1.isBatchTx());
assertFalse(mg2.isBatchTx());
assertNotEquals(mg1, mg2);
assertNotEquals(tinkerGraphMg1, tinkerGraphMg2);
StandardJanusGraph standardJanusGraph1 = (StandardJanusGraph) tinkerGraphMg1;
StandardJanusGraph standardJanusGraph2 = (StandardJanusGraph) tinkerGraphMg2;
assertTrue(standardJanusGraph1.getConfiguration().isBatchLoading());
assertFalse(standardJanusGraph2.getConfiguration().isBatchLoading());
}
use of org.janusgraph.core.JanusGraph in project janusgraph by JanusGraph.
the class JanusGraphTest method setAndCheckGraphOption.
private <A> void setAndCheckGraphOption(ConfigOption<A> opt, ConfigOption.Type requiredType, A firstValue, A secondValue) {
// Sanity check: make sure the Type of the configuration option is what we expect
Preconditions.checkState(opt.getType().equals(requiredType));
final EnumSet<ConfigOption.Type> allowedTypes = EnumSet.of(ConfigOption.Type.GLOBAL, ConfigOption.Type.GLOBAL_OFFLINE, ConfigOption.Type.MASKABLE);
Preconditions.checkState(allowedTypes.contains(opt.getType()));
// Sanity check: it's kind of pointless for the first and second values to be identical
Preconditions.checkArgument(!firstValue.equals(secondValue));
// Get full string path of config option
final String path = ConfigElement.getPath(opt);
// Set and check initial value before and after database restart
mgmt.set(path, firstValue);
assertEquals(firstValue.toString(), mgmt.get(path));
// Close open tx first. This is specific to BDB + GLOBAL_OFFLINE.
// Basically: the BDB store manager throws a fit if shutdown is called
// with one or more transactions still open, and GLOBAL_OFFLINE calls
// shutdown on our behalf when we commit this change.
tx.rollback();
mgmt.commit();
clopen();
// Close tx again following clopen
tx.rollback();
assertEquals(firstValue.toString(), mgmt.get(path));
// Set and check updated value before and after database restart
mgmt.set(path, secondValue);
assertEquals(secondValue.toString(), mgmt.get(path));
mgmt.commit();
clopen();
tx.rollback();
assertEquals(secondValue.toString(), mgmt.get(path));
// Open a separate graph "g2"
JanusGraph g2 = JanusGraphFactory.open(config);
JanusGraphManagement m2 = g2.openManagement();
assertEquals(secondValue.toString(), m2.get(path));
// GLOBAL_OFFLINE options should be unmodifiable with g2 open
if (opt.getType().equals(ConfigOption.Type.GLOBAL_OFFLINE)) {
try {
mgmt.set(path, firstValue);
mgmt.commit();
fail("Option " + path + " with type " + ConfigOption.Type.GLOBAL_OFFLINE + " should not be modifiable with concurrent instances");
} catch (RuntimeException e) {
log.debug("Caught expected exception", e);
}
assertEquals(secondValue.toString(), mgmt.get(path));
// GLOBAL and MASKABLE should be modifiable even with g2 open
} else {
mgmt.set(path, firstValue);
assertEquals(firstValue.toString(), mgmt.get(path));
mgmt.commit();
clopen();
assertEquals(firstValue.toString(), mgmt.get(path));
}
m2.rollback();
g2.close();
}
use of org.janusgraph.core.JanusGraph in project janusgraph by JanusGraph.
the class JanusGraphStepStrategyTest method generateTestParameters.
@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> generateTestParameters() {
final StandardJanusGraph graph = (StandardJanusGraph) StorageSetup.getInMemoryGraph();
final GraphTraversalSource g = graph.traversal();
// create a basic schema so that order pushdown can be tested as this optimization requires a JanusGraph
// transaction registered against a non-EmptyGraph
final JanusGraphManagement mgmt = graph.openManagement();
mgmt.makePropertyKey("name").dataType(String.class).make();
mgmt.makePropertyKey("lang").dataType(String.class).make();
mgmt.commit();
return Arrays.asList(new Object[][] { { g.V().out(), g_V().out(), Collections.emptyList() }, { g.V().has("name", "marko").out(), g_V("name", eq("marko")).out(), Collections.emptyList() }, { g.V().has("name", "marko").has("age", gt(31).and(lt(10))).out(), g_V("name", eq("marko"), "age", gt(31), "age", lt(10)).out(), Collections.emptyList() }, { g.V().has("name", "marko").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))), Collections.singletonList(FilterRankingStrategy.instance()) }, { g.V().has("name", "marko").as("a").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko")).as("a").or(has("age"), has("age", gt(32))).has("lang", "java"), Collections.emptyList() }, { g.V().has("name", "marko").as("a").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))).as("a"), Collections.singletonList(FilterRankingStrategy.instance()) }, { g.V().dedup().has("name", "marko").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))).dedup(), Collections.singletonList(FilterRankingStrategy.instance()) }, { g.V().as("a").dedup().has("name", "marko").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))).dedup().as("a"), Collections.singletonList(FilterRankingStrategy.instance()) }, { g.V().as("a").has("name", "marko").as("b").or(has("age"), has("age", gt(32))).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java")).or(has("age"), has("age", gt(32))).as("b", "a"), Collections.singletonList(FilterRankingStrategy.instance()) }, { g.V().as("a").dedup().has("name", "marko").or(has("age"), has("age", gt(32))).filter(has("name", "bob")).has("lang", "java"), g_V("name", eq("marko"), "lang", eq("java"), "name", eq("bob")).or(has("age"), has("age", gt(32))).dedup().as("a"), Arrays.asList(InlineFilterStrategy.instance(), FilterRankingStrategy.instance()) }, { g.V().has("name", "marko").or(not(has("age")), has("age", gt(32))).has("name", "bob").has("lang", "java"), g_V("name", eq("marko"), "name", eq("bob"), "lang", eq("java")).or(not(filter(properties("age"))), has("age", gt(32))), TraversalStrategies.GlobalCache.getStrategies(JanusGraph.class).toList() }, { g.V().has("name", eq("marko").and(eq("bob").and(eq("stephen")))).out("knows"), g_V("name", eq("marko"), "name", eq("bob"), "name", eq("stephen")).out("knows"), Collections.emptyList() }, { g.V().hasId(1), g_V(T.id, 1), Collections.emptyList() }, { g.V().hasId(within(1, 2)), g_V(T.id, 1, T.id, 2), Collections.emptyList() }, { g.V().hasId(1).has("name", "marko"), g_V(T.id, 1, "name", eq("marko")), Collections.emptyList() }, { g.V().hasId(1).hasLabel("Person"), g_V(T.id, 1, "~label", eq("Person")), Collections.emptyList() }, { g.V().hasLabel("Person").has("lang", "java").order().by("name"), g_V("~label", eq("Person"), "lang", eq("java"), new HasStepFolder.OrderEntry("name", Order.incr)), Collections.emptyList() }, // same as above, different order
{ g.V().hasLabel("Person").has("lang", "java").order().by("name", Order.decr), g_V("~label", eq("Person"), "lang", eq("java"), new HasStepFolder.OrderEntry("name", Order.decr)), Collections.emptyList() }, // if multiple order steps are specified in a row, only the last will be folded in because it overrides previous ordering
{ g.V().hasLabel("Person").has("lang", "java").order().by("lang", Order.incr).order().by("name", Order.decr), g_V("~label", eq("Person"), "lang", eq("java"), new HasStepFolder.OrderEntry("name", Order.decr)), Collections.emptyList() }, // do not folder in orders that include a nested traversal
{ g.V().hasLabel("Person").order().by(values("age")), g_V("~label", eq("Person")).order().by(values("age")), Collections.emptyList() }, // age property is not registered in the schema so the order should not be folded in
{ g.V().hasLabel("Person").has("lang", "java").order().by("age"), g_V("~label", eq("Person"), "lang", eq("java")).order().by("age"), Collections.emptyList() }, // into a single within(ids) lookup
{ g.V().hasId(1).hasId(2), g_V(T.id, 1).hasId(2), Collections.emptyList() } });
}
use of org.janusgraph.core.JanusGraph in project janusgraph by JanusGraph.
the class VertexIDAssignerTest method testCustomIdAssignment.
private void testCustomIdAssignment(CustomIdStrategy idStrategy) {
LongSet vertexIds = new LongHashSet();
final long maxCount = idAssigner.getIDManager().getVertexCountBound();
long count = 1;
for (int trial = 0; trial < 10; trial++) {
final JanusGraph graph = getInMemoryGraph(true, true);
int numVertices = 1000;
final List<JanusGraphVertex> vertices = new ArrayList<>(numVertices);
try {
for (int i = 0; i < numVertices; i++, count++) {
final long userVertexId;
switch(idStrategy) {
case LOW:
userVertexId = count;
break;
case HIGH:
userVertexId = maxCount - count;
break;
default:
throw new RuntimeException("Unsupported custom id strategy: " + idStrategy);
}
final long id = idAssigner.getIDManager().toVertexId(userVertexId);
JanusGraphVertex next = graph.addVertex(T.id, id, "user_id", userVertexId);
vertices.add(next);
}
// Verify that ids are set, unique and consistent with user id basis
for (JanusGraphVertex v : vertices) {
assertTrue(v.hasId());
long id = v.longId();
assertTrue(id > 0 && id < Long.MAX_VALUE);
assertTrue(vertexIds.add(id));
assertEquals((long) v.value("user_id"), idAssigner.getIDManager().fromVertexId(id));
}
} finally {
graph.tx().rollback();
graph.close();
}
}
}
use of org.janusgraph.core.JanusGraph in project janusgraph by JanusGraph.
the class JanusGraphAppTest method createSchema.
@Test
public void createSchema() throws ConfigurationException {
final JanusGraphApp app = new JanusGraphApp(CONF_FILE);
final GraphTraversalSource g = app.openGraph();
app.createSchema();
final JanusGraph janusGraph = (JanusGraph) g.getGraph();
final JanusGraphManagement management = janusGraph.openManagement();
final List<String> vertexLabels = StreamSupport.stream(management.getVertexLabels().spliterator(), false).map(Namifiable::name).collect(Collectors.toList());
final List<String> expectedVertexLabels = Stream.of("titan", "location", "god", "demigod", "human", "monster").collect(Collectors.toList());
assertTrue(vertexLabels.containsAll(expectedVertexLabels));
final List<String> edgeLabels = StreamSupport.stream(management.getRelationTypes(EdgeLabel.class).spliterator(), false).map(Namifiable::name).collect(Collectors.toList());
final List<String> expectedEdgeLabels = Stream.of("father", "mother", "brother", "pet", "lives", "battled").collect(Collectors.toList());
assertTrue(edgeLabels.containsAll(expectedEdgeLabels));
final EdgeLabel father = management.getEdgeLabel("father");
assertTrue(father.isDirected());
assertFalse(father.isUnidirected());
assertEquals(Multiplicity.MANY2ONE, father.multiplicity());
final List<String> propertyKeys = StreamSupport.stream(management.getRelationTypes(PropertyKey.class).spliterator(), false).map(Namifiable::name).collect(Collectors.toList());
final List<String> expectedPropertyKeys = Stream.of("name", "age", "time", "place", "reason").collect(Collectors.toList());
assertTrue(propertyKeys.containsAll(expectedPropertyKeys));
final PropertyKey place = management.getPropertyKey("place");
assertEquals(Cardinality.SINGLE, place.cardinality());
assertEquals(Geoshape.class, place.dataType());
final JanusGraphIndex nameIndex = management.getGraphIndex("nameIndex");
assertTrue(nameIndex.isCompositeIndex());
assertTrue(nameIndex.getIndexedElement().equals(JanusGraphVertex.class));
final PropertyKey[] nameIndexKeys = nameIndex.getFieldKeys();
assertEquals(1, nameIndexKeys.length);
assertEquals("name", nameIndexKeys[0].name());
}
Aggregations