コード例 #1
0
ファイル: MongoGrainConfig.cs プロジェクト: wxlevel/Ray
        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;
            }
        }
コード例 #2
0
ファイル: MongoStorageAttribute.cs プロジェクト: wangsic/Ray
        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 });
            }
        }
コード例 #3
0
        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 });
            }
        }
コード例 #4
0
ファイル: MongoGrainConfig.cs プロジェクト: wxlevel/Ray
        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);
        }
コード例 #5
0
ファイル: MongoEventStorage.cs プロジェクト: wxlevel/Ray
        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);
        }
コード例 #6
0
ファイル: MongoGrainConfig.cs プロジェクト: wxlevel/Ray
 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);
 }
コード例 #7
0
ファイル: MongoEventStorage.cs プロジェクト: zmk523/Ray
        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());
        }
コード例 #8
0
ファイル: MongoGrainConfig.cs プロジェクト: wxlevel/Ray
        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
                    })
                }
                    );
            }
        }
コード例 #9
0
 public EntityStorage(IMongoStorage mongoStorage, IIndexes <TEntity> indexes)
 {
     Collection = mongoStorage.GetCollection <TEntity>();
     indexes.CreateIndexes(Collection);
 }
コード例 #10
0
ファイル: MongoStateStorage.cs プロジェクト: wxlevel/Ray
 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);
 }
コード例 #11
0
 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}");
 }
コード例 #12
0
        private static void InternalCanGetCollection(IMongoStorage mongoStorage)
        {
            var collection = mongoStorage.GetCollection <WithCollectionAttributeEntity, Guid>();

            Assert.NotNull(collection);
        }
コード例 #13
0
 public GenericMongoRepository(IMongoStorage mongoStorage)
 {
     this.Collection = mongoStorage.GetCollection <TEntity, TKey>();
 }
コード例 #14
0
 public MongoRepository(IMongoStorage mongoStorage, IIndexes <TEntity> indexes)
 {
     this.Collection = mongoStorage.GetCollection <TEntity>();
     indexes.CreateIndexes(this.Collection);
 }