/// <summary>
        /// Get all pages from datafile based only in Page Position (get from disk - not from cache)
        /// </summary>
        private IEnumerable <BsonDocument> DumpPages()
        {
            var collections = _header.GetCollections().ToDictionary(x => x.Value, x => x.Key);

            foreach (var buffer in _disk.ReadFull())
            {
                var page = new BasePage(buffer);

                var doc = new BsonDocument
                {
                    ["position"] = Number(buffer.Position),
                    ["origin"]   = page.PageID == 0 && page.TransactionID == 0 && page.PageType == PageType.Empty ? "blank" :
                                   buffer.Position < _disk.LogStartPosition ? "data" : "log",

                    ["pageID"]     = (int)page.PageID,
                    ["pageType"]   = page.PageType.ToString(),
                    ["nextPageID"] = (int)page.NextPageID,
                    ["prevPageID"] = (int)page.PrevPageID,
                    ["collection"] = collections.GetOrDefault(page.ColID, "-"),

                    ["transactionID"] = (int)page.TransactionID,
                    ["isConfirmed"]   = page.IsConfirmed,

                    ["itemsCount"]       = (int)page.ItemsCount,
                    ["freeBytes"]        = page.FreeBytes,
                    ["usedBytes"]        = (int)page.UsedBytes,
                    ["fragmentedBytes"]  = (int)page.FragmentedBytes,
                    ["nextFreePosition"] = (int)page.NextFreePosition,
                    ["highestIndex"]     = (int)page.HighestIndex
                };

                yield return(doc);
            }
        }
Exemple #2
0
        public FileReaderV8(HeaderPage header, DiskService disk)
        {
            _collections = header.GetCollections().ToDictionary(x => x.Key, x => x.Value);

            // using writer stream from pool (no need to return)
            _stream = disk.GetPool(FileOrigin.Data).Writer;
        }
        public FileReaderV8(HeaderPage header, DiskService disk)
        {
            _collections = header.GetCollections().ToDictionary(x => x.Key, x => x.Value);

            // using writer stream from pool (no need to return)
            _stream = new ConcurrentStream(disk.Writer);
        }
Exemple #4
0
        public override IEnumerable <BsonDocument> Input(BsonValue options)
        {
            var pageID = GetOption(options, "pageID");

            // get any transaction from current thread ID
            var transaction = _monitor.GetThreadTransaction();
            var snapshot    = transaction.CreateSnapshot(LockMode.Read, "$", false);

            _collections = _header.GetCollections().ToDictionary(x => x.Value, x => x.Key);

            try
            {
                var result = pageID != null?
                             this.GetList((uint)pageID.AsInt32, null, transaction, snapshot) :
                                 this.GetAllList(transaction, snapshot);

                foreach (var page in result)
                {
                    yield return(page);
                }
            }
            finally
            {
                transaction.Commit();
            }
        }
Exemple #5
0
        public FileReaderV8(HeaderPage header, DiskService disk)
        {
            this.UserVersion = header.UserVersion;

            _collections = header.GetCollections().ToDictionary(x => x.Key, x => x.Value);

            // using writer stream from pool (no need to return)
            _stream = disk.GetPool(FileOrigin.Data).Writer;

            _buffer = BufferPool.Rent(PAGE_SIZE);
        }
Exemple #6
0
        public FileReaderV8(HeaderPage header, DiskService disk)
        {
            // get a copy of pragmas
            this.Pragmas = new EnginePragmas(header.UpdateBuffer(), header);

            _collections = header.GetCollections().ToDictionary(x => x.Key, x => x.Value);

            // using writer stream from pool (no need to return)
            _stream = disk.GetPool(FileOrigin.Data).Writer;

            _buffer = BufferPool.Rent(PAGE_SIZE);
        }
