/// <summary>
        /// Gets the index of a value in the array.
        /// </summary>
        /// <param name="value">The value to search for.</param>
        /// <param name="index">The zero based index at which to start the search.</param>
        /// <param name="count">The number of elements to search.</param>
        /// <returns>The zero based index of the value (or -1 if not found).</returns>
        public override int IndexOf(BsonValue value, int index, int count)
        {
            ThrowIfDisposed();
            using (var stream = new ByteBufferStream(_slice, ownsByteBuffer: false))
                using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
                {
                    var context = BsonDeserializationContext.CreateRoot(bsonReader);

                    bsonReader.ReadStartDocument();
                    var i = 0;
                    while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                    {
                        bsonReader.SkipName();
                        if (i >= index)
                        {
                            if (count == 0)
                            {
                                return(-1);
                            }

                            if (DeserializeBsonValue(context).Equals(value))
                            {
                                return(i);
                            }

                            count--;
                        }
                        else
                        {
                            bsonReader.SkipValue();
                        }

                        i++;
                    }
                    bsonReader.ReadEndDocument();

                    return(-1);
                }
        }
        /// <summary>
        /// Tests whether the array contains a value.
        /// </summary>
        /// <param name="value">The value to test for.</param>
        /// <returns>True if the array contains the value.</returns>
        public override bool Contains(BsonValue value)
        {
            ThrowIfDisposed();
            using (var stream = new ByteBufferStream(_slice, ownsByteBuffer: false))
                using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
                {
                    var context = BsonDeserializationContext.CreateRoot <RawBsonArray>(bsonReader);

                    bsonReader.ReadStartDocument();
                    while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                    {
                        bsonReader.SkipName();
                        if (DeserializeBsonValue(context).Equals(value))
                        {
                            return(true);
                        }
                    }
                    bsonReader.ReadEndDocument();

                    return(false);
                }
        }
        public void TestBsonAwesome()
        {
            string byteString = @"1\x00\x00\x00\x04BSON\x00&\x00\x00\x00\x020\x00\x08\x00\x00\x00awesome\x00\x011\x00333333\x14@\x102\x00\xc2\x07\x00\x00\x00\x00";

            byte[] bytes  = DecodeByteString(byteString);
            var    stream = new MemoryStream(bytes);

            using (var bsonReader = new BsonBinaryReader(stream))
            {
                bsonReader.ReadStartDocument();
                Assert.Equal(BsonType.Array, bsonReader.ReadBsonType());
                Assert.Equal("BSON", bsonReader.ReadName());
                bsonReader.ReadStartArray();
                Assert.Equal(BsonType.String, bsonReader.ReadBsonType());
                Assert.Equal("awesome", bsonReader.ReadString());
                Assert.Equal(BsonType.Double, bsonReader.ReadBsonType());
                Assert.Equal(5.05, bsonReader.ReadDouble());
                Assert.Equal(BsonType.Int32, bsonReader.ReadBsonType());
                Assert.Equal(1986, bsonReader.ReadInt32());
                bsonReader.ReadEndArray();
                bsonReader.ReadEndDocument();
            }
        }
Beispiel #4
0
        public static object Deserialize(
            this byte[] serializedBytes,
            Type type)
        {
            if (serializedBytes == null)
            {
                throw new ArgumentNullException(nameof(serializedBytes));
            }

            using (var memoryStream = new MemoryStream(serializedBytes))
            {
                using (var reader = new BsonBinaryReader(memoryStream))
                {
                    var result = BsonSerializer.Deserialize(reader, type);

                    reader.Close();

                    memoryStream.Close();

                    return(result);
                }
            }
        }
        /// <summary>
        /// Tries to get an element of this document.
        /// </summary>
        /// <param name="name">The name of the element.</param>
        /// <param name="element">The element.</param>
        /// <returns>
        /// True if an element with that name was found.
        /// </returns>
        public override bool TryGetElement(string name, out BsonElement element)
        {
            ThrowIfDisposed();
            using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), true), true, _readerSettings))
            {
                bsonReader.ReadStartDocument();
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    if (bsonReader.ReadName() == name)
                    {
                        var value = DeserializeBsonValue(bsonReader);
                        element = new BsonElement(name, value);
                        return(true);
                    }

                    bsonReader.SkipValue();
                }
                bsonReader.ReadEndDocument();

                element = null;
                return(false);
            }
        }
Beispiel #6
0
        // public indexers
        /// <summary>
        /// Gets or sets a value by position.
        /// </summary>
        /// <param name="index">The position.</param>
        /// <returns>The value.</returns>
        public override BsonValue this[int index]
        {
            get
            {
                if (index < 0)
                {
                    throw new ArgumentOutOfRangeException("index");
                }
                ThrowIfDisposed();

                using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
                    using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
                    {
                        bsonReader.ReadStartDocument();
                        var i = 0;
                        while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                        {
                            bsonReader.SkipName();
                            if (i == index)
                            {
                                var context = BsonDeserializationContext.CreateRoot(bsonReader);
                                return(DeserializeBsonValue(context));
                            }

                            bsonReader.SkipValue();
                            i++;
                        }
                        bsonReader.ReadEndDocument();

                        throw new ArgumentOutOfRangeException("index");
                    }
            }
            set
            {
                throw new NotSupportedException("RawBsonArray instances are immutable.");
            }
        }
Beispiel #7
0
        private void Open(String path)
        {
            // TODO - test for file/directory
            if (!File.Exists(path))
            {
                throw new FTDCException($"Cannot load ${path}");
            }

            using var sourceStream = new FileStream(path, FileMode.Open);
            using var reader       = new BsonBinaryReader(sourceStream);
            while (!reader.IsAtEndOfFile())
            {
                var result = BsonSerializer.Deserialize <FTDCFileContents>(reader);

                // FIXME - we should only decompress the data we actually want to use, but this will
                // have to wait until we have timeseries parsing in place
                result.DecompressData();

                Contents.Add(result);
            }

            MetricsStart = Contents.Min(d => d._id);
            MetricsEnd   = Contents.Max(d => d._id);
        }
