Example #1
0
        public async Task <bool> SaveAsync(IEventBase <K> evt, byte[] bytes, string uniqueId = null)
        {
            var table = await tableInfo.GetTable(evt.Timestamp);

            if (!saveSqlDict.TryGetValue(table.Name, out var saveSql))
            {
                saveSql = $"INSERT INTO {table.Name}(stateid,uniqueId,typecode,data,version) VALUES(@StateId,@UniqueId,@TypeCode,@Data,@Version)";
                saveSqlDict.TryAdd(table.Name, saveSql);
            }
            if (string.IsNullOrEmpty(uniqueId))
            {
                uniqueId = evt.GetUniqueId();
            }
            try
            {
                using (var conn = tableInfo.CreateConnection())
                {
                    await conn.ExecuteAsync(saveSql, new { StateId = evt.StateId.ToString(), UniqueId = uniqueId, evt.TypeCode, Data = bytes, evt.Version });
                }
                return(true);
            }
            catch (Exception ex)
            {
                if (!(ex is PostgresException e && e.SqlState == "23505"))
                {
                    throw ex;
                }
            }
            return(false);
        }
Example #2
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);
        }