Exemple #7
0
        private IEnumerable <BsonDocument> DumpPages(uint?pageID)
        {
            var monitor     = _engine.GetMonitor();
            var collections = _header.GetCollections().ToDictionary(x => x.Value, x => x.Key);

            var transaction = monitor.GetTransaction(true, out var isNew);

            try
            {
                var snapshot = transaction.CreateSnapshot(LockMode.Read, "$", false);

                var start = pageID.HasValue ? pageID.Value : 0;
                var end   = pageID.HasValue ? pageID.Value : _header.LastPageID;

                for (uint i = start; i <= Math.Min(end, _header.LastPageID); i++)
                {
                    var page = snapshot.GetPage <BasePage>(i, out var origin, out var position, out var walVersion);

                    var doc = new BsonDocument
                    {
                        ["pageID"]           = (int)page.PageID,
                        ["pageType"]         = page.PageType.ToString(),
                        ["_position"]        = position,
                        ["_origin"]          = origin.ToString(),
                        ["_version"]         = walVersion,
                        ["nextPageID"]       = (int)page.NextPageID,
                        ["prevPageID"]       = (int)page.PrevPageID,
                        ["collection"]       = collections.GetOrDefault(page.ColID, "-"),
                        ["itemsCount"]       = (int)page.ItemsCount,
                        ["freeBytes"]        = page.FreeBytes,
                        ["usedBytes"]        = (int)page.UsedBytes,
                        ["fragmentedBytes"]  = (int)page.FragmentedBytes,
                        ["nextFreePosition"] = (int)page.NextFreePosition,
                        ["highestIndex"]     = (int)page.HighestIndex
                    };

                    if (pageID.HasValue)
                    {
                        doc["buffer"] = page.Buffer.ToArray();
                    }

                    yield return(doc);

                    transaction.Safepoint();
                }
            }
            finally
            {
                transaction.Commit();
            }
        }
Exemple #8
0
        private IEnumerable <BsonDocument> SysDump(FileOrigin origin)
        {
            var collections = _header.GetCollections().ToDictionary(x => x.Value, x => x.Key);

            foreach (var buffer in _disk.ReadFull(origin))
            {
                var page   = new BasePage(buffer);
                var pageID = page.PageID;

                if (origin == FileOrigin.Data && buffer.Position > 0 && pageID == 0)
                {
                    // this will fix print PageID in data file bubbles pages
                    pageID = (uint)(buffer.Position / PAGE_SIZE);
                }

                var doc = new BsonDocument();

                doc["_position"] = (int)buffer.Position;

                doc["pageID"]     = (int)pageID;
                doc["pageType"]   = page.PageType.ToString();
                doc["nextPageID"] = dumpPageID(page.NextPageID);
                doc["prevPageID"] = dumpPageID(page.PrevPageID);

                doc["collection"]    = collections.GetOrDefault(page.ColID, "-");
                doc["transactionID"] = (int)page.TransactionID;
                doc["isConfirmed"]   = page.IsConfirmed;

                doc["itemsCount"]       = (int)page.ItemsCount;
                doc["freeBytes"]        = page.FreeBytes;
                doc["usedBytes"]        = (int)page.UsedBytes;
                doc["fragmentedBytes"]  = (int)page.FragmentedBytes;
                doc["nextFreePosition"] = (int)page.NextFreePosition;
                doc["highestIndex"]     = (int)page.HighestIndex;

                if (page.PageType == PageType.Header)
                {
                    var header = new HeaderPage(buffer);

                    doc["freeEmptyPageID"] = dumpPageID(header.FreeEmptyPageID);
                    doc["lastPageID"]      = (int)header.LastPageID;
                    doc["creationTime"]    = header.CreationTime;
                    doc["userVersion"]     = header.UserVersion;
                    doc["collections"]     = new BsonDocument(header.GetCollections().ToDictionary(x => x.Key, x => new BsonValue((int)x.Value)));
                }
                else if (page.PageType == PageType.Collection)
                {
                    var collection = new CollectionPage(buffer);

                    doc["lastAnalyzed"]    = collection.LastAnalyzed;
                    doc["creationTime"]    = collection.CreationTime;
                    doc["freeDataPageID"]  = new BsonArray(collection.FreeDataPageID.Select(x => dumpPageID(x)));
                    doc["freeIndexPageID"] = new BsonArray(collection.FreeIndexPageID.Select(x => dumpPageID(x)));
                    doc["indexes"]         = new BsonArray(collection.GetCollectionIndexes().Select(x => new BsonDocument
                    {
                        ["name"]           = x.Name,
                        ["expression"]     = x.Expression,
                        ["unique"]         = x.Unique,
                        ["headPageID"]     = dumpPageID(x.Head.PageID),
                        ["tailPageID"]     = dumpPageID(x.Tail.PageID),
                        ["maxLevel"]       = (int)x.MaxLevel,
                        ["keyCount"]       = (int)x.KeyCount,
                        ["uniqueKeyCount"] = (int)x.UniqueKeyCount
                    }));
                }

                yield return(doc);
            }

            BsonValue dumpPageID(uint pageID)
            {
                return(pageID == uint.MaxValue ? BsonValue.Null : new BsonValue((int)pageID));
            }
        }
