internal TableSource CopySourceTable(TableSource tableSource, IIndexSet indexSet)
        {
            lock (commitLock) {
                try {
                    // The unique id that identifies this table,
                    int tableId    = NextTableId();
                    var sourceName = tableSource.SourceName;

                    // Create the object.
                    var masterTable = new TableSource(this, StoreSystem, LargeObjectStore, tableId, sourceName);

                    masterTable.CopyFrom(tableId, tableSource, indexSet);

                    // Add to the list of all tables.
                    tableSources.Add(tableId, masterTable);

                    // Add this to the list of deleted tables,
                    MarkUncommitted(tableId);

                    // Commit this
                    StateStore.Flush();

                    // And return it.
                    return(masterTable);
                } catch (IOException e) {
                    throw new Exception(String.Format("Unable to copy source table '{0}' because of an error.", tableSource.TableInfo.TableName), e);
                }
            }
        }
Exemplo n.º 2
0
        private void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    // Dispose and invalidate the indexes
                    // This is really a safety measure to ensure the index can't be
                    // used outside the scope of the lifetime of this object.
                    if (columnIndexes != null)
                    {
                        foreach (var columnIndex in columnIndexes)
                        {
                            if (columnIndex != null)
                            {
                                columnIndex.Dispose();
                            }
                        }
                    }
                }

                columnIndexes = null;
                rowIndex      = null;
                EventRegistry = null;
                indexRebuilds = null;
                indexSet      = null;
                Transaction   = null;
                disposed      = true;
            }
        }
Exemplo n.º 3
0
        private void DisposeAllIndices()
        {
            // Dispose all the IIndexSet for each table
            try {
                foreach (var tableIndex in tableIndices)
                {
                    tableIndex.Dispose();
                }
            } catch (Exception) {
                // TODO: Report the error ...
            }

            // Dispose all tables we dropped (they will be in the cleanup_queue.
            try {
                if (cleanupQueue != null)
                {
                    for (int i = 0; i < cleanupQueue.Count; i += 2)
                    {
                        var       tableSource = (TableSource)cleanupQueue[i];
                        IIndexSet indexSet    = (IIndexSet)cleanupQueue[i + 1];
                        indexSet.Dispose();
                    }
                }
            } catch (Exception) {
                // TODO: Report the error
            } finally {
                cleanupQueue = null;
            }
        }
Exemplo n.º 4
0
        internal void RemoveVisibleTable(ITableSource table)
        {
            if (Transaction.ReadOnly())
            {
                throw new Exception("Transaction is Read-only.");
            }

            var i = IndexOfTable(visibleTables, table.TableId);

            if (i != -1)
            {
                visibleTables.RemoveAt(i);
                IIndexSet indexSet = tableIndices[i];
                tableIndices.RemoveAt(i);
                if (cleanupQueue == null)
                {
                    cleanupQueue = new List <object>();
                }

                cleanupQueue.Add(table);
                cleanupQueue.Add(indexSet);

                // Remove from the table cache
                var tableName = table.TableInfo.TableName;
                tableCache.Remove(tableName);
            }
        }
Exemplo n.º 5
0
        internal void UpdateVisibleTable(TableSource table, IIndexSet indexSet)
        {
            if (Transaction.ReadOnly())
            {
                throw new Exception("Transaction is Read-only.");
            }

            RemoveVisibleTable(table);
            AddVisibleTable(table, indexSet);
        }
Exemplo n.º 6
0
        private void AddVisibleTable(ITableSource table, IIndexSet indexSet)
        {
            if (Transaction.ReadOnly())
            {
                throw new Exception("Transaction is Read-only.");
            }

            visibleTables.Add(table);
            tableIndices.Add(indexSet);
        }
Exemplo n.º 7
0
        private void SetIndexSetForTable(ITableSource source, IIndexSet indexSet)
        {
            var tableName = source.TableInfo.TableName;

            if (!visibleTables.ContainsKey(tableName))
            {
                throw new Exception("Table source not found in this transaction.");
            }

            tableIndices[tableName] = indexSet;
        }
