Пример #1
0
        public async Task <Optional <EntityStreamData> > GetData(QueryConfig cfg, CancellationToken token)
        {
            using (var db = await _db.CreateAsync(token).ConfigureFalse())
            {
                var snapshot = Optional <Snapshot> .Empty;
                if (!cfg.IgnoreSnapshots)
                {
                    snapshot = await GetSnapshot(db, cfg).ConfigureFalse();
                }
                var vers = snapshot.IsEmpty ? 1:snapshot.Value.Version + 1;
                token.ThrowIfCancellationRequested();
                var all = await db.QueryAsAsync <Commit>(
                    q => q.From <Commit>()
                    .Where(d => d.TenantId == cfg.TenantId)
                    .AndIf(() => cfg.EntityId.HasValue, d => d.EntityId == cfg.EntityId.Value)
                    .And(d => d.Version >= Math.Max(cfg.VersionStart, vers))
                    .AndIf(() => cfg.DateEnd.HasValue, d => d.Timestamp <= cfg.DateEnd)
                    .AndIf(() => cfg.DateStart.HasValue, d => d.Timestamp >= cfg.DateStart)
                    .AndIf(() => cfg.VersionEnd.HasValue, d => d.Version <= cfg.VersionEnd)
                    .OrderBy(d => d.Version)
                    .SelectAll(useAsterisk: true)
                    , token).ConfigureFalse();

                var rez = new EntityStreamData();
                rez.LatestSnapshot = snapshot;
                rez.Commits        = all;
                return(rez.ToOptional());
            }
        }
Пример #2
0
        public Task <Optional <EntityStreamData> > GetData(QueryConfig cfg, CancellationToken token)
        {
            var esd = new EntityStreamData();

            if (!cfg.IgnoreSnapshots)
            {
                esd.LatestSnapshot = GetSnapshot(cfg);
            }
            var ver = esd.LatestSnapshot.IsEmpty ? 1 : esd.LatestSnapshot.Value.Version + 1;

            cfg.VersionStart = Math.Max(cfg.VersionStart, ver);
            cfg.VersionEnd   = cfg.VersionEnd ?? int.MaxValue;
            lock (_sync)
            {
                esd.Commits =
                    _commits.Where(d => d.TenantId == cfg.TenantId).Where(d =>
                {
                    if (cfg.EntityId.HasValue)
                    {
                        return(d.EntityId == cfg.EntityId);
                    }
                    return(true);
                }).Where(d => d.Version >= cfg.VersionStart && d.Version <= cfg.VersionEnd)
                    .Where(d => d.Timestamp >= (cfg.DateStart ?? DateTimeOffset.MinValue))
                    .Where(d => d.Timestamp <= (cfg.DateEnd ?? DateTimeOffset.MaxValue))
                    .OrderBy(d => d.Version)
                    .ToArray();
            }

            return(Task.FromResult(esd.ToOptional()));
        }