public async Task CreateIndex(IMongoStorage storage) { if (!IndexCreated) { var stateCollection = storage.GetCollection <BsonDocument>(EventDataBase, SnapshotCollection); var stateIndex = await stateCollection.Indexes.ListAsync(); var stateIndexList = await stateIndex.ToListAsync(); if (!stateIndexList.Exists(p => p["name"] == "State")) { await stateCollection.Indexes.CreateOneAsync(new CreateIndexModel <BsonDocument>("{'StateId':1}", new CreateIndexOptions { Name = "State", Unique = true })); } var collection = storage.GetCollection <BsonDocument>(EventDataBase, C_CName); var index = await collection.Indexes.ListAsync(); var indexList = await index.ToListAsync(); if (!indexList.Exists(p => p["name"] == "Name")) { await collection.Indexes.CreateOneAsync(new CreateIndexModel <BsonDocument>("{'Name':1}", new CreateIndexOptions { Name = "Name", Unique = true })); } IndexCreated = true; } }
public async Task CreateCollectionIndex(IMongoStorage storage) { var collectionService = storage.GetCollection <BsonDocument>(EventDataBase, C_CName); 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 }); } }
public async Task CreateStateIndex(IMongoStorage storage) { var collectionService = storage.GetCollection <BsonDocument>(EventDataBase, SnapshotCollection); CancellationTokenSource cancel = new CancellationTokenSource(1); var index = await collectionService.Indexes.ListAsync(); var indexList = await index.ToListAsync(); if (!indexList.Exists(p => p["name"] == "State")) { await collectionService.Indexes.CreateOneAsync("{'StateId':1}", new CreateIndexOptions { Name = "State", Unique = true }); } }
public CollectionInfo GetCollection(IMongoStorage storage, DateTime sysStartTime, DateTime eventTime) { CollectionInfo lastCollection = null; var cList = GetAllCollectionList(storage); 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 { Id = ObjectId.GenerateNewId().ToString(), Version = cVersion, Type = EventCollection, CreateTime = DateTime.UtcNow, Name = EventCollection + "_" + cVersion }; try { storage.GetCollection <CollectionInfo>(EventDataBase, C_CName).InsertOne(collection); collectionList.Add(collection); lastCollection = collection; CreateEventIndex(storage, collection.Name).GetAwaiter().GetResult(); } catch (MongoWriteException ex) { if (ex.WriteError.Category == ServerErrorCategory.DuplicateKey) { collectionList = null; return(GetCollection(storage, sysStartTime, eventTime)); } } } } } return(lastCollection); }
public async Task <IList <IEventBase <K> > > GetListAsync(K stateId, Int64 startVersion, Int64 endVersion, DateTime?startTime = null) { var collectionList = grainConfig.GetCollectionList(mongoStorage, mongoStorage.Config.SysStartTime, startTime); var list = new List <IEventBase <K> >(); Int64 readVersion = 0; foreach (var collection in collectionList) { var filterBuilder = Builders <BsonDocument> .Filter; var filter = filterBuilder.Eq("StateId", stateId) & filterBuilder.Lte("Version", endVersion) & filterBuilder.Gt("Version", startVersion); var cursor = await mongoStorage.GetCollection <BsonDocument>(grainConfig.EventDataBase, collection.Name).FindAsync <BsonDocument>(filter, cancellationToken: new CancellationTokenSource(10000).Token); foreach (var document in cursor.ToEnumerable()) { var typeCode = document["TypeCode"].AsString; if (MessageTypeMapper.EventTypeDict.TryGetValue(typeCode, out var type)) { var data = document["Data"].AsByteArray; using (var ms = new MemoryStream(data)) { if (Serializer.Deserialize(type, ms) is IEventBase <K> evt) { readVersion = evt.Version; if (readVersion <= endVersion) { list.Add(evt); } } } } } if (readVersion >= endVersion) { break; } } return(list); }
public List <CollectionInfo> GetAllCollectionList(IMongoStorage storage) { if (collectionList == null) { lock (collectionLock) { if (collectionList == null) { collectionList = storage.GetCollection <CollectionInfo>(EventDataBase, C_CName).Find <CollectionInfo>(c => c.Type == EventCollection).ToList(); } } } return(collectionList); }
public async Task <List <EventInfo <K> > > GetListAsync(K stateId, Int64 startVersion, Int64 endVersion, DateTime?startTime = null) { var collectionList = mongoAttr.GetCollectionList(mongoStorage, mongoStorage.Config.SysStartTime, startTime); var list = new List <EventInfo <K> >(); Int64 readVersion = 0; foreach (var collection in collectionList) { var filterBuilder = Builders <BsonDocument> .Filter; var filter = filterBuilder.Eq("StateId", stateId) & filterBuilder.Lte("Version", endVersion) & filterBuilder.Gt("Version", startVersion); var cursor = await mongoStorage.GetCollection <BsonDocument>(mongoAttr.EventDataBase, collection.Name).FindAsync <BsonDocument>(filter, cancellationToken: new CancellationTokenSource(3000).Token); foreach (var document in cursor.ToEnumerable()) { var typeCode = document["TypeCode"].AsString; var type = MessageTypeMapping.GetType(typeCode); var data = document["Data"].AsByteArray; var eventInfo = new EventInfo <K>(); eventInfo.IsComplete = document["IsComplete"].AsBoolean; using (var ms = new MemoryStream(data)) { var @event = Serializer.Deserialize(type, ms) as IEventBase <K>; readVersion = @event.Version; eventInfo.Event = @event; } if (readVersion <= endVersion) { list.Add(eventInfo); } } if (readVersion >= endVersion) { break; } } return(list.OrderBy(e => e.Event.Version).ToList()); }
public async Task CreateEventIndex(IMongoStorage storage, string collectionName) { var collectionService = storage.GetCollection <BsonDocument>(EventDataBase, collectionName); var indexList = (await collectionService.Indexes.ListAsync()).ToList(); if (!indexList.Exists(p => p["name"] == "State_Version") && !indexList.Exists(p => p["name"] == "State_UniqueId")) { await collectionService.Indexes.CreateManyAsync( 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,'UniqueId':1}", new CreateIndexOptions { Name = "State_UniqueId", Unique = true }) } ); } }
public EntityStorage(IMongoStorage mongoStorage, IIndexes <TEntity> indexes) { Collection = mongoStorage.GetCollection <TEntity>(); indexes.CreateIndexes(Collection); }
public async Task DeleteAsync(K id) { var filterBuilder = Builders <BsonDocument> .Filter; var filter = filterBuilder.Eq("StateId", id); await mongoStorage.GetCollection <BsonDocument>(database, collection).DeleteManyAsync(filter); }
private static void InternalCanThrowExceptionIfCollectionNameAttributeEmpty(IMongoStorage mongoStorage) { Assert.Throws <ArgumentException>( () => { mongoStorage.GetCollection <WithEmptyCollectionAttributeEntity, Guid>(); }, $"There is empty collection name at {typeof(CollectionNameAttribute).Name} in {typeof(WithEmptyCollectionAttributeEntity).Name}"); }
private static void InternalCanGetCollection(IMongoStorage mongoStorage) { var collection = mongoStorage.GetCollection <WithCollectionAttributeEntity, Guid>(); Assert.NotNull(collection); }
public GenericMongoRepository(IMongoStorage mongoStorage) { this.Collection = mongoStorage.GetCollection <TEntity, TKey>(); }
public MongoRepository(IMongoStorage mongoStorage, IIndexes <TEntity> indexes) { this.Collection = mongoStorage.GetCollection <TEntity>(); indexes.CreateIndexes(this.Collection); }