Beispiel #8
0
        private BsonDocument JObjectToBson(JObject _JsonObject)
        {
            // https://stackoverflow.com/a/62104268
            //Write JObject to MemoryStream
            using var stream = new MemoryStream();
            using (var writer = new BsonDataWriter(stream)
            {
                CloseOutput = false
            })
            {
                _JsonObject.WriteTo(writer);
            }
            stream.Position = 0; //for reading the steam immediately

            //Read the object from MemoryStream
            BsonDocument bsonData;

            using (var reader = new BsonBinaryReader(stream))
            {
                var context = BsonDeserializationContext.CreateRoot(reader);
                bsonData = BsonDocumentSerializer.Instance.Deserialize(context);
            }
            return(bsonData);
        }
        public void Test20KDocument()
        {
            // manufacture an approximately 20K document using 200 strings each 100 characters long
            // it's enough to cause the document to straddle a chunk boundary
            var document = new BsonDocument();
            var value    = new string('x', 100);

            for (int i = 0; i < 200; i++)
            {
                var name = i.ToString();
                document.Add(name, value);
            }

            // round trip tests
            var bson       = document.ToBson();
            var rehydrated = BsonSerializer.Deserialize <BsonDocument>(bson);

            Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));

            // test failure mode when 20 bytes are truncated from the buffer
            using (var byteBuffer = new MultiChunkBuffer(BsonChunkPool.Default))
                using (var byteBufferStream = new ByteBufferStream(byteBuffer, ownsBuffer: true))
                {
                    using (var memoryStream = new MemoryStream(bson))
                    {
                        memoryStream.CopyTo(byteBufferStream);
                    }
                    byteBufferStream.SetLength(byteBufferStream.Length - 20);
                    byteBufferStream.Position = 0;

                    using (var bsonReader = new BsonBinaryReader(byteBufferStream))
                    {
                        Assert.Throws <EndOfStreamException>(() => BsonSerializer.Deserialize <BsonDocument>(bsonReader));
                    }
                }
        }
        /// <summary>
        /// Gets the index of a value in the array.
        /// </summary>
        /// <param name="value">The value to search for.</param>
        /// <param name="index">The zero based index at which to start the search.</param>
        /// <param name="count">The number of elements to search.</param>
        /// <returns>The zero based index of the value (or -1 if not found).</returns>
        public override int IndexOf(BsonValue value, int index, int count)
        {
            ThrowIfDisposed();
            using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
            {
                bsonReader.ReadStartDocument();
                var i = 0;
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    bsonReader.SkipName();
                    if (i >= index)
                    {
                        if (count == 0)
                        {
                            return(-1);
                        }

                        if (DeserializeBsonValue(bsonReader).Equals(value))
                        {
                            return(i);
                        }

                        count--;
                    }
                    else
                    {
                        bsonReader.SkipValue();
                    }

                    i++;
                }
                bsonReader.ReadEndDocument();

                return(-1);
            }
        }
            private long FindWPosition()
            {
                var type0SectionPosition = FindType0SectionPosition();

                _stream.Position = type0SectionPosition + 1;
                using (var reader = new BsonBinaryReader(_stream))
                {
                    reader.ReadStartDocument();
                    while (reader.ReadBsonType() != 0)
                    {
                        if (reader.ReadName() == "writeConcern")
                        {
                            reader.ReadStartDocument();
                            while (reader.ReadBsonType() != 0)
                            {
                                if (reader.ReadName() == "w")
                                {
                                    if (reader.CurrentBsonType == BsonType.Int32)
                                    {
                                        return(_stream.Position);
                                    }
                                    goto notFound;
                                }

                                reader.SkipValue();
                            }
                            goto notFound;
                        }

                        reader.SkipValue();
                    }
                }

notFound:
                throw new InvalidOperationException("{ w : <Int32> } not found.");
            }
        private Type1CommandMessageSection <RawBsonDocument> ReadType1Section(BsonBinaryReader reader)
        {
            var stream = reader.BsonStream;

            var payloadStartPosition = stream.Position;
            var payloadLength        = stream.ReadInt32();

            EnsureType1PayloadLengthIsValid(payloadLength);
            var payloadEndPosition = payloadStartPosition + payloadLength;
            var identifier         = stream.ReadCString(Utf8Encodings.Strict);
            var serializer         = RawBsonDocumentSerializer.Instance;
            var context            = BsonDeserializationContext.CreateRoot(reader);
            var documents          = new List <RawBsonDocument>();

            while (stream.Position < payloadEndPosition)
            {
                var document = serializer.Deserialize(context);
                documents.Add(document);
            }
            EnsurePayloadEndedAtEndPosition(stream, payloadEndPosition);
            var batch = new BatchableSource <RawBsonDocument>(documents, canBeSplit: false);

            return(new Type1CommandMessageSection <RawBsonDocument>(identifier, batch, serializer));
        }
        /// <summary>
        /// Tests whether the document contains an element with the specified name.
        /// </summary>
        /// <param name="name">The name of the element to look for.</param>
        /// <returns>
        /// True if the document contains an element with the specified name.
        /// </returns>
        public override bool Contains(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            ThrowIfDisposed();

            using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), true), true, _readerSettings))
            {
                bsonReader.ReadStartDocument();
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    if (bsonReader.ReadName() == name)
                    {
                        return(true);
                    }
                    bsonReader.SkipValue();
                }
                bsonReader.ReadEndDocument();

                return(false);
            }
        }
