Beispiel #1
0
        private static void WriteCString(ByteWriter writer, string s)
        {
            var bytes = Encoding.UTF8.GetBytes(s);

            writer.Write(bytes);
            writer.Write((byte)0x00);
        }
Beispiel #2
0
 protected override void WriteContent(ByteWriter writer)
 {
     foreach (var block in _dataBlocks.Values)
     {
         writer.Write(block.Position.Index);
         writer.Write(block.ExtendPageID);
         writer.Write((ushort)block.Data.Length);
         writer.Write(block.Data);
     }
 }
Beispiel #3
0
        public static void WriteArray(ByteWriter writer, BsonArray array)
        {
            writer.Write(array.GetBytesCount(false));

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

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

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

            writer.Write((byte)0x00);
        }
Beispiel #5
0
        protected override void WriteContent(ByteWriter writer)
        {
            foreach (var node in _nodes.Values)
            {
                writer.Write(node.Position.Index);               // node Index on this page
                writer.Write((byte)node.Prev.Length);            // level length
                writer.Write(node.Slot);                         // index slot
                writer.Write(node.PrevNode);                     // prev node list
                writer.Write(node.NextNode);                     // next node list
                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]);
                }
            }
        }
Beispiel #6
0
        private void WriteHeader(ByteWriter writer)
        {
            writer.Write(this.PageID);
            writer.Write((byte)this.PageType);

            writer.Write(this.PrevPageID);
            writer.Write(this.NextPageID);
            writer.Write((UInt16)this.ItemCount);
            writer.Write((UInt16)this.FreeBytes);
            writer.Skip(8); // reserved 8 bytes
        }
Beispiel #7
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)
            {
                // write Field+Expression only if index are used
                if (index.Field.Length > 0)
                {
                    writer.Write(index.Field + "=" + "$." + index.Field);
                }
                else
                {
                    writer.Write("");
                }

                writer.Write(index.Unique);
                writer.Write(index.HeadNode);
                writer.Write(index.TailNode);
                writer.Write(index.FreeIndexPageID);
            }

            // position on page-footer (avoid file structure change)
            writer.Position = BasePage.PAGE_SIZE - 8 - CollectionIndex.INDEX_PER_COLLECTION;

            foreach (var index in this.Indexes)
            {
                writer.Write(index.MaxLevel);
            }

            writer.Write(this.Sequence);
        }
Beispiel #8
0
        private static 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);
                WriteCString(writer, key);
                writer.Write((Double)value.RawValue);
                break;

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

            case BsonType.Document:
                writer.Write((byte)0x03);
                WriteCString(writer, key);
                WriteDocument(writer, (BsonDocument)value);
                break;

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

            case BsonType.Binary:
                writer.Write((byte)0x05);
                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);
                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);
                WriteCString(writer, key);
                writer.Write(((ObjectId)value.RawValue).ToByteArray());
                break;

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

            case BsonType.DateTime:
                writer.Write((byte)0x09);
                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);
                WriteCString(writer, key);
                break;

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

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

            case BsonType.Decimal:
                writer.Write((byte)0x13);
                WriteCString(writer, key);
                writer.Write((Decimal)value.RawValue);
                break;

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

            case BsonType.MaxValue:
                writer.Write((byte)0x7F);
                WriteCString(writer, key);
                break;
            }
        }
Beispiel #9
0
 protected override void WriteContent(ByteWriter writer)
 {
     writer.Write(_data);
 }
Beispiel #10
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.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]);
            }

            writer.Position = BasePage.PAGE_SIZE - 1;
            writer.Write(this.Recovery);
        }