Example #1
0
        public static async Task <Recorder> RecordAsync(this IReadOnlyStream stream)
        {
            var recorder = new Recorder();
            await stream.ReadAsync(recorder).ConfigureAwait(false);

            return(recorder);
        }
Example #2
0
        async Task CopyAsync(
            IReadOnlyStream source,
            IWriteOnlyStream destination,
            CancellationToken cancellationToken = default)
        {
            byte[] buffer = this._bufferPool.Rent(4096);
            try
            {
                int bytesRead;
                while ((bytesRead = await source
                                    .ReadAsync(buffer, 0, buffer.Length, cancellationToken)
                                    .ConfigureAwait(false)) != 0)
                {
                    // Transform

                    await destination
                    .WriteAsync(buffer, 0, bytesRead, cancellationToken)
                    .ConfigureAwait(false);
                }
            }
            finally
            {
                this._bufferPool.Return(buffer);
            }
        }
Example #3
0
        public async Task <TResult> RunAsync <TResult>(IPayloadProcessor processor, CancellationToken cancellationToken)
            where TResult : new()
        {
            long    startIndex = 1;
            TResult state      = default(TResult);
            string  snapshotId = this._source.Id + "/" + typeof(TResult).Name;

            if (_snapshots != null)
            {
                var si = await LoadSnapshot(snapshotId, cancellationToken).ConfigureAwait(false);

                if (si != null)
                {
                    state = (TResult)si.Payload;
                    if (_upToIndex.HasValue && si.SourceVersion == _upToIndex.Value)
                    {
                        return(state);
                    }

                    startIndex = si.SourceVersion + 1;
                }
            }

            if (state == null)
            {
                state = new TResult();
            }

            var reducer = new Reducer <TResult>(state, _onMissing, processor);
            await _source.ReadAsync(reducer, startIndex, _upToIndex ?? long.MaxValue, cancellationToken)
            .ConfigureAwait(false);

            if (_snapshots != null && reducer.LastIndex > 0)
            {
                await _snapshots.AddAsync(snapshotId, new SnapshotInfo(
                                              _source.Id,
                                              reducer.LastIndex,
                                              state,
                                              "1"
                                              )).ConfigureAwait(false);
            }

            return(state);
        }
 public static Task ReadAsync(this IReadOnlyStream stream, ChunkProcessor fn)
 {
     return(stream.ReadAsync(new LambdaSubscription(fn), 0, long.MaxValue, CancellationToken.None));
 }
 public static Task ReadAsync(this IReadOnlyStream stream, ISubscription subscription, long fromIndexInclusive, long toIndexInclusive)
 {
     return(stream.ReadAsync(subscription, fromIndexInclusive, toIndexInclusive, CancellationToken.None));
 }
 public static Task ReadAsync(this IReadOnlyStream stream, ISubscription subscription, long fromIndexInclusive, CancellationToken cancellationToken)
 {
     return(stream.ReadAsync(subscription, fromIndexInclusive, long.MaxValue, cancellationToken));
 }
 public static Task ReadAsync(this IReadOnlyStream stream, ISubscription subscription)
 {
     return(stream.ReadAsync(subscription, 0, long.MaxValue, CancellationToken.None));
 }