Exemplo n.º 1
0
        /// <summary>
        /// reset package queues with airingId
        /// </summary>
        /// <param name="queueNames">The queue names.</param>
        /// <param name="airingId">The airing id.</param>
        /// <param name="destinationCode">The destination code</param>
        public void ResetFor(IList <string> queueNames, string airingId, string destinationCode)
        {
            IMongoQuery query = Query.EQ("AssetId", airingId);

            if (!string.IsNullOrEmpty(destinationCode))
            {
                query = Query.And(query, Query.EQ("Flights.Destinations.Name", destinationCode));
            }

            var currentAiring = _currentAirings.FindOne(query);

            if (currentAiring == null)
            {
                return;
            }

            IList <ChangeNotification> changeNotifications = GetChangeNotifications(queueNames, currentAiring);
            List <UpdateBuilder>       updateValues        = new List <UpdateBuilder>();

            updateValues.Add(Update.PullAllWrapped("DeliveredTo", changeNotifications.Select(e => e.QueueName)));
            updateValues.Add(Update.PullAllWrapped("IgnoredQueues", changeNotifications.Select(e => e.QueueName)));
            updateValues.Add(Update.AddToSetEachWrapped("ChangeNotifications", changeNotifications.AsEnumerable()));

            IMongoUpdate update = Update.Combine(updateValues);

            _currentAirings.Update(query, update);
        }
        public void TestAddToSetEachWrapped()
        {
            var update   = Update.AddToSetEachWrapped("name", _a, _b, null);
            var expected = "{ \"$addToSet\" : { \"name\" : { \"$each\" : [{ \"X\" : 1 }, { \"X\" : 2 }, null] } } }";

            Assert.Equal(expected, update.ToJson());
        }
Exemplo n.º 3
0
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            SecUtility.CheckArrayParameter(ref usernames, true, true, InvalidUsernameCharacters, MAX_USERNAME_LENGTH, USER_NAMES);
            SecUtility.CheckArrayParameter(ref roleNames, true, true, InvalidRoleCharacters, MAX_ROLE_LENGTH, ROLE_NAMES);

            // ensure lowercase
            var roles = roleNames.Select(role => role.ToLowerInvariant()).ToList();

            var users = usernames.Select(username => username.ToLowerInvariant()).ToList();


            // first add any non-existant roles to roles collection

            // a) pull all roles, filter out existing, push new
            //    ...or
            // b) save all passed in roles
            foreach (var role in roles)
            {
                CreateRole(role);
            }


            // now update all users' roles
            var query = Query.In(ElementNames.LowercaseUsername, new BsonArray(users.ToArray()));

            var update = Update.AddToSetEachWrapped <string>(ElementNames.Roles, roles);

            var result = UserCollection.Update(query, update, UpdateFlags.Multi, SafeMode.True);

            if (result.HasLastErrorMessage)
            {
                throw new ProviderException(result.LastErrorMessage);
            }
        }
Exemplo n.º 4
0
        public void TestAddToSetEach()
        {
            var server     = Configuration.TestServer;
            var database   = Configuration.TestDatabase;
            var collection = Configuration.GetTestCollection <C>();

            collection.RemoveAll();
            var c = new C {
                InnerObjects = new List <D>()
            };

            collection.Insert(c);
            var id = c.Id;

            var query  = Query.EQ("_id", id);
            var update = Update.AddToSet("InnerObjects", 1);

            collection.Update(query, update);
            var d1 = new D {
                X = 1
            };

            update = Update.AddToSetWrapped("InnerObjects", d1);
            collection.Update(query, update);

            var d2 = new D {
                X = 2
            };
            var d3 = new D {
                X = 3
            };

            update = Update.AddToSetEachWrapped("InnerObjects", d1, d2, d3);
            collection.Update(query, update);

            var    document = collection.FindOneAs <BsonDocument>();
            var    json     = document.ToJson();
            string expected;

            if (server.BuildInfo.Version >= new Version(2, 6, 0))
            {
                expected = "{ '_id' : ObjectId('#ID'), 'InnerObjects' : [1, { 'X' : 1 }, { 'X' : 2 }, { 'X' : 3 }] }";
            }
            else
            {
                // prior to version 2.5.2 the server would reorder the elements when Update was used
                expected = "{ 'InnerObjects' : [1, { 'X' : 1 }, { 'X' : 2 }, { 'X' : 3 }], '_id' : ObjectId('#ID') }";
            }
            expected = expected.Replace("#ID", id.ToString());
            expected = expected.Replace("'", "\"");
            Assert.AreEqual(expected, json);
        }
