private List <List <string> > PerformIndexedNestedLoopsJoin(List <string> joinStructure, List <List <string> > leftTableRecords, List <List <string> > rightTableRecords, string fkColumn, string rightTableName, bool indexOnRight) { var records = new List <List <string> >(); var rightTableStructure = SelectedTablesStructure.Find(elem => elem.Item1 == rightTableName).Item2; foreach (var leftRecord in leftTableRecords) { var fkColumnValue = leftRecord[joinStructure.IndexOf(fkColumn)]; foreach (var rightRecord in rightTableRecords) { if (rightRecord[0] == fkColumnValue) { if (indexOnRight == true) { for (int idx = 1; idx < rightRecord.Count; idx++) { var joinResultRecord = leftRecord.ToList(); var rightRecordColumns = (rightRecord[idx] + "#" + MongoDB.GetRecordValueWithKey(rightTableName, rightRecord[idx])).Split('#'); for (int jdx = 0; jdx < rightRecordColumns.Length; jdx++) { if (jdx != rightTableStructure.IndexOf(fkColumn)) { joinResultRecord.Add(rightRecordColumns[jdx]); } } records.Add(joinResultRecord); } } else { var joinResultRecord = leftRecord.ToList(); for (int jdx = 0; jdx < rightRecord.Count; jdx++) { if (jdx != rightTableStructure.IndexOf(fkColumn)) { joinResultRecord.Add(rightRecord[jdx]); } } records.Add(joinResultRecord); } } } } return(records); }
private void InsertRecord(string key, string value) { try { // Insert into main table data file MongoDB.InsertKVIntoCollection(TableName, key, value); // Insert into FK index file foreach (var newFKRecords in NewForeignKeyEntries) { // Check if the Foreign Key from the referenced table has any other assigned records from the current table if (MongoDB.CollectionContainsKey(newFKRecords.MongoDBFilename, newFKRecords.ForeignKeyRecord.Key)) { var existingReferences = MongoDB.GetRecordValueWithKey(newFKRecords.MongoDBFilename, newFKRecords.ForeignKeyRecord.Key); existingReferences += "#" + newFKRecords.ForeignKeyRecord.Value; // who needs update when you can just delete and add back MongoDB.RemoveKVFromCollection(newFKRecords.MongoDBFilename, newFKRecords.ForeignKeyRecord.Key); MongoDB.InsertKVIntoCollection(newFKRecords.MongoDBFilename, newFKRecords.ForeignKeyRecord.Key, existingReferences); } // Otherwise just add a new Key-Value entry else { MongoDB.InsertKVIntoCollection(newFKRecords.MongoDBFilename, newFKRecords.ForeignKeyRecord.Key, newFKRecords.ForeignKeyRecord.Value); } } // Insert into Unique Key files var columnValues = (key + '#' + value).Split('#'); foreach (var uniqueKey in UniqueKeyData) { var uqValue = columnValues.ElementAt(UniqueKeyPositions.Find(elem => elem.Key == uniqueKey.Item1.ToString()).Value); MongoDB.InsertKVIntoCollection(uniqueKey.Item2, uqValue, key); } // Insert into any index files foreach (var indexFile in TableUtils.GetIndexFiles(DatabaseName, TableName)) { var indexKey = ""; var recordColumns = (key + '#' + value).Split('#'); // Build the key from the specified Index KV file foreach (var indexColumn in indexFile.IndexColumns) { indexKey += recordColumns[ColumnsInfo.FindIndex(elem => elem.ColumnName == indexColumn)] + '#'; } indexKey = indexKey.Remove(indexKey.Length - 1); if (indexFile.IsUnique) { MongoDB.InsertKVIntoCollection(indexFile.IndexFileName, indexKey, key); } else { if (MongoDB.CollectionContainsKey(indexFile.IndexFileName, indexKey)) { // Append the new record PK to the value of the index key var indexValue = MongoDB.GetRecordValueWithKey(indexFile.IndexFileName, indexKey) + '#' + key; MongoDB.RemoveKVFromCollection(indexFile.IndexFileName, indexKey); MongoDB.InsertKVIntoCollection(indexFile.IndexFileName, indexKey, indexValue); } else { MongoDB.InsertKVIntoCollection(indexFile.IndexFileName, indexKey, key); } } } } catch (Exception ex) { throw new Exception(ex.Message); } }