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

            writer.Write(bytes);
            writer.Write((byte)0x00);
        }
Example #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);
     }
 }
Example #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);
        }
Example #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);
        }
        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]);
                }
            }
        }
        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
        }
Example #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.Expression);
                }
                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);
        }
Example #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.Decimal:
                writer.Write((byte)0x13);
                this.WriteCString(writer, key);
                writer.Write((Decimal)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;
            }
        }
 protected override void WriteContent(ByteWriter writer)
 {
     writer.Write(_data);
 }
        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);
        }