Beispiel #1
0
        public HashSet <string> TryGet(IEnumerable <string> keys, Action <string, Stream> streamReader)
        {
            var keysCopied = keys.ToArray();

            var request = new GetRequest {
                CorrelationId = _correlationId
            };

            request.Keys.AddRange(keysCopied);
            var results = Task.Run(async() => _clientConfig.UseStreaming
                ? await GetStream(request)
                : (await _lightClient.GetAsync(request)).Results
                                   ).GetAwaiter().GetResult();

            var ret = results.Where(res => res.Result == GetResponseEntry.Types.GetResult.Success).ToArray();

            foreach (var kv in ret)
            {
                streamReader(keysCopied[kv.Index], ValueCompressor.Decompress(kv.ValueMetadata.Compression, kv.Value.ToStream()));
            }

            return(ret.Select(kv => keysCopied[kv.Index]).ToHashSet());
        }
Beispiel #2
0
        private IEnumerable <string> TryAdd(IEnumerable <string> keys, Action <string, Stream> streamWriter, bool allowOverride,
                                            DateTimeOffset expiry, AvailabilityLevel requiredAvailabilityLevel)
        {
            // TODO: Add reactions to other AvailabilityLevels

            var preparedBatch = new List <AddRequestEntry>();

            foreach (var key in keys)
            {
                using (var valueStream = new MemoryStream())
                    using (var compressedStream = new LightWriteStream(ValueCompressor.Compress(_clientConfig.Compression, valueStream)))
                    {
                        streamWriter(key, compressedStream);
                        compressedStream.Flush();
                        valueStream.Position = 0;
                        var value = valueStream.ToByteArray();

                        var entry = new AddRequestEntry
                        {
                            Key           = key,
                            Expiry        = expiry.ToTimestamp(),
                            ValueMetadata = new ValueMetadata
                            {
                                Compression    = _clientConfig.Compression,
                                HashedWith     = _clientConfig.HashedWith,
                                Hash           = ByteString.CopyFrom(_valueHasher.ComputeHash(_clientConfig.HashedWith, value)),
                                SizeFull       = (uint)compressedStream.Position,
                                SizeCompressed = (uint)value.Length
                            },
                            Value = ByteString.CopyFrom(value)
                        };

                        preparedBatch.Add(entry);
                    }
            }

            var addResponse = Task.Run(async() =>
            {
                if (_clientConfig.UseStreaming)
                {
                    return(await AddStreaming(preparedBatch, allowOverride));
                }

                var request = new AddRequest
                {
                    Header = new Header
                    {
                        OverrideExisting = allowOverride,
                        CorrelationId    = _correlationId,
                        ChunksCount      = (uint)preparedBatch.Count
                    }
                };
                request.Entries.Add(preparedBatch);
                return(await _lightClient.AddAsync(request));
            }).GetAwaiter().GetResult();

            return(addResponse.Results.
                   Select((r, i) => (r, i)).
                   Where(res => res.Item1 == AddResponse.Types.AddResult.KeyAdded).
                   Select(res => preparedBatch[res.Item2].Key).ToHashSet()); // TODO: Reconfirm if successful or failed keys should be returned
        }