Exemplo n.º 1
0
        /// <summary>
        /// Retrives a container from the cachel
        /// </summary>
        /// <param name="treeAddress">The address of the container</param>
        /// <returns>The container</returns>
        private BTreeContainer GetContainerFromCache(BTreeAddress treeAddress)
        {
            BTreeContainer container = null;

            _cache.TryGetValue(treeAddress, out container);
            return(container);
        }
Exemplo n.º 2
0
        public BTreeContainer GetTree(BTreeAddress address)
        {
            BTreeContainer container;

            _cache.TryGetValue(address, out container);
            return(container);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Attempts to insert a row into a btree
        /// </summary>
        /// <param name="insert">The row to insert</param>
        /// <returns>True if successful, otherwise false</returns>
        public bool InsertRow(RowInsert insert)
        {
            if (insert == null)
            {
                throw new ArgumentNullException(nameof(insert));
            }

            bool isSuccessful = false;

            BTreeContainer container;
            BTreeAddress   address = insert.Table.BTreeAddress;

            if (_cache.ContainsKey(insert.Table.BTreeAddress))
            {
                if (_cache.TryGetValue(address, out container))
                {
                    isSuccessful = container.TryInsertRow(insert);
                }
            }
            else
            {
                AddContainerToCache(address);
                if (_cache.TryGetValue(address, out container))
                {
                    isSuccessful = container.TryInsertRow(insert);
                }
            }

            return(isSuccessful);
        }
Exemplo n.º 4
0
 public BTreeContainer(BTreeAddress address, TreeDictionary <int, Page> tree, DbStorage storage, TableSchema2 schema, Process process)
 {
     _tree    = tree;
     _address = address;
     _storage = storage;
     _schema  = schema;
     _process = process;
 }
Exemplo n.º 5
0
 public RowInsert(List <RowValue2> values, TableSchema2 table, Guid?participantId, bool isReferenceInsert, BTreeAddress address)
 {
     _values        = values;
     _table         = table;
     _participantId = participantId;
     _xactId        = Guid.NewGuid();
     SortByBinaryFormat();
     _address = address;
 }
Exemplo n.º 6
0
 public RowForm2(string databaseName, string tableName, ColumnSchema[] columns, int databaseId, int tableId)
 {
     _columns      = columns;
     _databaseName = databaseName;
     _tableName    = tableName;
     _values       = new List <RowValue2>();
     SetColumnsForValues();
     _address = new BTreeAddress {
         DatabaseId = databaseId, TableId = tableId
     };
 }
Exemplo n.º 7
0
        /// <summary>
        /// Attempts to save the specified B-tree to disk
        /// </summary>
        /// <param name="address">The specific b-tree to save</param>
        /// <returns>True if successful, otherwise false.</returns>
        public bool SyncTreeToDisk(BTreeAddress address)
        {
            bool           isSuccessful = false;
            BTreeContainer container;

            if (_cache.TryGetValue(address, out container))
            {
                container.SetContainerState(BTreeContainerState.LockedForStorageSync);
                isSuccessful = container.SyncToDisk();
                container.SetContainerState(BTreeContainerState.Ready);
            }
            return(isSuccessful);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Gets a page with the specified id from the data file on disk
        /// </summary>
        /// <param name="id">The page to get from disk</param>
        /// <returns>A page object</returns>
        public Page GetPage(int id, BTreeAddress address)
        {
            Page page       = null;
            int  lineNumber = _dataDirectory.GetLineNumberForPageId(id);

            if (lineNumber != 0)
            {
                byte[] data = GetBinaryPageDataFromDisk(lineNumber);
                page = new Page(data, address);
            }

            return(page);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Returns all rows for the specified table (via tree address)
        /// </summary>
        /// <param name="treeAddress">The tree's address (dbId, tableId)</param>
        /// <returns>All rows for the table</returns>
        public List <RowStruct> GetAllRows(BTreeAddress treeAddress)
        {
            var          result   = new List <RowStruct>();
            Database2    database = _process.GetDatabase2(treeAddress.DatabaseId);
            TableSchema2 schema   = database.GetTable(treeAddress.TableId).Schema;

            if (CacheHasContainer(treeAddress))
            {
                result.AddRange(GetContainerFromCache(treeAddress).GetAllRows(schema, false));
            }
            else
            {
                AddContainerToCache(treeAddress);
                result.AddRange(GetContainerFromCache(treeAddress).GetAllRows(schema, false));
            }

            return(result);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Tries to load a container from disk file. If this is a fresh database, will return a new container.
        /// </summary>
        /// <param name="address">The address of the container to get</param>
        /// <returns>A container from disk</returns>
        private BTreeContainer GetContainerFromDisk(BTreeAddress address)
        {
            Database2    db      = _process.GetDatabase2(address.DatabaseId);
            DbStorage    storage = db.Storage;
            var          tree    = new TreeDictionary <int, Page>();
            TableSchema2 schema  = db.GetTable(address.TableId).Schema;

            // get the first page
            Page page = storage.GetPage(1, address);

            // if this is a brand new table
            if (page == null)
            {
                page = new Page(1, address.TableId, address.DatabaseId, schema, _process);
            }

            tree.Add(page.Id, page);

            return(new BTreeContainer(address, tree, storage, schema, _process));
        }
Exemplo n.º 11
0
 /// <summary>
 /// Checks the cache for the specified container
 /// </summary>
 /// <param name="address">The address of the container</param>
 /// <returns>True if the cache has the container, otherwise false</returns>
 private bool CacheHasContainer(BTreeAddress address)
 {
     return(_cache.ContainsKey(address));
 }
Exemplo n.º 12
0
        /// <summary>
        /// Adds a container to cache from disk. If this is a new table, will add a new container to the cache.
        /// </summary>
        /// <param name="address">The address of this btree</param>
        private void AddContainerToCache(BTreeAddress address)
        {
            BTreeContainer container = GetContainerFromDisk(address);

            _cache.TryAdd(address, container);
        }
Exemplo n.º 13
0
 /// <summary>
 /// Gets a page from disk
 /// </summary>
 /// <param name="id">The id of the page to get</param>
 /// <param name="address">The btree address of the page</param>
 /// <returns>The page specified</returns>
 public Page GetPage(int id, BTreeAddress address)
 {
     return(_data.GetPage(id, address));
 }
Exemplo n.º 14
0
 /// <summary>
 /// Gets a table based on the specified BTreeAddress
 /// </summary>
 /// <param name="address">The address of the table</param>
 /// <returns>The table</returns>
 private Table2 GetTable(BTreeAddress address)
 {
     return(GetTable(address.DatabaseId, address.TableId));
 }
Exemplo n.º 15
0
 public List <Row2> GetAllRows(BTreeAddress treeAddress)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 16
0
 public Page(byte[] data, BTreeAddress address) : this(data, address.TableId, address.DatabaseId)
 {
 }