Exemplo n.º 1
0
        public override void Update()
        {
            // 1. Project

            MongoCollection <BsonDocument> projectCollection = Database.GetCollection("Project");

            // Drop unused index
            if (projectCollection.IndexExistsByName("Created_1"))
            {
                projectCollection.DropIndexByName("Created_1");
            }

            // Add indices
            projectCollection.CreateIndex(new IndexKeysBuilder().Ascending("Name"), new IndexOptionsBuilder().SetSparse(true));
            projectCollection.CreateIndex(new IndexKeysBuilder().Ascending("Created", "ProductId"), new IndexOptionsBuilder().SetSparse(true));
            projectCollection.CreateIndex(new IndexKeysBuilder().Ascending("Created", "Name"), new IndexOptionsBuilder().SetSparse(true));


            // 2. User

            MongoCollection <BsonDocument> userCollection = Database.GetCollection("User");

            // Drop unused index
            if (userCollection.IndexExistsByName("Roles_1"))
            {
                userCollection.DropIndexByName("Roles_1");
            }

            // Add indices
            userCollection.CreateIndex(new IndexKeysBuilder().Ascending("Roles", "Created"), new IndexOptionsBuilder().SetSparse(true));
            userCollection.CreateIndex(new IndexKeysBuilder().Ascending("Roles", "Name"), new IndexOptionsBuilder().SetSparse(true));
            userCollection.CreateIndex(new IndexKeysBuilder().Ascending("Roles", "ProductId"), new IndexOptionsBuilder().SetSparse(true));
        }
Exemplo n.º 2
0
 public void CreateIndex(IMongoIndexKeys keys, IMongoIndexOptions options = null)
 {
     if (options != null)
     {
         //there is the possibility that create index trow if an index with same name and
         //different options/keys is created
         try
         {
             _collection.CreateIndex(keys, options);
         }
         catch (MongoWriteConcernException ex)
         {
             //probably index extist with different options, lets check if name is specified
             var    optionsDoc = options.ToBsonDocument();
             String indexName;
             if (optionsDoc.Names.Contains("name"))
             {
                 indexName = optionsDoc["name"].AsString;
             }
             else
             {
                 //determine the index name from fields, fragile but works with this version of drivers
                 indexName = GetIndexName(keys);
             }
             _collection.DropIndexByName(indexName);
             _collection.CreateIndex(keys, options);
         }
     }
     else
     {
         _collection.CreateIndex(keys);
     }
 }
