use of org.neo4j.kernel.impl.util.AutoCreatingHashMap in project neo4j by neo4j.
the class CsvInputBatchImportIT method verifyImportedData.
// ======================================================
// Below is code for verifying the imported data
// ======================================================
private void verifyImportedData(List<InputNode> nodeData, List<InputRelationship> relationshipData) {
// Build up expected data for the verification below
Map<String, InputNode> /*id*/
expectedNodes = new HashMap<>();
Map<String, String[]> expectedNodeNames = new HashMap<>();
Map<String, Map<String, Map<String, AtomicInteger>>> /*end node name*/
expectedRelationships = new AutoCreatingHashMap<>(nested(String.class, nested(String.class, values(AtomicInteger.class))));
Map<String, AtomicLong> expectedNodeCounts = new AutoCreatingHashMap<>(values(AtomicLong.class));
Map<String, Map<String, Map<String, AtomicLong>>> expectedRelationshipCounts = new AutoCreatingHashMap<>(nested(String.class, nested(String.class, values(AtomicLong.class))));
buildUpExpectedData(nodeData, relationshipData, expectedNodes, expectedNodeNames, expectedRelationships, expectedNodeCounts, expectedRelationshipCounts);
// Do the verification
GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabase(directory.graphDbDir());
try (Transaction tx = db.beginTx()) {
// Verify nodes
for (Node node : db.getAllNodes()) {
String name = (String) node.getProperty("name");
String[] labels = expectedNodeNames.remove(name);
assertEquals(asSet(labels), names(node.getLabels()));
}
assertEquals(0, expectedNodeNames.size());
// Verify relationships
for (Relationship relationship : db.getAllRelationships()) {
String startNodeName = (String) relationship.getStartNode().getProperty("name");
Map<String, Map<String, AtomicInteger>> inner = expectedRelationships.get(startNodeName);
String endNodeName = (String) relationship.getEndNode().getProperty("name");
Map<String, AtomicInteger> innerInner = inner.get(endNodeName);
String type = relationship.getType().name();
int countAfterwards = innerInner.get(type).decrementAndGet();
assertThat(countAfterwards, greaterThanOrEqualTo(0));
if (countAfterwards == 0) {
innerInner.remove(type);
if (innerInner.isEmpty()) {
inner.remove(endNodeName);
if (inner.isEmpty()) {
expectedRelationships.remove(startNodeName);
}
}
}
}
assertEquals(0, expectedRelationships.size());
// Verify counts, TODO how to get counts store other than this way?
NeoStores neoStores = ((GraphDatabaseAPI) db).getDependencyResolver().resolveDependency(RecordStorageEngine.class).testAccessNeoStores();
Function<String, Integer> labelTranslationTable = translationTable(neoStores.getLabelTokenStore(), ReadOperations.ANY_LABEL);
for (Pair<Integer, Long> count : allNodeCounts(labelTranslationTable, expectedNodeCounts)) {
assertEquals("Label count mismatch for label " + count.first(), count.other().longValue(), neoStores.getCounts().nodeCount(count.first().intValue(), newDoubleLongRegister()).readSecond());
}
Function<String, Integer> relationshipTypeTranslationTable = translationTable(neoStores.getRelationshipTypeTokenStore(), ReadOperations.ANY_RELATIONSHIP_TYPE);
for (Pair<RelationshipCountKey, Long> count : allRelationshipCounts(labelTranslationTable, relationshipTypeTranslationTable, expectedRelationshipCounts)) {
RelationshipCountKey key = count.first();
assertEquals("Label count mismatch for label " + key, count.other().longValue(), neoStores.getCounts().relationshipCount(key.startLabel, key.type, key.endLabel, newDoubleLongRegister()).readSecond());
}
tx.success();
} finally {
db.shutdown();
}
}
use of org.neo4j.kernel.impl.util.AutoCreatingHashMap in project neo4j by neo4j.
the class CsvInputBatchImportIT method verifyImportedData.
// ======================================================
// Below is code for verifying the imported data
// ======================================================
private void verifyImportedData(List<InputEntity> nodeData, List<InputEntity> relationshipData) {
// Build up expected data for the verification below
Map<String, InputEntity> /*id*/
expectedNodes = new HashMap<>();
Map<String, String[]> expectedNodeNames = new HashMap<>();
Map<String, Map<String, Consumer<Object>>> expectedNodePropertyVerifiers = new HashMap<>();
Map<String, Map<String, Map<String, AtomicInteger>>> /*end node name*/
expectedRelationships = new AutoCreatingHashMap<>(nested(nested(values(AtomicInteger.class))));
Map<String, AtomicLong> expectedNodeCounts = new AutoCreatingHashMap<>(values(AtomicLong.class));
Map<String, Map<String, Map<String, AtomicLong>>> expectedRelationshipCounts = new AutoCreatingHashMap<>(nested(nested(values(AtomicLong.class))));
buildUpExpectedData(nodeData, relationshipData, expectedNodes, expectedNodeNames, expectedNodePropertyVerifiers, expectedRelationships, expectedNodeCounts, expectedRelationshipCounts);
// Do the verification
DatabaseManagementService managementService = new TestDatabaseManagementServiceBuilder(testDirectory.homePath()).build();
GraphDatabaseService db = managementService.database(DEFAULT_DATABASE_NAME);
try (Transaction tx = db.beginTx()) {
// Verify nodes
for (Node node : tx.getAllNodes()) {
String name = (String) node.getProperty("name");
String[] labels = expectedNodeNames.remove(name);
assertEquals(asSet(labels), names(node.getLabels()));
// Verify node properties
Map<String, Consumer<Object>> expectedPropertyVerifiers = expectedNodePropertyVerifiers.remove(name);
Map<String, Object> actualProperties = node.getAllProperties();
// The id does not exist in expected properties
actualProperties.remove("id");
for (Map.Entry actualProperty : actualProperties.entrySet()) {
Consumer v = expectedPropertyVerifiers.get(actualProperty.getKey());
if (v != null) {
v.accept(actualProperty.getValue());
}
}
}
assertEquals(0, expectedNodeNames.size());
// Verify relationships
for (Relationship relationship : tx.getAllRelationships()) {
String startNodeName = (String) relationship.getStartNode().getProperty("name");
Map<String, Map<String, AtomicInteger>> inner = expectedRelationships.get(startNodeName);
String endNodeName = (String) relationship.getEndNode().getProperty("name");
Map<String, AtomicInteger> innerInner = inner.get(endNodeName);
String type = relationship.getType().name();
int countAfterwards = innerInner.get(type).decrementAndGet();
assertThat(countAfterwards).isGreaterThanOrEqualTo(0);
if (countAfterwards == 0) {
innerInner.remove(type);
if (innerInner.isEmpty()) {
inner.remove(endNodeName);
if (inner.isEmpty()) {
expectedRelationships.remove(startNodeName);
}
}
}
}
assertEquals(0, expectedRelationships.size());
RecordStorageEngine storageEngine = ((GraphDatabaseAPI) db).getDependencyResolver().resolveDependency(RecordStorageEngine.class);
NeoStores neoStores = storageEngine.testAccessNeoStores();
CountsAccessor counts = storageEngine.countsAccessor();
Function<String, Integer> labelTranslationTable = translationTable(neoStores.getLabelTokenStore(), ANY_LABEL);
for (Pair<Integer, Long> count : allNodeCounts(labelTranslationTable, expectedNodeCounts)) {
assertEquals(count.other().longValue(), counts.nodeCount(count.first(), NULL), "Label count mismatch for label " + count.first());
}
Function<String, Integer> relationshipTypeTranslationTable = translationTable(neoStores.getRelationshipTypeTokenStore(), ANY_RELATIONSHIP_TYPE);
for (Pair<RelationshipCountKey, Long> count : allRelationshipCounts(labelTranslationTable, relationshipTypeTranslationTable, expectedRelationshipCounts)) {
RelationshipCountKey key = count.first();
assertEquals(count.other().longValue(), counts.relationshipCount(key.startLabel, key.type, key.endLabel, NULL), "Label count mismatch for label " + key);
}
tx.commit();
} finally {
managementService.shutdown();
}
}
Aggregations