Пример #1
0
        public bool Exist(MessageKey key, string filter)
        {
            Guard.Instance.ArgumentNotNull(() => key, key);

            var query = string.IsNullOrWhiteSpace(filter) ?
                        new QueryDocument() :
                        filter.ToQueryDocument();

            if (query.Contains(Constants.FieldNames.Id))
            {
                if (query[Constants.FieldNames.Id] != key.MessageId.ToBson())
                {
                    // Filter already has a different message's id. So the query will always return false.
                    return(false);
                }
            }
            else
            {
                // The filter does not include the message's id. Then add it to que query.
                query.Add(Constants.FieldNames.Id, key.MessageId.ToBson());
            }

            string collectionName = MongoDbConstants.GetCollectionNameForMessage(key.TopicId);

            return(DB.GetCollection <Message>(collectionName).Exists(query));
        }
Пример #2
0
        public void WhenMessageExists_MustReturnMessage()
        {
            var col = base.mongoDb.GetCollection <Message>(MongoDbConstants.GetCollectionNameForMessage(topicId));
            var msg = new Message
            {
                UtcReceivedOn = DateTime.UtcNow,
                TopicId       = topicId,
                Payload       = new byte[] { 1, 2, 3, 4, 5 }
            };

            col.Save(msg);

            Assert.IsNotNull(msg.Id);

            var key = new MessageKey {
                MessageId = msg.Id.Value, TopicId = topicId
            };
            var query  = new MessageByMessageKey(base.connectionString);
            var result = query.Get(key);

            Assert.IsNotNull(result);
            Assert.AreEqual(msg.Id, result.Id);
            Assert.AreEqual(msg.UtcReceivedOn.Date, result.UtcReceivedOn.Date);
            Assert.AreEqual(msg.TopicId, result.TopicId);
            CollectionAssert.AreEqual(msg.Payload, result.Payload);
        }
Пример #3
0
        public void MakeTransient(MessageKey key)
        {
            MongoCollection <Message> collection =
                DB.GetCollection <Message>(MongoDbConstants.GetCollectionNameForMessage(key.TopicId));

            collection.Remove(key.MessageId);
        }
Пример #4
0
        public void Update(Message entity)
        {
            MongoCollection <Message> collection =
                DB.GetCollection <Message>(MongoDbConstants.GetCollectionNameForMessage(entity.TopicId));

            collection.Save(entity);
        }
        public SubscriptionsByTopicAndTopicGroup(string connectionString, IQueryGroupAncestors queryGroupAncestors) : base(connectionString)
        {
            this.queryGroupAncestors = queryGroupAncestors;
            var topicCollectionName = MongoDbConstants.GetCollectionNameForType <Topic>();

            topicCollection = DB.GetCollection <Topic>(topicCollectionName);
            var subscriptionCollectionName = MongoDbConstants.GetCollectionNameForType <Subscription>();

            subscriptionCollection = DB.GetCollection <Subscription>(subscriptionCollectionName);
        }
Пример #6
0
        public void Setup()
        {
            var colG = base.mongoDb.GetCollection <Group>(MongoDbConstants.GetCollectionNameForType <Group>());
            var colT = base.mongoDb.GetCollection <Topic>(MongoDbConstants.GetCollectionNameForType <Topic>());

            for (var i = 0; i < 4; i++)
            {
                // Add group
                var group = new Group
                {
                    Name     = "Group Foo " + i,
                    ParentId = (i == 1 || i == 2) ? groupIds[i - 1] : new Identity?()                 // Set group hierarchy
                };
                colG.Save(group);
                groupIds[i] = group.Id.Value;

                // Add a topic to the group
                var topic = new Topic {
                    Name = "Topic foo " + i, GroupId = group.Id.Value
                };
                colT.Save(topic);
                topicIds[i] = topic.Id.Value;

                // Add a message to the topic
                var colM    = base.mongoDb.GetCollection <Message>(MongoDbConstants.GetCollectionNameForMessage(topicIds[i]));
                var message = new Message
                {
                    UtcReceivedOn = DateTime.UtcNow,
                    TopicId       = topicIds[i],
                    Payload       = new byte[] { 1, 2, 3, 4, 5 }
                };
                colM.Save(message);
                msgIds[i] = message.Id.Value;
            }

            childGroupsOfGroup = Mock.Of <IChildGroupsOfGroup>(q =>
                                                               q.GetChildrenIds(groupIds[0]) == groupIds.Skip(1).Take(1) &&
                                                               q.GetChildrenIds(groupIds[1]) == groupIds.Skip(2).Take(1) &&
                                                               q.GetChildrenIds(groupIds[2]) == new Identity[0] &&
                                                               q.GetChildrenIds(groupIds[3]) == new Identity[0]);

            topicsByGroup = Mock.Of <ITopicsByGroup>(q =>
                                                     q.GetTopicIds(groupIds[0], null, null) == topicIds.Skip(0).Take(1) &&
                                                     q.GetTopicIds(groupIds[1], null, null) == topicIds.Skip(1).Take(1) &&
                                                     q.GetTopicIds(groupIds[2], null, null) == topicIds.Skip(2).Take(1) &&
                                                     q.GetTopicIds(groupIds[3], null, null) == topicIds.Skip(3).Take(1));
        }
