AsyncCursorHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Linq;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. namespace MongoDB.Driver
  20. {
  21. internal static class AsyncCursorHelper
  22. {
  23. public async static Task<bool> AnyAsync<T>(Task<IAsyncCursor<T>> cursorTask, CancellationToken cancellationToken)
  24. {
  25. using (var cursor = await cursorTask.ConfigureAwait(false))
  26. {
  27. while (await cursor.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  28. {
  29. var current = cursor.Current;
  30. if (current.Any())
  31. {
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. }
  38. public async static Task<T> FirstAsync<T>(Task<IAsyncCursor<T>> cursorTask, CancellationToken cancellationToken)
  39. {
  40. using (var cursor = await cursorTask.ConfigureAwait(false))
  41. {
  42. while (await cursor.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  43. {
  44. var current = cursor.Current;
  45. if (current.Any())
  46. {
  47. return current.First();
  48. }
  49. }
  50. throw new InvalidOperationException("The source sequence is empty.");
  51. }
  52. }
  53. public async static Task<T> FirstOrDefaultAsync<T>(Task<IAsyncCursor<T>> cursorTask, CancellationToken cancellationToken)
  54. {
  55. using (var cursor = await cursorTask.ConfigureAwait(false))
  56. {
  57. while (await cursor.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  58. {
  59. var current = cursor.Current;
  60. if (current.Any())
  61. {
  62. return current.FirstOrDefault();
  63. }
  64. }
  65. return default(T);
  66. }
  67. }
  68. public async static Task<T> SingleAsync<T>(Task<IAsyncCursor<T>> cursorTask, CancellationToken cancellationToken)
  69. {
  70. using (var cursor = await cursorTask.ConfigureAwait(false))
  71. {
  72. while (await cursor.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  73. {
  74. var current = cursor.Current;
  75. if (current.Any())
  76. {
  77. return current.Single();
  78. }
  79. }
  80. throw new InvalidOperationException("The source sequence is empty.");
  81. }
  82. }
  83. public async static Task<T> SingleOrDefaultAsync<T>(Task<IAsyncCursor<T>> cursorTask, CancellationToken cancellationToken)
  84. {
  85. using (var cursor = await cursorTask.ConfigureAwait(false))
  86. {
  87. while (await cursor.MoveNextAsync(cancellationToken).ConfigureAwait(false))
  88. {
  89. var current = cursor.Current;
  90. if (current.Any())
  91. {
  92. return current.SingleOrDefault();
  93. }
  94. }
  95. return default(T);
  96. }
  97. }
  98. }
  99. }