Exemplo n.º 8
0
        private void AddVisibleTable(ITableSource table, IIndexSet indexSet)
        {
            if (Transaction.ReadOnly())
            {
                throw new Exception("Transaction is Read-only.");
            }

            var tableName = table.TableInfo.TableName;

            visibleTables.Set(tableName, table);
            tableIndices.Add(tableName, indexSet);
        }
Exemplo n.º 9
0
        public TransactionTable(ITransaction transaction, TableSource tableSource, TableEventRegistry eventRegistry)
        {
            Transaction    = transaction;
            TableSource    = tableSource;
            EventRegistry  = eventRegistry;
            Context        = tableSource.DatabaseContext;
            indexSet       = transaction.GetTableManager().GetIndexSetForTable(tableSource);
            rowListRebuild = 0;

            var colCount = ColumnCount;

            indexRebuilds    = new int[colCount];
            columnIndexes    = new ColumnIndex[colCount];
            lastEntryRICheck = eventRegistry.EventCount;
        }
Exemplo n.º 10
0
        private void SetIndexSetForTable(ITableSource source, IIndexSet indexSet)
        {
            int sz = tableIndices.Count;

            for (int i = 0; i < sz; ++i)
            {
                if (visibleTables[i].TableId == source.TableId)
                {
                    tableIndices[i] = indexSet;
                    return;
                }
            }

            throw new Exception("Table source not found in this transaction.");
        }
Exemplo n.º 11
0
        private void CopyTable(ITableSource tableSource, IIndexSet indexSet)
        {
            var tableInfo = tableSource.TableInfo;
            var tableName = tableInfo.TableName;

            if (visibleTables.ContainsKey(tableName))
            {
                throw new ObjectNotFoundException(tableName);
            }

            // Copy the table and add to the list of visible tables.
            var source = Composite.CopySourceTable(tableSource, indexSet);

            AddVisibleTable(source, source.CreateIndexSet());

            // Log in the journal that this transaction touched the table_id.
            int tableId = source.TableId;

            Transaction.OnTableCreated(tableId, tableName);

            Transaction.CreateNativeSequence(tableName);
        }
Exemplo n.º 12
0
        private void CopyTable(ITableSource tableSource, IIndexSet indexSet)
        {
            var tableInfo = tableSource.TableInfo;
            var tableName = tableInfo.TableName;
            var source    = FindVisibleTable(tableName, false);

            if (source != null)
            {
                throw new ObjectNotFoundException(tableName);
            }

            // Copy the table and add to the list of visible tables.
            source = Composite.CopySourceTable(tableSource, indexSet);

            AddVisibleTable(source, source.CreateIndexSet());

            // Log in the journal that this transaction touched the table_id.
            int tableId = source.TableId;

            Transaction.Registry.RegisterEvent(new TableCreatedEvent(tableId, tableName));

            Transaction.CreateNativeSequence(tableName);
        }
Exemplo n.º 13
0
        private void DisposeAllIndices()
        {
            // Dispose all the IIndexSet for each table
            try {
                if (tableIndices != null)
                {
                    foreach (var tableIndex in tableIndices.Values)
                    {
                        tableIndex.Dispose();
                    }

                    tableIndices.Clear();
                }
            } catch (Exception ex) {
                Transaction.OnError(ex);
            }

            // Dispose all tables we dropped (they will be in the cleanup_queue.
            try {
                if (cleanupQueue != null)
                {
                    for (int i = 0; i < cleanupQueue.Count; i += 2)
                    {
                        var       tableSource = (TableSource)cleanupQueue[i];
                        IIndexSet indexSet    = (IIndexSet)cleanupQueue[i + 1];
                        indexSet.Dispose();
                    }

                    cleanupQueue.Clear();
                }
            } catch (Exception ex) {
                Transaction.OnError(ex);
            } finally {
                cleanupQueue = null;
            }
        }
