public async Task <bool> DeleteAsync(object id)
 {
     using (var db = PostgreSqlDbContext.Create(this._options))
     {
         return(await db.ExecuteAsync(this.deleteSql, new { Id = id.ToString() }) > 0);
     }
 }
Beispiel #2
0
        public async Task SqlSaveAsync(PostgreSqlDbContext db, List <EventStorageBufferWrap> eventWraps)
        {
            using (var trans = db.BeginTransaction())
            {
                try
                {
                    foreach (var wrap in eventWraps)
                    {
                        EventSingleStorageModel model = wrap.Value;
                        var data = this.GetSerializer().Serialize(model.Event);

                        wrap.Result = await db.ExecuteAsync(this.insertSql, new
                        {
                            StateId       = model.StateId,
                            RelationEvent = model.Event.RelationEvent,
                            TypeCode      = model.Event.TypeCode,
                            Data          = data,
                            DataType      = this.options.SerializationType,
                            Version       = model.Event.Version,
                            AddTime       = model.Event.Timestamp
                        }) > 0;
                    }
                    trans.Commit();
                    eventWraps.ForEach(wrap => wrap.TaskSource.TrySetResult(wrap.Result));
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    eventWraps.ForEach(wrap => wrap.TaskSource.TrySetException(ex));
                }
            }
        }
        public async Task SqlSaveAsync(PostgreSqlDbContext db, List <IDataflowBufferWrap <EventStorageModel> > eventWraps)
        {
            using (var trans = db.BeginTransaction())
            {
                try
                {
                    foreach (var wrap in eventWraps)
                    {
                        EventStorageModel model = wrap.Data;
                        var data = this.GetSerializer().Serialize(model.Event);

                        wrap.Data.Result = await db.ExecuteAsync(this.insertSql, new
                        {
                            Id            = model.Id,
                            RelationEvent = model.Event.RelationEvent,
                            TypeCode      = model.Event.TypeCode,
                            Data          = data,
                            DataType      = this.options.SerializationType,
                            Version       = model.Event.Version,
                            AddTime       = model.Event.Timestamp
                        }) > 0;
                    }
                    trans.Commit();
                    eventWraps.ForEach(wrap => wrap.CompleteHandler(wrap.Data.Result));
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    eventWraps.ForEach(wrap => wrap.ExceptionHandler(ex));
                }
            }
        }
Beispiel #4
0
 public Task <bool> SaveAsync(EventCollectionStorageModel events)
 {
     using (var db = PostgreSqlDbContext.Create(this.options))
     {
         this.BinarySaveAsync(db, events.Events);
         return(Task.FromResult(true));
     }
 }
 public async Task <bool> UpdateAsync <TState>(object id, TState state) where TState : IState, new()
 {
     using (var db = PostgreSqlDbContext.Create(this._options))
     {
         var data = this.GetSerializer().Serialize(state);
         return(await db.ExecuteAsync(this.updateSql, new { Id = id.ToString(), Data = data, DataType = this._options.SerializationType }) > 0);
     }
 }
        public async Task <bool> SaveAsync(EventCollectionStorageModel events)
        {
            using (var db = PostgreSqlDbContext.Create(this.options))
            {
                await db.OpenAsync();

                this.BinarySaveAsync(db, events.Events);
                return(true);
            }
        }
        public async Task <TState> ReadAsync <TState>(object id) where TState : IState, new()
        {
            using (var db = PostgreSqlDbContext.Create(this._options))
            {
                var list = await db.QueryAsync <dynamic>(this.selectSql, new { Id = id.ToString() });

                if (list.Count() == 0)
                {
                    return(default(TState));
                }
                var data = list.FirstOrDefault();
                return(this.GetSerializer(data.datatype).Deserialize <TState>(data.data));
            }
        }
