Example #1
0
        public async Task <IList <IFullyEvent <PrimaryKey> > > GetList(PrimaryKey stateId, long latestTimestamp, long startVersion, long endVersion)
        {
            var list = new List <IFullyEvent <PrimaryKey> >((int)(endVersion - startVersion));
            await Task.Run(async() =>
            {
                var getTableListTask = config.GetSubTables();
                if (!getTableListTask.IsCompletedSuccessfully)
                {
                    await getTableListTask;
                }
                var stateIdStr = typeof(PrimaryKey) == typeof(long) ? stateId.ToString() : $"'{stateId.ToString()}'";
                using (var conn = config.CreateConnection() as NpgsqlConnection)
                {
                    await conn.OpenAsync();
                    foreach (var table in getTableListTask.Result.Where(t => t.EndTime >= latestTimestamp))
                    {
                        var sql = $"COPY (SELECT typecode,data,version,timestamp from {table.SubTable} WHERE stateid={stateIdStr} and version>={startVersion} and version<={endVersion} order by version asc) TO STDOUT (FORMAT BINARY)";
                        using (var reader = conn.BeginBinaryExport(sql))
                        {
                            while (reader.StartRow() != -1)
                            {
                                var typeCode  = reader.Read <string>(NpgsqlDbType.Varchar);
                                var data      = reader.Read <string>(NpgsqlDbType.Json);
                                var version   = reader.Read <long>(NpgsqlDbType.Bigint);
                                var timestamp = reader.Read <long>(NpgsqlDbType.Bigint);
                                if (version <= endVersion && version >= startVersion)
                                {
                                    if (serializer.Deserialize(TypeContainer.GetType(typeCode), Encoding.Default.GetBytes(data)) is IEvent evt)
                                    {
                                        list.Add(new FullyEvent <PrimaryKey>
                                        {
                                            StateId = stateId,
                                            Event   = evt,
                                            Base    = new EventBase(version, timestamp)
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
            });

            return(list.OrderBy(e => e.Base.Version).ToList());
        }
Example #2
0
        public async Task <IList <IFullyEvent <PrimaryKey> > > GetList(PrimaryKey stateId, long latestTimestamp, long startVersion, long endVersion)
        {
            var list = new List <IFullyEvent <PrimaryKey> >((int)(endVersion - startVersion));
            await Task.Run(async() =>
            {
                var getTableListTask = config.GetSubTables();
                if (!getTableListTask.IsCompletedSuccessfully)
                {
                    await getTableListTask;
                }
                var stateIdStr = typeof(PrimaryKey) == typeof(long) ? stateId.ToString() : $"'{stateId.ToString()}'";
                using (var conn = config.CreateConnection())
                {
                    await conn.OpenAsync();
                    foreach (var table in getTableListTask.Result.Where(t => t.EndTime >= latestTimestamp))
                    {
                        var sql        = $"SELECT typecode,data,version,timestamp from {table.SubTable} WHERE stateid=@StateId and version>=@StartVersion and version<=@EndVersion order by version asc";
                        var originList = await conn.QueryAsync <EventModel>(sql, new
                        {
                            StateId      = stateId,
                            StartVersion = startVersion,
                            EndVersion   = endVersion
                        });
                        foreach (var item in originList)
                        {
                            if (serializer.Deserialize(TypeContainer.GetType(item.TypeCode), Encoding.Default.GetBytes(item.Data)) is IEvent evt)
                            {
                                list.Add(new FullyEvent <PrimaryKey>
                                {
                                    StateId = stateId,
                                    Event   = evt,
                                    Base    = new EventBase(item.Version, item.Timestamp)
                                });
                            }
                        }
                    }
                }
            });

            return(list.OrderBy(e => e.Base.Version).ToList());
        }