Exemplo n.º 14
0
 internal static void UpdateVisibleTable(this ITransaction transaction, TableSource tableSource, IIndexSet indexSet)
 {
     transaction.GetTableManager().UpdateVisibleTable(tableSource, indexSet);
 }
Exemplo n.º 15
0
        internal TableSource CopySourceTable(TableSource tableSource, IIndexSet indexSet)
        {
            lock (commitLock) {
                try {
                    // The unique id that identifies this table,
                    int tableId = NextTableId();
                    var sourceName = tableSource.SourceName;

                    // Create the object.
                    var masterTable = new TableSource(this, StoreSystem, LargeObjectStore, tableId, sourceName);

                    masterTable.CopyFrom(tableId, tableSource, indexSet);

                    // Add to the list of all tables.
                    tableSources.Add(tableId, masterTable);

                    // Add this to the list of deleted tables,
                    MarkUncommitted(tableId);

                    // Commit this
                    StateStore.Flush();

                    // And return it.
                    return masterTable;
                } catch (IOException e) {
                    throw new Exception(String.Format("Unable to copy source table '{0}' because of an error.", tableSource.TableInfo.TableName), e);
                }
            }
        }
Exemplo n.º 16
0
 ITableSource ITableSourceComposite.CopySourceTable(ITableSource tableSource, IIndexSet indexSet)
 {
     return CopySourceTable((TableSource) tableSource, indexSet);
 }
Exemplo n.º 17
0
 public IndexSetAccessor(IIndexSet <TKey, TValue> indexable, TKey key)
 {
     Indexable = indexable;
     Key       = key;
 }
Exemplo n.º 18
0
        public void CommitIndexSet(IIndexSet indexSet)
        {
            var removedBlocks = new List<IndexBlock>();

            lock (this) {
                var sIndexSet = (SnapshotIndexSet)indexSet;
                var indices = sIndexSet.AllIndices.ToList();

                try {
                    try {
                        Store.Lock();

                        // For each Index in the index set,
                        foreach (var index in indices) {
                            int indexNum = index.IndexNumber;

                            // The IndexBlock we are changing
                            var curIndexBlock = indexBlocks[indexNum];

                            // Get all the blocks in the list
                            var blocks = index.AllBlocks.ToList();

                            // Make up a new block list for this index set.
                            long blockP;
                            using (var area = Store.CreateArea(16 + (blocks.Count*28))) {
                                blockP = area.Id;
                                area.WriteInt4(1); // version
                                area.WriteInt4(0); // reserved
                                area.WriteInt8(blocks.Count); // block count

                                foreach (var block in blocks) {
                                    var mappedBlock = (IMappedBlock) block;

                                    long bottomInt = 0;
                                    long topInt = 0;
                                    int blockSize = mappedBlock.Count;
                                    if (blockSize > 0) {
                                        bottomInt = mappedBlock.Bottom;
                                        topInt = mappedBlock.Top;
                                    }

                                    long blockPointer = mappedBlock.BlockPointer;

                                    // Is the block new or was it changed?
                                    if (blockPointer == -1 || mappedBlock.HasChanged) {
                                        // If this isn't -1 then write this sector on the list of
                                        // sectors to delete during GC.
                                        if (blockPointer != -1)
                                            curIndexBlock.AddDeletedArea(blockPointer);

                                        // This is a new block or a block that's been changed
                                        // Write the block to the file system
                                        blockPointer = mappedBlock.Flush();
                                    }

                                    area.WriteInt8(bottomInt);
                                    area.WriteInt8(topInt);
                                    area.WriteInt8(blockPointer);
                                    area.WriteInt4(blockSize | (((int) mappedBlock.CompactType) << 24));
                                }

                                // Finish initializing the area
                                area.Flush();
                            }

                            // Add the deleted blocks
                            var deletedBlocks = index.DeletedBlocks.ToArray();
                            for (int i = 0; i < deletedBlocks.Length; ++i) {
                                long delBlockP = deletedBlocks[i].BlockPointer;
                                if (delBlockP != -1)
                                    curIndexBlock.AddDeletedArea(delBlockP);
                            }

                            // Mark the current block as deleted
                            curIndexBlock.MarkAsDeleted();

                            // Now create a new IndexBlock object
                            var newIndexBlock = new IndexBlock(this, indexNum, curIndexBlock.BlockSize, blockP);
                            newIndexBlock.Parent = curIndexBlock;

                            // Add reference to the new one
                            newIndexBlock.AddReference();

                            // Update the index_blocks list
                            indexBlocks[indexNum] = newIndexBlock;

                            // We remove this later.
                            removedBlocks.Add(curIndexBlock);
                        }

                        // Commit the new index header (index_blocks)
                        CommitIndexHeader();
                    } finally {
                        Store.Unlock();
                    }

                    // Commit finished.

                } catch (IOException e) {
                    throw new InvalidOperationException("Error while committing index changed", e);
                }

            }

            // Remove all the references for the changed blocks,
            foreach (var block in removedBlocks) {
                block.RemoveReference();
            }
        }