Beispiel #8
0
        public async Task <List <EventModel> > GetListAsync(EventQueryModel query)
        {
            var list = new List <EventModel>(query.Limit);

            using (var db = PostgreSqlDbContext.Create(this.options))
            {
                StringBuilder sql = new StringBuilder($"COPY (SELECT typecode,data,datatype,version FROM {tableName} WHERE version > '{query.StartVersion}'");
                if (query.StateId != null)
                {
                    sql.Append($" and stateid = '{query.StateId}'");
                }
                if (query.StartVersion > 0)
                {
                    sql.Append($" and version <= '{query.EndVersion}'");
                }
                if (!string.IsNullOrEmpty(query.EventTypeCode))
                {
                    sql.Append($" and typecode = '{query.EventTypeCode}'");
                }
                if (!string.IsNullOrEmpty(query.RelationEvent))
                {
                    sql.Append($" and relationevent = '{query.RelationEvent}'");
                }
                sql.Append(" ORDER BY version ASC) TO STDOUT(FORMAT BINARY)");

                await db.OpenAsync();

                using (var reader = db.BeginBinaryExport(sql.ToString()))
                {
                    while (reader.StartRow() != -1)
                    {
                        string typeCode  = reader.Read <string>(NpgsqlDbType.Varchar);
                        byte[] dataBytes = reader.Read <byte[]>(NpgsqlDbType.Bytea);
                        string dataType  = reader.Read <string>(NpgsqlDbType.Varchar);
                        long   version   = reader.Read <Int64>(NpgsqlDbType.Bigint);

                        //Get event type
                        EventModel eventModel = this.ConversionEvent(typeCode, dataType, dataBytes, version);
                        if (eventModel != null)
                        {
                            list.Add(eventModel);
                        }
                    }
                }
            }
            return(list);
        }
Beispiel #9
0
 private async Task CreateTable(string name, object stateId, string sql)
 {
     try
     {
         int stateIdLength = this.GetStateIdLength(stateId);
         sql = string.Format(sql, name, stateIdLength);
         using (var db = PostgreSqlDbContext.Create(this._options))
         {
             await db.ExecuteAsync(sql);
         }
     }
     catch (Exception ex)
     {
         this._logger.LogError(ex, $"[{ProviderName}] Creating table {name} failed");
         throw ex;
     }
 }
Beispiel #10
0
 public void BinarySaveAsync(PostgreSqlDbContext db, List <EventStorageModel> events)
 {
     using (var writer = db.BeginBinaryImport(this.insertBinarySql))
     {
         foreach (var e in events)
         {
             var data = this.GetSerializer().Serialize(e.Event);
             writer.StartRow();
             writer.Write(e.StateId.ToString(), NpgsqlDbType.Varchar);
             writer.Write(e.Event.RelationEvent, NpgsqlDbType.Varchar);
             writer.Write(e.Event.TypeCode, NpgsqlDbType.Varchar);
             writer.Write(data, NpgsqlDbType.Bytea);
             writer.Write(this.options.SerializationType, NpgsqlDbType.Varchar);
             writer.Write(e.Event.Version, NpgsqlDbType.Bigint);
             writer.Write(e.Event.Timestamp, NpgsqlDbType.Bigint);
         }
         writer.Complete();
     }
 }
Beispiel #11
0
        public async Task SaveAsync(List <EventStorageBufferWrap> eventWraps)
        {
            using (var db = PostgreSqlDbContext.Create(this.options))
            {
                await db.OpenAsync();

                try
                {
                    var eventList = eventWraps.Select(f => f.Value).ToList <EventStorageModel>();
                    this.BinarySaveAsync(db, eventList);
                    eventWraps.ForEach(wrap => wrap.TaskSource.TrySetResult(true));
                    return;
                }
                catch
                {
                    await this.SqlSaveAsync(db, eventWraps);
                }
            }
        }
        public async Task SaveAsync(List <IDataflowBufferWrap <EventStorageModel> > eventWraps)
        {
            using (var db = PostgreSqlDbContext.Create(this.options))
            {
                await db.OpenAsync();

                try
                {
                    var eventList = eventWraps.Select(f => f.Data).ToList <EventModel>();
                    this.BinarySaveAsync(db, eventList);
                    eventWraps.ForEach(wrap => wrap.CompleteHandler(true));
                    return;
                }
                catch
                {
                    await this.SqlSaveAsync(db, eventWraps);
                }
            }
        }
Beispiel #13
0
        public async Task <EventModel> GetAsync(object stateId, long version)
        {
            using (var db = PostgreSqlDbContext.Create(this.options))
            {
                StringBuilder sql = new StringBuilder($"COPY (SELECT typecode,data,datatype FROM {tableName} WHERE stateid = '{stateId.ToString()}' and  version = '{version}' ) TO STDOUT(FORMAT BINARY)");
                await db.OpenAsync();

                using (var reader = db.BeginBinaryExport(sql.ToString()))
                {
                    while (reader.StartRow() != -1)
                    {
                        string typeCode  = reader.Read <string>(NpgsqlDbType.Varchar);
                        byte[] dataBytes = reader.Read <byte[]>(NpgsqlDbType.Bytea);
                        string dataType  = reader.Read <string>(NpgsqlDbType.Varchar);

                        return(this.ConversionEvent(typeCode, dataType, dataBytes, version));
                    }
                    return(null);
                }
            }
        }