BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / System.Core / System.Linq.Parallel.QueryNodes / QueryJoinNode.cs
1 //
2 // QueryJoinNode.cs
3 //
4 // Author:
5 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
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
27 #if NET_4_0
28 using System;
29 using System.Threading;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Collections.Concurrent;
33
34 namespace System.Linq.Parallel.QueryNodes
35 {
36         internal class QueryJoinNode<TFirst, TSecond, TKey, TResult> : QueryMuxNode<TFirst, TSecond, TResult>
37         {
38                 struct VSlot<T>
39                 {
40                         public readonly bool HasValue;
41                         public readonly T Value;
42
43                         public VSlot (T value)
44                         {
45                                 HasValue = true;
46                                 Value = value;
47                         }
48                 }
49
50                 Func<TFirst, TKey> firstKeySelector;
51                 Func<TSecond, TKey> secondKeySelector;
52                 Func<TFirst, TSecond, TResult> resultSelector;
53                 IEqualityComparer<TKey> comparer;
54
55                 internal QueryJoinNode (QueryBaseNode<TFirst> first,
56                                         QueryBaseNode<TSecond> second, 
57                                         Func<TFirst, TKey> firstKeySelector,
58                                         Func<TSecond, TKey> secondKeySelector,
59                                         Func<TFirst, TSecond, TResult> resultSelector,
60                                         IEqualityComparer<TKey> comparer) : base (first, second)
61                 {
62                         this.firstKeySelector = firstKeySelector;
63                         this.secondKeySelector = secondKeySelector;
64                         this.resultSelector = resultSelector;
65                         this.comparer = comparer;
66                 }
67
68                 internal override IEnumerable<TResult> GetSequential ()
69                 {
70                         return Parent.GetSequential ().Join (Second.GetSequential (), 
71                                                              firstKeySelector, 
72                                                              secondKeySelector, 
73                                                              resultSelector, 
74                                                              comparer);
75                 }
76
77                 internal override IList<IEnumerable<TResult>> GetEnumerables (QueryOptions options)
78                 {
79                         var first = Parent.GetEnumerables (options);
80                         var second = Second.GetEnumerables (options);
81
82                         if (first.Count != second.Count)
83                                 throw new InvalidOperationException ("Internal size mismatch");
84
85                         var store = new ConcurrentDictionary<TKey, Tuple<VSlot<TFirst>, VSlot<TSecond>>> (comparer);
86
87                         return first
88                                 .Select ((f, i) => GetEnumerable (f, second[i], store, firstKeySelector, secondKeySelector, resultSelector))
89                                 .ToList ();
90
91                 }               
92
93                 internal override IList<IEnumerable<KeyValuePair<long, TResult>>> GetOrderedEnumerables (QueryOptions options)
94                 {
95                         var first = Parent.GetOrderedEnumerables (options);
96                         var second = Second.GetOrderedEnumerables (options);
97
98                         if (first.Count != second.Count)
99                                 throw new InvalidOperationException ("Internal size mismatch");
100
101                         var store = new ConcurrentDictionary<TKey, Tuple<VSlot<KeyValuePair<long, TFirst>>, VSlot<KeyValuePair<long, TSecond>>>> (comparer);
102                         
103                         return first
104                                 .Select ((f, i) => GetEnumerable<KeyValuePair<long, TFirst>, KeyValuePair<long, TSecond>, KeyValuePair<long, TResult>> (f, 
105                                                                   second[i], 
106                                                                   store,
107                                                                   (e) => firstKeySelector (e.Value),
108                                                                   (e) => secondKeySelector (e.Value),
109                                                                   (e1, e2) => new KeyValuePair<long, TResult> (e1.Key, resultSelector (e1.Value, e2.Value))))
110                                 .ToList ();
111                 }
112
113                 IEnumerable<T> GetEnumerable<U, V, T> (IEnumerable<U> first, 
114                                                        IEnumerable<V> second,
115                                                        ConcurrentDictionary<TKey, Tuple<VSlot<U>, VSlot<V>>> store,
116                                                        Func<U, TKey> fKeySelect,
117                                                        Func<V, TKey> sKeySelect,
118                                                        Func<U, V, T> resultor)
119                 {
120                         IEnumerator<U> eFirst = first.GetEnumerator ();
121                         IEnumerator<V> eSecond = second.GetEnumerator ();
122
123                         try {
124                                 while (eFirst.MoveNext ()) {
125                                         if (!eSecond.MoveNext ())
126                                                 yield break;
127
128                                         U e1 = eFirst.Current;
129                                         V e2 = eSecond.Current;
130
131                                         TKey key1 = fKeySelect (e1);
132                                         TKey key2 = sKeySelect (e2);
133
134                                         if (comparer.Equals (key1, key2)) {
135                                                 yield return resultor (e1, e2);
136                                                 continue;
137                                         }
138                                         
139                                         Tuple<VSlot<U>, VSlot<V>> kvp;
140                                         
141                                         do {
142                                                 if (store.TryRemove (key1, out kvp) && kvp.Item2.HasValue) {
143                                                         yield return resultor (e1, kvp.Item2.Value);
144                                                         break;
145                                                 }
146                                         } while (!store.TryAdd (key1, Tuple.Create (new VSlot<U> (e1), new VSlot<V> ())));
147                                                         
148                                         do {
149                                                 if (store.TryRemove (key2, out kvp) && kvp.Item1.HasValue) {
150                                                         yield return resultor (kvp.Item1.Value, e2);
151                                                         break;
152                                                 }
153                                         } while (!store.TryAdd (key2, Tuple.Create (new VSlot<U> (), new VSlot<V> (e2))));
154                                 }
155                         } finally {
156                                 eFirst.Dispose ();
157                                 eSecond.Dispose ();
158                         }
159                 }
160         }
161 }
162
163 #endif