示例#1
0
        /// <summary>
        /// Main method - serialize document. Uses ByteWriter
        /// </summary>
        public byte[] Serialize(BsonDocument doc)
        {
            var count = doc.GetBytesCount(true);
            var writer = new ByteWriter(count);

            this.WriteDocument(writer, doc);

            return writer.Buffer;
        }
示例#2
0
文件: DataPage.cs 项目: apkd/LiteDB
 protected override void WriteContent(ByteWriter writer)
 {
     foreach (var block in this.DataBlocks.Values)
     {
         writer.Write(block.Position.Index);
         writer.Write(block.ExtendPageID);
         writer.Write((ushort)block.Data.Length);
         writer.Write(block.Data);
     }
 }
示例#3
0
        public void WriteArray(ByteWriter writer, BsonArray array)
        {
            writer.Write(array.GetBytesCount(false));

            for (var i = 0; i < array.Count; i++)
            {
                this.WriteElement(writer, i.ToString(), array[i] ?? BsonValue.Null);
            }

            writer.Write((byte)0x00);
        }
示例#4
0
        /// <summary>
        /// Write a bson document
        /// </summary>
        public void WriteDocument(ByteWriter writer, BsonDocument doc)
        {
            writer.Write(doc.GetBytesCount(false));

            foreach (var key in doc.Keys)
            {
                this.WriteElement(writer, key, doc[key] ?? BsonValue.Null);
            }

            writer.Write((byte)0x00);
        }
示例#5
0
        protected override void WriteContent(ByteWriter writer)
        {
            foreach (var node in this.Nodes.Values)
            {
                writer.Write(node.Position.Index); // node Index on this page
                writer.Write((byte)node.Prev.Length); // level length
                writer.Write(node.KeyLength); // valueLength
                writer.WriteBsonValue(node.Key, node.KeyLength); // value
                writer.Write(node.DataBlock); // data block reference

                for (var j = 0; j < node.Prev.Length; j++)
                {
                    writer.Write(node.Prev[j]);
                    writer.Write(node.Next[j]);
                }
            }
        }
示例#6
0
        protected override void WriteContent(ByteWriter writer)
        {
            writer.Write(this.CollectionName);
            writer.Write(this.FreeDataPageID);
            writer.Write(this.DocumentCount);

            foreach (var index in this.Indexes)
            {
                writer.Write(index.Field);
                writer.Write(index.HeadNode);
                writer.Write(index.TailNode);
                writer.Write(index.FreeIndexPageID);
                writer.Write(index.Options.Unique);
                writer.Write(index.Options.IgnoreCase);
                writer.Write(index.Options.TrimWhitespace);
                writer.Write(index.Options.EmptyStringToNull);
                writer.Write(index.Options.RemoveAccents);
            }
        }
示例#7
0
文件: EmptyPage.cs 项目: apkd/LiteDB
 protected override void WriteContent(ByteWriter writer)
 {
 }
