forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpanOrQuery.cs
More file actions
340 lines (298 loc) · 11.1 KB
/
Copy pathSpanOrQuery.cs
File metadata and controls
340 lines (298 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
using J2N.Collections.Generic.Extensions;
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
using System.Text;
using JCG = J2N.Collections.Generic;
namespace Lucene.Net.Search.Spans
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using AtomicReaderContext = Lucene.Net.Index.AtomicReaderContext;
using IBits = Lucene.Net.Util.IBits;
using IndexReader = Lucene.Net.Index.IndexReader;
using Term = Lucene.Net.Index.Term;
using TermContext = Lucene.Net.Index.TermContext;
using ToStringUtils = Lucene.Net.Util.ToStringUtils;
/// <summary>
/// Matches the union of its clauses. </summary>
public class SpanOrQuery : SpanQuery // LUCENENET specific: Not implementing ICloneable per Microsoft's recommendation
{
private readonly IList<SpanQuery> clauses;
private string field;
/// <summary>
/// Construct a <see cref="SpanOrQuery"/> merging the provided <paramref name="clauses"/>. </summary>
public SpanOrQuery(params SpanQuery[] clauses) : this((IList<SpanQuery>)clauses) { }
// LUCENENET specific overload.
// LUCENENET TODO: API - This constructor was added to eliminate casting with PayloadSpanUtil. Make public?
// It would be more useful if the type was an IEnumerable<SpanQuery>, but
// need to rework the allocation below. It would also be better to change AddClause() to Add() to make
// the C# collection initializer function.
internal SpanOrQuery(IList<SpanQuery> clauses)
{
// copy clauses array into an ArrayList
this.clauses = new JCG.List<SpanQuery>(clauses.Count);
for (int i = 0; i < clauses.Count; i++)
{
AddClause(clauses[i]);
}
}
/// <summary>
/// Adds a <paramref name="clause"/> to this query </summary>
public void AddClause(SpanQuery clause)
{
if (field is null)
{
field = clause.Field;
}
else if (clause.Field != null && !clause.Field.Equals(field, StringComparison.Ordinal))
{
throw new ArgumentException("Clauses must have same field.");
}
this.clauses.Add(clause);
}
/// <summary>
/// Return the clauses whose spans are matched. </summary>
public virtual SpanQuery[] GetClauses()
{
return clauses.ToArray();
}
public override string Field => field;
public override void ExtractTerms(ISet<Term> terms)
{
foreach (SpanQuery clause in clauses)
{
clause.ExtractTerms(terms);
}
}
public override object Clone()
{
int sz = clauses.Count;
SpanQuery[] newClauses = new SpanQuery[sz];
for (int i = 0; i < sz; i++)
{
newClauses[i] = (SpanQuery)clauses[i].Clone();
}
SpanOrQuery soq = new SpanOrQuery(newClauses);
soq.Boost = Boost;
return soq;
}
public override Query Rewrite(IndexReader reader)
{
SpanOrQuery clone = null;
for (int i = 0; i < clauses.Count; i++)
{
SpanQuery c = clauses[i];
SpanQuery query = (SpanQuery)c.Rewrite(reader);
if (query != c) // clause rewrote: must clone
{
if (clone is null)
{
clone = (SpanOrQuery)this.Clone();
}
clone.clauses[i] = query;
}
}
if (clone != null)
{
return clone; // some clauses rewrote
}
else
{
return this; // no clauses rewrote
}
}
public override string ToString(string field)
{
StringBuilder buffer = new StringBuilder();
buffer.Append("spanOr([");
bool first = true;
foreach (SpanQuery clause in clauses)
{
if (!first) buffer.Append(", ");
buffer.Append(clause.ToString(field));
first = false;
}
buffer.Append("])");
buffer.Append(ToStringUtils.Boost(Boost));
return buffer.ToString();
}
public override bool Equals(object o)
{
if (this == o)
{
return true;
}
if (o is null || this.GetType() != o.GetType())
{
return false;
}
SpanOrQuery that = (SpanOrQuery)o;
if (!clauses.Equals(that.clauses))
{
return false;
}
// LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled
return NumericUtils.SingleToSortableInt32(Boost) == NumericUtils.SingleToSortableInt32(that.Boost);
}
public override int GetHashCode()
{
//If this doesn't work, hash all elements together instead. This version was used to reduce time complexity
int h = clauses.GetHashCode();
h ^= (h << 10) | (h >>> 23);
h ^= J2N.BitConversion.SingleToRawInt32Bits(Boost);
return h;
}
private class SpanQueue : PriorityQueue<Spans>
{
public SpanQueue(int size)
: base(size)
{
}
protected internal override bool LessThan(Spans spans1, Spans spans2)
{
if (spans1.Doc == spans2.Doc)
{
if (spans1.Start == spans2.Start)
{
return spans1.End < spans2.End;
}
else
{
return spans1.Start < spans2.Start;
}
}
else
{
return spans1.Doc < spans2.Doc;
}
}
}
public override Spans GetSpans(AtomicReaderContext context, IBits acceptDocs, IDictionary<Term, TermContext> termContexts)
{
if (clauses.Count == 1) // optimize 1-clause case
{
return (clauses[0]).GetSpans(context, acceptDocs, termContexts);
}
return new SpansAnonymousClass(this, context, acceptDocs, termContexts);
}
private sealed class SpansAnonymousClass : Spans
{
private readonly SpanOrQuery outerInstance;
private readonly AtomicReaderContext context;
private readonly IBits acceptDocs;
private readonly IDictionary<Term, TermContext> termContexts;
public SpansAnonymousClass(SpanOrQuery outerInstance, AtomicReaderContext context, IBits acceptDocs, IDictionary<Term, TermContext> termContexts)
{
this.outerInstance = outerInstance;
this.context = context;
this.acceptDocs = acceptDocs;
this.termContexts = termContexts;
queue = null;
}
private SpanQueue queue;
private long cost;
private bool InitSpanQueue(int target)
{
queue = new SpanQueue(outerInstance.clauses.Count);
foreach (var clause in outerInstance.clauses)
{
Spans spans = clause.GetSpans(context, acceptDocs, termContexts);
cost += spans.GetCost();
if (((target == -1) && spans.MoveNext()) || ((target != -1) && spans.SkipTo(target)))
{
queue.Add(spans);
}
}
return queue.Count != 0;
}
public override bool MoveNext()
{
if (queue is null)
{
return InitSpanQueue(-1);
}
if (queue.Count == 0) // all done
{
return false;
}
if (Top.MoveNext()) // move to next
{
queue.UpdateTop();
return true;
}
queue.Pop(); // exhausted a clause
return queue.Count != 0;
}
private Spans Top => queue.Top;
public override bool SkipTo(int target)
{
if (queue is null)
{
return InitSpanQueue(target);
}
bool skipCalled = false;
while (queue.Count != 0 && Top.Doc < target)
{
if (Top.SkipTo(target))
{
queue.UpdateTop();
}
else
{
queue.Pop();
}
skipCalled = true;
}
if (skipCalled)
{
return queue.Count != 0;
}
return MoveNext();
}
public override int Doc => Top.Doc;
public override int Start => Top.Start;
public override int End => Top.End;
public override ICollection<byte[]> GetPayload()
{
JCG.List<byte[]> result = null;
Spans theTop = Top;
if (theTop != null && theTop.IsPayloadAvailable)
{
result = new JCG.List<byte[]>(theTop.GetPayload());
}
return result;
}
public override bool IsPayloadAvailable
{
get
{
Spans top = Top;
return top != null && top.IsPayloadAvailable;
}
}
public override string ToString()
{
return "spans(" + outerInstance + ")@" + ((queue is null) ? "START" : (queue.Count > 0 ? (Doc + ":" + Start + "-" + End) : "END"));
}
public override long GetCost()
{
return cost;
}
}
}
}