/// <summary>
        /// Return the only record in the result stream.
        /// </summary>
        /// <param name="result">The result stream</param>
        /// <typeparam name="TResult">The type of the returning value.</typeparam>
        /// <returns>The single element of the input sequence, or default(<paramref name="TResult">TResult</paramref> if the sequence contains no elements.</returns>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="result">result</paramref> is null.</exception>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="operation">operation</paramref> is null.</exception>
        /// <exception cref="T:System.InvalidOperationException">The input sequence contains more than one element.</exception>
        public static async Task <TResult> SingleOrDefaultAsync <TResult>(this IResultCursor result, Func <IRecord, TResult> operation)
        {
            if (result == null)
            {
                throw new ArgumentNullException(nameof(result));
            }

            if (operation == null)
            {
                throw new ArgumentNullException(nameof(operation));
            }

            if (!await result.FetchAsync().ConfigureAwait(false))
            {
                return(default(TResult));
            }

            var record = result.Current;

            if (!await result.FetchAsync().ConfigureAwait(false))
            {
                return(operation(record));
            }

            throw new InvalidOperationException("The result contains more than one element.");
        }
コード例 #2
0
        /// <summary>
        ///     Simplifies the <c>while</c>, <see cref="IResultCursor.FetchAsync" /> pairing, allowing a called to just use a
        ///     <see cref="foreach" />.
        /// </summary>
        /// <param name="cursor">The <see cref="IResultCursor" /> instance to read from.</param>
        /// <returns><c>yield</c>s the <see cref="IRecord" /> retrieved from the <paramref name="cursor" />.</returns>
        public static async IAsyncEnumerable <IRecord> GetRecords(this IResultCursor cursor)
        {
            Ensure.That(cursor).IsNotNull();

            var fetched = await cursor.FetchAsync();

            while (fetched)
            {
                yield return(cursor.Current);

                fetched = await cursor.FetchAsync();
            }
        }
コード例 #3
0
        /// <summary>
        ///     Simplifies the <c>while</c>, <see cref="IResultCursor.FetchAsync" /> pairing, allowing a called to just use a
        ///     <see cref="foreach" />.
        /// </summary>
        /// <remarks>
        ///     You will want <typeparamref name="T" /> to be one of the ones that the <see cref="IDriver" /> can handle, i.e.
        ///     <see cref="INode" /> etc.<br />
        ///     NB. If you want to pull more than just one property from the <paramref name="cursor" />, don't use this method, use
        ///     <see cref="GetRecords" /> instead.
        /// </remarks>
        /// <typeparam name="T">The <see cref="Type" /> to try to get the <paramref name="identifier" /> as.</typeparam>
        /// <param name="cursor">The <see cref="IResultCursor" /> instance to read from.</param>
        /// <param name="identifier">The identifier to pull out from.</param>
        /// <returns><c>yield</c>s the <typeparamref name="T" /> retrieved from the <paramref name="cursor" />.</returns>
        public static async IAsyncEnumerable <T> GetContent <T>(this IResultCursor cursor, string identifier)
        {
            Ensure.That(cursor).IsNotNull();
            Ensure.That(identifier).IsNotNullOrWhiteSpace();

            var fetched = await cursor.FetchAsync();

            while (fetched)
            {
                yield return(cursor.GetValue <T>(identifier));

                fetched = await cursor.FetchAsync();
            }
        }
コード例 #4
0
 public static async IAsyncEnumerable <IRecord> AsyncResults(this IResultCursor resultCursor)
 {
     while (await resultCursor.FetchAsync().ConfigureAwait(false))
     {
         yield return(resultCursor.Current);
     }
 }
コード例 #5
0
 public IEnumerable <IRecord> Records()
 {
     while (_executor.RunSync(() => _cursor.FetchAsync()))
     {
         yield return(_cursor.Current);
     }
 }
コード例 #6
0
        private static async Task AssertCannotAccessRecords(IResultCursor cursor)
        {
            await ThrowsResultConsumedException(async() => await cursor.FetchAsync());
            await ThrowsResultConsumedException(async() => await cursor.PeekAsync());

            ThrowsResultConsumedException(() => cursor.Current);
            await ThrowsResultConsumedException(async() => await cursor.SingleAsync());
            await ThrowsResultConsumedException(async() => await cursor.ToListAsync());
            await ThrowsResultConsumedException(async() => await cursor.ForEachAsync(r => { }));
        }
コード例 #7
0
 /// <summary>
 /// Return the only record in the result stream.
 /// </summary>
 /// <param name="result">The result stream</param>
 /// <param name="operation">The operation to carry out on each record.</param>
 /// <returns>The only record in the result stream.</returns>
 /// <remarks>Throws <exception cref="InvalidOperationException"></exception>
 /// if the result contains more than one record or the result is empty.</remarks>
 public static async Task <T> SingleAsync <T>(this IResultCursor result, Func <IRecord, T> operation)
 {
     Throw.ArgumentNullException.IfNull(result, nameof(result));
     if (await result.FetchAsync().ConfigureAwait(false))
     {
         var record = result.Current;
         if (!await result.FetchAsync().ConfigureAwait(false))
         {
             return(operation(record));
         }
         else
         {
             throw new InvalidOperationException("The result contains more than one element.");
         }
     }
     else
     {
         throw new InvalidOperationException("The result is empty.");
     }
 }