Exemplo n.º 3
0
        public override void UpdateDocument(MongoCollection<BsonDocument> collection, BsonDocument document) {
            if (collection.IndexExistsByName("Username_1"))
                collection.DropIndexByName("Username_1");

            if (document.Contains("Username"))
                document.Remove("Username");

            collection.Save(document);
        }
        public override void Update()
        {
            MongoCollection <BsonDocument> statLoginCollection = Database.GetCollection("StatUserLoginV2");

            // Drop unused index
            if (statLoginCollection.IndexExistsByName("Tick_1_EventId_1"))
            {
                statLoginCollection.DropIndexByName("Tick_1_EventId_1");
            }
        }
        public override void Update()
        {
            // Indices for Project colelction
            MongoCollection <BsonDocument> project = Database.GetCollection("Project");

            project.CreateIndex(new IndexKeysBuilder().Ascending("UserId"), new IndexOptionsBuilder().SetSparse(true));
            project.CreateIndex(new IndexKeysBuilder().Ascending("Created"), new IndexOptionsBuilder().SetSparse(true));
            project.CreateIndex(new IndexKeysBuilder().Ascending("VideoSource"), new IndexOptionsBuilder().SetSparse(true));
            project.CreateIndex(new IndexKeysBuilder().Ascending("NameSort"), new IndexOptionsBuilder().SetSparse(true));
            project.CreateIndex(new IndexKeysBuilder().Ascending("ProductId", "Created"), new IndexOptionsBuilder().SetSparse(true));
            project.CreateIndex(new IndexKeysBuilder().Ascending("ProductId", "Name"), new IndexOptionsBuilder().SetSparse(true));

            MongoCollection <BsonDocument> processedScreenshot = Database.GetCollection("ProcessedScreenshot");

            if (processedScreenshot.IndexExistsByName("VideoFileHash_1_TimeOffset_1"))
            {
                processedScreenshot.DropIndexByName("VideoFileHash_1_TimeOffset_1");
            }
            processedScreenshot.CreateIndex(new IndexKeysBuilder().Ascending("SourceFileId"), new IndexOptionsBuilder().SetSparse(true));

            MongoCollection <BsonDocument> processedVideo = Database.GetCollection("ProcessedVideo");

            if (processedVideo.IndexExistsByName("OriginalVideoFileHash_1_OutputFormat_1"))
            {
                processedVideo.DropIndexByName("OriginalVideoFileHash_1_OutputFormat_1");
            }
            processedVideo.CreateIndex(new IndexKeysBuilder().Ascending("SourceFileId"), new IndexOptionsBuilder().SetSparse(true));

            var videoQueueCollection = Database.GetCollection("VideoQueue");

            if (videoQueueCollection.IndexExistsByName("VideoFileHash_1"))
            {
                videoQueueCollection.DropIndexByName("VideoFileHash_1");
            }

            MongoCollection <BsonDocument> userProfile = Database.GetCollection("UserProfile");

            if (userProfile.IndexExistsByName("AppName_1_UserId_1"))
            {
                userProfile.DropIndexByName("AppName_1_UserId_1");
            }

            userProfile.CreateIndex(new IndexKeysBuilder().Ascending("UserId", "AppName"), new IndexOptionsBuilder().SetSparse(true).SetUnique(true));
            userProfile.CreateIndex(new IndexKeysBuilder().Ascending("Created"), new IndexOptionsBuilder().SetSparse(true));
            userProfile.CreateIndex(new IndexKeysBuilder().Ascending("UserNameSort"), new IndexOptionsBuilder().SetSparse(true));

            MongoCollection <BsonDocument> userMembership = Database.GetCollection("UserMembership");

            userMembership.CreateIndex(new IndexKeysBuilder().Ascending("UserIdentifier"), new IndexOptionsBuilder().SetSparse(true));
            userMembership.CreateIndex(new IndexKeysBuilder().Ascending("UserId"), new IndexOptionsBuilder().SetSparse(true));

            Database.GetCollection("HitsCountV2").CreateIndex(new IndexKeysBuilder().Ascending("ProjectId"), new IndexOptionsBuilder().SetSparse(true));
        }
Exemplo n.º 6
0
 /// <summary>
 ///     删除索引[KEY_ID]以外
 /// </summary>
 /// <param name="indexName"></param>
 /// <param name="mongoCol"></param>
 /// <returns></returns>
 public static bool DropMongoIndex(string indexName, MongoCollection mongoCol)
 {
     if (indexName == ConstMgr.KeyId)
     {
         return(false);
     }
     if (mongoCol.IndexExistsByName(indexName))
     {
         mongoCol.DropIndexByName(indexName);
     }
     return(true);
 }
Exemplo n.º 7
0
 public void Drop(MongoCollection collection)
 {
     if (Exists(collection))
     {
         log.Info("Dropping index {0} from collection {1}",name,collection.Name);
         collection.DropIndexByName(name);
     }
     else
     {
         log.Info("Index {0} does not exist on collection {1}, dropping skipped",name,collection.Name);
     }
 }
        /// <summary>
        /// 删除索引[KEY_ID]以外
        /// </summary>
        /// <param name="indexName"></param>
        /// <returns></returns>
        public static Boolean DropMongoIndex(String indexName)
        {
            if (indexName == KEY_ID)
            {
                return(false);
            }
            MongoCollection mongoCol = SystemManager.GetCurrentCollection();

            if (mongoCol.IndexExistsByName(indexName))
            {
                mongoCol.DropIndexByName(indexName);
            }
            return(true);
        }
Exemplo n.º 9
0
        public override void UpdateDocument(MongoCollection <BsonDocument> collection, BsonDocument document)
        {
            if (collection.IndexExistsByName("Username_1"))
            {
                collection.DropIndexByName("Username_1");
            }

            if (document.Contains("Username"))
            {
                document.Remove("Username");
            }

            collection.Save(document);
        }
Exemplo n.º 10
0
        public static Task <CommandResult> DropIndexByNameAsync(this MongoCollection collection, String indexName)
        {
            var tcs = new TaskCompletionSource <CommandResult>();

            ThreadPool.QueueUserWorkItem(_ =>
            {
                try
                {
                    var result = collection.DropIndexByName(indexName);
                    tcs.SetResult(result);
                }
                catch (Exception exc)
                {
                    tcs.SetException(exc);
                }
            });
            return(tcs.Task);
        }