示例#8
0
        private void WriteElement(ByteWriter writer, string key, BsonValue value)
        {
            // cast RawValue to avoid one if on As<Type>
            switch (value.Type)
            {
                case BsonType.Double:
                    writer.Write((byte)0x01);
                    this.WriteCString(writer, key);
                    writer.Write((Double)value.RawValue);
                    break;

                case BsonType.String:
                    writer.Write((byte)0x02);
                    this.WriteCString(writer, key);
                    this.WriteString(writer, (String)value.RawValue);
                    break;

                case BsonType.Document:
                    writer.Write((byte)0x03);
                    this.WriteCString(writer, key);
                    this.WriteDocument(writer, new BsonDocument((Dictionary<string, BsonValue>)value.RawValue));
                    break;

                case BsonType.Array:
                    writer.Write((byte)0x04);
                    this.WriteCString(writer, key);
                    this.WriteArray(writer, new BsonArray((List<BsonValue>)value.RawValue));
                    break;

                case BsonType.Binary:
                    writer.Write((byte)0x05);
                    this.WriteCString(writer, key);
                    var bytes = (byte[])value.RawValue;
                    writer.Write(bytes.Length);
                    writer.Write((byte)0x00); // subtype 00 - Generic binary subtype
                    writer.Write(bytes);
                    break;

                case BsonType.Guid:
                    writer.Write((byte)0x05);
                    this.WriteCString(writer, key);
                    var guid = ((Guid)value.RawValue).ToByteArray();
                    writer.Write(guid.Length);
                    writer.Write((byte)0x04); // UUID
                    writer.Write(guid);
                    break;

                case BsonType.ObjectId:
                    writer.Write((byte)0x07);
                    this.WriteCString(writer, key);
                    writer.Write(((ObjectId)value.RawValue).ToByteArray());
                    break;

                case BsonType.Boolean:
                    writer.Write((byte)0x08);
                    this.WriteCString(writer, key);
                    writer.Write((byte)(((Boolean)value.RawValue) ? 0x01 : 0x00));
                    break;

                case BsonType.DateTime:
                    writer.Write((byte)0x09);
                    this.WriteCString(writer, key);
                    var date = (DateTime)value.RawValue;
                    // do not convert to UTC min/max date values - #19
                    var utc = (date == DateTime.MinValue || date == DateTime.MaxValue) ? date : date.ToUniversalTime();
                    var ts = utc - BsonValue.UnixEpoch;
                    writer.Write(Convert.ToInt64(ts.TotalMilliseconds));
                    break;

                case BsonType.Null:
                    writer.Write((byte)0x0A);
                    this.WriteCString(writer, key);
                    break;

                case BsonType.Int32:
                    writer.Write((byte)0x10);
                    this.WriteCString(writer, key);
                    writer.Write((Int32)value.RawValue);
                    break;

                case BsonType.Int64:
                    writer.Write((byte)0x12);
                    this.WriteCString(writer, key);
                    writer.Write((Int64)value.RawValue);
                    break;

                case BsonType.MinValue:
                    writer.Write((byte)0xFF);
                    this.WriteCString(writer, key);
                    break;

                case BsonType.MaxValue:
                    writer.Write((byte)0x7F);
                    this.WriteCString(writer, key);
                    break;
            }
        }
示例#9
0
 private void WriteCString(ByteWriter writer, string s)
 {
     var bytes = Encoding.UTF8.GetBytes(s);
     writer.Write(bytes);
     writer.Write((byte)0x00);
 }
示例#10
0
        private void WriteElement(ByteWriter writer, string key, BsonValue value)
        {
            // cast RawValue to avoid one if on As<Type>
            switch (value.Type)
            {
            case BsonType.Double:
                writer.Write((byte)0x01);
                this.WriteCString(writer, key);
                writer.Write((Double)value.RawValue);
                break;

            case BsonType.String:
                writer.Write((byte)0x02);
                this.WriteCString(writer, key);
                this.WriteString(writer, (String)value.RawValue);
                break;

            case BsonType.Document:
                writer.Write((byte)0x03);
                this.WriteCString(writer, key);
                this.WriteDocument(writer, new BsonDocument((Dictionary <string, BsonValue>)value.RawValue));
                break;

            case BsonType.Array:
                writer.Write((byte)0x04);
                this.WriteCString(writer, key);
                this.WriteArray(writer, new BsonArray((List <BsonValue>)value.RawValue));
                break;

            case BsonType.Binary:
                writer.Write((byte)0x05);
                this.WriteCString(writer, key);
                var bytes = (byte[])value.RawValue;
                writer.Write(bytes.Length);
                writer.Write((byte)0x00);     // subtype 00 - Generic binary subtype
                writer.Write(bytes);
                break;

            case BsonType.Guid:
                writer.Write((byte)0x05);
                this.WriteCString(writer, key);
                var guid = ((Guid)value.RawValue).ToByteArray();
                writer.Write(guid.Length);
                writer.Write((byte)0x04);     // UUID
                writer.Write(guid);
                break;

            case BsonType.ObjectId:
                writer.Write((byte)0x07);
                this.WriteCString(writer, key);
                writer.Write(((ObjectId)value.RawValue).ToByteArray());
                break;

            case BsonType.Boolean:
                writer.Write((byte)0x08);
                this.WriteCString(writer, key);
                writer.Write((byte)(((Boolean)value.RawValue) ? 0x01 : 0x00));
                break;

            case BsonType.DateTime:
                writer.Write((byte)0x09);
                this.WriteCString(writer, key);
                var date = (DateTime)value.RawValue;
                // do not convert to UTC min/max date values - #19
                var utc = (date == DateTime.MinValue || date == DateTime.MaxValue) ? date : date.ToUniversalTime();
                var ts  = utc - BsonValue.UnixEpoch;
                writer.Write(Convert.ToInt64(ts.TotalMilliseconds));
                break;

            case BsonType.Null:
                writer.Write((byte)0x0A);
                this.WriteCString(writer, key);
                break;

            case BsonType.Int32:
                writer.Write((byte)0x10);
                this.WriteCString(writer, key);
                writer.Write((Int32)value.RawValue);
                break;

            case BsonType.Int64:
                writer.Write((byte)0x12);
                this.WriteCString(writer, key);
                writer.Write((Int64)value.RawValue);
                break;

            case BsonType.MinValue:
                writer.Write((byte)0xFF);
                this.WriteCString(writer, key);
                break;

            case BsonType.MaxValue:
                writer.Write((byte)0x7F);
                this.WriteCString(writer, key);
                break;
            }
        }