Пример #7
0
        public void SetUp()
        {
            var group = new Group {
                Name = "aaa"
            };

            mongoDb.GetCollectionByType <Group>().Insert(group);

            fooTopic = new Topic {
                Name = "FooTopic", Description = "a", GroupId = group.Id.Value
            };
            barTopic = new Topic {
                Name = "BarTopic", Description = "a", GroupId = group.Id.Value
            };

            mongoDb.GetCollectionByType <Topic>()
            .InsertMany(fooTopic, barTopic);

            mongoDb.GetCollection <Message>(MongoDbConstants.GetCollectionNameForMessage(fooTopic.Id.Value))
            .InsertMany(new Message
            {
                UtcReceivedOn = new DateTime(2011, 1, 1),
                TopicId       = fooTopic.Id.Value
            },
                        new Message
            {
                UtcReceivedOn = new DateTime(2011, 2, 2),
                TopicId       = fooTopic.Id.Value
            },
                        new Message
            {
                UtcReceivedOn = new DateTime(2011, 10, 2),
                TopicId       = fooTopic.Id.Value
            });

            mongoDb.GetCollection <Message>(MongoDbConstants.GetCollectionNameForMessage(barTopic.Id.Value))
            .InsertMany(new Message
            {
                UtcReceivedOn = new DateTime(2011, 1, 1),
                TopicId       = barTopic.Id.Value
            },
                        new Message
            {
                UtcReceivedOn = new DateTime(2011, 2, 2),
                TopicId       = barTopic.Id.Value
            });
        }
Пример #8
0
        private IEnumerable <MessageKey> GetMessageKeys(IEnumerable <Identity> topicIds, Identity?last = null)
        {
            foreach (var topicId in topicIds)
            {
                var col   = DB.GetCollection <Message>(MongoDbConstants.GetCollectionNameForMessage(topicId));
                var query = last.HasValue ? Query.GT("_id", BsonValue.Create(last.Value)) : null;

                var cursor = col.Find(query)
                             .SetFields("_id");

                foreach (var message in cursor)
                {
                    yield return(new MessageKey {
                        MessageId = message.Id.Value, TopicId = topicId
                    });
                }
            }
        }
Пример #9
0
        public IEnumerable <T> Execute <T>(string query, int?skip, int?limit)
        {
            Guard.Instance.ArgumentValid(() => skip, () => (skip.HasValue && skip.Value < 0));
            Guard.Instance.ArgumentValid(() => limit, () => (limit.HasValue && limit.Value <= 0));
            var collection = DB.GetCollection <T>(MongoDbConstants.GetCollectionNameForType <T>());
            var queryDoc   = query.ToQueryDocument();
            var cursor     = collection.Find(queryDoc);

            if (skip.HasValue)
            {
                cursor = cursor.SetSkip(skip.Value);
            }
            if (limit.HasValue)
            {
                cursor = cursor.SetLimit(limit.Value);
            }
            return(cursor);
        }
Пример #10
0
        public void Setup()
        {
            topicId = Identity.Random(12);
            var col = base.mongoDb.GetCollection <Message>(MongoDbConstants.GetCollectionNameForMessage(topicId));

            var ids = new List <Identity>();

            for (int i = 0; i < 3; i++)
            {
                var msg = new Message
                {
                    UtcReceivedOn = DateTime.UtcNow,
                    TopicId       = topicId,
                    Payload       = new byte[] { 1, 2, 3, 4, 5 }
                };
                col.Save(msg);
                ids.Add(msg.Id.Value);
            }

            msgIds = ids.ToArray();
        }
Пример #11
0
        public void WhenMessageDoesNotExist_MustReturnNull()
        {
            var col = base.mongoDb.GetCollection <Message>(MongoDbConstants.GetCollectionNameForMessage(topicId));
            var msg = new Message
            {
                UtcReceivedOn = DateTime.UtcNow,
                TopicId       = topicId,
                Payload       = new byte[] { 1, 2, 3, 4, 5 }
            };

            col.Save(msg);

            Assert.IsNotNull(msg.Id);

            var key = new MessageKey {
                MessageId = Identity.Random(12), TopicId = topicId
            };
            var query  = new MessageByMessageKey(base.connectionString);
            var result = query.Get(key);

            Assert.IsNull(result);
        }
Пример #12
0
        public IEnumerable <MessageKey> Get(Identity topicId, Identity?last = null, int?skip = null, int?limit = null)
        {
            var col = DB.GetCollection <Message>(MongoDbConstants.GetCollectionNameForMessage(topicId));

            var query  = last.HasValue ? Query.GT("_id", BsonValue.Create(last.Value)) : null;
            var cursor = col.Find(query);

            if (skip.HasValue)
            {
                cursor.SetSkip(skip.Value);
            }
            if (limit.HasValue)
            {
                cursor.SetLimit(limit.Value);
            }

            return(cursor.Select(msg => new MessageKey
            {
                TopicId = msg.TopicId,
                MessageId = msg.Id.Value
            }));
        }
Пример #13
0
 public Repository(string connectionString) : base(connectionString)
 {
     collection = DB.GetCollection <TEntity>(MongoDbConstants.GetCollectionNameForType <TEntity>());
 }
Пример #14
0
        public bool Exist(MessageKey key)
        {
            string collectionName = MongoDbConstants.GetCollectionNameForMessage(key.TopicId);

            return(DB.GetCollection <Message>(collectionName).Exists(key.MessageId));
        }
Пример #15
0
        public Message Get(MessageKey key)
        {
            string collectionName = MongoDbConstants.GetCollectionNameForMessage(key.TopicId);

            return(DB.GetCollection <Message>(collectionName).FindById(key.MessageId));
        }
Пример #16
0
        public bool Exist <TEntity>(Identity id)
        {
            string collectionName = MongoDbConstants.GetCollectionNameForType <TEntity>();

            return(DB.GetCollection(collectionName).Exists(id));
        }
Пример #17
0
        public TEntity Get <TEntity>(Identity id) where TEntity : class
        {
            string collectionName = MongoDbConstants.GetCollectionNameForType <TEntity>();

            return(DB.GetCollection <TEntity>(collectionName).FindById(id));
        }