Beispiel #14
0
 // constructors
 public BinaryMessageEncoderFactory(BsonBinaryReader binaryReader)
     : this(Ensure.IsNotNull(binaryReader, "binaryReader"), null)
 {
 }
Beispiel #15
0
        /***************************************************/
        /**** Test Functions                            ****/
        /***************************************************/

        public static void TestBson()
        {
            List <BHoMObject> nodes = new List <BHoMObject>
            {
                new Node {
                    Point = new Point(1, 2, 3), Name = "A"
                },
                new Node {
                    Point = new Point(4, 5, 6), Name = "B"
                },
                new Node {
                    Point = new Point(7, 8, 9), Name = "C"
                }
            };


            List <object> items = new List <object>
            {
                new A(-6, -7)
                {
                    a = 1, publicField = -4
                },
                new B {
                    a = 2, b = 45
                },
                new C {
                    a = 3, c = 56
                },
                new D {
                    a = 4, b = 67, d = 123
                },
                new E {
                    a = 5, c = 78, e = 456
                },
                new Node {
                    Point = new Point(1, 2, 3), Name = "A"
                },
                nodes,
                new Dictionary <string, A> {
                    { "A", new A {
                          a = 1
                      } },
                    { "C", new C {
                          a = 3, c = 56
                      } },
                    { "E", new E {
                          a = 5, c = 78, e = 456
                      } }
                }
            };

            List <BsonDocument> docs   = items.Select(x => x.ToBsonDocument()).ToList();
            List <object>       items2 = docs.Select(x => BsonSerializer.Deserialize(x, typeof(object))).ToList();

            foreach (BsonDocument doc in docs)
            {
                Console.WriteLine(doc.ToJson());
                Console.WriteLine();
            }

            string outputFileRoot = @"C:\Users\adecler\Documents\"; // initialize to the file to write to.

            File.WriteAllLines(@"C:\Users\adecler\Documents\json_Save.txt", docs.Select(x => x.ToJson()));

            FileStream mongoStream = new FileStream(outputFileRoot + "bsonSave_Mongo.txt", FileMode.Create);
            var        writer      = new BsonBinaryWriter(mongoStream);

            BsonSerializer.Serialize(writer, typeof(object), docs);
            mongoStream.Flush();
            mongoStream.Close();

            FileStream      csharpStream = new FileStream(outputFileRoot + "bsonSave_CSharp.txt", FileMode.Create);
            BinaryFormatter formatter    = new BinaryFormatter();

            formatter.Serialize(csharpStream, docs);
            csharpStream.Close();

            FileStream          mongoReadStream = File.OpenRead(outputFileRoot + "bsonSave_Mongo.txt");
            var                 reader          = new BsonBinaryReader(mongoReadStream);
            List <BsonDocument> readBson        = BsonSerializer.Deserialize(reader, typeof(object)) as List <BsonDocument>;
            List <object>       items3          = readBson.Select(x => BsonSerializer.Deserialize(x, typeof(object))).ToList();

            // Directly writing and reading objects to the stream using Bson serializer seems to have a problem when reading back
            //FileStream objectStream = new FileStream(outputFileRoot + "objectSave_Mongo.txt", FileMode.Create);
            //var objectWriter = new BsonBinaryWriter(objectStream);
            //BsonSerializer.Serialize(objectWriter, typeof(List<object>), items);
            //objectStream.Flush();
            //objectStream.Close();

            //FileStream objectReadStream = File.OpenRead(outputFileRoot + "objectSave_Mongo.txt");
            //var objectReader = new BsonBinaryReader(objectReadStream);
            //var readObject = BsonSerializer.Deserialize(reader, typeof(object));


            Console.WriteLine("Done!");
        }
 /// <summary>
 /// Writes a raw BSON document.
 /// </summary>
 /// <param name="slice">The byte buffer containing the raw BSON document.</param>
 public virtual void WriteRawBsonDocument(IByteBuffer slice)
 {
     // overridden in BsonBinaryWriter
     using (var bsonReader = new BsonBinaryReader(new BsonBuffer(slice, false), true, BsonBinaryReaderSettings.Defaults))
     {
         var document = BsonSerializer.Deserialize<BsonDocument>(bsonReader);
         BsonDocumentSerializer.Instance.Serialize(this, typeof(BsonDocument), document, null);
     }
 }
        /// <summary>
        /// Tries to get the value of an element of this document.
        /// </summary>
        /// <param name="name">The name of the element.</param>
        /// <param name="value">The value of the element.</param>
        /// <returns>
        /// True if an element with that name was found.
        /// </returns>
        public override bool TryGetValue(string name, out BsonValue value)
        {
            ThrowIfDisposed();
            using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), true), true, _readerSettings))
            {
                bsonReader.ReadStartDocument();
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    if (bsonReader.ReadName() == name)
                    {
                        value = DeserializeBsonValue(bsonReader);
                        return true;
                    }

                    bsonReader.SkipValue();
                }
                bsonReader.ReadEndDocument();

                value = null;
                return false;
            }
        }
        private TCommandResult ProcessResponse(ConnectionId connectionId, CommandMessage responseMessage)
        {
            using (new CommandMessageDisposer(responseMessage))
            {
                var rawDocument = responseMessage.Sections.OfType <Type0CommandMessageSection <RawBsonDocument> >().Single().Document;

                var binaryReaderSettings = new BsonBinaryReaderSettings();
                if (_messageEncoderSettings != null)
                {
                    binaryReaderSettings.Encoding           = _messageEncoderSettings.GetOrDefault <UTF8Encoding>(MessageEncoderSettingsName.ReadEncoding, Utf8Encodings.Strict);
                    binaryReaderSettings.GuidRepresentation = _messageEncoderSettings.GetOrDefault <GuidRepresentation>(MessageEncoderSettingsName.GuidRepresentation, GuidRepresentation.CSharpLegacy);
                }
                ;

                BsonValue clusterTime;
                if (rawDocument.TryGetValue("$clusterTime", out clusterTime))
                {
                    // note: we are assuming that _session is an instance of ClusterClockAdvancingClusterTime
                    // and that calling _session.AdvanceClusterTime will have the side effect of advancing the cluster's ClusterTime also
                    var materializedClusterTime = ((RawBsonDocument)clusterTime).Materialize(binaryReaderSettings);
                    _session.AdvanceClusterTime(materializedClusterTime);
                }

                BsonValue operationTime;
                if (rawDocument.TryGetValue("operationTime", out operationTime))
                {
                    _session.AdvanceOperationTime(operationTime.AsBsonTimestamp);
                }

                if (!rawDocument.GetValue("ok", false).ToBoolean())
                {
                    var materializedDocument = rawDocument.Materialize(binaryReaderSettings);

                    var commandName = _command.GetElement(0).Name;
                    if (commandName == "$query")
                    {
                        commandName = _command["$query"].AsBsonDocument.GetElement(0).Name;
                    }

                    var notPrimaryOrNodeIsRecoveringException = ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, materializedDocument, "errmsg");
                    if (notPrimaryOrNodeIsRecoveringException != null)
                    {
                        throw notPrimaryOrNodeIsRecoveringException;
                    }

                    var mappedException = ExceptionMapper.Map(connectionId, materializedDocument);
                    if (mappedException != null)
                    {
                        throw mappedException;
                    }

                    string    message;
                    BsonValue errmsgBsonValue;
                    if (materializedDocument.TryGetValue("errmsg", out errmsgBsonValue) && errmsgBsonValue.IsString)
                    {
                        var errmsg = errmsgBsonValue.ToString();
                        message = string.Format("Command {0} failed: {1}.", commandName, errmsg);
                    }
                    else
                    {
                        message = string.Format("Command {0} failed.", commandName);
                    }

                    throw new MongoCommandException(connectionId, message, _command, materializedDocument);
                }

                using (var stream = new ByteBufferStream(rawDocument.Slice, ownsBuffer: false))
                {
                    using (var reader = new BsonBinaryReader(stream, binaryReaderSettings))
                    {
                        var context = BsonDeserializationContext.CreateRoot(reader);
                        return(_resultSerializer.Deserialize(context));
                    }
                }
            }
        }
        /// <summary>
        /// Writes a raw BSON array.
        /// </summary>
        /// <param name="slice">The byte buffer containing the raw BSON array.</param>
        public virtual void WriteRawBsonArray(IByteBuffer slice)
        {
            // overridden in BsonBinaryWriter

            using (var bsonBuffer = new BsonBuffer())
            {
                BsonArray array;

                // wrap the array in a fake document so we can deserialize it
                var arrayLength = slice.Length;
                var documentLength = arrayLength + 8;
                bsonBuffer.WriteInt32(documentLength);
                bsonBuffer.WriteByte((byte)BsonType.Array);
                bsonBuffer.WriteByte((byte)'x');
                bsonBuffer.WriteByte((byte)0);
                bsonBuffer.ByteBuffer.WriteBytes(slice);
                bsonBuffer.WriteByte((byte)0);

                bsonBuffer.Position = 0;
                using (var bsonReader = new BsonBinaryReader(bsonBuffer, true, BsonBinaryReaderSettings.Defaults))
                {
                    bsonReader.ReadStartDocument();
                    bsonReader.ReadName("x");
                    array = (BsonArray)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), null);
                    bsonReader.ReadEndDocument();
                }

                BsonArraySerializer.Instance.Serialize(this, typeof(BsonArray), array, null);
            }
        }
