예제 #1
0
        /// <summary>
        /// Get all document using an indexInfo as start point (_id index).
        /// </summary>
        public IEnumerable <BsonDocument> GetDocuments(string collection)
        {
            var colPageID  = (uint)_header["collections"].AsDocument[collection].AsInt32;
            var col        = this.ReadPage(colPageID);
            var headPageID = (uint)col["indexes"][0]["headPageID"].AsInt32;

            var indexPages = this.VisitIndexPages(headPageID);

            foreach (var indexPageID in indexPages)
            {
                var indexPage = this.ReadPage(indexPageID);

                foreach (var node in indexPage["nodes"].AsArray)
                {
                    var dataBlock = node["dataBlock"];

                    // if datablock link to a data page
                    if (dataBlock["pageID"].AsInt32 != -1)
                    {
                        // read dataPage and data block
                        var dataPage = this.ReadPage((uint)dataBlock["pageID"].AsInt32);

                        if (dataPage["pageType"].AsInt32 != 4)
                        {
                            continue;
                        }

                        var block = dataPage["blocks"].AsArray.FirstOrDefault(x => x["index"] == dataBlock["index"]).AsDocument;

                        if (block == null)
                        {
                            continue;
                        }

                        // read byte[] from block or from extend pages
                        var data = block["extendPageID"] == -1 ?
                                   block["data"].AsBinary :
                                   this.ReadExtendData((uint)block["extendPageID"].AsInt32);

                        if (data.Length == 0)
                        {
                            continue;
                        }

                        // BSON format still same from all version
                        var doc = BsonSerializer.Deserialize(data);

                        // change _id PK in _chunks collection
                        if (collection == "_chunks")
                        {
                            var parts = doc["_id"].AsString.Split('\\');

                            if (!int.TryParse(parts[1], out var n))
                            {
                                throw LiteException.InvalidFormat("_id");
                            }

                            doc["_id"] = new BsonDocument
                            {
                                ["f"] = parts[0],
                                ["n"] = n
                            };
                        }

                        yield return(doc);
                    }
                }
            }
        }