5 // Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2010 Jérémie "Garuma" Laval
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Collections.Concurrent;
32 using System.Threading;
33 using System.Threading.Tasks;
35 namespace System.Linq.Parallel.QueryNodes
37 internal interface QueryStartNode : IVisitableNode {
41 internal class QueryStartNode<T> : QueryBaseNode<T>, QueryStartNode
43 readonly IEnumerable<T> source;
44 readonly Partitioner<T> customPartitioner;
46 internal QueryStartNode (IEnumerable<T> source)
49 throw new ArgumentNullException ("source");
54 internal QueryStartNode (Partitioner<T> custom)
57 throw new ArgumentNullException ("custom");
59 this.customPartitioner = custom;
62 // If possible, this property will return the number of element the query
63 // is going to process. If that number if pretty low, executing the query
64 // sequentially is better
70 ICollection coll = source as ICollection;
71 return coll == null ? -1 : coll.Count;
75 public override void Visit (INodeVisitor visitor)
77 visitor.Visit ((QueryStartNode)this);
80 internal override IEnumerable<T> GetSequential ()
85 return WrapHelper.Wrap (customPartitioner.GetPartitions (1))[0];
88 internal override IList<IEnumerable<T>> GetEnumerables (QueryOptions options)
90 if (customPartitioner != null) {
91 return WrapHelper.Wrap (customPartitioner.GetPartitions (options.PartitionCount));
94 Partitioner<T> partitioner
95 = (options.UseStrip) ? ParallelPartitioner.CreateForStrips (source, 1) : ParallelPartitioner.CreateBest (source);
97 return WrapHelper.Wrap (partitioner.GetPartitions (options.PartitionCount));
100 internal override IList<IEnumerable<KeyValuePair<long, T>>> GetOrderedEnumerables (QueryOptions options)
102 OrderablePartitioner<T> partitioner = null;
103 if (customPartitioner != null) {
104 partitioner = customPartitioner as OrderablePartitioner<T>;
105 if (partitioner == null)
106 throw new InvalidOperationException ("The partitionner you are using doesn't support ordered partitionning");
109 (options.UseStrip) ? ParallelPartitioner.CreateForStrips (source, 1) : ParallelPartitioner.CreateBest (source);
112 options.PartitionerSettings = Tuple.Create (partitioner.KeysOrderedAcrossPartitions,
113 partitioner.KeysOrderedInEachPartition,
114 partitioner.KeysNormalized);
116 // We only support one style of partitioning at the moment.
117 // Standard partitioners follow this style.
118 if (options.UseStrip && (!partitioner.KeysOrderedInEachPartition || partitioner.KeysOrderedAcrossPartitions))
119 throw new NotImplementedException ("Partitioner must have KeysOrderedInEachPartition "
120 + "and !KeysOrderedAcrossPartitions"
121 + "to be used with indexed operators");
123 return WrapHelper.Wrap (partitioner.GetOrderablePartitions (options.PartitionCount));