use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.
the class JanusGraphTest method testVertexTTLWithCompositeIndex.
@Test
@FeatureFlag(feature = JanusGraphFeature.CellTtl)
public void testVertexTTLWithCompositeIndex() throws Exception {
PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make();
PropertyKey time = mgmt.makePropertyKey("time").dataType(Long.class).make();
final JanusGraphIndex index1 = mgmt.buildIndex("index1", Vertex.class).addKey(name).buildCompositeIndex();
final JanusGraphIndex index2 = mgmt.buildIndex("index2", Vertex.class).addKey(name).addKey(time).buildCompositeIndex();
VertexLabel label1 = mgmt.makeVertexLabel("event").setStatic().make();
mgmt.setTTL(label1, Duration.ofSeconds(1));
assertEquals(Duration.ZERO, mgmt.getTTL(name));
assertEquals(Duration.ZERO, mgmt.getTTL(time));
assertEquals(Duration.ofSeconds(1), mgmt.getTTL(label1));
mgmt.commit();
JanusGraphVertex v1 = tx.addVertex(T.label, "event", "name", "some event", "time", System.currentTimeMillis());
tx.commit();
Object id = v1.id();
v1 = getV(graph, id);
assertNotNull(v1);
assertNotEmpty(graph.query().has("name", "some event").vertices());
Thread.sleep(1001);
graph.tx().rollback();
v1 = getV(graph, id);
assertNull(v1);
assertEmpty(graph.query().has("name", "some event").vertices());
}
use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.
the class JanusGraphIndexTest method shouldUpdateIndexFieldsAfterIndexModification.
@RepeatedIfExceptionsTest(repeats = 4, minSuccess = 2)
public void shouldUpdateIndexFieldsAfterIndexModification() throws InterruptedException, ExecutionException {
clopen(option(FORCE_INDEX_USAGE), true, option(LOG_READ_INTERVAL, MANAGEMENT_LOG), Duration.ofMillis(5000));
String key1 = "testKey1";
String key2 = "testKey2";
String key3 = "testKey3";
String vertexL = "testVertexLabel";
String indexName = "mixed";
PropertyKey p1 = mgmt.makePropertyKey(key1).dataType(Long.class).make();
PropertyKey p2 = mgmt.makePropertyKey(key2).dataType(Long.class).make();
mgmt.makeVertexLabel(vertexL).make();
JanusGraphIndex index = mgmt.buildIndex(indexName, Vertex.class).indexOnly(mgmt.getVertexLabel(vertexL)).addKey(mgmt.getPropertyKey(key1)).addKey(mgmt.getPropertyKey(key2)).buildMixedIndex(INDEX);
if (index.getIndexStatus(p1) == SchemaStatus.INSTALLED) {
mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.REGISTER_INDEX).get();
mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.ENABLE_INDEX).get();
} else if (index.getIndexStatus(p1) == SchemaStatus.REGISTERED) {
mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.ENABLE_INDEX).get();
}
mgmt.commit();
JanusGraphVertex vertex = graph.addVertex(vertexL);
vertex.property(key1, 111L);
vertex.property(key2, 222L);
graph.tx().commit();
// By default ES indexes documents each second. By sleeping here we guarantee that documents are indexed.
// It is just for testing. A better approach is to use Refresh API.
Thread.sleep(1500L);
// we open an implicit transaction and do not commit/rollback it by intention, so that we can ensure
// adding new property to the index wouldn't cause existing transactions to fail
assertEquals(1, graph.traversal().V().hasLabel(vertexL).has(key1, 111L).count().next());
assertEquals(1, graph.traversal().V().hasLabel(vertexL).has(key1, 111L).toList().size());
JanusGraph graph2 = JanusGraphFactory.open(config);
assertEquals(1, graph2.traversal().V().hasLabel(vertexL).has(key1, 111L).count().next());
assertEquals(1, graph2.traversal().V().hasLabel(vertexL).has(key1, 111L).toList().size());
mgmt = graph.openManagement();
PropertyKey testKey3 = mgmt.makePropertyKey(key3).dataType(Long.class).make();
mgmt.addIndexKey(mgmt.getGraphIndex(indexName), testKey3);
mgmt.commit();
ManagementSystem.awaitGraphIndexStatus(graph, indexName).status(SchemaStatus.REGISTERED, SchemaStatus.ENABLED).call();
mgmt = graph.openManagement();
mgmt.updateIndex(mgmt.getGraphIndex(indexName), SchemaAction.REINDEX).get();
mgmt.commit();
graph.addVertex(T.label, vertexL, key1, 1L, key2, 2L, key3, 3L);
graph.tx().commit();
assertTrue(graph.traversal().V().hasLabel(vertexL).has(key3, 3L).hasNext());
try {
graph2.addVertex(T.label, vertexL, key1, 1L, key2, 2L, key3, 3L);
// this assertion might be flaky which is why we mark this test as RepeatedIfExceptionsTest.
// the reason is, we cannot make sure when the schema update broadcast will be received by the graph2
// instance.
JanusGraphException ex = assertThrows(JanusGraphException.class, () -> graph2.tx().commit());
assertEquals("testKey3 is not available in mixed index mixed", ex.getCause().getMessage());
// graph2 needs some time to read from ManagementLogger asynchronously and updates cache
// LOG_READ_INTERVAL is 5 seconds, so we wait for the same time here to ensure periodic read is triggered
Thread.sleep(5000);
graph2.tx().commit();
assertTrue(graph2.traversal().V().hasLabel(vertexL).has(key3, 3L).hasNext());
} finally {
graph2.close();
}
}
use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.
the class JanusGraphIndexTest method shouldAwaitMultipleStatuses.
@Test
public void shouldAwaitMultipleStatuses() throws InterruptedException, ExecutionException {
final PropertyKey key1 = makeKey("key1", String.class);
final JanusGraphIndex index = mgmt.buildIndex("randomMixedIndex", Vertex.class).addKey(key1).buildMixedIndex(INDEX);
if (index.getIndexStatus(key1) == SchemaStatus.INSTALLED) {
mgmt.updateIndex(mgmt.getGraphIndex("randomMixedIndex"), SchemaAction.REGISTER_INDEX).get();
mgmt.updateIndex(mgmt.getGraphIndex("randomMixedIndex"), SchemaAction.ENABLE_INDEX).get();
} else if (index.getIndexStatus(key1) == SchemaStatus.REGISTERED) {
mgmt.updateIndex(mgmt.getGraphIndex("randomMixedIndex"), SchemaAction.ENABLE_INDEX).get();
}
final PropertyKey key2 = makeKey("key2", String.class);
mgmt.addIndexKey(index, key2);
mgmt.commit();
// key1 now has status ENABLED, let's ensure we can watch for REGISTERED and ENABLED
try {
ManagementSystem.awaitGraphIndexStatus(graph, "randomMixedIndex").status(SchemaStatus.REGISTERED, SchemaStatus.ENABLED).call();
} catch (final Exception e) {
fail("Failed to awaitGraphIndexStatus on multiple statuses.");
}
}
use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.
the class JanusGraphBaseTest method getExternalIndex.
public JanusGraphIndex getExternalIndex(Class<? extends Element> clazz, String backingIndex) {
String prefix;
if (Vertex.class.isAssignableFrom(clazz))
prefix = "v";
else if (Edge.class.isAssignableFrom(clazz))
prefix = "e";
else if (JanusGraphVertexProperty.class.isAssignableFrom(clazz))
prefix = "p";
else
throw new AssertionError(clazz.toString());
final String indexName = prefix + backingIndex;
JanusGraphIndex index = mgmt.getGraphIndex(indexName);
if (index == null) {
index = mgmt.buildIndex(indexName, clazz).buildMixedIndex(backingIndex);
}
return index;
}
use of org.janusgraph.core.schema.JanusGraphIndex in project janusgraph by JanusGraph.
the class JanusGraphOperationCountingTest method checkPropertyLockingAndIndex.
@Test
public void checkPropertyLockingAndIndex() {
PropertyKey uid = makeKey("uid", String.class);
JanusGraphIndex index = mgmt.buildIndex("uid", Vertex.class).unique().addKey(uid).buildCompositeIndex();
mgmt.setConsistency(index, ConsistencyModifier.LOCK);
mgmt.makePropertyKey("name").dataType(String.class).make();
mgmt.makePropertyKey("age").dataType(Integer.class).make();
finishSchema();
metricsPrefix = "checkPropertyLockingAndIndex";
JanusGraphTransaction tx = graph.buildTransaction().groupName(metricsPrefix).start();
JanusGraphVertex v = tx.addVertex("uid", "v1", "age", 25, "name", "john");
assertEquals(25, v.property("age").value());
tx.commit();
verifyStoreMetrics(EDGESTORE_NAME);
verifyLockingOverwrite(1);
verifyStoreMetrics(METRICS_STOREMANAGER_NAME, ImmutableMap.of(M_MUTATE, 1L));
resetMetrics();
tx = graph.buildTransaction().groupName(metricsPrefix).start();
v = Iterables.getOnlyElement(tx.query().has("uid", Cmp.EQUAL, "v1").vertices());
assertEquals(25, v.property("age").value());
tx.commit();
verifyStoreMetrics(EDGESTORE_NAME, ImmutableMap.of(M_GET_SLICE, 1L));
verifyStoreMetrics(INDEXSTORE_NAME, ImmutableMap.of(M_GET_SLICE, 1L));
verifyStoreMetrics(METRICS_STOREMANAGER_NAME);
}
Aggregations