Ejemplo n.º 1
0
        public async Task <TEntity> SingleOrDefaultAsync(CancellationToken cancellationToken)
        {
            return(await Task.Run(() =>
            {
                cancellationToken.ThrowIfCancellationRequested();

                var scan = PrepareScan();
                var client = _pool.GetClient();
                var scanner = client.scannerOpenWithScan(_tableName, scan, null);
                var data = client.scannerGetList(scanner, _limit);
                _pool.ReleaseClient(client);
                return data;
            }, cancellationToken)
                   .ContinueWith(x =>
            {
                cancellationToken.ThrowIfCancellationRequested();

                if (x.Result.Count > 1)
                {
                    throw new CommandException($"Expected 1 result, got {x.Result.Count}");
                }
                if (x.Result.Count == 0)
                {
                    return default(TEntity);
                }
                var generator = new EntityGenerator <TEntity>(_map);

                return generator.BuildEntity(x.Result.Single());
            }, cancellationToken));
        }
Ejemplo n.º 2
0
        public async Task <TEntity> SingleOrDefaultAsync(CancellationToken cancellationToken)
        {
            // Cannot spawn an IO-Bound task because internal thrift limitation (does not expose any IO bound methods)
            return(await Task.Run(() =>
            {
                cancellationToken.ThrowIfCancellationRequested();
                var cols = ConvertToByteArrays(_columns);
                var rows = ConvertToByteArrays(_rows);
                var client = _pool.GetClient();
                var data = client.getRowsWithColumns(_tableName, rows.Count > 0 ? rows : null,
                                                     cols.Count > 0 ? cols : null, null);
                _pool.ReleaseClient(client);
                return data;
            }, cancellationToken)
                   .ContinueWith(x =>
            {
                cancellationToken.ThrowIfCancellationRequested();

                if (x.Result.Count > 1)
                {
                    throw new CommandException($"Expected 1 result, got {x.Result.Count}");
                }
                if (x.Result.Count == 0)
                {
                    return default(TEntity);
                }
                var generator = new EntityGenerator <TEntity>(_map);

                return generator.BuildEntity(x.Result.Single());
            }, cancellationToken));
        }
Ejemplo n.º 3
0
        public TEntity SingleOrDefault()
        {
            var result = ExtractResult();

            if (result.Count > 1)
            {
                throw new CommandException($"Expected 1 result, got {result.Count}");
            }
            if (result.Count == 0)
            {
                return(default(TEntity));
            }

            var generator = new EntityGenerator <TEntity>(_map);

            return(generator.BuildEntity(result.Single()));
        }