private void TestAddIndex() { try { _logger.Message("Testing AddIndex"); //making the table String indexPath = GetFilePath.TableColumnIndex(dbName, tableName, "Int"); List <Column> cols = new List <Column>(); cols.Add(new Column(Column.DataType.Int, "Int", 100)); cols.Add(new Column(Column.DataType.Double, "Double", 1)); cols.Add(new Column(Column.DataType.Char, "String", 20)); manager.CreateTable(dbName, tableName, cols); //making records to be inserted List <String> l1 = new List <string>(); l1.Add("5"); l1.Add("5.1"); l1.Add("random1"); Record r1 = new Record(l1); List <String> l2 = new List <string>(); l2.Add("2048000"); l2.Add("5.2"); l2.Add("random2"); Record r2 = new Record(l2); List <String> l3 = new List <string>(); l3.Add("-409600"); l3.Add("5.3"); l3.Add("random3"); Record r3 = new Record(l3); manager.InsertRecord(r1); manager.InsertRecord(r2); manager.InsertRecord(r3); manager.AddIndex(new Column(Column.DataType.Int, "Int", 100)); Assert.IsTrue(File.Exists(indexPath)); BtreeDictionary <Index <int>, int> btree = (BtreeDictionary <Index <int>, int>)Converter.FileToObject(indexPath); Assert.AreEqual(3, btree.Count); Assert.IsTrue(btree.ContainsKey(new Index <int>(2048000, 54))); Assert.IsTrue(btree.ContainsValue(96)); } catch (Exception e) { _logger.Error(e.Message); } finally { manager.DropTable(dbName, tableName); } }
/** * Takes dictionary containing the address => to be deleted record * And deletes that particular column value from the index path */ public void DeleteRecordsFromIndices(Dictionary <int, Record> uselessRecords) { foreach (Column indexColumn in table.IndexColumns) { int position = table.GetColumnIndex(indexColumn); String indexFile = GetFilePath.TableColumnIndex(table.DbName, table.Name, table.Columns[position].Name); if (table.Columns[position].Type == Column.DataType.Int) { BtreeDictionary <Index <int>, int> intbptree = (BtreeDictionary <Index <int>, int>)Converter.FileToObject(indexFile); foreach (KeyValuePair <int, Record> pair in uselessRecords) { int address = pair.Key; String value = pair.Value.Fields[position]; if (value != null) { intbptree.Remove(new Index <int>(int.Parse(value), address)); } } Converter.ObjectToFile(intbptree, indexFile); } else if (table.Columns[position].Type == Column.DataType.Double) { BtreeDictionary <Index <double>, int> doublebptree = (BtreeDictionary <Index <double>, int>)Converter.FileToObject(indexFile); foreach (KeyValuePair <int, Record> pair in uselessRecords) { int address = pair.Key; String value = pair.Value.Fields[position]; if (value != null) { doublebptree.Remove(new Index <double>(double.Parse(value), address)); } } Converter.ObjectToFile(doublebptree, indexFile); } else if (table.Columns[position].Type == Column.DataType.Char) { BtreeDictionary <Index <String>, int> stringbptree = (BtreeDictionary <Index <String>, int>)Converter.FileToObject(indexFile); foreach (KeyValuePair <int, Record> pair in uselessRecords) { int address = pair.Key; String value = pair.Value.Fields[position]; if (value != null) { stringbptree.Remove(new Index <String>(value, address)); } } Converter.ObjectToFile(stringbptree, indexFile); } } }
/** * Inserts a record into the indices of the table * record -> to be inserted * address -> entry in the records file */ public void InsertRecordToIndices(Record record, int address) { foreach (Column indexColumn in table.IndexColumns) { int position = table.GetColumnIndex(indexColumn); String indexFile = GetFilePath.TableColumnIndex(table.DbName, table.Name, table.Columns[position].Name); String value = record.Fields[position]; if (table.Columns[position].Type == Column.DataType.Int) { if (value != null) { BtreeDictionary <Index <int>, int> intbptree = (BtreeDictionary <Index <int>, int>)Converter.FileToObject(indexFile); intbptree.Add(new Index <int>(int.Parse(value), address), address); Converter.ObjectToFile(intbptree, indexFile); } } else if (table.Columns[position].Type == Column.DataType.Double) { if (value != null) { BtreeDictionary <Index <double>, int> doublebptree = (BtreeDictionary <Index <double>, int>)Converter.FileToObject(indexFile); doublebptree.Add(new Index <double>(double.Parse(value), address), address); Converter.ObjectToFile(doublebptree, indexFile); } } else if (table.Columns[position].Type == Column.DataType.Char) { if (value != null) { BtreeDictionary <Index <String>, int> stringbptree = (BtreeDictionary <Index <String>, int>)Converter.FileToObject(indexFile); stringbptree.Add(new Index <String>(value, address), address); Converter.ObjectToFile(stringbptree, indexFile); } } } }
/** * Adding index to particular column * if some records were already there */ public void AddIndex(Column index) { //making the index file String indexFile = GetFilePath.TableColumnIndex(table.DbName, table.Name, index.Name); Dictionary <int, Record> allRecords = GetAddressRecordDict(null); int position = table.GetColumnIndex(index); BtreeDictionary <Index <int>, int> intbptree; BtreeDictionary <Index <String>, int> stringbptree; BtreeDictionary <Index <double>, int> doublebptree; if (index.Type == Column.DataType.Int) { intbptree = new BtreeDictionary <Index <int>, int>(); //making a b+tree foreach (KeyValuePair <int, Record> keyValuePair in allRecords) { String s = keyValuePair.Value.Fields[position]; if (s != null) { Index <int> idx = new Index <int>(int.Parse(s), keyValuePair.Key); intbptree.Add(idx, keyValuePair.Key); } //BUG not inserting empty or null values as of now /*else * { * Index<int> idx = new Index<int>(Constants.NullAsInt, keyValuePair.Key); * intbptree.Add(idx, keyValuePair.Key); * }*/ } //writing b+tree to the file Converter.ObjectToFile(intbptree, indexFile); } else if (index.Type == Column.DataType.Double) { doublebptree = new BtreeDictionary <Index <double>, int>(); //making a b+tree foreach (KeyValuePair <int, Record> keyValuePair in allRecords) { String s = keyValuePair.Value.Fields[position]; if (s != null) { Index <double> idx = new Index <double>(double.Parse(s), keyValuePair.Key); doublebptree.Add(idx, keyValuePair.Key); } } //writing b+tree to the file Converter.ObjectToFile(doublebptree, indexFile); } else if (index.Type == Column.DataType.Char) { stringbptree = new BtreeDictionary <Index <String>, int>(); //making a b+tree foreach (KeyValuePair <int, Record> keyValuePair in allRecords) { String s = keyValuePair.Value.Fields[position]; if (s != null) { Index <String> idx = new Index <String>(s, keyValuePair.Key); stringbptree.Add(idx, keyValuePair.Key); } } //writing b+tree to the file Converter.ObjectToFile(stringbptree, indexFile); } //adding entry to list in table and update back to file also table.IndexColumns.Add(index); UpdateTableToFile(); }