Beispiel #20
0
        public static string ConvertToJson(string bsonString)
        {
            StringBuilder  jsonString = new StringBuilder();
            string         nameStr;
            string         valueStr;
            Stack <string> stack = new Stack <string>();
            string         stackTop;
            Stack <bool>   stackFirst = new Stack <bool>();

            byte[] buffer = Convert.FromBase64String(bsonString);
            string text   = Encoding.UTF8.GetString(buffer);

            using (var stream = new MemoryStream(buffer))
                using (var reader = new BsonBinaryReader(stream))
                {
                    bool exit      = false;
                    bool firstElem = true;
                    reader.ReadStartDocument();
                    jsonString.Append("{ ");
                    while (!reader.IsAtEndOfFile() && !exit)
                    {
                        var bsonType = reader.ReadBsonType(); //.CurrentBsonType;
                        switch (bsonType)
                        {
                        case MongoDB.Bson.BsonType.Array:
                            nameStr = reader.ReadName();
                            reader.ReadStartArray();
                            if (firstElem)
                            {
                                firstElem = false;
                            }
                            else
                            {
                                jsonString.Append(", ");
                            }
                            jsonString.Append($"\"{nameStr}\" : [ ");
                            stack.Push("Array");
                            stackFirst.Push(firstElem);
                            firstElem = true;
                            break;

                        case MongoDB.Bson.BsonType.Binary:
                            break;

                        case MongoDB.Bson.BsonType.Boolean:
                            nameStr  = reader.ReadName();
                            valueStr = reader.ReadBoolean().ToString();
                            if (firstElem)
                            {
                                firstElem = false;
                            }
                            else
                            {
                                jsonString.Append(", ");
                            }
                            jsonString.Append($"\"{nameStr}\" : \"{valueStr}\"");
                            break;

                        case MongoDB.Bson.BsonType.DateTime:
                            nameStr  = reader.ReadName();
                            valueStr = reader.ReadString();
                            if (firstElem)
                            {
                                firstElem = false;
                            }
                            else
                            {
                                jsonString.Append(", ");
                            }
                            jsonString.Append($"\"{nameStr}\" : \"{valueStr}\"");
                            break;

                        case MongoDB.Bson.BsonType.Decimal128:
                            nameStr  = reader.ReadName();
                            valueStr = reader.ReadDecimal128().ToString();
                            if (firstElem)
                            {
                                firstElem = false;
                            }
                            else
                            {
                                jsonString.Append(", ");
                            }
                            jsonString.Append($"\"{nameStr}\" : \"{valueStr}\"");
                            break;

                        case MongoDB.Bson.BsonType.Document:
                            nameStr = reader.ReadName();
                            reader.ReadStartDocument();
                            if (firstElem)
                            {
                                firstElem = false;
                            }
                            else
                            {
                                jsonString.Append(", ");
                            }
                            jsonString.Append($"\"{nameStr}\" : {{ ");
                            stack.Push("Document");
                            stackFirst.Push(firstElem);
                            firstElem = true;
                            break;

                        case MongoDB.Bson.BsonType.Double:
                            nameStr  = reader.ReadName();
                            valueStr = reader.ReadDouble().ToString();
                            if (firstElem)
                            {
                                firstElem = false;
                            }
                            else
                            {
                                jsonString.Append(", ");
                            }
                            jsonString.Append($"\"{nameStr}\" : \"{valueStr}\"");
                            break;

                        case MongoDB.Bson.BsonType.EndOfDocument:
                            if (stack.Count == 0)
                            {
                                jsonString.Append("} ");
                                exit = true;
                            }
                            else
                            {
                                stackTop  = stack.Pop();
                                firstElem = stackFirst.Pop();
                                switch (stackTop)
                                {
                                case "Array":
                                    jsonString.Append("]");
                                    reader.ReadEndArray();
                                    break;

                                case "Document":
                                    jsonString.Append("}");
                                    reader.ReadEndDocument();
                                    break;

                                default:
                                    break;
                                }
                            }
                            break;

                        case MongoDB.Bson.BsonType.Int32:
                            nameStr  = reader.ReadName();
                            valueStr = reader.ReadInt32().ToString();
                            if (firstElem)
                            {
                                firstElem = false;
                            }
                            else
                            {
                                jsonString.Append(", ");
                            }
                            jsonString.Append($"\"{nameStr}\" : \"{valueStr}\"");
                            break;

                        case MongoDB.Bson.BsonType.Int64:
                            nameStr  = reader.ReadName();
                            valueStr = reader.ReadInt64().ToString();
                            if (firstElem)
                            {
                                firstElem = false;
                            }
                            else
                            {
                                jsonString.Append(", ");
                            }
                            jsonString.Append($"\"{nameStr}\" : \"{valueStr}\"");
                            break;

                        case MongoDB.Bson.BsonType.JavaScript:
                            break;

                        case MongoDB.Bson.BsonType.JavaScriptWithScope:
                            break;

                        case MongoDB.Bson.BsonType.MaxKey:
                            nameStr = reader.ReadName();
                            reader.ReadMaxKey();
                            valueStr = "";
                            jsonString.Append($"\"{nameStr}\" : \"{valueStr}\"");
                            break;

                        case MongoDB.Bson.BsonType.MinKey:
                            nameStr = reader.ReadName();
                            reader.ReadMinKey();
                            valueStr = "";
                            jsonString.Append($"\"{nameStr}\" : \"{valueStr}\"");
                            break;

                        case MongoDB.Bson.BsonType.Null:
                            nameStr = reader.ReadName();
                            reader.ReadNull();
                            valueStr = "null";
                            if (firstElem)
                            {
                                firstElem = false;
                            }
                            else
                            {
                                jsonString.Append(", ");
                            }
                            jsonString.Append($"\"{nameStr}\" : \"{valueStr}\"");
                            break;

                        case MongoDB.Bson.BsonType.ObjectId:
                            nameStr  = reader.ReadName();
                            valueStr = reader.ReadObjectId().ToString();
                            if (firstElem)
                            {
                                firstElem = false;
                            }
                            else
                            {
                                jsonString.Append(", ");
                            }
                            jsonString.Append($"\"{nameStr}\" : \"{valueStr}\"");
                            break;

                        case MongoDB.Bson.BsonType.RegularExpression:
                            break;

                        case MongoDB.Bson.BsonType.String:
                            if (firstElem)
                            {
                                firstElem = false;
                            }
                            else
                            {
                                jsonString.Append(", ");
                            }
                            if (reader.State == BsonReaderState.Name)
                            {
                                nameStr = reader.ReadName();
                                jsonString.Append($"\"{nameStr}\" : ");
                            }
                            valueStr = reader.ReadString();
                            jsonString.Append($"\"{valueStr}\"");
                            break;

                        case MongoDB.Bson.BsonType.Symbol:
                            nameStr  = reader.ReadName();
                            valueStr = reader.ReadSymbol();
                            if (firstElem)
                            {
                                firstElem = false;
                            }
                            else
                            {
                                jsonString.Append(", ");
                            }
                            jsonString.Append($"\"{nameStr}\" : \"{valueStr}\"");
                            break;

                        case MongoDB.Bson.BsonType.Timestamp:
                            nameStr  = reader.ReadName();
                            valueStr = reader.ReadTimestamp().ToString();
                            if (firstElem)
                            {
                                firstElem = false;
                            }
                            else
                            {
                                jsonString.Append(", ");
                            }
                            jsonString.Append($"\"{nameStr}\" : \"{valueStr}\"");
                            break;

                        case MongoDB.Bson.BsonType.Undefined:
                            nameStr = reader.ReadName();
                            reader.ReadUndefined();
                            valueStr = "undefined";
                            if (firstElem)
                            {
                                firstElem = false;
                            }
                            else
                            {
                                jsonString.Append(", ");
                            }
                            jsonString.Append($"\"{nameStr}\" : \"{valueStr}\"");
                            break;

                        default:
                            break;
                        }
                    }
                    reader.ReadEndDocument();
                }


            return(jsonString.ToString());;
        }
 public override void CopyTo(object[] array, int arrayIndex)
 {
     ThrowIfDisposed();
     using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
     {
         bsonReader.ReadStartDocument();
         while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
         {
             bsonReader.SkipName();
             array[arrayIndex++] = DeserializeBsonValue(bsonReader).RawValue;
         }
         bsonReader.ReadEndDocument();
     }
 }
        /// <summary>
        /// Tests whether the array contains a value.
        /// </summary>
        /// <param name="value">The value to test for.</param>
        /// <returns>True if the array contains the value.</returns>
        public override bool Contains(BsonValue value)
        {
            ThrowIfDisposed();
            using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
            {
                bsonReader.ReadStartDocument();
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    bsonReader.SkipName();
                    if (DeserializeBsonValue(bsonReader).Equals(value))
                    {
                        return true;
                    }
                }
                bsonReader.ReadEndDocument();

                return false;
            }
        }
        // public indexers
        /// <summary>
        /// Gets or sets a value by position.
        /// </summary>
        /// <param name="index">The position.</param>
        /// <returns>The value.</returns>
        public override BsonValue this[int index]
        {
            get
            {
                if (index < 0)
                {
                    throw new ArgumentOutOfRangeException("index");
                }
                ThrowIfDisposed();

                using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
                {
                    bsonReader.ReadStartDocument();
                    var i = 0;
                    while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                    {
                        bsonReader.SkipName();
                        if (i == index)
                        {
                            return DeserializeBsonValue(bsonReader);
                        }

                        bsonReader.SkipValue();
                        i++;
                    }
                    bsonReader.ReadEndDocument();

                    throw new ArgumentOutOfRangeException("index");
                }
            }
            set
            {
                throw new NotSupportedException("RawBsonArray instances are immutable.");
            }
        }