Exemplo n.º 19
0
        public void CommitIndexSet(IIndexSet indexSet)
        {
            var removedBlocks = new List <IndexBlock>();

            lock (this) {
                var sIndexSet = (SnapshotIndexSet)indexSet;
                var indices   = sIndexSet.AllIndices.ToList();

                try {
                    try {
                        Store.Lock();

                        // For each Index in the index set,
                        foreach (var index in indices)
                        {
                            int indexNum = index.IndexNumber;

                            // The IndexBlock we are changing
                            var curIndexBlock = indexBlocks[indexNum];

                            // Get all the blocks in the list
                            var blocks = index.AllBlocks.ToList();

                            // Make up a new block list for this index set.
                            long blockP;
                            using (var area = Store.CreateArea(16 + (blocks.Count * 28))) {
                                blockP = area.Id;
                                area.WriteInt4(1);                                 // version
                                area.WriteInt4(0);                                 // reserved
                                area.WriteInt8(blocks.Count);                      // block count

                                foreach (var block in blocks)
                                {
                                    var mappedBlock = (IMappedBlock)block;

                                    long bottomInt = 0;
                                    long topInt    = 0;
                                    int  blockSize = mappedBlock.Count;
                                    if (blockSize > 0)
                                    {
                                        bottomInt = mappedBlock.Bottom;
                                        topInt    = mappedBlock.Top;
                                    }

                                    long blockPointer = mappedBlock.BlockPointer;

                                    // Is the block new or was it changed?
                                    if (blockPointer == -1 || mappedBlock.HasChanged)
                                    {
                                        // If this isn't -1 then write this sector on the list of
                                        // sectors to delete during GC.
                                        if (blockPointer != -1)
                                        {
                                            curIndexBlock.AddDeletedArea(blockPointer);
                                        }

                                        // This is a new block or a block that's been changed
                                        // Write the block to the file system
                                        blockPointer = mappedBlock.Flush();
                                    }

                                    area.WriteInt8(bottomInt);
                                    area.WriteInt8(topInt);
                                    area.WriteInt8(blockPointer);
                                    area.WriteInt4(blockSize | (((int)mappedBlock.CompactType) << 24));
                                }

                                // Finish initializing the area
                                area.Flush();
                            }

                            // Add the deleted blocks
                            var deletedBlocks = index.DeletedBlocks.ToArray();
                            for (int i = 0; i < deletedBlocks.Length; ++i)
                            {
                                long delBlockP = deletedBlocks[i].BlockPointer;
                                if (delBlockP != -1)
                                {
                                    curIndexBlock.AddDeletedArea(delBlockP);
                                }
                            }

                            // Mark the current block as deleted
                            curIndexBlock.MarkAsDeleted();

                            // Now create a new IndexBlock object
                            var newIndexBlock = new IndexBlock(this, indexNum, curIndexBlock.BlockSize, blockP);
                            newIndexBlock.Parent = curIndexBlock;

                            // Add reference to the new one
                            newIndexBlock.AddReference();

                            // Update the index_blocks list
                            indexBlocks[indexNum] = newIndexBlock;

                            // We remove this later.
                            removedBlocks.Add(curIndexBlock);
                        }

                        // Commit the new index header (index_blocks)
                        CommitIndexHeader();
                    } finally {
                        Store.Unlock();
                    }

                    // Commit finished.
                } catch (IOException e) {
                    throw new InvalidOperationException("Error while committing index changed", e);
                }
            }

            // Remove all the references for the changed blocks,
            foreach (var block in removedBlocks)
            {
                block.RemoveReference();
            }
        }
 ITableSource ITableSourceComposite.CopySourceTable(ITableSource tableSource, IIndexSet indexSet)
 {
     return(CopySourceTable((TableSource)tableSource, indexSet));
 }
 internal static void UpdateVisibleTable(this ITransaction transaction, TableSource tableSource, IIndexSet indexSet)
 {
     transaction.GetTableManager().UpdateVisibleTable(tableSource, indexSet);
 }
