void EnsureIndexHasBeenCreated(IEnumerable <string> sagaDataPropertyPathsToIndex, MongoCollection <BsonDocument> collection) { if (!indexCreated) { lock (this) { if (!indexCreated) { collection.ResetIndexCache(); // var indexes = collection.GetIndexes(); foreach (var propertyToIndex in sagaDataPropertyPathsToIndex.Except(new[] { "Id" })) { var indexDefinition = IndexKeys.Ascending(propertyToIndex); //if (IndexAlreadyExists(indexes, propertyToIndex)) //{ // AssertIndexIsCorrect(indexes, propertyToIndex); //} collection.EnsureIndex(indexDefinition, IndexOptions.SetBackground(false).SetUnique(true)); } //collection.ReIndex(); indexCreated = true; } } } }
protected override void init_table() { try { var table1 = GetDB.GetCollection("RegisterLog"); if (table1 != null) { table1.CreateIndex(IndexKeys.Ascending(StatUnitRemain.ACC_KEY), IndexOptions.SetBackground(true)); table1.CreateIndex(IndexKeys.Ascending("channel"), IndexOptions.SetBackground(true)); table1.CreateIndex(IndexKeys.Ascending("time"), IndexOptions.SetBackground(true)); } var table2 = GetDB.GetCollection("link_phone"); if (table2 != null) { table2.CreateIndex(IndexKeys.Ascending("channel"), IndexOptions.SetBackground(true)); table2.CreateIndex(IndexKeys.Ascending("active_time"), IndexOptions.SetBackground(true)); } var table3 = GetDB.GetCollection(TableName.PLAYER_LOGIN); if (table3 != null) { table3.CreateIndex(IndexKeys.Ascending("channel"), IndexOptions.SetBackground(true)); table3.CreateIndex(IndexKeys.Ascending("time"), IndexOptions.SetBackground(true)); table3.CreateIndex(IndexKeys.Ascending(StatUnitRemain.ACC_KEY), IndexOptions.SetBackground(true)); table3.CreateIndex(IndexKeys.Ascending("acc_dev"), IndexOptions.SetBackground(true)); } } catch (System.Exception ex) { } }
protected override void InitializeCollection(MongoDatabase database) { base.InitializeCollection(database); _collection.CreateIndex(IndexKeys.Ascending(FieldNames.ProjectId), IndexOptions.SetBackground(true)); _collection.CreateIndex(IndexKeys.Ascending(FieldNames.Url), IndexOptions.SetBackground(true)); }
public void TestBackground() { var options = IndexOptions.SetBackground(true); string expected = "{ \"background\" : true }"; Assert.Equal(expected, options.ToJson()); }
void EnsureIndexHasBeenCreated(IEnumerable <string> sagaDataPropertyPathsToIndex, MongoCollection <BsonDocument> collection) { if (!indexEnsuredRecently) { lock (indexEnsuredRecentlyLock) { if (!indexEnsuredRecently) { log.Info("Re-declaring indexes with unique constraints for the following paths: {0}", string.Join(", ", sagaDataPropertyPathsToIndex)); collection.ResetIndexCache(); // var indexes = collection.GetIndexes(); foreach (var propertyToIndex in sagaDataPropertyPathsToIndex.Except(new[] { "Id" })) { var indexDefinition = IndexKeys.Ascending(propertyToIndex); //if (IndexAlreadyExists(indexes, propertyToIndex)) //{ // AssertIndexIsCorrect(indexes, propertyToIndex); //} collection.EnsureIndex(indexDefinition, IndexOptions.SetBackground(false).SetUnique(true)); } //collection.ReIndex(); indexEnsuredRecently = true; } } } }
/// <summary> /// Constructs the timeout storage, connecting to the Mongo database pointed to by the given connection string, /// storing the timeouts in the given collection /// </summary> public MongoDbTimeoutStorage(string connectionString, string collectionName) { var database = MongoHelper.GetDatabase(connectionString); collection = database.GetCollection(collectionName); collection.CreateIndex(IndexKeys.Ascending(TimeProperty), IndexOptions.SetBackground(true).SetUnique(false)); }
protected override void InitializeCollection(MongoDatabase database) { base.InitializeCollection(database); _collection.CreateIndex(IndexKeys.Ascending(FieldNames.Invites_Token), IndexOptions.SetBackground(true)); _collection.CreateIndex(IndexKeys.Ascending(FieldNames.Invites_EmailAddress), IndexOptions.SetBackground(true)); _collection.CreateIndex(IndexKeys.Ascending(FieldNames.StripeCustomerId), IndexOptions.SetUnique(true).SetSparse(true).SetBackground(true)); }
protected override void InitializeCollection(MongoDatabase database) { base.InitializeCollection(database); _collection.CreateIndex(IndexKeys <User> .Ascending(u => u.OrganizationIds), IndexOptions.SetBackground(true)); _collection.CreateIndex(IndexKeys <User> .Ascending(u => u.EmailAddress), IndexOptions.SetUnique(true).SetBackground(true)); _collection.CreateIndex(IndexKeys.Ascending(FieldNames.OAuthAccounts_Provider, FieldNames.OAuthAccounts_ProviderUserId), IndexOptions.SetUnique(true).SetSparse(true).SetBackground(true)); _collection.CreateIndex(IndexKeys <User> .Ascending(u => u.Roles), IndexOptions.SetBackground(true)); }
static void CreateIndex(MongoCollection col, string field) { if (field == "_id") //create proper index first! { return; } col.EnsureIndex(IndexKeys.Ascending(field), IndexOptions.SetBackground(true)); }
protected override void InitializeCollection(MongoDatabase database) { base.InitializeCollection(database); _collection.CreateIndex(IndexKeys.Ascending(FieldNames.ProjectId), IndexOptions.SetBackground(true)); _collection.CreateIndex(IndexKeys.Ascending(FieldNames.StackId), IndexOptions.SetBackground(true)); _collection.CreateIndex(IndexKeys.Ascending(FieldNames.OrganizationId, FieldNames.Date_UTC), IndexOptions.SetBackground(true)); _collection.CreateIndex(IndexKeys.Descending(FieldNames.ProjectId, FieldNames.Date_UTC, FieldNames.IsFixed, FieldNames.IsHidden, FieldNames.Type), IndexOptions.SetBackground(true)); _collection.CreateIndex(IndexKeys.Descending(FieldNames.StackId, FieldNames.Date_UTC), IndexOptions.SetBackground(true)); }
protected override void init_table() { try { var table = GetDB.GetCollection(TableName.PUMP_PLAYER_MONEY); table.CreateIndex(IndexKeys.Ascending("genTime"), IndexOptions.SetBackground(true)); } catch (System.Exception ex) { } }
protected override void init_table() { try { var table = GetDB.GetCollection("PayLog"); table.CreateIndex(IndexKeys.Ascending(StatLTV.ACC_KEY_1), IndexOptions.SetBackground(true)); table.CreateIndex(IndexKeys.Ascending("channel"), IndexOptions.SetBackground(true)); table.CreateIndex(IndexKeys.Ascending("time"), IndexOptions.SetBackground(true)); } catch (System.Exception ex) { } }
public override void createIndex(MongodbHelper db, string url) { MongoDatabase mdb = db.MongoDb; if (mdb == null) { return; } var table = mdb.GetCollection(TableName.OPLOG); table.CreateIndex(IndexKeys.Ascending("OpType"), IndexOptions.SetBackground(true)); }
/// <summary> /// 创建索引 /// </summary> public void CreateIndex() { LogService.Write("创建索引..."); try { //创建索引配置库索引 MongoDatabase dbWcf = MongoDBConn.GetDatabase("WcfLogServer");//数据库名字 MongoCollection <BsonDocument> tableInfoSetting = dbWcf["InfoSetting"]; var indexesWcf = tableInfoSetting.GetIndexes().Select(a => a["name"].ToString()).ToList(); if (!indexesWcf.Any(index => index.Contains("CollectionName"))) { tableInfoSetting.CreateIndex(IndexKeys.Descending("CollectionName"), IndexOptions.SetBackground(true).SetUnique(false)); LogService.Write("创建库:WcfLogServer 表:InfoSetting 列:CollectionName的索引"); } //创建数据库索引 string dbName = "WcfLog" + GetCurrentDate(); string[] orderby = { "CreateTime" }; List <InfoSetting> list = MongoDBUtil.GetAll <InfoSetting>("WcfLogServer", "InfoSetting", new BsonDocument(), orderby, new BsonDocument()); if (list != null && list.Count > 0) { MongoDatabase db = MongoDBConn.GetDatabase(dbName);//数据库名字 foreach (InfoSetting info in list) { if (!string.IsNullOrEmpty(info.IndexName)) { MongoCollection <BsonDocument> table = db[info.CollectionName]; var indexes = table.GetIndexes().Select(a => a["name"].ToString()).ToList(); List <string> listIndexes = info.IndexName.Split(',').ToList(); if (listIndexes != null && listIndexes.Count > 0) { foreach (string item in listIndexes) { if (!indexes.Any(index => index.Contains(item))) { table.CreateIndex(IndexKeys.Ascending(item), IndexOptions.SetBackground(true).SetUnique(false)); LogService.Write("创建库:" + dbName + " 表:" + info.CollectionName + " 列:" + item + "的索引"); } } } } } } } catch (Exception ex) { LogService.Write(ex); } }
public override void createIndex(MongodbHelper db, string url) { MongoDatabase mdb = db.MongoDb; if (mdb == null) { return; } if (url == WebConfigurationManager.AppSettings["account"]) { return; } var table = mdb.GetCollection(TableName.PLAYER_INFO); table.CreateIndex(IndexKeys.Ascending("gold"), IndexOptions.SetBackground(true)); table.CreateIndex(IndexKeys.Ascending("ticket"), IndexOptions.SetBackground(true)); }
public override void createIndex(MongodbHelper db, string url) { MongoDatabase mdb = db.MongoDb; if (mdb == null) { return; } try { var table = mdb.GetCollection(TableName.PUMP_PLAYER_MONEY); table.CreateIndex(IndexKeys.Ascending("playerId"), IndexOptions.SetBackground(true)); table.CreateIndex(IndexKeys.Ascending("gameId"), IndexOptions.SetBackground(true)); table.CreateIndex(IndexKeys.Ascending("itemId"), IndexOptions.SetBackground(true)); table.CreateIndex(IndexKeys.Ascending("reason"), IndexOptions.SetBackground(true)); } catch (System.Exception ex) { } }
/// <summary> /// Installs the specified container. /// </summary> /// <param name="container">The container.</param> /// <param name="store">The store.</param> public void Install(IWindsorContainer container, IConfigurationStore store) { container.Register( Component.For(typeof(IMonitoringRepository <>)) .ImplementedBy(typeof(MonitoringRepository <>)) .LifestyleScoped <MessageScopeAccesor>() .DependsOn(Dependency.OnValue("connectionName", "MonitoringStatistics")) ); container.Register( Component.For <IServiceInfoRepository>() .ImplementedBy <ServiceInfoRepository>() .LifestyleScoped <MessageScopeAccesor>() .Named("BusInfoRepository") .DependsOn(Dependency.OnValue("connectionName", "MonitoringStatistics")) .DependsOn(Dependency.OnValue("collection", "BusInfo")) ); container.Register( Component.For <IServiceInfoRepository>() .ImplementedBy <ServiceInfoRepository>() .LifestyleScoped <MessageScopeAccesor>() .Named("ServiceInfoRepository") .DependsOn(Dependency.OnValue("connectionName", "MonitoringStatistics")) .DependsOn(Dependency.OnValue("collection", "ServiceInfo")) ); container.Register(Component.For <ITranslator>() .ImplementedBy <Translator>().LifeStyle.Singleton.Named("Translator")); container.Register( Component.For <IServiceInfoService>() .ImplementedBy <ServiceInfoService>() .LifestyleScoped <MessageScopeAccesor>() .Named("ServiceInfoService") .DynamicParameters((k, d) => { d["busRepository"] = k.Resolve <IServiceInfoRepository>("BusInfoRepository"); d["serviceRepository"] = k.Resolve <IServiceInfoRepository>("ServiceInfoRepository"); }) ); foreach (var collection in _collectionList) { MongoHelperProvider.Instance.GetHelper("MonitoringStatistics").Repository.GetCollection(collection) .EnsureIndex(new IndexKeysBuilder().Descending("UtcTimeTakenSample"), IndexOptions.SetBackground(true).SetSparse(true).SetTimeToLive(TimeSpan.FromDays(7))); MongoHelperProvider.Instance.GetHelper("MonitoringStatistics").Repository.GetCollection(collection) .EnsureIndex(new IndexKeysBuilder().Ascending("Identification._id").Descending("UtcTimeTakenSample"), IndexOptions.SetBackground(true).SetSparse(true)); } MongoHelperProvider.Instance.GetHelper("MonitoringStatistics").Repository.GetCollection("ErrorHandlerEntity") .EnsureIndex(new IndexKeysBuilder().Descending("SolvedAt"), IndexOptions.SetBackground(true).SetSparse(true).SetTimeToLive(TimeSpan.FromDays(3))); MongoHelperProvider.Instance.GetHelper("MonitoringStatistics").Repository.GetCollection("ErrorHandlerEntity") .EnsureIndex(new IndexKeysBuilder().Ascending("Status"), IndexOptions.SetBackground(true).SetSparse(true)); MongoHelperProvider.Instance.GetHelper("MonitoringStatistics").Repository.GetCollection("ErrorHandlerEntity") .EnsureIndex(new IndexKeysBuilder().Ascending("ServiceId._id").Descending("UtcSuccessAt"), IndexOptions.SetBackground(true).SetSparse(true)); MongoHelperProvider.Instance.GetHelper("MonitoringStatistics").Repository.GetCollection("ErrorHandlerEntity") .EnsureIndex(new IndexKeysBuilder().Descending("UtcSuccessAt"), IndexOptions.SetBackground(true).SetSparse(true)); }
private static void AddIndex() { Collections.ProductViewCollection.EnsureIndex(IndexKeys <MongoProductView> .Ascending(item => item.Counter), IndexOptions.SetBackground(true)); Console.WriteLine(""); }
public ActionResult Index() { var profiler = MiniProfiler.Current; using (profiler.Step("Set page title")) { ViewBag.Title = "Home Page"; } using (profiler.Step("Doing complex stuff")) { using (profiler.Step("Step A")) { Thread.Sleep(100); } using (profiler.Step("Step B")) { Thread.Sleep(250); } } // create couple of indexes Repository.FooCollection.CreateIndex(IndexKeys.Ascending("i"), IndexOptions.SetBackground(true)); // update docs just to update docs (meaningless activity) Repository.FooCollection.FindAndModify( new FindAndModifyArgs { Query = Query.EQ("r", 0.12345), SortBy = SortBy.Ascending("i"), Update = Update.Set("updated", true) }); var model = new MongoDemoModel { FooCount = (int)Repository.FooCollection.Count(), FooCountQuery = (int)Repository.FooCollection.Count(Query.LT("r", 0.5)), AggregateResult = string.Join(Environment.NewLine, Repository.FooCollection.Aggregate( new AggregateArgs { OutputMode = AggregateOutputMode.Cursor, Pipeline = new[] { new BsonDocument { { "$match", new BsonDocument { { "r", new BsonDocument { { "$gt", 0.2 } } } } } }, new BsonDocument { { "$group", new BsonDocument { { "_id", new BsonDocument { { "$cond", new BsonArray { new BsonDocument { { "$lt", new BsonArray { "$r", 0.6 } } }, "lessthen0.6", "morethan0.6" } } } }, { "count", new BsonDocument { { "$sum", 1 } } } } } } } }).ToList()), ExplainResult = Repository.FooCollection.FindAs <BsonDocument>(Query.GT("r", 0.5)) .SetLimit(10) .SetSortOrder(SortBy.Descending("i")) .Explain() .ToString(), FiveDocs = string.Join("\n", Repository.FooCollection.FindAs <BsonDocument>(Query.GTE("r", 0.8)) .SetLimit(5) .SetSortOrder(SortBy.Descending("r")) .ToList()) }; // drop all indexes Repository.FooCollection.DropAllIndexes(); // add couple of records // single record Repository.BarCollection.Insert(new BsonDocument { { "timestamp", DateTime.Now } }); // 2 records at once Repository.BarCollection.InsertBatch(new[] { new BsonDocument { { "timestamp", DateTime.Now } }, new BsonDocument { { "timestamp", DateTime.Now.AddSeconds(1) } } }); // update couple of records Repository.FooCollection.Update(Query.LT("r", 0.01), Update.Inc("up", 1), UpdateFlags.Multi); // find one record var oneRecord = Repository.FooCollection.FindOneAs <BsonDocument>(Query.GT("r", Random.NextDouble())); oneRecord.Set("meta", "updated"); Repository.FooCollection.Save(oneRecord); // testing typed collections Repository.BazzCollection.Insert(new BazzItem { CurrentTimestamp = DateTime.Now, SomeRandomInt = Random.Next(0, 256), SomeRandomDouble = Random.NextDouble() }); return(View(model)); }
public void EnsureIndex(string fieldName, string collectionName = null) { string collection = collectionName == null ? _collectionName : collectionName; _database.GetCollection(collection).CreateIndex(IndexKeys.Ascending(fieldName), IndexOptions.SetBackground(true)); }
private static void CreateIndexes(MongoCollection <BsonDocument> sourceCollection, MongoCollection <BsonDocument> targetCollection, FlexibleOptions options) { if (options == null) { options = new FlexibleOptions(); } var logger = NLog.LogManager.GetLogger("CreateIndexes"); logger.Debug("{2} - {0}.{1} - Start index creation", sourceCollection.Database.Name, sourceCollection.Name, Thread.CurrentThread.ManagedThreadId); var command = new CommandDocument(); command.Add("createIndexes", targetCollection.Name); var indexList = new BsonArray(); command.Add("indexes", indexList); // Copying Indexes - If Any foreach (IndexInfo idx in sourceCollection.GetIndexes().ToList()) { // Skipping "_id_" default index - Since Every mongodb Collection has it if (idx.Name == "_id_") { continue; } // Recreating Index Options based on the current index options var opts = IndexOptions.SetBackground(idx.IsBackground || options.Get("indexes-background", false)) .SetSparse(idx.IsSparse || options.Get("indexes-sparse", false)) .SetUnique(idx.IsUnique).SetName(idx.Name).SetDropDups(idx.DroppedDups); if (idx.TimeToLive < TimeSpan.MaxValue) { opts.SetTimeToLive(idx.TimeToLive); } // Adding Index try { if (targetCollection.Database.Server.BuildInfo.Version.Major < 2 && targetCollection.Database.Server.BuildInfo.Version.MajorRevision < 6) { logger.Debug("{2} - {0}.{1} - Creating index: {2}", sourceCollection.Database.Name, sourceCollection, idx.Name, Thread.CurrentThread.ManagedThreadId); targetCollection.CreateIndex(idx.Key, opts); } else { logger.Debug("{2} - {0}.{1} - Prepare index creation: {2}", sourceCollection.Database.Name, sourceCollection, idx.Name, Thread.CurrentThread.ManagedThreadId); // removes the namespace to allow mongodb to generate the correct one... var doc = idx.RawDocument; doc.Remove("ns"); if (options.Get("indexes-background", false)) { doc["background"] = true; } if (options.Get("indexes-sparse", false)) { doc["sparse"] = true; } indexList.Add(doc); } } catch (Exception ex) { // check for timeout exception that may occur if the collection is large... if (ex is System.IO.IOException || ex is System.Net.Sockets.SocketException || (ex.InnerException != null && ex.InnerException is System.Net.Sockets.SocketException)) { logger.Warn("{3} - {0}.{1} - Timeout creating index {2}, this may occur in large collections. You should check manually after a while.", sourceCollection.Database.Name, sourceCollection.Name, idx.Name, Thread.CurrentThread.ManagedThreadId); // wait for index creation.... for (var i = 0; i < 30; i++) { System.Threading.Thread.Sleep(10000); try { if (targetCollection.IndexExists(idx.Name)) { break; } } catch { } } } else { logger.Error(ex, "{0}.{1} - Error creating index {2}" + idx.Name); } logger.Warn("{3} - {0}.{1} - Index details: {2}", sourceCollection.Database.Name, sourceCollection.Name, idx.RawDocument.ToJson(), Thread.CurrentThread.ManagedThreadId); } } if (indexList.Count > 0) { try { logger.Debug("{3} - {0}.{1} - Creating {2} indexes", sourceCollection.Database.Name, sourceCollection, indexList.Count, Thread.CurrentThread.ManagedThreadId); targetCollection.Database.RunCommand(command); } catch (Exception ex) { // check for timeout exception that may occur if the collection is large... if (ex is System.IO.IOException || ex is System.Net.Sockets.SocketException || (ex.InnerException != null && ex.InnerException is System.Net.Sockets.SocketException)) { logger.Warn("{3} - {0}.{1} - Timeout creating {2} indexes, this may occur in large collections. You should check manually after a while.", sourceCollection.Database.Name, sourceCollection.Name, indexList.Count, Thread.CurrentThread.ManagedThreadId); logger.Warn("{3} - {0}.{1} - Index details: {2}", sourceCollection.Database.Name, sourceCollection.Name, command.ToJson(), Thread.CurrentThread.ManagedThreadId); } else { logger.Error(ex, "{2} - {0}.{1} - Error creating indexes", sourceCollection.Database.Name, sourceCollection.Name, Thread.CurrentThread.ManagedThreadId); logger.Error("{3} - {0}.{1} - Index details: {2}", sourceCollection.Database.Name, sourceCollection.Name, command.ToJson(), Thread.CurrentThread.ManagedThreadId); } } } logger.Debug("{2} - {0}.{1} - Index creation completed", sourceCollection.Database.Name, sourceCollection, Thread.CurrentThread.ManagedThreadId); }
protected override void InitializeCollection(MongoDatabase database) { base.InitializeCollection(database); _collection.CreateIndex(IndexKeys.Ascending(FieldNames.ProjectId, FieldNames.SignatureHash), IndexOptions.SetUnique(true).SetBackground(true)); _collection.CreateIndex(IndexKeys.Descending(FieldNames.ProjectId, FieldNames.LastOccurrence), IndexOptions.SetBackground(true)); _collection.CreateIndex(IndexKeys.Descending(FieldNames.ProjectId, FieldNames.IsHidden, FieldNames.DateFixed, FieldNames.SignatureInfo_Path), IndexOptions.SetBackground(true)); }
private static void Maintainance(object o) { try { var url = o as MongodbServerUrl; LocalLoggingService.Info("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", "服务器", string.Format("对服务器 '{0}' 开始一次维护", url.Name)); Stopwatch sw = Stopwatch.StartNew(); ServerInfo oldServerInfo = null; locker.EnterReadLock(); if (servers.ContainsKey(url)) { oldServerInfo = servers[url]; } locker.ExitReadLock(); var serverInfo = new ServerInfo(); serverInfo.Url = url; serverInfo.Databases = new List <DatabaseInfo>(); var server = MongoServer.Create(url.Master); //元数据 var metaDatabase = server.GetDatabase(MongodbServerConfiguration.MetaDataDbName); var metaCollectionNames = metaDatabase.GetCollectionNames().Where(name => !name.Contains("system.index") && !name.Contains("$")).ToList(); var descriptions = new List <MongodbDatabaseDescription>(); foreach (var metaCollectionName in metaCollectionNames) { try { var metaCollection = metaDatabase.GetCollection(metaCollectionName); descriptions.Add(metaCollection.FindOneAs <MongodbDatabaseDescription>()); } catch (Exception ex) { LocalLoggingService.Error("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", "获取元数据出错", ex.Message); } } serverInfo.Descriptions = descriptions; //获取所有的数据库 var databaseNames = server.GetDatabaseNames().Where(name => { var categories = descriptions.Select(description => description.DatabasePrefix).Distinct(); foreach (var categoryName in categories) { if (name.StartsWith(categoryName)) { return(true); } } return(false); }).ToList(); Parallel.ForEach(databaseNames, databaseName => { var swdb = Stopwatch.StartNew(); if (!databaseName.Contains("__")) { LocalLoggingService.Warning("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", "数据库", string.Format("数据库名 '{0}' 不包含__", databaseName)); return; } var datepart = databaseName.Substring(databaseName.LastIndexOf("__") + 2); if (datepart.Length != 6) { LocalLoggingService.Warning("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", "数据库", string.Format("数据名 '{0}' 日期部分长度不为6", databaseName)); return; } try { if (fristLoaded && oldServerInfo != null && oldServerInfo.Databases.Exists(db => db.DatabaseName == databaseName) && datepart != DateTime.Now.ToString("yyyyMM")) { var oldDb = oldServerInfo.Databases.SingleOrDefault(db => db.DatabaseName == databaseName); if (oldDb != null) { if (!serverInfo.Databases.Contains(oldDb)) { serverInfo.Databases.Add(oldDb); } LocalLoggingService.Debug("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", "数据库", string.Format("对数据库 '{0}' 完成一次维护,直接使用现有数据.oldServerInfo", databaseName)); return; } } } catch (Exception ex) { string reason = ""; if (oldServerInfo.Databases == null) { reason = "oldServerInfo.Databases == null"; } else if (oldServerInfo.Databases.Exists(d => d == null)) { reason = "oldServerInfo.Databases.Exists(d => d == null)"; } else { reason = "other"; } LocalLoggingService.Error("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", string.Format("获取已有数据库 {0} 出错:{1}", databaseName, reason), ex.ToString()); } var database = server.GetDatabase(databaseName); var databaseInfo = new DatabaseInfo(); databaseInfo.DatabaseName = databaseName; try { var databaseStatusResult = database.GetStats();//获取数据库信息 if (databaseStatusResult != null) { var databaseStatus = new DatabaseStatus { AverageObjectSize = databaseStatusResult.AverageObjectSize, CollectionCount = databaseStatusResult.CollectionCount, DataSize = databaseStatusResult.DataSize, FileSize = databaseStatusResult.FileSize, IndexSize = databaseStatusResult.IndexSize, IndexCount = databaseStatusResult.IndexCount, ExtentCount = databaseStatusResult.ExtentCount, ObjectCount = databaseStatusResult.ObjectCount, StorageSize = databaseStatusResult.StorageSize, }; databaseInfo.DatabaseStatus = databaseStatus; } } catch (Exception ex) { LocalLoggingService.Error("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", string.Format("获取数据库 '{0}' 状态出错", databaseName), ex.ToString()); } var prefixPart = databaseName.Substring(0, databaseName.LastIndexOf("__")); databaseInfo.DatabasePrefix = prefixPart; var date = DateTime.MinValue; if (DateTime.TryParse(string.Format("{0}/{1}/{2}", datepart.Substring(0, 4), datepart.Substring(4, 2), "01"), out date)) { databaseInfo.DatabaseDate = date; } else { LocalLoggingService.Warning("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", string.Format("数据库名 '{0}' 日期部分解析错误", databaseName)); return; } var description = descriptions.Where(d => d.DatabasePrefix == prefixPart).FirstOrDefault(); if (description == null) { LocalLoggingService.Warning("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", "数据库", string.Format("没有取到数据描述 '{0}'", prefixPart)); return; } databaseInfo.Collections = new List <CollectionInfo>(); var expireDays = description.ExpireDays; if (expireDays > 0) { if (databaseInfo.DatabaseDate.AddMonths(1).AddDays(-1).AddDays(expireDays) < DateTime.Now && datepart != DateTime.Now.ToString("yyyyMM")) { database.Drop(); LocalLoggingService.Info("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", "数据库", string.Format("清除历史数据-删除库:{0}", databaseName)); return; } } var collectionNames = database.GetCollectionNames().Where(name => !name.Contains("system.index") && !name.Contains("$") && !name.Contains("__")).ToList(); foreach (var collectionName in collectionNames) { var swcoll = Stopwatch.StartNew(); try { var collection = database.GetCollection(collectionName); //if (expireDays > 0) //{ // var statTimeColumn = description.MongodbColumnDescriptionList.FirstOrDefault(c => c.IsTimeColumn); // if (statTimeColumn != null) // { // var query = Query.LT(statTimeColumn.ColumnName, DateTime.Now.AddDays(-expireDays)); // collection.Remove(query); // if (collection.Count() == 0) // { // collection.Drop(); // LocalLoggingService.Info("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter 清除历史数据-无数据删除表", databaseName, collectionName); // continue; // } // } //} var collectionInfo = new CollectionInfo(); collectionInfo.CollectionName = collectionName; collectionInfo.ListFilterColumns = new List <ListFilterColumnInfo>(); collectionInfo.CascadeFilterColumns = new List <CascadeFilterColumnInfo>(); collectionInfo.TextboxFilterColumns = new List <TextboxFilterColumnInfo>(); var collectionStatus = new CollectionStatus { IndexStatusList = new List <IndexStatus>(), }; CollectionStatsResult collectionStatusResult = null; try { if (collection.Count() > 0) { collectionStatusResult = collection.GetStats(); collectionStatus.AverageObjectSize = collectionStatusResult.AverageObjectSize; collectionStatus.DataSize = collectionStatusResult.DataSize; collectionStatus.StorageSize = collectionStatusResult.StorageSize; collectionStatus.LastExtentSize = collectionStatusResult.LastExtentSize; collectionStatus.Namespace = collectionStatusResult.Namespace; collectionStatus.ExtentCount = collectionStatusResult.ExtentCount; collectionStatus.Flags = collectionStatusResult.Flags; collectionStatus.IndexCount = collectionStatusResult.IndexCount; collectionStatus.ObjectCount = collectionStatusResult.ObjectCount; collectionStatus.PaddingFactor = collectionStatusResult.PaddingFactor; collectionStatus.TotalIndexSize = collectionStatusResult.TotalIndexSize; } } catch (Exception ex) { LocalLoggingService.Error("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter 获取表状态出错", string.Format("库名:{0} 表名:{1}", databaseName, collectionName), ex.ToString()); } var indexes = collection.GetIndexes(); collectionStatus.IndexStatusList = indexes.Select(i => new IndexStatus { Name = i.Name, Namespace = i.Namespace, Unique = i.IsUnique, }).ToList(); collectionStatus.IndexStatusList.ForEach(i => { if (collectionStatusResult != null && collectionStatusResult.IndexSizes.ContainsKey(i.Name)) { i.Size = collectionStatusResult.IndexSizes[i.Name]; } }); collectionInfo.CollectionStatus = collectionStatus; var indexColumnDescriptions = description.MongodbColumnDescriptionList.Where(c => c.MongodbIndexOption != MongodbIndexOption.None).ToList(); foreach (var indexColumnDescription in indexColumnDescriptions) { try { switch (indexColumnDescription.MongodbIndexOption) { case MongodbIndexOption.Ascending: { collection.EnsureIndex(IndexKeys.Ascending(indexColumnDescription.ColumnName), IndexOptions.SetBackground(true)); break; } case MongodbIndexOption.Descending: { collection.EnsureIndex(IndexKeys.Descending(indexColumnDescription.ColumnName), IndexOptions.SetBackground(true)); break; } case MongodbIndexOption.AscendingAndUnique: { collection.EnsureIndex(IndexKeys.Ascending(indexColumnDescription.ColumnName), IndexOptions.SetBackground(true).SetUnique(true).SetDropDups(true)); break; } case MongodbIndexOption.DescendingAndUnique: { collection.EnsureIndex(IndexKeys.Descending(indexColumnDescription.ColumnName), IndexOptions.SetBackground(true).SetUnique(true).SetDropDups(true)); break; } } } catch (Exception ex) { LocalLoggingService.Error("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", "创建索引出错", ex.Message); } } collectionInfo.CollectionStatus.LastEnsureIndexTime = DateTime.Now; //加载数据库中的元数据 var metaCollection = database.GetCollection("Metadata__" + collectionInfo.CollectionName); var meta = metaCollection.FindOneAs <CollectionMetadata>(); if (meta == null) { meta = new CollectionMetadata { CollectionName = collectionInfo.CollectionName, ListFilterColumns = new List <ListFilterColumnInfo>(), CascadeFilterColumns = new List <CascadeFilterColumnInfo>(), TextboxFilterColumns = new List <TextboxFilterColumnInfo>(), }; } var textboxColumnDescriptions = description.MongodbColumnDescriptionList.Where(c => c.MongodbFilterOption == MongodbFilterOption.TextBoxFilter).ToList(); foreach (var textboxColumnDescription in textboxColumnDescriptions) { if (!collectionInfo.CollectionStatus.IndexStatusList.Exists(index => index.Name.Contains(textboxColumnDescription.ColumnName))) { continue; } var textboxColumnInfo = new TextboxFilterColumnInfo(); textboxColumnInfo.ColumnName = textboxColumnDescription.ColumnName; collectionInfo.TextboxFilterColumns.Add(textboxColumnInfo); if (meta.TextboxFilterColumns.Count(c => c.ColumnName == textboxColumnInfo.ColumnName) == 0) { meta.TextboxFilterColumns.Add(textboxColumnInfo); } } var filterColumnDescriptions = description.MongodbColumnDescriptionList.Where (c => c.MongodbFilterOption == MongodbFilterOption.DropDownListFilter || c.MongodbFilterOption == MongodbFilterOption.CheckBoxListFilter).ToList(); foreach (var filterColumnDescription in filterColumnDescriptions) { try { if (!collectionInfo.CollectionStatus.IndexStatusList.Exists(index => index.Name.Contains(filterColumnDescription.ColumnName))) { continue; } var filterColumnInfo = new ListFilterColumnInfo(); filterColumnInfo.ColumnName = filterColumnDescription.ColumnName; filterColumnInfo.DistinctValues = new List <ItemPair>(); if (oldServerInfo != null && oldServerInfo.Databases != null) { var oldDatabase = oldServerInfo.Databases.FirstOrDefault(d => d != null && d.DatabaseName == databaseInfo.DatabaseName); if (oldDatabase != null && oldDatabase.Collections != null) { var oldCollection = oldDatabase.Collections.FirstOrDefault(d => d != null && d.CollectionName == collectionInfo.CollectionName); if (oldCollection != null && oldCollection.ListFilterColumns != null) { var oldColumn = oldCollection.ListFilterColumns.FirstOrDefault(d => d != null && d.ColumnName == filterColumnInfo.ColumnName); if (oldColumn != null) { filterColumnInfo.DistinctValues = oldColumn.DistinctValues; } } } } var column = meta.ListFilterColumns.SingleOrDefault(c => c.ColumnName == filterColumnDescription.ColumnName); if (column != null) { foreach (var value in filterColumnInfo.DistinctValues) { if (column.DistinctValues.Count(v => string.Equals(v.Value.ToString(), value.Value.ToString(), StringComparison.InvariantCultureIgnoreCase)) == 0) { LocalLoggingService.Debug("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", "列", string.Format("添加新过滤索引项 {0} 到元数据 {1}.{2}.{3}", value.Value.ToString(), databaseName, collectionName, column.ColumnName)); column.DistinctValues.Add(value); } } filterColumnInfo.DistinctValues = column.DistinctValues; } else { meta.ListFilterColumns.Add(filterColumnInfo); } collectionInfo.ListFilterColumns.Add(filterColumnInfo); } catch (Exception ex) { LocalLoggingService.Error("{0} {1} {2} {3} {4}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", "列", "创建过滤数据出错", ex.Message); } } var cascadeFilterColumnDescriptions = description.MongodbColumnDescriptionList.Where(c => c.MongodbCascadeFilterOption != MongodbCascadeFilterOption.None).ToList(); foreach (var cascadeFilterColumnDescription in cascadeFilterColumnDescriptions) { try { if (!collectionInfo.CollectionStatus.IndexStatusList.Exists(index => index.Name.Contains(cascadeFilterColumnDescription.ColumnName))) { continue; } var filterColumnInfo = new CascadeFilterColumnInfo(); filterColumnInfo.ColumnName = cascadeFilterColumnDescription.ColumnName; filterColumnInfo.DistinctValues = new List <string>(); if (oldServerInfo != null && oldServerInfo.Databases != null) { var oldDatabase = oldServerInfo.Databases.FirstOrDefault(d => d != null && d.DatabaseName == databaseInfo.DatabaseName); if (oldDatabase != null && oldDatabase.Collections != null) { var oldCollection = oldDatabase.Collections.FirstOrDefault(d => d != null && d.CollectionName == collectionInfo.CollectionName); if (oldCollection != null && oldCollection.CascadeFilterColumns != null) { var oldColumn = oldCollection.CascadeFilterColumns.FirstOrDefault(d => d != null && d.ColumnName == filterColumnInfo.ColumnName); if (oldColumn != null) { filterColumnInfo.DistinctValues = oldColumn.DistinctValues; } } } } var column = meta.CascadeFilterColumns.SingleOrDefault(c => c.ColumnName == cascadeFilterColumnDescription.ColumnName); if (column != null) { foreach (var value in filterColumnInfo.DistinctValues) { if (column.DistinctValues.Count(v => string.Equals(v, value, StringComparison.InvariantCultureIgnoreCase)) == 0) { LocalLoggingService.Debug("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", "列", string.Format("添加新级联过滤索引项 {0} 到元数据 {1}.{2}.{3}", value, databaseName, collectionName, column.ColumnName)); column.DistinctValues.Add(value); } } filterColumnInfo.DistinctValues = column.DistinctValues; } else { meta.CascadeFilterColumns.Add(filterColumnInfo); } collectionInfo.CascadeFilterColumns.Add(filterColumnInfo); } catch (Exception ex) { LocalLoggingService.Error("{0} {1} {2} {3} {4}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", "列", "创建级联过滤数据出错", ex.Message); } } try { metaCollection.Save(meta); } catch (Exception ex) { LocalLoggingService.Error("{0} {1} {2} {3} {4}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", "表", "保存表元数据出错", ex.Message); } databaseInfo.Collections.Add(collectionInfo); } catch (Exception ex) { LocalLoggingService.Error("{0} {1} {2} {3} {4}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", "表", "维护其它出错", ex.Message); } } if (databaseInfo.Collections.Count == 0) { //database.Drop(); //AppInfoCenterService.LoggingService.Debug(MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", "数据库", // string.Format("清除历史数据-删除库:{0}", databaseName)); } else { if (!serverInfo.Databases.Contains(databaseInfo)) { serverInfo.Databases.Add(databaseInfo); } } LocalLoggingService.Debug("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", "数据库", string.Format("对数据库 '{0}' 完成一次维护,耗时 {1} 毫秒", databaseName, swdb.ElapsedMilliseconds)); }); LocalLoggingService.Info("{0} {1} {2} {3}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", "服务器", string.Format("对服务器 '{0}' 完成一次维护,耗时 {1} 毫秒", url.Name, sw.ElapsedMilliseconds)); locker.EnterWriteLock(); if (servers.ContainsKey(url)) { servers[url] = serverInfo; } else { servers.Add(url, serverInfo); } locker.ExitWriteLock(); fristLoaded = true; } catch (Exception ex) { LocalLoggingService.Error("{0} {1} {2}", MongodbServerConfiguration.ModuleName, "MongodbServerMaintainceCenter", ex.Message); } }