private IEnumerable <Pair <RelationshipCountKey, long> > AllRelationshipCounts(System.Func <string, int> labelTranslationTable, System.Func <string, int> relationshipTypeTranslationTable, IDictionary <string, IDictionary <string, IDictionary <string, AtomicLong> > > counts) { ICollection <Pair <RelationshipCountKey, long> > result = new List <Pair <RelationshipCountKey, long> >(); foreach (KeyValuePair <string, IDictionary <string, IDictionary <string, AtomicLong> > > startLabel in Counts.SetOfKeyValuePairs()) { foreach (KeyValuePair <string, IDictionary <string, AtomicLong> > type in startLabel.Value.entrySet()) { foreach (KeyValuePair <string, AtomicLong> endLabel in type.Value.entrySet()) { RelationshipCountKey key = new RelationshipCountKey(labelTranslationTable(startLabel.Key), relationshipTypeTranslationTable(type.Key), labelTranslationTable(endLabel.Key)); result.Add(Pair.of(key, endLabel.Value.longValue())); } } } return(result); }
// ====================================================== // Below is code for verifying the imported data // ====================================================== private void VerifyImportedData(IList <InputEntity> nodeData, IList <InputEntity> relationshipData) { // Build up expected data for the verification below IDictionary <string, InputEntity> expectedNodes = new Dictionary <string, InputEntity>(); IDictionary <string, string[]> expectedNodeNames = new Dictionary <string, string[]>(); IDictionary <string, IDictionary <string, System.Action <object> > > expectedNodePropertyVerifiers = new Dictionary <string, IDictionary <string, System.Action <object> > >(); IDictionary <string, IDictionary <string, IDictionary <string, AtomicInteger> > > expectedRelationships = new AutoCreatingHashMap <string, IDictionary <string, IDictionary <string, AtomicInteger> > >(nested(typeof(string), nested(typeof(string), values(typeof(AtomicInteger))))); IDictionary <string, AtomicLong> expectedNodeCounts = new AutoCreatingHashMap <string, AtomicLong>(values(typeof(AtomicLong))); IDictionary <string, IDictionary <string, IDictionary <string, AtomicLong> > > expectedRelationshipCounts = new AutoCreatingHashMap <string, IDictionary <string, IDictionary <string, AtomicLong> > >(nested(typeof(string), nested(typeof(string), values(typeof(AtomicLong))))); BuildUpExpectedData(nodeData, relationshipData, expectedNodes, expectedNodeNames, expectedNodePropertyVerifiers, expectedRelationships, expectedNodeCounts, expectedRelationshipCounts); // Do the verification GraphDatabaseService db = (new TestGraphDatabaseFactory()).newEmbeddedDatabase(Directory.databaseDir()); try { using (Transaction tx = Db.beginTx()) { // Verify nodes foreach (Node node in Db.AllNodes) { string name = ( string )node.GetProperty("name"); string[] labels = expectedNodeNames.Remove(name); assertEquals(asSet(labels), Names(node.Labels)); // Verify node properties IDictionary <string, System.Action <object> > expectedPropertyVerifiers = expectedNodePropertyVerifiers.Remove(name); IDictionary <string, object> actualProperties = node.AllProperties; actualProperties.Remove("id"); // The id does not exist in expected properties foreach (DictionaryEntry actualProperty in actualProperties.SetOfKeyValuePairs()) { System.Action v = expectedPropertyVerifiers[actualProperty.Key]; if (v != null) { v(actualProperty.Value); } } } assertEquals(0, expectedNodeNames.Count); // Verify relationships foreach (Relationship relationship in Db.AllRelationships) { string startNodeName = ( string )relationship.StartNode.getProperty("name"); IDictionary <string, IDictionary <string, AtomicInteger> > inner = expectedRelationships[startNodeName]; string endNodeName = ( string )relationship.EndNode.getProperty("name"); IDictionary <string, AtomicInteger> innerInner = inner[endNodeName]; string type = relationship.Type.name(); int countAfterwards = innerInner[type].decrementAndGet(); assertThat(countAfterwards, greaterThanOrEqualTo(0)); if (countAfterwards == 0) { innerInner.Remove(type); if (innerInner.Count == 0) { inner.Remove(endNodeName); if (inner.Count == 0) { expectedRelationships.Remove(startNodeName); } } } } assertEquals(0, expectedRelationships.Count); // Verify counts, TODO how to get counts store other than this way? NeoStores neoStores = (( GraphDatabaseAPI )db).DependencyResolver.resolveDependency(typeof(RecordStorageEngine)).testAccessNeoStores(); System.Func <string, int> labelTranslationTable = TranslationTable(neoStores.LabelTokenStore, StatementConstants.ANY_LABEL); foreach (Pair <int, long> count in AllNodeCounts(labelTranslationTable, expectedNodeCounts)) { assertEquals("Label count mismatch for label " + count.First(), count.Other(), neoStores.Counts.nodeCount(count.First(), newDoubleLongRegister()).readSecond()); } System.Func <string, int> relationshipTypeTranslationTable = TranslationTable(neoStores.RelationshipTypeTokenStore, StatementConstants.ANY_RELATIONSHIP_TYPE); foreach (Pair <RelationshipCountKey, long> count in AllRelationshipCounts(labelTranslationTable, relationshipTypeTranslationTable, expectedRelationshipCounts)) { RelationshipCountKey key = count.First(); assertEquals("Label count mismatch for label " + key, count.Other(), neoStores.Counts.relationshipCount(key.StartLabel, key.Type, key.EndLabel, newDoubleLongRegister()).readSecond()); } tx.Success(); } } finally { Db.shutdown(); } }