QuerySelectManyNode.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // QueryConcatNode.cs
  3. //
  4. // Author:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. //
  7. // Copyright (c) 2010 Jérémie "Garuma" Laval
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.Threading;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. namespace System.Linq.Parallel.QueryNodes
  31. {
  32. internal class QuerySelectManyNode<TSource, TCollection, TResult> : QueryStreamNode<TResult, TSource>
  33. {
  34. Func<TSource, IEnumerable<TCollection>> collectionSelector;
  35. Func<TSource, int, IEnumerable<TCollection>> collectionSelectorIndexed;
  36. Func<TSource, TCollection, TResult> resultSelector;
  37. internal QuerySelectManyNode (QueryBaseNode<TSource> parent,
  38. Func<TSource, int, IEnumerable<TCollection>> collectionSelectorIndexed,
  39. Func<TSource, TCollection, TResult> resultSelector)
  40. : base (parent, true)
  41. {
  42. this.collectionSelectorIndexed = collectionSelectorIndexed;
  43. this.resultSelector = resultSelector;
  44. }
  45. internal QuerySelectManyNode (QueryBaseNode<TSource> parent,
  46. Func<TSource, IEnumerable<TCollection>> collectionSelector,
  47. Func<TSource, TCollection, TResult> resultSelector)
  48. : base (parent, false)
  49. {
  50. this.collectionSelector = collectionSelector;
  51. this.resultSelector = resultSelector;
  52. }
  53. internal override IEnumerable<TResult> GetSequential ()
  54. {
  55. IEnumerable<TSource> source = Parent.GetSequential ();
  56. return IsIndexed ?
  57. source.SelectMany (collectionSelectorIndexed, resultSelector) :
  58. source.SelectMany (collectionSelector, resultSelector);
  59. }
  60. internal override IList<IEnumerable<TResult>> GetEnumerablesIndexed (QueryOptions options)
  61. {
  62. return Parent.GetOrderedEnumerables (options)
  63. .Select ((i) => GetEnumerableInternal (i,
  64. (kv) => collectionSelectorIndexed (kv.Value, (int)kv.Key),
  65. (e, c) => resultSelector (e.Value, c)))
  66. .ToList ();
  67. }
  68. internal override IList<IEnumerable<TResult>> GetEnumerablesNonIndexed (QueryOptions options)
  69. {
  70. return Parent.GetEnumerables (options)
  71. .Select ((i) => GetEnumerableInternal (i,
  72. collectionSelector,
  73. (e, c) => resultSelector (e, c)))
  74. .ToList ();
  75. }
  76. internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
  77. {
  78. var source = Parent.GetOrderedEnumerables (options);
  79. var sizeRequests = new SizeRequest[source.Count];
  80. long deviation = 0;
  81. Barrier barrier = new Barrier (source.Count, delegate (Barrier b) {
  82. Array.Sort (sizeRequests, KeyComparator);
  83. for (int i = 0; i < b.ParticipantCount; ++i) {
  84. if (sizeRequests[i].Key == -1)
  85. continue;
  86. sizeRequests[i].Key = deviation;
  87. deviation += sizeRequests[i].Size;
  88. }
  89. });
  90. return source
  91. .Select ((i, ind) => GetOrderedEnumerableInternal (i, sizeRequests, ind, barrier))
  92. .ToList ();
  93. }
  94. IEnumerable<TResult> GetEnumerableInternal<T> (IEnumerable<T> source,
  95. Func<T, IEnumerable<TCollection>> collectionner,
  96. Func<T, TCollection, TResult> packer)
  97. {
  98. foreach (T element in source)
  99. foreach (TCollection item in collectionner (element))
  100. yield return packer (element, item);
  101. }
  102. IEnumerable<KeyValuePair<long, TResult>> GetOrderedEnumerableInternal (IEnumerable<KeyValuePair<long, TSource>> source,
  103. SizeRequest[] sizeRequests,
  104. int index,
  105. Barrier barrier)
  106. {
  107. IEnumerator<KeyValuePair<long, TSource>> enumerator = source.GetEnumerator ();
  108. bool isIndexed = IsIndexed;
  109. try {
  110. while (true) {
  111. KeyValuePair<long, TSource> element;
  112. IEnumerable<TCollection> collection;
  113. if (enumerator.MoveNext ()) {
  114. element = enumerator.Current;
  115. collection = isIndexed ?
  116. collectionSelectorIndexed (element.Value, (int)element.Key) :
  117. collectionSelector (element.Value);
  118. var count = GetCount (ref collection);
  119. sizeRequests[index].Update (element.Key, count, collection, element.Value);
  120. }
  121. barrier.SignalAndWait ();
  122. long i = sizeRequests[index].Key;
  123. collection = sizeRequests[index].Collection;
  124. var elementValue = sizeRequests[index].ElementValue;
  125. sizeRequests[index].Clear ();
  126. if (i == -1)
  127. break;
  128. foreach (TCollection item in collection)
  129. yield return new KeyValuePair<long, TResult> (i++, resultSelector (elementValue, item));
  130. }
  131. } finally {
  132. barrier.RemoveParticipant ();
  133. enumerator.Dispose ();
  134. }
  135. }
  136. /* If getting Count is a O(1) operation (i.e. actual is a ICollection) then return it immediatly
  137. * if not process the IEnumerable into a List and return the Count from that (i.e. enumerable
  138. * processing will only happen once in case of e.g. a Linq query)
  139. */
  140. static int GetCount<T> (ref IEnumerable<T> actual)
  141. {
  142. ICollection coll = actual as ICollection;
  143. if (coll != null)
  144. return coll.Count;
  145. var foo = actual.ToList ();
  146. actual = foo;
  147. return foo.Count;
  148. }
  149. static int KeyComparator (SizeRequest e1, SizeRequest e2)
  150. {
  151. return e1.Key.CompareTo (e2.Key);
  152. }
  153. struct SizeRequest
  154. {
  155. public long Key;
  156. public int Size;
  157. public IEnumerable<TCollection> Collection;
  158. public TSource ElementValue;
  159. public void Update (long k, int s, IEnumerable<TCollection> c, TSource ev)
  160. {
  161. Key = k;
  162. Size = s;
  163. Collection = c;
  164. ElementValue = ev;
  165. }
  166. public void Clear ()
  167. {
  168. Key = -1;
  169. Size = 0;
  170. Collection = null;
  171. ElementValue = default (TSource);
  172. }
  173. }
  174. }
  175. }