Пример #1
0
        public async Task <bool> SaveAsync(IEventBase <K> data, byte[] bytes, string uniqueId = null)
        {
            var mEvent = new MongoEvent <K>
            {
                Id       = new ObjectId(),
                StateId  = data.StateId,
                Version  = data.Version,
                TypeCode = data.TypeCode,
                Data     = bytes,
                UniqueId = string.IsNullOrEmpty(uniqueId) ? data.GetUniqueId() : uniqueId
            };

            try
            {
                await mongoStorage.GetCollection <MongoEvent <K> >(grainConfig.EventDataBase, grainConfig.GetCollection(mongoStorage, mongoStorage.Config.SysStartTime, data.Timestamp).Name).InsertOneAsync(mEvent);

                return(true);
            }
            catch (MongoWriteException ex)
            {
                if (ex.WriteError.Category != ServerErrorCategory.DuplicateKey)
                {
                    throw ex;
                }
                else
                {
                    logger.LogError(ex, $"Event Duplicate,Event:{Newtonsoft.Json.JsonConvert.SerializeObject(data)}");
                }
            }
            return(false);
        }
Пример #2
0
        public async Task TransactionSaveAsync(List <EventSaveWrap <K> > list)
        {
            var inserts = new List <MongoEvent <K> >();

            foreach (var data in list)
            {
                var mEvent = new MongoEvent <K>
                {
                    Id       = new ObjectId(),
                    StateId  = data.Evt.StateId,
                    Version  = data.Evt.Version,
                    TypeCode = data.Evt.GetType().FullName,
                    Data     = data.Bytes,
                    UniqueId = string.IsNullOrEmpty(data.UniqueId) ? data.Evt.Version.ToString() : data.UniqueId
                };
                inserts.Add(mEvent);
            }
            var collectionTask = grainConfig.GetCollection(DateTime.UtcNow);

            if (!collectionTask.IsCompleted)
            {
                await collectionTask;
            }
            await grainConfig.Storage.GetCollection <MongoEvent <K> >(grainConfig.DataBase, collectionTask.Result.Name).InsertManyAsync(inserts);
        }
Пример #3
0
        public async ValueTask TransactionSaveAsync(List <EventSaveWrap <K> > list)
        {
            var inserts = new List <MongoEvent <K> >();

            foreach (var data in list)
            {
                var mEvent = new MongoEvent <K>
                {
                    Id       = new ObjectId(),
                    StateId  = data.Evt.StateId,
                    Version  = data.Evt.Version,
                    TypeCode = data.Evt.TypeCode,
                    Data     = data.Bytes,
                    UniqueId = data.UniqueId
                };
                inserts.Add(mEvent);
            }
            await mongoStorage.GetCollection <MongoEvent <K> >(grainConfig.EventDataBase, grainConfig.GetCollection(mongoStorage, mongoStorage.Config.SysStartTime, DateTime.UtcNow).Name).InsertManyAsync(inserts);
        }