/// <summary> /// Determines whether the cursor returned by a cursor source contains any documents. /// </summary> /// <typeparam name="TDocument">The type of the document.</typeparam> /// <param name="source">The source.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A Task whose result is true if the cursor contains any documents.</returns> public static async Task <bool> AnyAsync <TDocument>(this IAsyncCursorSource <TDocument> source, CancellationToken cancellationToken = default(CancellationToken)) { using (var cursor = await source.ToCursorAsync(cancellationToken).ConfigureAwait(false)) { return(await cursor.AnyAsync(cancellationToken).ConfigureAwait(false)); } }
public static IAsyncEnumerable <TDocument> ToAsyncEnumerable <TDocument>( this IAsyncCursorSource <TDocument> source, CancellationToken cancellationToken = default(CancellationToken)) { return(AsyncEnumerable.Create( token => { IAsyncCursor <TDocument>?cursor = null; async ValueTask <bool> MoveNextAsync() { cursor ??= await source.ToCursorAsync(cancellationToken); return await cursor.MoveNextAsync(token); } return AsyncEnumerator.Create( MoveNextAsync, () => cursor?.Current ?? ImmutableList <TDocument> .Empty, () => { cursor?.Dispose(); return default; }); }) .SelectMany(x => x.ToAsyncEnumerable())); }
/// <summary> /// Returns the first document of a cursor returned by a cursor source. /// </summary> /// <typeparam name="TDocument">The type of the document.</typeparam> /// <param name="source">The source.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The first document.</returns> public static TDocument First <TDocument>(this IAsyncCursorSource <TDocument> source, CancellationToken cancellationToken = default(CancellationToken)) { using (var cursor = source.ToCursor(cancellationToken)) { return(cursor.First(cancellationToken)); } }
/// <summary> /// Converts the async cursor source to an async enumerable. /// </summary> /// <param name="source">The source to convert.</param> /// <param name="cancellationToken">The cancellation token to use to cancel the operation.</param> /// <typeparam name="TDocument">The type of the documents in the async cursor source.</typeparam> /// <returns>An <see cref="IAsyncEnumerable{TDocument}"/> to iterate over asynchronously.</returns> public static async IAsyncEnumerable <TDocument> ToAsyncEnumerable <TDocument>(this IAsyncCursorSource <TDocument> source, [EnumeratorCancellation] CancellationToken cancellationToken = default) { var cursor = await source.ToCursorAsync(cancellationToken).ConfigureAwait(false); try { while (await cursor.MoveNextAsync(cancellationToken).ConfigureAwait(false)) { foreach (var document in cursor.Current) { yield return(document); cancellationToken.ThrowIfCancellationRequested(); } } } finally { if (cursor is AsyncCursor <TDocument> asyncCursor) { await asyncCursor.CloseAsync(cancellationToken).ConfigureAwait(false); } cursor.Dispose(); } }
public static Task <Dictionary <TKey, TDocument> > ToDictionaryAsync <TDocument, TKey>( this IAsyncCursorSource <TDocument> source, Func <TDocument, TKey> keySelector, CancellationToken cancellationToken = default) where TKey : notnull { return(ToDictionaryAsync(source, keySelector, 0, cancellationToken)); }
public static async Task ForEachPipelineAsync <TDocument>(this IAsyncCursorSource <TDocument> source, Func <TDocument, Task> processor, CancellationToken cancellationToken = default) { using (var cursor = await source.ToCursorAsync(cancellationToken)) { await cursor.ForEachPipelineAsync(processor, cancellationToken); } }
public AsyncEnumeratorAdapter( IAsyncCursorSource <T> asyncCursorSource, CancellationToken cancellationToken) { _asyncCursorSource = asyncCursorSource; _cancellationToken = cancellationToken; }
/// <summary> /// Calls a delegate for each document returned by the cursor. /// </summary> /// <remarks> /// If your delegate is going to take a long time to execute or is going to block /// consider using a different overload of ForEachAsync that uses a delegate that /// returns a Task instead. /// </remarks> /// <typeparam name="TDocument">The type of the document.</typeparam> /// <param name="source">The source.</param> /// <param name="processor">The processor.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A Task that completes when all the documents have been processed.</returns> public static async Task ForEachAsync <TDocument>(this IAsyncCursorSource <TDocument> source, Action <TDocument, int> processor, CancellationToken cancellationToken = default(CancellationToken)) { using (var cursor = await source.ToCursorAsync(cancellationToken).ConfigureAwait(false)) { await cursor.ForEachAsync(processor, cancellationToken).ConfigureAwait(false); } }
/// <summary> /// Executes the query and returns the results as a streamed async enumeration. /// </summary> /// <typeparam name="T">The type of the returned entities.</typeparam> /// <param name="source">The query source.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param> /// <returns>The streamed async enumeration containing the results.</returns> internal static IAsyncEnumerable <T> ToAsyncEnumerable <T>(this IAsyncCursorSource <T> source, CancellationToken cancellationToken) { if (source == null) { throw new ArgumentNullException(nameof(source)); } return(ExecuteAsync(source, cancellationToken));
/// <summary> /// 获取给定的 <see cref="IAsyncCursorSource{BsonDocument"/> 异步游标所迭代的到的第一个 <see cref="BsonDocument"/> /// 对象,并将该对象反序列化为给定的实体对象类型。 /// </summary> /// <param name="source">包含数据的异步游标。</param> /// <typeparam name="TEntity">要转换到的实体对象类型。</typeparam> /// <returns> /// 给定的异步游标所迭代到的第一个实体对象。如果给定的异步游标无法迭代出任何对象,返回 /// <code>default(<typeparamref name="TEntity"/>)</code>。 /// </returns> public static async Task <TEntity> FirstEntityOrDefaultAsync <TEntity>( this IAsyncCursorSource <BsonDocument> source) { Contract.NotNull(source, nameof(source)); var bsonDoc = await source.FirstOrDefaultAsync(); if (Equals(bsonDoc, default(TEntity))) { return(default);
public async Task <BsonDocument> ReadVersionAsync(string symbol) { Log.Debug("reading {symbol} version", symbol); IAsyncCursorSource <BsonDocument> versions = this._versions.AsQueryable() .Where(x => x ["symbol"] == symbol) .OrderByDescending(x => x ["version"]) .Take(1); var rtn = await versions.FirstOrDefaultAsync <BsonDocument>(); Log.Debug("read {0} version: {1}".Args(symbol, rtn)); return(rtn); }
static async IAsyncEnumerable <T> ExecuteAsync(IAsyncCursorSource <T> source, [EnumeratorCancellation] CancellationToken cancellationToken) { using var cursor = await source.ToCursorAsync(); while (await cursor.MoveNextAsync(cancellationToken)) { foreach (var element in cursor.Current) { yield return(element); } } }
/// <summary> /// 获取给定的异步游标的所有数据,并将其从 BsonDocument 转换为相应的实体对象。 /// </summary> /// <param name="source">包含数据的异步游标。</param> /// <typeparam name="TEntity">实体对象类型。</typeparam> /// <returns>包含数据的实体对象列表。</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="source"/> 为 null。 /// </exception> public static async Task <List <TEntity> > ToEntityListAsync <TEntity>(this IAsyncCursorSource <BsonDocument> source) { Contract.NotNull(source, nameof(source)); var result = new List <TEntity>(); var cursor = await source.ToCursorAsync(); while (await cursor.MoveNextAsync()) { result.AddRange(cursor.Current.Select(doc => BsonSerializer.Deserialize <TEntity>(doc))); } return(result); }
/// <summary> /// 收集给定 <see cref="IAsyncCursor{T}"/> 中的数据到 <see cref="HashSet{T}"/> 中。 /// </summary> /// <param name="source">指向目标数据的 <see cref="IAsyncCursor{T}"/> 对象。</param> /// <typeparam name="T">目标数据类型。</typeparam> /// <returns>收集到的 <see cref="HashSet{T}"/> 对象。</returns> /// <exception cref="ArgumentNullException"><paramref name="source"/> 为 null。</exception> public static async Task <HashSet <T> > ToHashSetAsync <T>(this IAsyncCursorSource <T> source) { Contract.NotNull(source, nameof(source)); var set = new HashSet <T>(); var cursor = await source.ToCursorAsync(); while (await cursor.MoveNextAsync()) { foreach (var value in cursor.Current) { set.Add(value); } } return(set); }
public static async Task <Dictionary <TKey, TDocument> > ToDictionaryAsync <TDocument, TKey>( this IAsyncCursorSource <TDocument> source, Func <TDocument, TKey> keySelector, int capacity, CancellationToken cancellationToken = default) where TKey : notnull { var documents = new Dictionary <TKey, TDocument>(capacity); using IAsyncCursor <TDocument> cursor = await source.ToCursorAsync(cancellationToken).ConfigureAwait(false); while (await cursor.MoveNextAsync(cancellationToken).ConfigureAwait(false)) { foreach (TDocument document in cursor.Current) { documents.Add(keySelector(document), document); } } return(documents); }
/// <summary> /// Executes the query and returns the results as a streamed async enumeration. /// </summary> /// <typeparam name="T">The type of the returned entities.</typeparam> /// <param name="source">The query source.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param> /// <returns>The streamed async enumeration containing the results.</returns> internal static IAsyncEnumerable <T> ToAsyncEnumerable <T>([NotNull] this IAsyncCursorSource <T> source, CancellationToken cancellationToken) { if (source == null) { throw new ArgumentNullException(nameof(source)); } return(ExecuteAsync()); async IAsyncEnumerable <T> ExecuteAsync() { using var cursor = await source.ToCursorAsync(); while (await cursor.MoveNextAsync(cancellationToken)) { foreach (var element in cursor.Current) { yield return(element); } } } }
public IEnumerable <T> GetIterator(IAsyncCursorSource <BsonDocument> cursorSource, bool reset, Type targetType = null) { _finder = cursorSource; // if (_size == 0 || reset) // { // _size = _finder.Count(); // } GetCache(reset); if (_position == -1) { _position = 0; } //if (reset) cursor.Dispose(); if (_elementCache != null && _elementCache.Count() > _position) { while (_elementCache.Count > 0) { foreach (var element in _elementCache) { var deserialized = BsonSerializer.Deserialize <T>(element); base.FormatFields(deserialized as ExpandoObject); yield return(deserialized); Interlocked.Increment(ref _position); } GetCache(false); } ; //var element = _elementCache[_position]; //var output = BsonSerializer.Deserialize<ExpandoObject>(element); //return element; } else { //return null; } }
public static Task <IList <TDocument> > ToIListAsync <TDocument>(this IAsyncCursorSource <TDocument> source, CancellationToken cancellationToken = default) => source.ToListAsync(cancellationToken) .ContinueWith <IList <TDocument> >(q => q.Result, TaskContinuationOptions.ExecuteSynchronously);
public AsyncEnumerableAdapter(IAsyncCursorSource <T> asyncCursorSource) { _asyncCursorSource = asyncCursorSource; }
public static IAsyncEnumerable <T> ToAsyncEnumerable <T>( this IAsyncCursorSource <T> asyncCursorSource) => new AsyncEnumerableAdapter <T>(asyncCursorSource);
// constructors public AsyncCursorSourceEnumerableAdapter(IAsyncCursorSource <TDocument> source, CancellationToken cancellationToken) { _source = Ensure.IsNotNull(source, nameof(source)); _cancellationToken = cancellationToken; }
public static IAsyncEnumerable <T> ToAsyncEnumerable <T>(this IAsyncCursorSource <T> cursorSource) => cursorSource.ToCursorAsync().ToAsyncEnumerable();
/// <summary> /// Wraps a cursor source in an IEnumerable. Each time GetEnumerator is called a new cursor is fetched from the cursor source. /// </summary> /// <typeparam name="TDocument">The type of the document.</typeparam> /// <param name="source">The source.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>An IEnumerable.</returns> public static IEnumerable <TDocument> ToEnumerable <TDocument>(this IAsyncCursorSource <TDocument> source, CancellationToken cancellationToken = default(CancellationToken)) { return(new AsyncCursorSourceEnumerableAdapter <TDocument>(source, cancellationToken)); }
/// <summary> /// Goes thru each element in a sequence and applies processor /// </summary> public static Task ForEach <T>(this IAsyncCursorSource <T> source, Func <T, Task> processor) => source.ForEachAsync(processor);