public void TestFindOneById()
        {
            _gridFS.Delete(Query.Null);
            Assert.IsFalse(_gridFS.Exists("HelloWorld.txt"));

            var fileInfo  = UploadHelloWorld();
            var foundInfo = _gridFS.FindOneById(fileInfo.Id);

            Assert.AreEqual(fileInfo, foundInfo);
        }
Example #2
0
        public void FindOneByIdTest()
        {
            gridFS.Delete(Query.Null);
            Assert.IsFalse(gridFS.Exists(UploadFileName));

            var fileInfo  = UploadHelloWord();
            var foundInfo = gridFS.FindOneById(fileInfo.Id);

            Assert.AreEqual(fileInfo, foundInfo);
        }
        public void OpenCreateWithIdTest()
        {
            gridFS.Files.RemoveAll();
            gridFS.Chunks.RemoveAll();
            gridFS.Chunks.ResetIndexCache();

            var createOptions = new MongoGridFSCreateOptions
            {
                Id = 1
            };

            using (var stream = gridFS.Create(FileName, createOptions)) {
                var bytes = new byte[] { 1, 2, 3, 4 };
                stream.Write(bytes, 0, 4);
            }

            var fileInfo = gridFS.FindOneById(FileName);

            Assert.AreEqual(BsonInt32.One, fileInfo.Id);
        }
Example #4
0
        /// <summary>
        /// Get a non running message from queue
        /// </summary>
        /// <param name="query">query where top level fields do not contain operators. Lower level fields can however. eg: valid {a: {$gt: 1}, "b.c": 3}, invalid {$and: [{...}, {...}]}</param>
        /// <param name="resetRunning">duration before this message is considered abandoned and will be given with another call to Get()</param>
        /// <param name="wait">duration to keep polling before returning null</param>
        /// <param name="poll">duration between poll attempts</param>
        /// <param name="approximateWait">whether to fluctuate the wait time randomly by +-10 percent. This ensures Get() calls seperate in time when multiple Queues are used in loops started at the same time</param>
        /// <returns>message or null</returns>
        /// <exception cref="ArgumentNullException">query is null</exception>
        public Message Get(QueryDocument query, TimeSpan resetRunning, TimeSpan wait, TimeSpan poll, bool approximateWait)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            //reset stuck messages
            collection.Update(
                new QueryDocument {
                { "running", true }, { "resetTimestamp", new BsonDocument("$lte", DateTime.UtcNow) }
            },
                new UpdateDocument("$set", new BsonDocument("running", false)),
                UpdateFlags.Multi
                );

            var builtQuery = new QueryDocument("running", false);

            foreach (var field in query)
            {
                builtQuery.Add("payload." + field.Name, field.Value);
            }

            builtQuery.Add("earliestGet", new BsonDocument("$lte", DateTime.UtcNow));

            var resetTimestamp = DateTime.UtcNow;

            try
            {
                resetTimestamp += resetRunning;
            }
            catch (ArgumentOutOfRangeException)
            {
                resetTimestamp = resetRunning > TimeSpan.Zero ? DateTime.MaxValue : DateTime.MinValue;
            }

            var sort = new SortByDocument {
                { "priority", 1 }, { "created", 1 }
            };
            var update = new UpdateDocument("$set", new BsonDocument {
                { "running", true }, { "resetTimestamp", resetTimestamp }
            });
            var fields = new FieldsDocument {
                { "payload", 1 }, { "streams", 1 }
            };

            var end = DateTime.UtcNow;

            try
            {
                if (approximateWait)
                {
                    //fluctuate randomly by 10 percent
                    wait += TimeSpan.FromMilliseconds(wait.TotalMilliseconds * GetRandomDouble(-0.1, 0.1));
                }

                end += wait;
            }
            catch (Exception e)
            {
                if (!(e is OverflowException) && !(e is ArgumentOutOfRangeException))
                {
                    throw e;//cant cover
                }
                end = wait > TimeSpan.Zero ? DateTime.MaxValue : DateTime.MinValue;
            }

            while (true)
            {
                var message = collection.FindAndModify(builtQuery, sort, update, fields, false, false).ModifiedDocument;
                if (message != null)
                {
                    var handleStreams  = new List <KeyValuePair <BsonValue, Stream> >();
                    var messageStreams = new Dictionary <string, Stream>();
                    foreach (var streamId in message["streams"].AsBsonArray)
                    {
                        var fileInfo = gridfs.FindOneById(streamId);

                        var stream = fileInfo.OpenRead();

                        handleStreams.Add(new KeyValuePair <BsonValue, Stream>(streamId, stream));
                        messageStreams.Add(fileInfo.Name, stream);
                    }

                    var handle = new Handle(message["_id"].AsObjectId, handleStreams);
                    return(new Message(handle, message["payload"].AsBsonDocument, messageStreams));
                }

                if (DateTime.UtcNow >= end)
                {
                    return(null);
                }

                try
                {
                    Thread.Sleep(poll);
                }
                catch (ArgumentOutOfRangeException)
                {
                    if (poll < TimeSpan.Zero)
                    {
                        poll = TimeSpan.Zero;
                    }
                    else
                    {
                        poll = TimeSpan.FromMilliseconds(int.MaxValue);
                    }

                    Thread.Sleep(poll);
                }

                if (DateTime.UtcNow >= end)
                {
                    return(null);
                }
            }
        }
