public void Remove <T>(T value, long index, IIndexedObjectComparer <T> c) { CheckReadOnly(); // Search for the position of the last value in the set, long p1, p2; SearchFirstAndLast(value, c, out p1, out p2); // If the value isn't found report the error, if (p1 < 0) { throw new ApplicationException("Value '" + value + "' was not found in the set."); } IIndexCursor cursor = GetCursor(p1, p2); while (cursor.MoveNext()) { if (cursor.Current == index) { // Remove the value and return cursor.Remove(); return; } } }
public void Insert <T>(T value, long index, IIndexedObjectComparer <T> c) { CheckReadOnly(); // Search for the position of the last value in the set, long pos = SearchLast(value, c); // If pos < 0 then the value was not found, if (pos < 0) { // Correct it to the point where the value must be inserted pos = -(pos + 1); } else { // If the value was found in the set, insert after the last value, ++pos; } // Insert the value by moving to the position, shifting the data 8 bytes // and writing the long value. file.Position = (pos * 8); file.Shift(8); fileWriter.Write(index); }
public bool InsertUnique <T>(T value, long index, IIndexedObjectComparer <T> c) { CheckReadOnly(); // Search for the position of the last value in the set, long pos = SearchLast(value, c); // If pos < 0 then the value was not found, if (pos < 0) { // Correct it to the point where the value must be inserted pos = -(pos + 1); } else { // If the value was found in the set, return false and don't change the // list. return(false); } // Insert the value by moving to the position, shifting the data 8 bytes // and writing the long value. file.Position = (pos * 8); file.Shift(8); fileWriter.Write(index); // Return true because we changed the list, return(true); }
public DbIndex GetIndex(string columnName) { CheckColumnNameValid(columnName); StringDictionary p = TableProperties; long columnId = p.GetValue(columnName + ".id", -1); if (columnId == -1) { throw new ApplicationException("Column '" + columnName + "' not found"); } if (!p.GetValue(columnName + ".index", false)) { throw new ApplicationException("Column '" + columnName + "' is not indexed"); } // Fetch the index object, IDataFile df = GetDataFile(GetIndexIdKey(columnId)); SortedIndex list = new SortedIndex(df); // And return it, IIndexedObjectComparer <string> comparer = GetIndexComparerFor(columnName, columnId); return(new DbIndex(this, currentVersion, comparer, columnId, list)); }
public long SearchLast <T>(T value, IIndexedObjectComparer <T> c) { long low = 0; long high = Count - 1; return(SearchLast(value, c, low, high)); }
internal DbIndex(DbIndex source) { table = source.table; tableVersion = source.tableVersion; collator = source.collator; columnid = source.columnid; index = source.index; start = source.start; end = source.end; }
internal Directory(ITransaction transaction, Key directoryPropertiesKey, Key propertySetKey, Key indexSetKey, short itemKeyType, int itemKeyPrimary) { this.transaction = transaction; this.directoryPropertiesKey = directoryPropertiesKey; this.indexSetKey = indexSetKey; this.propertySetKey = propertySetKey; this.itemKeyType = itemKeyType; this.itemKeyPrimary = itemKeyPrimary; collator = new ItemComparer(this); }
private long SearchFirst <T>(T value, IIndexedObjectComparer <T> c, long low, long high) { if (low > high) { return(-1); } while (true) { // If low is the same as high, we are either at the first value or at // the position to insert the value, if ((high - low) <= 4) { for (long i = low; i <= high; ++i) { file.Position = (i * 8); long val = fileReader.ReadInt64(); int res = c.Compare(val, value); if (res == 0) { return(i); } if (res > 0) { return(-(i + 1)); } } return(-(high + 2)); } // The index half way between the low and high point long mid = (low + high) >> 1; // Reaf the middle value from the data file, file.Position = (mid * 8); long midVal = fileReader.ReadInt64(); // Compare it with the value int res1 = c.Compare(midVal, value); if (res1 < 0) { low = mid + 1; } else if (res1 > 0) { high = mid - 1; } else // if (res == 0) { high = mid; } } }
private void AddRowToIndexSet(long rowid) { // Get the set of columns that are indexed in this table, string[] indexedCols = IndexedColumns; foreach (String col in indexedCols) { // Resolve the column name to an id, turn it into a OrderedList64Bit, // and insert the row in the correct location in the index, long columnid = GetColumnId(col); IDataFile df = GetDataFile(GetIndexIdKey(columnid)); SortedIndex index = new SortedIndex(df); IIndexedObjectComparer <string> indexComparer = GetIndexComparerFor(col, columnid); index.Insert(GetValue(rowid, columnid), rowid, indexComparer); } }
private void SearchFirstAndLast <T>(T value, IIndexedObjectComparer <T> c, out long first, out long last) { long low = 0; long high = Count - 1; if (low > high) { first = -1; last = -1; return; } while (true) { // If low is the same as high, we are either at the first value or at // the position to insert the value, if ((high - low) <= 4) { first = SearchFirst(value, c, low, high); last = SearchLast(value, c, low, high); return; } // The index half way between the low and high point long mid = (low + high) >> 1; // Reaf the middle value from the data file, file.Position = (mid * 8); long midVal = fileReader.ReadInt64(); // Compare it with the value int res = c.Compare(midVal, value); if (res < 0) { low = mid + 1; } else if (res > 0) { high = mid - 1; } else // if (res == 0) { first = SearchFirst(value, c, low, high); last = SearchLast(value, c, low, high); return; } } }
public void AddIndex(string columnName, string culture) { CheckColumnNameValid(columnName); StringDictionary p = TableProperties; // Check the column name exists, long columnid = p.GetValue(columnName + ".id", -1); if (columnid == -1) { throw new ApplicationException("Column " + columnName + " not found"); } // Check if index property set, if (p.GetValue(columnName + ".index", false)) { throw new ApplicationException("Index already on column " + columnName); } // Check the collator encoded string, if (culture != null) { new CultureInfo(culture); } // Add to the column list, string columnList = p.GetValue("index_column_list", ""); columnList = AddToColumnSet(columnName, columnList); p.SetValue("index_column_list", columnList); // Set the index property, p.SetValue(columnName + ".index", true); if (culture != null) { p.SetValue(columnName + ".culture", culture); } // Build the index, IIndexedObjectComparer <string> indexComparer = GetIndexComparerFor(columnName, columnid); BuildIndex(columnid, indexComparer); cachedIndexList = null; // Add this event to the transaction log, AddTransactionEvent("addIndex", columnName); ++currentVersion; }
internal DbIndex(DbTable table, long tableVersion, IIndexedObjectComparer <string> collator, long columnid, IIndex index, long start, long end) { this.table = table; this.tableVersion = tableVersion; this.collator = collator; this.columnid = columnid; this.index = index; if (start <= end) { this.start = start; this.end = end; } else { this.start = start; this.end = start; } }
private void BuildIndex(long columnid, IIndexedObjectComparer <string> indexComparer) { // Get the index object IDataFile df = GetDataFile(GetIndexIdKey(columnid)); // Get the index and clear it, SortedIndex index = new SortedIndex(df); index.Clear(); // For each row in this table, foreach (DbRow row in this) { // Get the column value and the rowid string columnValue = row.GetValue(columnid); long rowid = row.RowId; // Add it into the index, index.Insert(columnValue, rowid, indexComparer); } // Done. }
internal DbIndex(DbTable table, long tableVersion, IIndexedObjectComparer <string> collator, long columnid, IIndex index) : this(table, tableVersion, collator, columnid, index, 0, index.Count) { }