Exemplo n.º 22
0
        private void SetIndexSetForTable(ITableSource source, IIndexSet indexSet)
        {
            int sz = tableIndices.Count;
            for (int i = 0; i < sz; ++i) {
                if (visibleTables[i].TableId == source.TableId) {
                    tableIndices[i] = indexSet;
                    return;
                }
            }

            throw new Exception("Table source not found in this transaction.");
        }
Exemplo n.º 23
0
        private void CopyTable(ITableSource tableSource, IIndexSet indexSet)
        {
            var tableInfo = tableSource.TableInfo;
            var tableName = tableInfo.TableName;
            var source = FindVisibleTable(tableName, false);
            if (source != null)
                throw new ObjectNotFoundException(tableName);

            // Copy the table and add to the list of visible tables.
            source = Composite.CopySourceTable(tableSource, indexSet);

            AddVisibleTable(source, source.CreateIndexSet());

            // Log in the journal that this transaction touched the table_id.
            int tableId = source.TableId;
            Transaction.Registry.RegisterEvent(new TableCreatedEvent(tableId, tableName));

            Transaction.CreateNativeSequence(tableName);
        }
Exemplo n.º 24
0
        private void AddVisibleTable(ITableSource table, IIndexSet indexSet)
        {
            if (Transaction.ReadOnly())
                throw new Exception("Transaction is Read-only.");

            visibleTables.Add(table);
            tableIndices.Add(indexSet);
        }
Exemplo n.º 25
0
        internal void UpdateVisibleTable(TableSource table, IIndexSet indexSet)
        {
            if (Transaction.ReadOnly())
                throw new Exception("Transaction is Read-only.");

            RemoveVisibleTable(table);
            AddVisibleTable(table, indexSet);
        }
Exemplo n.º 26
0
        private void CopyTable(ITableSource tableSource, IIndexSet indexSet)
        {
            var tableInfo = tableSource.TableInfo;
            var tableName = tableInfo.TableName;

            if (visibleTables.ContainsKey(tableName))
                throw new ObjectNotFoundException(tableName);

            // Copy the table and add to the list of visible tables.
            var source = Composite.CopySourceTable(tableSource, indexSet);

            AddVisibleTable(source, source.CreateIndexSet());

            // Log in the journal that this transaction touched the table_id.
            int tableId = source.TableId;
            Transaction.OnTableCreated(tableId, tableName);

            Transaction.CreateNativeSequence(tableName);
        }
Exemplo n.º 27
0
        private void SetIndexSetForTable(ITableSource source, IIndexSet indexSet)
        {
            var tableName = source.TableInfo.TableName;

            if (!visibleTables.ContainsKey(tableName))
                throw new Exception("Table source not found in this transaction.");

            tableIndices[tableName] = indexSet;
        }
Exemplo n.º 28
0
 public static IndexSetAccessor <TKey, TValue> Access <TKey, TValue>(this IIndexSet <TKey, TValue> indexable, TKey key)
 {
     return(new IndexSetAccessor <TKey, TValue>(indexable, key));
 }