Beispiel #24
0
        private void BsonToSql(string tableName, string input, CancellationToken token)
        {
            const string PARAM_FILENAME  = "@fileName";
            const string PARAM_DOCUMENT  = "@document";
            const string PARAM_PROCESSID = "@processId";

            const string QUERY = @"insert {0} (fileName,document,processId) values (@fileName,@document,@processId);";


            using (var cn = new SqlConnection(_attributes[CONNECTION_STRING]))
            {
                cn.Open();
                try
                {
                    using (SqlCommand cmd = new SqlCommand(String.Format(QUERY, tableName), cn))
                    {
                        cmd.Parameters.Add(PARAM_FILENAME, SqlDbType.NVarChar, 500);
                        cmd.Parameters.Add(PARAM_DOCUMENT, SqlDbType.NVarChar, -1);
                        var param = cmd.Parameters.Add(PARAM_PROCESSID, SqlDbType.Int);
                        param.Value = Int32.Parse(_attributes[RUNID]);
                        cmd.Prepare();
                        using (token.Register(cmd.Cancel))
                        {
                            string[] files = Directory.GetFiles(Path.GetDirectoryName(input), Path.GetFileName(input), SearchOption.TopDirectoryOnly);
                            foreach (string file in files)
                            {
                                token.ThrowIfCancellationRequested();
                                CheckTable(tableName, token);
                                cmd.Parameters[PARAM_FILENAME].Value = file;

                                FileInfo fileToLoad = new FileInfo(file);
                                using (FileStream originalFileStream = fileToLoad.OpenRead())
                                {
                                    if ((File.GetAttributes(fileToLoad.FullName) &
                                         FileAttributes.Hidden) != FileAttributes.Hidden & fileToLoad.Extension != ".json")
                                    {
                                        using (var reader = new BsonBinaryReader(originalFileStream))
                                        {
                                            while (!reader.IsAtEndOfFile())
                                            {
                                                token.ThrowIfCancellationRequested();

                                                var    bson = BsonSerializer.Deserialize <BsonDocument>(reader);
                                                string json = bson.ToJson(new JsonWriterSettings()
                                                {
                                                    OutputMode = JsonOutputMode.CanonicalExtendedJson
                                                });
                                                cmd.Parameters[PARAM_DOCUMENT].Value = json;
                                                cmd.ExecuteNonQuery();
                                            }
                                        }
                                    }

                                    _logger.Information("Loaded {From} => {To}", fileToLoad.Name, tableName);
                                }
                            }
                        }
                    }
                }
                catch (SqlException ex)
                {
                    throw ex;
                    //_logger.Write(String.Format("SqlServer exception: {0}", ex.Message));
                    //result = WfResult.Create(WfStatus.Failed, ex.Message, ex.ErrorCode);
                }
                finally
                {
                    if (cn.State != ConnectionState.Closed)
                    {
                        cn.Close();
                    }
                }
            }
        }
        /// <summary>
        /// Tests whether the document contains an element with the specified name.
        /// </summary>
        /// <param name="name">The name of the element to look for.</param>
        /// <returns>
        /// True if the document contains an element with the specified name.
        /// </returns>
        public override bool Contains(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            ThrowIfDisposed();

            using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), true), true, _readerSettings))
            {
                bsonReader.ReadStartDocument();
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    if (bsonReader.ReadName() == name)
                    {
                        return true;
                    }
                    bsonReader.SkipValue();
                }
                bsonReader.ReadEndDocument();

                return false;
            }
        }
