Beispiel #1
0
        /// <summary>
        /// Read any BsonValue. Use 1 byte for data type, 1 byte for length (optional), 0-255 bytes to value.
        /// For document or array, use BufferReader
        /// </summary>
        public static BsonValue ReadIndexKey(this BufferSlice buffer, int offset)
        {
            ExtendedLengthHelper.ReadLength(buffer[offset++], buffer[offset], out var type, out var len);

            switch (type)
            {
            case BsonType.Null: return(BsonValue.Null);

            case BsonType.Int32: return(buffer.ReadInt32(offset));

            case BsonType.Int64: return(buffer.ReadInt64(offset));

            case BsonType.Double: return(buffer.ReadDouble(offset));

            case BsonType.Decimal: return(buffer.ReadDecimal(offset));

            case BsonType.String:
                offset++;     // for byte length
                return(buffer.ReadString(offset, len));

            case BsonType.Document:
                using (var r = new BufferReader(buffer))
                {
                    r.Skip(offset);     // skip first byte for value.Type
                    return(r.ReadDocument());
                }

            case BsonType.Array:
                using (var r = new BufferReader(buffer))
                {
                    r.Skip(offset);     // skip first byte for value.Type
                    return(r.ReadArray());
                }

            case BsonType.Binary:
                offset++;     // for byte length
                return(buffer.ReadBytes(offset, len));

            case BsonType.ObjectId: return(buffer.ReadObjectId(offset));

            case BsonType.Guid: return(buffer.ReadGuid(offset));

            case BsonType.Boolean: return(buffer[offset] != 0);

            case BsonType.DateTime: return(buffer.ReadDateTime(offset));

            case BsonType.MinValue: return(BsonValue.MinValue);

            case BsonType.MaxValue: return(BsonValue.MaxValue);

            default: throw new NotImplementedException();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Wrtie any BsonValue. Use 1 byte for data type, 1 byte for length (optional), 0-255 bytes to value.
        /// For document or array, use BufferWriter
        /// </summary>
        public static void WriteIndexKey(this BufferSlice buffer, BsonValue value, int offset)
        {
            ENSURE(value.GetBytesCount(false) <= MAX_INDEX_KEY_LENGTH, "index key must have less than 1023 bytes");

            if (value.IsString)
            {
                var str       = value.AsString;
                var strLength = (ushort)Encoding.UTF8.GetByteCount(str);

                ExtendedLengthHelper.WriteLength(BsonType.String, strLength, out var typeByte, out var lengthByte);

                buffer[offset++] = typeByte;
                buffer[offset++] = lengthByte;
                buffer.Write(str, offset);
            }
            else if (value.IsBinary)
            {
                var arr = value.AsBinary;

                ExtendedLengthHelper.WriteLength(BsonType.Binary, (ushort)arr.Length, out var typeByte, out var lengthByte);

                buffer[offset++] = typeByte;
                buffer[offset++] = lengthByte;
                buffer.Write(arr, offset);
            }
            else
            {
                buffer[offset++] = (byte)value.Type;

                switch (value.Type)
                {
                case BsonType.Null:
                case BsonType.MinValue:
                case BsonType.MaxValue:
                    break;

                case BsonType.Int32: buffer.Write(value.AsInt32, offset); break;

                case BsonType.Int64: buffer.Write(value.AsInt64, offset); break;

                case BsonType.Double: buffer.Write(value.AsDouble, offset); break;

                case BsonType.Decimal: buffer.Write(value.AsDecimal, offset); break;

                case BsonType.Document:
                    using (var w = new BufferWriter(buffer))
                    {
                        w.Skip(offset);     // skip offset from buffer
                        w.WriteDocument(value.AsDocument, true);
                    }
                    break;

                case BsonType.Array:
                    using (var w = new BufferWriter(buffer))
                    {
                        w.Skip(offset);     // skip offset from buffer
                        w.WriteArray(value.AsArray, true);
                    }
                    break;

                case BsonType.ObjectId: buffer.Write(value.AsObjectId, offset); break;

                case BsonType.Guid: buffer.Write(value.AsGuid, offset); break;

                case BsonType.Boolean: buffer[offset] = (value.AsBoolean) ? (byte)1 : (byte)0; break;

                case BsonType.DateTime: buffer.Write(value.AsDateTime, offset); break;

                default: throw new NotImplementedException();
                }
            }
        }