Exemplo n.º 5
0
        private bool UpdateTagsInPost(IMongoRepository <Post> repo, Post post)
        {
            var tags = ComputeTagsFromBody(post.Body);

            // COMPARE AND SWAP (CAS)
            // - Compare the body text to make sure its not stale
            // - Swap in the freshly computed tags
            return(repo.Update(Query.And(
                                   Query.EQ("_id", post.Id),
                                   Query.EQ(Post.FN_BODY, post.Body)
                                   ),
                               Update.AddToSetEachWrapped(Post.FN_TAGS, tags),
                               UpdateFlags.None));
        }
        public void Save(string airingId, IEnumerable <ChangeNotification> changeNotifications)
        {
            IMongoQuery query = Query.EQ("AssetId", airingId);

            List <UpdateBuilder> updateValues = new List <UpdateBuilder>();

            updateValues.Add(Update.PullAllWrapped("DeliveredTo", changeNotifications.Select(e => e.QueueName)));
            updateValues.Add(Update.PullAllWrapped("IgnoredQueues", changeNotifications.Select(e => e.QueueName)));
            updateValues.Add(Update.AddToSetEachWrapped("ChangeNotifications", changeNotifications));


            IMongoUpdate update = Update.Combine(updateValues);

            _collection.Update(query, update);
        }
Exemplo n.º 7
0
        public void TestAddToSetEach()
        {
            var server     = Configuration.TestServer;
            var database   = Configuration.TestDatabase;
            var collection = Configuration.GetTestCollection <C>();

            collection.RemoveAll();
            var c = new C {
                InnerObjects = new List <D>()
            };

            collection.Insert(c);
            var id = c.Id;

            var query  = Query.EQ("_id", id);
            var update = Update.AddToSet("InnerObjects", 1);

            collection.Update(query, update);
            var d1 = new D {
                X = 1
            };

            update = Update.AddToSetWrapped("InnerObjects", d1);
            collection.Update(query, update);

            var d2 = new D {
                X = 2
            };
            var d3 = new D {
                X = 3
            };

            update = Update.AddToSetEachWrapped("InnerObjects", d1, d2, d3);
            collection.Update(query, update);

            var document = collection.FindOneAs <BsonDocument>();
            var json     = document.ToJson();
            var expected = "{ 'InnerObjects' : [1, { 'X' : 1 }, { 'X' : 2 }, { 'X' : 3 }], '_id' : ObjectId('#ID') }"; // server put _id at end?

            expected = expected.Replace("#ID", id.ToString());
            expected = expected.Replace("'", "\"");
            Assert.AreEqual(expected, json);
        }
Exemplo n.º 8
0
        public void TestAddToSetEach()
        {
            var server     = MongoServer.Create("mongodb://localhost/?safe=true");
            var database   = server["onlinetests"];
            var collection = database.GetCollection <C>("csharp111");

            collection.RemoveAll();
            var c = new C {
                InnerObjects = new List <D>()
            };

            collection.Insert(c);
            var id = c.Id;

            var query  = Query.EQ("_id", id);
            var update = Update.AddToSet("InnerObjects", 1);

            collection.Update(query, update);
            var d1 = new D {
                X = 1
            };

            update = Update.AddToSetWrapped("InnerObjects", d1);
            collection.Update(query, update);

            var d2 = new D {
                X = 2
            };
            var d3 = new D {
                X = 3
            };

            update = Update.AddToSetEachWrapped("InnerObjects", d1, d2, d3);
            collection.Update(query, update);

            var document = collection.FindOneAs <BsonDocument>();
            var json     = document.ToJson();
            var expected = "{ 'InnerObjects' : [1, { 'X' : 1 }, { 'X' : 2 }, { 'X' : 3 }], '_id' : { '$oid' : '#ID' } }"; // server put _id at end?

            expected = expected.Replace("#ID", id.ToString());
            expected = expected.Replace("'", "\"");
            Assert.AreEqual(expected, json);
        }
Exemplo n.º 9
0
        private void ResetQueueInMongo(string queueName, IMongoQuery filter, string changeNotificationType, bool resetDeletedAirings = true)
        {
            List <UpdateBuilder> updateAiringProperties = new List <UpdateBuilder>();

            updateAiringProperties.Add(Update.PullAllWrapped("DeliveredTo", queueName));
            updateAiringProperties.Add(Update.PullAllWrapped("IgnoredQueues", queueName));
            updateAiringProperties.Add(Update.AddToSetEachWrapped("ChangeNotifications",
                                                                  new ChangeNotification {
                QueueName = queueName,
                ChangeNotificationType = changeNotificationType,
                ChangedDateTime        = DateTime.UtcNow
            }));

            IMongoUpdate update = Update.Combine(updateAiringProperties);

            _currentAirings.Update(filter, update, UpdateFlags.Multi);
            if (resetDeletedAirings)
            {
                _deleteAirings.Update(filter, Update.Pull("DeliveredTo", queueName), UpdateFlags.Multi);
            }
        }
 public void TestAddToSetEachWrappedNullValues()
 {
     Assert.Throws <ArgumentNullException>(() => Update.AddToSetEachWrapped <C>("name", null));
 }
 public void TestAddToSetEachWrappedNullName()
 {
     Assert.Throws <ArgumentNullException>(() => Update.AddToSetEachWrapped(null, _a));
 }