Example #5
0
        public System.IO.Stream GetFile(ObjectId id)
        {
            MongoGridFSFileInfo file = _gridFs.FindOneById(id);

            return(file.OpenRead());
        }
Example #6
0
        public Stream GetFile(ObjectId id)
        {
            var file = _gridFs.FindOneById(id);

            return(file.OpenRead());
        }
Example #7
0
        public void Send()
        {
            var now = DateTime.Now;

            var payload = new BsonDocument {
                { "key1", 0 }, { "key2", true }
            };

            using (var streamOne = new MemoryStream())
                using (var streamTwo = new MemoryStream())
                {
                    gridfs.Upload(streamOne, "one");//making sure same file names are ok as long as their ids are diffrent

                    streamOne.WriteByte(111);
                    streamTwo.WriteByte(222);
                    streamOne.Position = 0;
                    streamTwo.Position = 0;
                    queue.Send(payload, now, 0.8, new Dictionary <string, Stream> {
                        { "one", streamOne }, { "two", streamTwo }
                    });
                }

            var expected = new BsonDocument
            {
                //_id added below
                { "payload", payload },
                { "running", false },
                { "resetTimestamp", new BsonDateTime(DateTime.MaxValue) },
                { "earliestGet", new BsonDateTime(now) },
                { "priority", 0.8 },
                //streams added below
                //created added below
            };

            var message = collection.FindOneAs <BsonDocument>();

            var actualCreated = message["created"];

            expected["created"] = actualCreated;
            actualCreated       = actualCreated.ToUniversalTime();

            var actualStreamIds = message["streams"].AsBsonArray;

            expected["streams"] = actualStreamIds;

            Assert.IsTrue(actualCreated <= DateTime.UtcNow);
            Assert.IsTrue(actualCreated > DateTime.UtcNow - TimeSpan.FromSeconds(10));

            expected.InsertAt(0, new BsonElement("_id", message["_id"]));
            Assert.AreEqual(expected, message);

            var fileOne = gridfs.FindOneById(actualStreamIds[0]);

            Assert.AreEqual("one", fileOne.Name);
            using (var stream = fileOne.OpenRead())
                Assert.AreEqual(111, stream.ReadByte());

            var fileTwo = gridfs.FindOneById(actualStreamIds[1]);

            Assert.AreEqual("two", fileTwo.Name);
            using (var stream = fileTwo.OpenRead())
                Assert.AreEqual(222, stream.ReadByte());
        }