示例#11
0
        protected override void WriteContent(ByteWriter writer)
        {
            writer.Write(this.CollectionName);
            writer.Write(this.FreeDataPageID);

            // to be compatible with v2_beta, write here only uint (4 bytes)
            writer.Write(this.DocumentCount > uint.MaxValue ?
                uint.MaxValue : (uint)this.DocumentCount);

            foreach (var index in this.Indexes)
            {
                writer.Write(index.Field);
                writer.Write(index.HeadNode);
                writer.Write(index.TailNode);
                writer.Write(index.FreeIndexPageID);
                writer.Write(index.Options.Unique);
                writer.Write(index.Options.IgnoreCase);
                writer.Write(index.Options.TrimWhitespace);
                writer.Write(index.Options.EmptyStringToNull);
                writer.Write(index.Options.RemoveAccents);
            }

            // write all document count 8 bytes here
            writer.Write(this.DocumentCount);
        }
示例#12
0
文件: ExtendPage.cs 项目: apkd/LiteDB
 protected override void WriteContent(ByteWriter writer)
 {
     writer.Write(this.Data);
 }
示例#13
0
        protected override void WriteContent(ByteWriter writer)
        {
            writer.Write(this.CollectionName);
            writer.Write(this.DocumentCount);
            writer.Write(this.FreeDataPageID);

            foreach (var index in this.Indexes)
            {
                writer.Write(index.Field);
                writer.Write(index.Unique);
                writer.Write(index.HeadNode);
                writer.Write(index.TailNode);
                writer.Write(index.FreeIndexPageID);
            }
        }
示例#14
0
文件: HeaderPage.cs 项目: apkd/LiteDB
        protected override void WriteContent(ByteWriter writer)
        {
            writer.Write(HEADER_INFO, HEADER_INFO.Length);
            writer.Write(FILE_VERSION);
            writer.Write((ushort)0); // to be compatible with old "ChangeID"
            writer.Write(this.FreeEmptyPageID);
            writer.Write(this.LastPageID);
            writer.Write(this.UserVersion);
            writer.Write(this.Password);
            writer.Write(this.Salt);

            writer.Write((byte)this.CollectionPages.Count);
            foreach (var key in this.CollectionPages.Keys)
            {
                writer.Write(key);
                writer.Write(this.CollectionPages[key]);
            }
        }
示例#15
0
        protected override void WriteContent(ByteWriter writer)
        {
            writer.Write(HEADER_INFO, HEADER_INFO.Length);
            writer.Write(FILE_VERSION);
            writer.Write(this.ChangeID);
            writer.Write(this.FreeEmptyPageID);
            writer.Write(this.LastPageID);
            writer.Skip(200);

            writer.Write((byte)this.CollectionPages.Count);
            foreach (var key in this.CollectionPages.Keys)
            {
                writer.Write(key);
                writer.Write(this.CollectionPages[key]);
            }
        }
示例#16
0
 public void Write(ByteWriter writer)
 {
     writer.Write(this.DbVersion);
     writer.Skip(198);
 }
示例#17
0
 protected abstract void WriteContent(ByteWriter writer);