예제 #1
0
 public List <CollectionInfo> GetAllCollectionList()
 {
     if (collectionList == null)
     {
         lock (collectionLock)
         {
             if (collectionList == null)
             {
                 collectionList = MongoStorage.GetCollection <CollectionInfo>(EventDataBase, C_CName).Find <CollectionInfo>(c => c.Type == EventCollection).ToList();
             }
         }
     }
     return(collectionList);
 }
예제 #2
0
        public CollectionInfo GetCollection(DateTime sysStartTime, DateTime eventTime)
        {
            CollectionInfo lastCollection = null;
            var            cList          = GetAllCollectionList();

            if (cList.Count > 0)
            {
                lastCollection = cList.Last();
            }
            //如果不需要分表,直接返回
            if (lastCollection != null && !this.sharding)
            {
                return(lastCollection);
            }
            var subTime  = eventTime.Subtract(sysStartTime);
            var cVersion = subTime.TotalDays > 0 ? Convert.ToInt32(Math.Floor(subTime.TotalDays / shardingDays)) : 0;

            if (lastCollection == null || cVersion > lastCollection.Version)
            {
                lock (collectionLock)
                {
                    if (lastCollection == null || cVersion > lastCollection.Version)
                    {
                        var collection = new CollectionInfo();
                        collection.Id         = ObjectId.GenerateNewId().ToString();
                        collection.Version    = cVersion;
                        collection.Type       = EventCollection;
                        collection.CreateTime = DateTime.UtcNow;
                        collection.Name       = EventCollection + "_" + cVersion;
                        try
                        {
                            MongoStorage.GetCollection <CollectionInfo>(EventDataBase, C_CName).InsertOne(collection);
                            collectionList.Add(collection);
                            lastCollection = collection;
                            CreateEventIndex(collection.Name);
                        }
                        catch (MongoWriteException ex)
                        {
                            if (ex.WriteError.Category == ServerErrorCategory.DuplicateKey)
                            {
                                collectionList = null;
                                return(GetCollection(sysStartTime, eventTime));
                            }
                        }
                    }
                }
            }
            return(lastCollection);
        }
예제 #3
0
 private void CreateCollectionIndex()
 {
     Task.Run(async() =>
     {
         var collectionService          = MongoStorage.GetCollection <BsonDocument>(EventDataBase, C_CName);
         CancellationTokenSource cancel = new CancellationTokenSource(1);
         var index     = await collectionService.Indexes.ListAsync();
         var indexList = await index.ToListAsync();
         if (!indexList.Exists(p => p["name"] == "Name"))
         {
             await collectionService.Indexes.CreateOneAsync("{'Name':1}", new CreateIndexOptions {
                 Name = "Name", Unique = true
             });
         }
     }).ConfigureAwait(false);
 }
예제 #4
0
        private void CreateEventIndex(string collectionName)
        {
            var collectionService = MongoStorage.GetCollection <BsonDocument>(EventDataBase, collectionName);
            var indexList         = collectionService.Indexes.List().ToList();

            if (!indexList.Exists(p => p["name"] == "State_Version") && !indexList.Exists(p => p["name"] == "State_MsgId"))
            {
                collectionService.Indexes.CreateMany(
                    new List <CreateIndexModel <BsonDocument> >()
                {
                    new CreateIndexModel <BsonDocument>("{'StateId':1,'Version':1}", new CreateIndexOptions {
                        Name = "State_Version", Unique = true
                    }),
                    new CreateIndexModel <BsonDocument>("{'StateId':1,'TypeCode':1,'MsgId':1}", new CreateIndexOptions {
                        Name = "State_MsgId", Unique = true
                    })
                }
                    );
            }
        }
예제 #5
0
 public static void InitMongoDb(this IServiceProvider provider)
 {
     MongoStorage.Init(provider);
 }