コード例 #8
0
        public static async Task <List <TReturn> > MapAsync <TReturn>(
            this IResultCursor resultCursor,
            Func <IRecord, TReturn> mapFunc)
        {
            var list = new List <TReturn>();

            while (await resultCursor.FetchAsync().ConfigureAwait(false))
            {
                list.Add(mapFunc(resultCursor.Current));
            }
            return(list);
        }
コード例 #9
0
        /// <summary>
        /// Pull all records in the result stream into memory and return in a list.
        /// </summary>
        /// <param name="result"> The result stream.</param>
        /// <returns>A list with all records in the result stream.</returns>
        public static async Task <List <IRecord> > ToListAsync(this IResultCursor result)
        {
            Throw.ArgumentNullException.IfNull(result, nameof(result));
            List <IRecord> list = new List <IRecord>();

            while (await result.FetchAsync().ConfigureAwait(false))
            {
                list.Add(result.Current);
            }

            return(list);
        }
コード例 #10
0
        /// <summary>
        /// Read each record in the result stream and apply the operation on each record.
        /// </summary>
        /// <param name="result">The result stream.</param>
        /// <param name="operation">The operation is carried out on each record.</param>
        /// <returns>The result summary after all records have been processed.</returns>
        public static async Task <IResultSummary> ForEachAsync(this IResultCursor result,
                                                               Action <IRecord> operation)
        {
            Throw.ArgumentNullException.IfNull(result, nameof(result));
            while (await result.FetchAsync().ConfigureAwait(false))
            {
                var record = result.Current;
                operation(record);
            }

            return(await result.ConsumeAsync().ConfigureAwait(false));
        }
コード例 #11
0
        /// <summary>
        /// Apply the operation on each record in the result stream and return the operation results in a list.
        /// </summary>
        /// <typeparam name="T">The return type of the list</typeparam>
        /// <param name="result">The result stream.</param>
        /// <param name="operation">The operation to carry out on each record.</param>
        /// <returns>A list of collected operation result.</returns>
        public static async Task <List <T> > ToListAsync <T>(this IResultCursor result, Func <IRecord, T> operation)
        {
            Throw.ArgumentNullException.IfNull(result, nameof(result));
            var list = new List <T>();

            while (await result.FetchAsync().ConfigureAwait(false))
            {
                var record = result.Current;
                list.Add(operation(record));
            }

            return(list);
        }
コード例 #12
0
        // Get records as a stream of buffered List
        public async IAsyncEnumerable <List <T> > FetchRecordsAsStream <T>(
            Func <IRecord, T> recordProcessor,
            string cypherQuery, object queryParams = null,
            long bufferSize = 100)
        {
            long recordsProcessed = 0;

            List <T>      resultBuffer = new List <T>();
            IAsyncSession session      = _driver.AsyncSession(o => o.WithDatabase(this._database));

            _logger.LogDebug($"Executing query: {cypherQuery}");

            if (queryParams == null)
            {
                queryParams = new {};
            }

            try
            {
                IResultCursor resultCursor = await session.RunAsync(cypherQuery, queryParams);

                _logger.LogDebug("Reading cursor");

                while (await resultCursor.FetchAsync())
                {
                    recordsProcessed += 1;

                    resultBuffer.Add(recordProcessor(resultCursor.Current));

                    if (resultBuffer.Count >= bufferSize)
                    {
                        _logger.LogDebug($"Records processed: {recordsProcessed} ...");
                        yield return(resultBuffer);

                        resultBuffer.Clear();
                    }
                }

                _logger.LogDebug($"* Total records processed: {recordsProcessed} *");
                yield return(resultBuffer);
            }
            finally
            {
                await session.CloseAsync();
            }
        }
コード例 #13
0
            public async Task TestDriverLifecycleExample()
            {
                // Given
                var driver = new DriverLifecycleExample(Uri, User, Password).Driver;
                var session = driver.AsyncSession();
                try
                {
                    // When & Then
                    IResultCursor result = await session.RunAsync("RETURN 1");

                    bool read = await result.FetchAsync();
                    read.Should().BeTrue();

                    result.Current[0].As<int>().Should().Be(1);
                }
                finally
                {
                    await session.CloseAsync();
                }
            }
コード例 #14
0
            public async Task TestCustomAuthExample()
            {
                // Given
                var driver = CreateDriverWithCustomizedAuth(Uri, User, Password, "native", "basic", null);
                var session = driver.AsyncSession();
                try
                {
                    // When & Then
                    IResultCursor result = await session.RunAsync("RETURN 1");

                    bool read = await result.FetchAsync();
                    read.Should().BeTrue();

                    result.Current[0].As<int>().Should().Be(1);
                }
                finally
                {
                    await session.CloseAsync();
                }
            }
コード例 #15
0
            public async Task TestConfigUnencryptedExample()
            {
                // Given
                var driver = CreateDriverWithCustomizedSecurityStrategy(Uri, User, Password);
                var session = driver.AsyncSession();
                try
                {
                    // When & Then
                    IResultCursor result = await session.RunAsync("RETURN 1");

                    bool read = await result.FetchAsync();
                    read.Should().BeTrue();

                    result.Current[0].As<int>().Should().Be(1);
                }
                finally
                {
                    await session.CloseAsync();
                }
            }