Ejemplo n.º 1
0
        /// <see cref="IEventStream.DiscardUpTo"/>
        public async Task <uint> DiscardUpTo(uint sequence, CancellationToken cancel = default(CancellationToken))
        {
            // First, try to seek forward to skip over many events at once
            var skip = await Storage.SeekAsync(sequence, 0, cancel);

            if (skip > Position)
            {
                Position = skip;
            }

            // Drop elements from _cache that are before the requested sequence number
            while (_cache.Count > 0 && _cache.Peek().Sequence < sequence)
            {
                var dequeued = _cache.Dequeue();
                Sequence = dequeued.Sequence;
            }

            // Continues until _cache contains at least one event past the target
            // sequence number (or no events left)
            while (_lastSequence < sequence)
            {
                Sequence = _lastSequence;

                const int maxBytes = 1024 * 1024 * 4;
                var       read     = await Storage.ReadAsync(Position, maxBytes, cancel);

                // Reached end of stream.
                if (read.Events.Count == 0)
                {
                    _cache.Clear();
                    Sequence = _lastSequence;
                    return(Sequence);
                }

                Position = read.NextPosition;

                // Moved past target sequence: append events to _cache
                var lastReadEvent = read.Events[read.Events.Count - 1];
                _lastSequence = lastReadEvent.Sequence;

                if (_lastSequence >= sequence)
                {
                    foreach (var e in read.Events)
                    {
                        if (e.Sequence >= sequence)
                        {
                            _cache.Enqueue(e);
                        }
                        else
                        {
                            Sequence = e.Sequence;
                        }
                    }
                }
            }

            return(Sequence);
        }
Ejemplo n.º 2
0
        public async Task <long> SeekAsync(uint key, long position = 0, CancellationToken cancel = new CancellationToken())
        {
            var sw = Stopwatch.StartNew();

            try
            {
                return(await Inner.SeekAsync(key, position, cancel));
            }
            finally
            {
                Trace.WriteLine("SeekAsync " + sw.ElapsedMilliseconds);
            }
        }
 public Task <long> SeekAsync(uint key, long position = 0, CancellationToken cancel = new CancellationToken()) =>
 Wrapped.SeekAsync(key, position, cancel);