示例#1
0
        public async Task <IEnumerable <(long, T)> > GetBatch(long startingOffset, int batchSize, CancellationToken cancellationToken)
        {
            Preconditions.CheckRange(batchSize, 1, nameof(batchSize));

            if (startingOffset < this.headOffset)
            {
                startingOffset = this.headOffset;
            }

            if (this.IsEmpty() || this.tailOffset < startingOffset)
            {
                return(Enumerable.Empty <(long, T)>());
            }

            var batch = new List <(long, T)>();

            byte[] startingKey = StoreUtils.GetKeyFromOffset(startingOffset);
            await this.entityStore.IterateBatch(
                startingKey,
                batchSize,
                (k, v) =>
            {
                long offsetFromKey = StoreUtils.GetOffsetFromKey(k);
                batch.Add((offsetFromKey, v));
                return(Task.CompletedTask);
            },
                cancellationToken);

            return(batch);
        }
示例#2
0
        public static async Task <ISequentialStore <T> > Create(IEntityStore <byte[], T> entityStore, long defaultHeadOffset)
        {
            Preconditions.CheckNotNull(entityStore, nameof(entityStore));
            Option <(byte[] key, T value)> firstEntry = await entityStore.GetFirstEntry(CancellationToken.None);

            Option <(byte[] key, T value)> lastEntry = await entityStore.GetLastEntry(CancellationToken.None);

            long MapLocalFunction((byte[] key, T) e) => StoreUtils.GetOffsetFromKey(e.key);

            long headOffset      = firstEntry.Map(MapLocalFunction).GetOrElse(defaultHeadOffset);
            long tailOffset      = lastEntry.Map(MapLocalFunction).GetOrElse(defaultHeadOffset - 1);
            var  sequentialStore = new SequentialStore <T>(entityStore, headOffset, tailOffset);

            return(sequentialStore);
        }