|
102 | 102 | import org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex; |
103 | 103 | import org.janusgraph.graphdb.vertices.CacheVertex; |
104 | 104 | import org.janusgraph.testutil.TestGraphConfigs; |
| 105 | +import org.javatuples.Pair; |
105 | 106 | import org.junit.jupiter.api.Tag; |
106 | 107 | import org.junit.jupiter.api.Test; |
107 | 108 | import org.junit.jupiter.api.TestInfo; |
@@ -1451,6 +1452,94 @@ public void testCompositeVsMixedIndexing() { |
1451 | 1452 | assertTrue(tx.traversal().V().has("intId2", 234).hasNext()); |
1452 | 1453 | } |
1453 | 1454 |
|
| 1455 | + @Test |
| 1456 | + public void testSubsetReindex() throws Exception { |
| 1457 | + |
| 1458 | + clopen(option(FORCE_INDEX_USAGE), true); |
| 1459 | + |
| 1460 | + mgmt.makeVertexLabel("cat").make(); |
| 1461 | + mgmt.makeVertexLabel("dog").make(); |
| 1462 | + |
| 1463 | + makeKey("id", Integer.class); |
| 1464 | + makeKey("name", String.class); |
| 1465 | + final PropertyKey typeKey = makeKey("type", String.class); |
| 1466 | + |
| 1467 | + String typeIndex = "searchByType"; |
| 1468 | + mgmt.buildIndex(typeIndex, Vertex.class) |
| 1469 | + .addKey(typeKey) |
| 1470 | + .buildCompositeIndex(); |
| 1471 | + mgmt.commit(); |
| 1472 | + |
| 1473 | + //Cats |
| 1474 | + int catsCount = 3; |
| 1475 | + for (int i = 0; i < catsCount; i++) { |
| 1476 | + Vertex v = tx.addVertex("cat"); |
| 1477 | + v.property("id", i); |
| 1478 | + v.property("name", "cat_" + i); |
| 1479 | + v.property("type", "cat"); |
| 1480 | + } |
| 1481 | + |
| 1482 | + //Dogs |
| 1483 | + for (int i = 0; i < 5; i++) { |
| 1484 | + Vertex v = tx.addVertex("dog"); |
| 1485 | + v.property("id", i); |
| 1486 | + v.property("name", "dog_" + i); |
| 1487 | + v.property("type", "dog"); |
| 1488 | + } |
| 1489 | + |
| 1490 | + tx.commit(); |
| 1491 | + |
| 1492 | + //Select a subset of vertices to index |
| 1493 | + clopen(option(FORCE_INDEX_USAGE), true); |
| 1494 | + List<Vertex> cats = tx.traversal().V().has("type", "cat").toList(); |
| 1495 | + assertEquals(catsCount, cats.size()); |
| 1496 | + String excludedCat = cats.get(cats.size() - 1).value("name"); |
| 1497 | + List<Pair<Object, String>> catsSubset = cats.subList(0, cats.size() - 1).stream() |
| 1498 | + .map(kitty -> new Pair<Object, String>(kitty.id(), kitty.value("name"))) |
| 1499 | + .collect(Collectors.toList()); |
| 1500 | + |
| 1501 | + List<Vertex> dogs = tx.traversal().V().has("type", "dog").toList(); |
| 1502 | + assertEquals(5, dogs.size()); |
| 1503 | + tx.rollback(); |
| 1504 | + |
| 1505 | + //Create new Index |
| 1506 | + graph.getOpenTransactions().forEach(JanusGraphTransaction::rollback); |
| 1507 | + mgmt = graph.openManagement(); |
| 1508 | + mgmt.getOpenInstances().stream().filter(i -> !i.contains("current")).forEach(i -> mgmt.forceCloseInstance(i)); |
| 1509 | + mgmt.commit(); |
| 1510 | + |
| 1511 | + String catsNameIndex = "searchByName_CatsOnly"; |
| 1512 | + mgmt = graph.openManagement(); |
| 1513 | + mgmt.buildIndex(catsNameIndex, Vertex.class) |
| 1514 | + .addKey(mgmt.getPropertyKey("name")) |
| 1515 | + .indexOnly(mgmt.getVertexLabel("cat")) |
| 1516 | + .buildCompositeIndex(); |
| 1517 | + mgmt.commit(); |
| 1518 | + |
| 1519 | + //Make Index as REGISTERED |
| 1520 | + mgmt = graph.openManagement(); |
| 1521 | + mgmt.updateIndex(mgmt.getGraphIndex(catsNameIndex), SchemaAction.REGISTER_INDEX).get(); |
| 1522 | + mgmt.commit(); |
| 1523 | + ManagementSystem.awaitGraphIndexStatus(graph, catsNameIndex).status(SchemaStatus.REGISTERED).call(); |
| 1524 | + |
| 1525 | + //Reindex a given subset |
| 1526 | + List<Object> reIndexOnlyIds = catsSubset.stream().map(Pair::getValue0).collect(Collectors.toList()); |
| 1527 | + mgmt = graph.openManagement(); |
| 1528 | + mgmt.updateIndex(mgmt.getGraphIndex(catsNameIndex), SchemaAction.REINDEX, reIndexOnlyIds).get(); |
| 1529 | + mgmt.commit(); |
| 1530 | + ManagementSystem.awaitGraphIndexStatus(graph, catsNameIndex).status(SchemaStatus.ENABLED).call(); |
| 1531 | + |
| 1532 | + clopen(option(FORCE_INDEX_USAGE), true); |
| 1533 | + catsSubset.forEach(kitty -> { |
| 1534 | + List<Vertex> catsByName = tx.traversal().V().hasLabel("cat").has("name", kitty.getValue1()).toList(); |
| 1535 | + assertEquals(1, catsByName.size()); |
| 1536 | + }); |
| 1537 | + |
| 1538 | + List<Vertex> catsByName = tx.traversal().V().hasLabel("cat").has("name", excludedCat).toList(); |
| 1539 | + assertEquals(0, catsByName.size()); |
| 1540 | + tx.rollback(); |
| 1541 | + } |
| 1542 | + |
1454 | 1543 | @Test |
1455 | 1544 | public void testIndexInlineProperties() throws NoSuchMethodException { |
1456 | 1545 |
|
|
0 commit comments