/** * 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(); }