Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FullOuterJoinedSource.cs
1using System;
3using System.Threading.Tasks;
9
11{
16 {
25 : base(Left, Right, Conditions)
26 {
27 }
28
40 public override async Task<IResultSetEnumerator> Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables,
41 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
42 {
43 ScriptNode LeftWhere = await Reduce(this.Left, Where);
44 KeyValuePair<VariableReference, bool>[] LeftOrder = await Reduce(this.Left, Order);
45
46 IResultSetEnumerator LeftEnum = await this.Left.Find(0, int.MaxValue, Generic, LeftWhere, Variables, LeftOrder, Node);
47
48 ScriptNode RightWhere = await Reduce(this.Right, this.Left, Where);
49
50 LeftEnum = new LeftOuterJoinedSource.LeftOuterJoinEnumerator(LeftEnum, this.Left.Name, this.Right, this.Right.Name, Generic,
51 this.Combine(RightWhere, this.Conditions), Variables, false);
52
53 ScriptNode RightWhere2 = await Reduce(this.Right, Where);
54 KeyValuePair<VariableReference, bool>[] RightOrder2 = await Reduce(this.Right, Order);
55
56 IResultSetEnumerator RightEnum = await this.Right.Find(0, int.MaxValue, Generic, RightWhere2, Variables, RightOrder2, Node);
57
58 ScriptNode LeftWhere2 = await Reduce(this.Left, this.Right, Where);
59
60 RightEnum = new LeftOuterJoinedSource.LeftOuterJoinEnumerator(RightEnum, this.Right.Name, this.Left, this.Left.Name, Generic,
61 this.Combine(LeftWhere2, this.Conditions), Variables, true);
62
63 IResultSetEnumerator e = new FullOuterJoinEnumerator(LeftEnum, RightEnum);
64
65 if (!(Where is null))
66 e = new ConditionalEnumerator(e, Variables, Where);
67
68 if (Offset > 0)
69 e = new OffsetEnumerator(e, Offset);
70
71 if (Top != int.MaxValue)
72 e = new MaxCountEnumerator(e, Top);
73
74 return e;
75 }
76
77 private class FullOuterJoinEnumerator : IResultSetEnumerator
78 {
79 private readonly Dictionary<object, bool> reportedLeft = new Dictionary<object, bool>();
80 private readonly IResultSetEnumerator leftEnum;
81 private readonly IResultSetEnumerator rightEnum;
82 private object current;
83 private bool leftMode;
84
85 public FullOuterJoinEnumerator(IResultSetEnumerator Left, IResultSetEnumerator Right)
86 {
87 this.leftEnum = Left;
88 this.rightEnum = Right;
89 this.leftMode = true;
90 this.current = null;
91 }
92
93 public object Current => this.current;
94
95 public void Dispose()
96 {
97 this.leftEnum.Dispose();
98 this.rightEnum.Dispose();
99 }
100
101 public bool MoveNext()
102 {
103 return this.MoveNextAsync().Result;
104 }
105
106 public async Task<bool> MoveNextAsync()
107 {
108 if (this.leftMode)
109 {
110 if (await this.leftEnum.MoveNextAsync())
111 {
112 this.current = this.leftEnum.Current;
113 this.reportedLeft[this.current] = true;
114 return true;
115 }
116 else
117 this.leftMode = false;
118 }
119
120 do
121 {
122 if (await this.rightEnum.MoveNextAsync())
123 this.current = this.rightEnum.Current;
124 else
125 return false;
126 }
127 while (this.reportedLeft.ContainsKey(this.current));
128
129 return true;
130 }
131
132 public void Reset()
133 {
134 this.reportedLeft.Clear();
135 this.current = null;
136 this.leftMode = true;
137 this.leftEnum.Reset();
138 this.rightEnum.Reset();
139 }
140 }
141
155 public override async Task<bool> Process(IProcessor<object> Processor, int Offset, int Top, bool Generic,
156 ScriptNode Where, Variables Variables, KeyValuePair<VariableReference, bool>[] Order,
157 ScriptNode Node)
158 {
159 ScriptNode LeftWhere = await Reduce(this.Left, Where);
160 KeyValuePair<VariableReference, bool>[] LeftOrder = await Reduce(this.Left, Order);
161
162 ScriptNode RightWhere = await Reduce(this.Right, this.Left, Where);
163 RightWhere = this.Combine(RightWhere, this.Conditions);
164
165 if (Top != int.MaxValue)
166 Processor = new MaxCountProcessor(Processor, Top);
167
168 if (Offset > 0)
169 Processor = new OffsetProcessor(Processor, Offset);
170
171 if (!(Where is null))
172 Processor = new ConditionalProcessor(Processor, Variables, Where);
173
174 FullOuterJoinProcessor OuterProcessor = new FullOuterJoinProcessor(Processor);
175
176 LeftOuterJoinedSource.OuterJoinLeftProcessor LeftProcessor =
177 new LeftOuterJoinedSource.OuterJoinLeftProcessor(OuterProcessor, this.Left.Name,
178 this.Right, this.Right.Name, Generic, RightWhere, Variables, false);
179
180 if (!await this.Left.Process(LeftProcessor, 0, int.MaxValue, Generic,
181 LeftWhere, Variables, LeftOrder, Node))
182 {
183 return false;
184 }
185
186 RightWhere = await Reduce(this.Right, Where);
187 KeyValuePair<VariableReference, bool>[] RightOrder = await Reduce(this.Right, Order);
188
189 LeftWhere = await Reduce(this.Left, this.Right, Where);
190 LeftWhere = this.Combine(LeftWhere, this.Conditions);
191
192 LeftOuterJoinedSource.OuterJoinLeftProcessor RightProcessor =
194 this.Left, this.Left.Name, Generic, LeftWhere, Variables, true);
195
196 OuterProcessor.LeftMode = false;
197
198 if (!await this.Right.Process(RightProcessor, 0, int.MaxValue, Generic,
199 RightWhere, Variables, RightOrder, Node))
200 {
201 return false;
202 }
203
204 return true;
205 }
206
207 private class FullOuterJoinProcessor : IProcessor<object>
208 {
209 private readonly Dictionary<object, bool> reportedLeft = new Dictionary<object, bool>();
210 private readonly IProcessor<object> processor;
211
212 public FullOuterJoinProcessor(IProcessor<object> Processor)
213 {
214 this.processor = Processor;
215 }
216
217 public bool LeftMode { get; set; } = true;
218 public bool IsAsynchronous => this.processor.IsAsynchronous;
219 public bool Flush() => this.processor.Flush();
220 public Task<bool> FlushAsync() => this.processor.FlushAsync();
221
222 public bool Process(object Object)
223 {
224 if (this.LeftMode)
225 {
226 this.reportedLeft[Object] = true;
227 return this.processor.Process(Object);
228 }
229 else
230 {
231 if (!this.reportedLeft.ContainsKey(Object))
232 return this.processor.Process(Object);
233
234 return true;
235 }
236 }
237
238 public async Task<bool> ProcessAsync(object Object)
239 {
240 if (this.LeftMode)
241 {
242 this.reportedLeft[Object] = true;
243 return await this.processor.ProcessAsync(Object);
244 }
245 else
246 {
247 if (!this.reportedLeft.ContainsKey(Object))
248 return await this.processor.ProcessAsync(Object);
249
250 return true;
251 }
252 }
253 }
254
255 }
256}
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
Enumerator that only returns elements matching a set of conditions.
Enumerator that limits the return set to a maximum number of records.
Enumerator that skips a given number of result records.
Processor that only processes elements matching a set of conditions.
Processor that limits the return set to a maximum number of records.
Processor that skips a given number of result records.
Data source formed through an (FULL [OUTER]|OUTER) JOIN of two sources.
override async Task< bool > Process(IProcessor< object > Processor, int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Processes objects matching filter conditions in Where .
override async Task< IResultSetEnumerator > Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Finds objects matching filter conditions in Where .
FullOuterJoinedSource(IDataSource Left, IDataSource Right, ScriptNode Conditions)
Data source formed through an (FULL [OUTER]|OUTER) JOIN of two sources.
Abstract base classes of joined sources.
Definition: JoinedSource.cs:15
ScriptNode Conditions
Conditions for join.
Definition: JoinedSource.cs:46
ScriptNode Combine(ScriptNode Where, ScriptNode On)
Combines one or two restrictions.
static async Task< ScriptNode > Reduce(IDataSource Source, ScriptNode Where)
Reduces a where clause to fit the current data source.
Data source formed through an LEFT [OUTER] JOIN of two sources.
Collection of variables.
Definition: Variables.cs:25
Interface for processors of objects.
Definition: IProcessor.cs:9
bool IsAsynchronous
If the processor operates asynchronously.
Definition: IProcessor.cs:13
Interface for data sources that can be used in SQL statements.
Definition: IDataSource.cs:14
Task< bool > Process(IProcessor< object > Processor, int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Processes objects matching filter conditions in Where .
Task< IResultSetEnumerator > Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Finds objects matching filter conditions in Where .
string Name
Collection name or alias.
Definition: IDataSource.cs:94