Beispiel #26
0
 public BinaryMessageEncoderFactory(BsonBinaryReader binaryReader, BsonBinaryWriter binaryWriter)
 {
     Ensure.That(binaryReader != null || binaryWriter != null, "bsonReader and bsonWriter cannot both be null.");
     _binaryReader = binaryReader;
     _binaryWriter = binaryWriter;
 }
        private TCommandResult ProcessResponse(ConnectionId connectionId, CommandMessage responseMessage)
        {
            using (new CommandMessageDisposer(responseMessage))
            {
                var rawDocument = responseMessage.Sections.OfType <Type0CommandMessageSection <RawBsonDocument> >().Single().Document;

                var binaryReaderSettings = new BsonBinaryReaderSettings();
                if (_messageEncoderSettings != null)
                {
                    binaryReaderSettings.Encoding = _messageEncoderSettings.GetOrDefault <UTF8Encoding>(MessageEncoderSettingsName.ReadEncoding, Utf8Encodings.Strict);
#pragma warning disable 618
                    if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)
                    {
                        binaryReaderSettings.GuidRepresentation = _messageEncoderSettings.GetOrDefault <GuidRepresentation>(MessageEncoderSettingsName.GuidRepresentation, GuidRepresentation.CSharpLegacy);
                    }
#pragma warning restore 618
                }
                ;

                BsonValue clusterTime;
                if (rawDocument.TryGetValue("$clusterTime", out clusterTime))
                {
                    // note: we are assuming that _session is an instance of ClusterClockAdvancingClusterTime
                    // and that calling _session.AdvanceClusterTime will have the side effect of advancing the cluster's ClusterTime also
                    var materializedClusterTime = ((RawBsonDocument)clusterTime).Materialize(binaryReaderSettings);
                    _session.AdvanceClusterTime(materializedClusterTime);
                }

                BsonValue operationTime;
                if (rawDocument.TryGetValue("operationTime", out operationTime))
                {
                    _session.AdvanceOperationTime(operationTime.AsBsonTimestamp);
                }

                if (rawDocument.GetValue("ok", false).ToBoolean())
                {
                    if (rawDocument.TryGetValue("recoveryToken", out var rawRecoveryToken))
                    {
                        var recoveryToken = ((RawBsonDocument)rawRecoveryToken).Materialize(binaryReaderSettings);
                        _session.CurrentTransaction.RecoveryToken = recoveryToken;
                    }
                }
                else
                {
                    var materializedDocument = rawDocument.Materialize(binaryReaderSettings);

                    var commandName = _command.GetElement(0).Name;
                    if (commandName == "$query")
                    {
                        commandName = _command["$query"].AsBsonDocument.GetElement(0).Name;
                    }

                    var notPrimaryOrNodeIsRecoveringException = ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, _command, materializedDocument, "errmsg");
                    if (notPrimaryOrNodeIsRecoveringException != null)
                    {
                        throw notPrimaryOrNodeIsRecoveringException;
                    }

                    var mappedException = ExceptionMapper.Map(connectionId, materializedDocument);
                    if (mappedException != null)
                    {
                        throw mappedException;
                    }

                    string    message;
                    BsonValue errmsgBsonValue;
                    if (materializedDocument.TryGetValue("errmsg", out errmsgBsonValue) && errmsgBsonValue.IsString)
                    {
                        var errmsg = errmsgBsonValue.ToString();
                        message = string.Format("Command {0} failed: {1}.", commandName, errmsg);
                    }
                    else
                    {
                        message = string.Format("Command {0} failed.", commandName);
                    }

                    var exception = new MongoCommandException(connectionId, message, _command, materializedDocument);

                    // https://jira.mongodb.org/browse/CSHARP-2678
                    if (IsRetryableWriteExceptionAndDeploymentDoesNotSupportRetryableWrites(exception))
                    {
                        throw WrapNotSupportedRetryableWriteException(exception);
                    }
                    else
                    {
                        throw exception;
                    }
                }

                if (rawDocument.Contains("writeConcernError"))
                {
                    var materializedDocument = rawDocument.Materialize(binaryReaderSettings);
                    var writeConcernError    = materializedDocument["writeConcernError"].AsBsonDocument;
                    var message            = writeConcernError.AsBsonDocument.GetValue("errmsg", null)?.AsString;
                    var writeConcernResult = new WriteConcernResult(materializedDocument);
                    throw new MongoWriteConcernException(connectionId, message, writeConcernResult);
                }

                using (var stream = new ByteBufferStream(rawDocument.Slice, ownsBuffer: false))
                {
                    using (var reader = new BsonBinaryReader(stream, binaryReaderSettings))
                    {
                        var context = BsonDeserializationContext.CreateRoot(reader);
                        return(_resultSerializer.Deserialize(context));
                    }
                }
            }
        }
