private void CheckUniqueIndexConstraint(string record) { var columnValues = record.Split('#'); var mongoDB = new MongoDBAcess(DatabaseName); var indexFiles = TableUtils.GetIndexFiles(DatabaseName, TableName); foreach (var file in indexFiles) { if (file.IsUnique) { var createKeyOfUqIndex = ""; var pk = ""; for (int idx = 0; idx < ColumnsInfo.Count; idx++) { if (file.IndexColumns.Exists(elem => elem == ColumnsInfo[idx].ColumnName)) { createKeyOfUqIndex += columnValues[idx] + '#'; } if (ColumnsInfo[idx].PK) { pk = columnValues[idx] + "#"; } } createKeyOfUqIndex = createKeyOfUqIndex.Remove(createKeyOfUqIndex.Length - 1); pk = pk.Remove(pk.Length - 1); MongoDB.InsertKVIntoCollection(file.IndexFileName, createKeyOfUqIndex, pk); } } }
private void RemoveFromIndexFiles(MongoDBAcess mongoDB) { var indexFiles = TableUtils.GetIndexFiles(DatabaseName, TableName); foreach (var index in indexFiles) { if (index.IsUnique) { // Entire KV pair needs to be removed from the file mongoDB.RemoveByValueFromCollection(index.IndexFileName, RemovedKey); } else { // Only the current key needs to be removed from the Key-Value mongoDB.RemoveValueFromCollection(index.IndexFileName, RemovedKey); } } }
private string CheckForIndex(List <KeyValuePair <string, List <Tuple <string, string> > > > conditionList, string tableName) { if (!conditionList.Any(elem => elem.Key == tableName)) { return(""); } var conditionsForTable = conditionList.Find(elem => elem.Key == tableName).Value; var columnNames = ""; foreach (var column in conditionsForTable) { columnNames += column.Item1 + "_"; } columnNames = columnNames.Remove(columnNames.Length - 1); // check if any unique key index files can be used var uniqueFiles = TableUtils.GetUniqueFiles(DatabaseName, tableName); foreach (var unique in uniqueFiles) { if (unique.Contains(columnNames)) { return(unique); } } var searchedIndexName = "Index_" + tableName + "_" + columnNames; var indexFiles = TableUtils.GetIndexFiles(DatabaseName, tableName); foreach (var index in indexFiles) { if (index.IndexFileName.Contains(searchedIndexName)) { // an index is used in the selection condition if the attributes are a prefix of the attributes in an existing index return(index.IndexFileName); } } return(""); }
private string CheckForIndex(List <Tuple <Tuple <string, string>, string> > conditionList, string tableName) { if (conditionList.Count == 0) { return(""); } // build the index name containing the attributes from the condition var columnNames = ""; foreach (var column in conditionList) { columnNames += column.Item1.Item2 + "_"; } columnNames = columnNames.Remove(columnNames.Length - 1); // check if any unique key index files can be used var uniqueFiles = TableUtils.GetUniqueFiles(DatabaseName, tableName); foreach (var unique in uniqueFiles) { if (unique.Contains(columnNames)) { return(unique); } } var searchedIndexName = "Index_" + tableName + "_" + columnNames; var indexFiles = TableUtils.GetIndexFiles(DatabaseName, tableName); foreach (var index in indexFiles) { if (index.IndexFileName.Contains(searchedIndexName)) { // an index is used in the selection condition if the attributes are a prefix of the attributes in an existing index return(index.IndexFileName); } } return(""); }
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); } }