public async Task <RoadNetworkChangesArchive> Get(ArchiveId id, CancellationToken ct = default)
        {
            var stream = new StreamName(id);

            if (_map.TryGet(stream, out var entry))
            {
                return((RoadNetworkChangesArchive)entry.Entity);
            }
            var page = await _store.ReadStreamForwards(stream, StreamVersion.Start, 1024, ct);

            if (page.Status == PageReadStatus.StreamNotFound)
            {
                var network = RoadNetworkChangesArchive.Factory();
                _map.Attach(new EventSourcedEntityMapEntry(network, stream, ExpectedVersion.NoStream));
                return(network);
            }
            IEventSourcedEntity entity = RoadNetworkChangesArchive.Factory();
            var messages = new List <object>(page.Messages.Length);

            foreach (var message in page.Messages)
            {
                messages.Add(
                    JsonConvert.DeserializeObject(
                        await message.GetJsonData(ct),
                        _mapping.GetEventType(message.Type),
                        _settings));
            }
            entity.RestoreFromEvents(messages.ToArray());
            while (!page.IsEnd)
            {
                messages.Clear();
                page = await page.ReadNext(ct);

                if (page.Status == PageReadStatus.StreamNotFound)
                {
                    var network = RoadNetworkChangesArchive.Factory();
                    _map.Attach(new EventSourcedEntityMapEntry(network, stream, ExpectedVersion.NoStream));
                    return(network);
                }
                foreach (var message in page.Messages)
                {
                    messages.Add(
                        JsonConvert.DeserializeObject(
                            await message.GetJsonData(ct),
                            _mapping.GetEventType(message.Type),
                            _settings));
                }
                entity.RestoreFromEvents(messages.ToArray());
            }
            _map.Attach(new EventSourcedEntityMapEntry(entity, stream, page.LastStreamVersion));
            return((RoadNetworkChangesArchive)entity);
        }
Exemplo n.º 2
0
        public async Task <Organization> TryGet(OrganizationId id, CancellationToken ct = default)
        {
            var stream = StreamNameFactory(id);

            if (_map.TryGet(stream, out var entry))
            {
                return((Organization)entry.Entity);
            }
            var organization = Organization.Factory();
            var page         = await _store.ReadStreamForwards(stream, StreamVersion.Start, 100, ct);

            if (page.Status == PageReadStatus.StreamNotFound)
            {
                return(null);
            }
            IEventSourcedEntity entity = organization;
            var messages = new List <object>(page.Messages.Length);

            foreach (var message in page.Messages)
            {
                messages.Add(
                    JsonConvert.DeserializeObject(
                        await message.GetJsonData(ct),
                        _mapping.GetEventType(message.Type),
                        _settings));
            }
            entity.RestoreFromEvents(messages.ToArray());
            while (!page.IsEnd)
            {
                messages.Clear();
                page = await page.ReadNext(ct);

                if (page.Status == PageReadStatus.StreamNotFound)
                {
                    return(null);
                }
                foreach (var message in page.Messages)
                {
                    messages.Add(
                        JsonConvert.DeserializeObject(
                            await message.GetJsonData(ct),
                            _mapping.GetEventType(message.Type),
                            _settings));
                }
                entity.RestoreFromEvents(messages.ToArray());
            }
            _map.Attach(new EventSourcedEntityMapEntry(entity, stream, page.LastStreamVersion));
            return(organization);
        }
Exemplo n.º 3
0
        public async Task <(RoadNetwork, int)> GetWithVersion(CancellationToken ct = default)
        {
            if (_map.TryGet(Stream, out var entry))
            {
                return((RoadNetwork)entry.Entity, entry.ExpectedVersion);
            }
            var roadNetwork = RoadNetwork.Factory();

            var(snapshot, version) = await _snapshotReader.ReadSnapshot(ct);

            if (version != ExpectedVersion.NoStream)
            {
                roadNetwork.RestoreFromSnapshot(snapshot);
                version += 1;
            }
            else
            {
                version = StreamVersion.Start;
            }

            var page = await _store.ReadStreamForwards(Stream, version, StreamPageSize, ct);

            if (page.Status == PageReadStatus.StreamNotFound)
            {
                var network = RoadNetwork.Factory();
                _map.Attach(new EventSourcedEntityMapEntry(network, Stream, ExpectedVersion.NoStream));
                return(network, ExpectedVersion.NoStream);
            }
            IEventSourcedEntity entity = roadNetwork;
            var messages = new List <object>(page.Messages.Length);

            foreach (var message in page.Messages)
            {
                messages.Add(
                    JsonConvert.DeserializeObject(
                        await message.GetJsonData(ct),
                        _mapping.GetEventType(message.Type),
                        _settings));
            }
            entity.RestoreFromEvents(messages.ToArray());
            while (!page.IsEnd)
            {
                messages.Clear();
                page = await page.ReadNext(ct);

                if (page.Status == PageReadStatus.StreamNotFound)
                {
                    var network = RoadNetwork.Factory();
                    _map.Attach(new EventSourcedEntityMapEntry(network, Stream, ExpectedVersion.NoStream));
                    return(network, ExpectedVersion.NoStream);
                }
                foreach (var message in page.Messages)
                {
                    messages.Add(
                        JsonConvert.DeserializeObject(
                            await message.GetJsonData(ct),
                            _mapping.GetEventType(message.Type),
                            _settings));
                }
                entity.RestoreFromEvents(messages.ToArray());
            }
            _map.Attach(new EventSourcedEntityMapEntry(entity, Stream, page.LastStreamVersion));
            return(roadNetwork, page.LastStreamVersion);
        }