Beispiel #28
0
 // constructors
 public QueryMessageBinaryEncoder(BsonBinaryReader binaryReader, BsonBinaryWriter binaryWriter)
 {
     Ensure.That(binaryReader != null || binaryWriter != null, "binaryReader and binaryWriter cannot both be null.");
     _binaryReader = binaryReader;
     _binaryWriter = binaryWriter;
 }
 private RawBsonArray DeserializeRawBsonArray(BsonBinaryReader bsonReader)
 {
     var slice = bsonReader.ReadRawBsonArray();
     var nestedArray = new RawBsonArray(slice);
     _disposableItems.Add(nestedArray);
     return nestedArray;
 }
        private IEnumerable<BsonElement> MaterializeThisLevel()
        {
            var elements = new List<BsonElement>();
            var readerSettings = _readerSettings.Clone();
            readerSettings.MaxDocumentSize = _slice.Length;
            using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), true), true, readerSettings))
            {
                bsonReader.ReadStartDocument();
                BsonType bsonType;
                while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument)
                {
                    var name = bsonReader.ReadName();
                    BsonValue value;
                    switch (bsonType)
                    {
                        case BsonType.Array: value = DeserializeLazyBsonArray(bsonReader); break;
                        case BsonType.Document: value = DeserializeLazyBsonDocument(bsonReader); break;
                        default: value = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null); break;
                    }
                    elements.Add(new BsonElement(name, value));
                }
                bsonReader.ReadEndDocument();
            }

            return elements;
        }
 private RawBsonDocument DeserializeRawBsonDocument(BsonBinaryReader bsonReader)
 {
     var slice = bsonReader.ReadRawBsonDocument();
     var nestedDocument = new RawBsonDocument(slice);
     _disposableItems.Add(nestedDocument);
     return nestedDocument;
 }
 private BsonValue DeserializeBsonValue(BsonBinaryReader bsonReader)
 {
     switch (bsonReader.GetCurrentBsonType())
     {
         case BsonType.Array: return DeserializeRawBsonArray(bsonReader);
         case BsonType.Document: return DeserializeRawBsonDocument(bsonReader);
         default: return (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null);
     }
 }
 /// <summary>
 /// Gets an enumerator that can enumerate the elements of the array.
 /// </summary>
 /// <returns>An enumerator.</returns>
 public override IEnumerator<BsonValue> GetEnumerator()
 {
     ThrowIfDisposed();
     using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
     {
         bsonReader.ReadStartDocument();
         while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
         {
             bsonReader.SkipName();
             yield return DeserializeBsonValue(bsonReader);
         }
         bsonReader.ReadEndDocument();
     }
 }
        /// <summary>
        /// Gets the value of an element.
        /// </summary>
        /// <param name="index">The zero based index of the element.</param>
        /// <returns>
        /// The value of the element.
        /// </returns>
        public override BsonValue GetValue(int index)
        {
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            ThrowIfDisposed();

            using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), true), true, _readerSettings))
            {
                bsonReader.ReadStartDocument();
                var i = 0;
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    bsonReader.SkipName();
                    if (i == index)
                    {
                        return DeserializeBsonValue(bsonReader);
                    }

                    bsonReader.SkipValue();
                    i++;
                }
                bsonReader.ReadEndDocument();

                throw new ArgumentOutOfRangeException("index");
            }
        }