Exemplo n.º 11
0
        /// <summary>
        ///     设定数据缓存时间(以创建时间为基础)
        /// </summary>
        /// <param name="collectionName"></param>
        /// <param name="ExpiresMinute"></param>
        /// <param name="database"></param>
        public static void SetCacheTime(string collectionName, int ExpiresMinute, string database = "")
        {
            if (string.IsNullOrEmpty(database))
            {
                database = _defaultDatabaseName;
            }
            MongoCollection col = GetDatabaseByType(database).GetCollection(collectionName);

            if (col.IndexExistsByName("Cache"))
            {
                col.DropIndexByName("Cache");
            }
            var option = new IndexOptionsBuilder();

            option.SetTimeToLive(new TimeSpan(0, ExpiresMinute, 0));
            option.SetName("Cache");
            var indexkeys = new IndexKeysBuilder();

            indexkeys.Ascending(new string[] { nameof(EntityBase.CreateDateTime) });
            col.CreateIndex(indexkeys, option);
        }
Exemplo n.º 12
0
 /// <summary>
 /// Drops an index on this collection.
 /// </summary>
 /// <param name="indexName">The name of the index.</param>
 /// <returns>A <see cref="CommandResult"/>.</returns>
 public virtual CommandResult DropIndexByName(string indexName)
 {
     return(_collection.DropIndexByName(indexName));
 }
Exemplo n.º 13
0
 /// <summary>
 ///     删除索引[KEY_ID]以外
 /// </summary>
 /// <param name="indexName"></param>
 /// <param name="mongoCol"></param>
 /// <returns></returns>
 public static bool DropMongoIndex(string indexName, MongoCollection mongoCol)
 {
     if (indexName == ConstMgr.KeyId)
     {
         return false;
     }
     if (mongoCol.IndexExistsByName(indexName))
     {
         mongoCol.DropIndexByName(indexName);
     }
     return true;
 }
Exemplo n.º 14
0
        /// <summary>
        /// 创建索引
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="v"></param>
        private void CreateIndex(MongoCollection collection, Variant v)
        {
            var indexs = collection.GetIndexes();
            HashSet<string> hs = new HashSet<string>();
            foreach (IndexInfo index in indexs)
            {
                hs.Add(index.Name);
            }
            foreach (var item in v)
            {
                string indexName = item.Key;
                Variant keys = item.Value as Variant;
                if (keys != null)
                {
                    IndexOptionsBuilder indexOpt = new IndexOptionsBuilder();
                    indexOpt.SetName(indexName);
                    if (keys.ContainsKey("dropDups"))
                    {
                        if (keys["dropDups"] is bool)
                        {
                            indexOpt.SetDropDups((bool)keys["dropDups"]);
                        }
                        keys.Remove("dropDups");
                    }

                    if (keys.ContainsKey("background"))
                    {
                        if (keys["background"] is bool)
                        {
                            indexOpt.SetBackground((bool)keys["background"]);
                        }
                        keys.Remove("background");
                    }

                    if (keys.ContainsKey("unique"))
                    {
                        if (keys["unique"] is bool)
                        {
                            indexOpt.SetUnique((bool)keys["unique"]);
                        }
                        keys.Remove("unique");
                    }

                    if (keys.ContainsKey("sparse"))
                    {
                        if (keys["sparse"] is bool)
                        {
                            indexOpt.SetSparse((bool)keys["sparse"]);
                        }
                        keys.Remove("sparse");
                    }

                    bool find = false;
                    IndexKeysBuilder indexKey = new IndexKeysBuilder();
                    foreach (var dsa in keys)
                    {
                        string ckey = dsa.Key;
                        if (ckey != string.Empty && dsa.Value is int)
                        {
                            int dec = (int)dsa.Value;
                            if (dec == 1)
                            {
                                indexKey.Ascending(ckey);
                                find = true;
                            }
                            else if (dec == -1)
                            {
                                indexKey.Descending(ckey);
                                find = true;
                            }
                        }
                    }
                    if (find)
                    {
                        if (!hs.Contains(indexName))
                            collection.CreateIndex(indexKey, indexOpt);
                    }
                    else
                    {
                        if (hs.Contains(indexName))
                            collection.DropIndexByName(indexName);
                    }
                }
            }
        }