Exemple #9
0
        private IEnumerable <BsonDocument> DumpPages(uint?pageID)
        {
            var collections = _header.GetCollections().ToDictionary(x => x.Value, x => x.Key);

            // get any transaction from current thread ID
            var transaction = _monitor.GetThreadTransaction();

            var snapshot = transaction.CreateSnapshot(LockMode.Read, "$", false);

            var start = pageID.HasValue ? pageID.Value : 0;
            var end   = pageID.HasValue ? pageID.Value : _header.LastPageID;

            for (uint i = start; i <= Math.Min(end, _header.LastPageID); i++)
            {
                var page = snapshot.GetPage <BasePage>(i, out var origin, out var position, out var walVersion);

                var doc = new BsonDocument
                {
                    ["pageID"]           = (int)page.PageID,
                    ["pageType"]         = page.PageType.ToString(),
                    ["_position"]        = position,
                    ["_origin"]          = origin.ToString(),
                    ["_version"]         = walVersion,
                    ["prevPageID"]       = (int)page.PrevPageID,
                    ["nextPageID"]       = (int)page.NextPageID,
                    ["slot"]             = (int)page.PageListSlot,
                    ["collection"]       = collections.GetOrDefault(page.ColID, "-"),
                    ["itemsCount"]       = (int)page.ItemsCount,
                    ["freeBytes"]        = page.FreeBytes,
                    ["usedBytes"]        = (int)page.UsedBytes,
                    ["fragmentedBytes"]  = (int)page.FragmentedBytes,
                    ["nextFreePosition"] = (int)page.NextFreePosition,
                    ["highestIndex"]     = (int)page.HighestIndex
                };

                if (page.PageType == PageType.Collection)
                {
                    var collectionPage = new CollectionPage(page.Buffer);
                    doc["dataPageList"] = new BsonArray(collectionPage.FreeDataPageList.Select(x => new BsonValue((int)x)));
                    doc["indexes"]      = new BsonArray(collectionPage.GetCollectionIndexes().Select(x => new BsonDocument
                    {
                        ["slot"]              = (int)x.Slot,
                        ["empty"]             = x.IsEmpty,
                        ["indexType"]         = (int)x.IndexType,
                        ["name"]              = x.Name,
                        ["expression"]        = x.Expression,
                        ["unique"]            = x.Unique,
                        ["head"]              = x.Head.ToBsonValue(),
                        ["tail"]              = x.Tail.ToBsonValue(),
                        ["maxLevel"]          = (int)x.MaxLevel,
                        ["freeIndexPageList"] = (int)x.FreeIndexPageList,
                    }));
                }

                if (pageID.HasValue)
                {
                    doc["buffer"] = page.Buffer.ToArray();
                }

                yield return(doc);

                transaction.Safepoint();
            }
        }