Beispiel #35
0
        // internal methods
        internal void ReadFrom(BsonBuffer buffer, IBsonSerializationOptions serializationOptions)
        {
            if (serializationOptions == null && typeof(TDocument) == typeof(BsonDocument))
            {
                serializationOptions = DocumentSerializationOptions.AllowDuplicateNamesInstance;
            }

            var messageStartPosition = buffer.Position;

            ReadMessageHeaderFrom(buffer);
            _responseFlags  = (ResponseFlags)buffer.ReadInt32();
            _cursorId       = buffer.ReadInt64();
            _startingFrom   = buffer.ReadInt32();
            _numberReturned = buffer.ReadInt32();

            if ((_responseFlags & ResponseFlags.CursorNotFound) != 0)
            {
                throw new MongoQueryException("Cursor not found.");
            }
            if ((_responseFlags & ResponseFlags.QueryFailure) != 0)
            {
                BsonDocument document;
                using (BsonReader bsonReader = new BsonBinaryReader(buffer, false, _readerSettings))
                {
                    document = (BsonDocument)BsonDocumentSerializer.Instance.Deserialize(bsonReader, typeof(BsonDocument), null);
                }

                var mappedException = ExceptionMapper.Map(document);
                if (mappedException != null)
                {
                    throw mappedException;
                }

                var err     = document.GetValue("$err", "Unknown error.");
                var message = string.Format("QueryFailure flag was {0} (response was {1}).", err, document.ToJson());
                throw new MongoQueryException(message, document);
            }

            _documents = new List <TDocument>(_numberReturned);
            for (int i = 0; i < _numberReturned; i++)
            {
                BsonBuffer sliceBuffer;
                if (buffer.ByteBuffer is MultiChunkBuffer)
                {
                    // we can use slightly faster SingleChunkBuffers for all documents that don't span chunk boundaries
                    var position = buffer.Position;
                    var length   = buffer.ReadInt32();
                    var slice    = buffer.ByteBuffer.GetSlice(position, length);
                    buffer.Position = position + length;
                    sliceBuffer     = new BsonBuffer(slice, true);
                }
                else
                {
                    sliceBuffer = new BsonBuffer(buffer.ByteBuffer, false);
                }

                using (var bsonReader = new BsonBinaryReader(sliceBuffer, true, _readerSettings))
                {
                    var document = (TDocument)_serializer.Deserialize(bsonReader, typeof(TDocument), serializationOptions);
                    _documents.Add(document);
                }
            }
        }
        /// <summary>
        /// Gets the index of a value in the array.
        /// </summary>
        /// <param name="value">The value to search for.</param>
        /// <param name="index">The zero based index at which to start the search.</param>
        /// <param name="count">The number of elements to search.</param>
        /// <returns>The zero based index of the value (or -1 if not found).</returns>
        public override int IndexOf(BsonValue value, int index, int count)
        {
            ThrowIfDisposed();
            using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
            {
                bsonReader.ReadStartDocument();
                var i = 0;
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    bsonReader.SkipName();
                    if (i >= index)
                    {
                        if (count == 0)
                        {
                            return -1;
                        }

                        if (DeserializeBsonValue(bsonReader).Equals(value))
                        {
                            return i;
                        }

                        count--;
                    }
                    else
                    {
                        bsonReader.SkipValue();
                    }

                    i++;
                }
                bsonReader.ReadEndDocument();

                return -1;
            }
        }