예제 #1
0
        private async Task SnapshotAsync(CancellationToken cancellation)
        {
            var snapshotRevisionThreshold = _options.SnapshotRevisionThreshold;

            if (snapshotRevisionThreshold < 0)
            {
                snapshotRevisionThreshold = 20;
            }

            using (var scope = _serviceProvider.CreateScope())
            {
                var scopedServiceProvider = scope.ServiceProvider;
                var entityStorageEngine   = scopedServiceProvider.GetRequiredService <IEntityStorageEngine>();
                var enumerator            = default(IAsyncEnumerator <IStream>);
                try
                {
                    enumerator = _streamStore.OpenStreamsToSnapshotAsync(snapshotRevisionThreshold, cancellation).GetEnumerator();

                    while (await enumerator.MoveNext(cancellation))
                    {
                        var stream = enumerator.Current;

                        if (stream.Snapshot == null && !stream.Commits.Any())
                        {
                            continue;
                        }

                        var serializedEntity = default(JToken);

                        if (stream.Snapshot == null)
                        {
                            serializedEntity = StreamRoot;
                        }
                        else
                        {
                            serializedEntity = JToken.Parse(CompressionHelper.Unzip(stream.Snapshot.Payload as byte[]));
                        }

                        foreach (var commit in stream.Commits)
                        {
                            serializedEntity = _differ.Patch(serializedEntity, JToken.Parse(CompressionHelper.Unzip(commit.Body as byte[])));
                        }

                        await stream.AddSnapshotAsync(CompressionHelper.Zip(serializedEntity.ToString()), cancellation);
                    }
                }
                finally
                {
                    enumerator?.Dispose();
                }
            }
        }