forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockJoinComparatorSource.cs
More file actions
270 lines (243 loc) · 11.1 KB
/
Copy pathBlockJoinComparatorSource.cs
File metadata and controls
270 lines (243 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
using Lucene.Net.Search;
using Lucene.Net.Util;
using System;
using System.IO;
using System.Threading;
namespace Lucene.Net.Index.Sorter
{
/*
* 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.
*/
/// <summary>
/// Helper class to sort readers that contain blocks of documents.
/// <para>
/// Note that this class is intended to used with <see cref="SortingMergePolicy"/>,
/// and for other purposes has some limitations:
/// <list type="bullet">
/// <item><description>Cannot yet be used with <see cref="IndexSearcher.SearchAfter(ScoreDoc, Query, Filter, int, Sort, CancellationToken)">
/// IndexSearcher.SearchAfter</see></description></item>
/// <item><description>Filling sort field values is not yet supported.</description></item>
/// </list>
/// @lucene.experimental
/// </para>
/// </summary>
// TODO: can/should we clean this thing up (e.g. return a proper sort value)
// and move to the join/ module?
public class BlockJoinComparerSource : FieldComparerSource
{
internal readonly Filter parentsFilter;
internal readonly Sort parentSort;
internal readonly Sort childSort;
/// <summary>
/// Create a new BlockJoinComparerSource, sorting only blocks of documents
/// with <paramref name="parentSort"/> and not reordering children with a block.
/// </summary>
/// <param name="parentsFilter"> Filter identifying parent documents </param>
/// <param name="parentSort"> Sort for parent documents </param>
public BlockJoinComparerSource(Filter parentsFilter, Sort parentSort)
: this(parentsFilter, parentSort, new Sort(SortField.FIELD_DOC))
{
}
/// <summary>
/// Create a new BlockJoinComparerSource, specifying the sort order for both
/// blocks of documents and children within a block.
/// </summary>
/// <param name="parentsFilter"> Filter identifying parent documents </param>
/// <param name="parentSort"> Sort for parent documents </param>
/// <param name="childSort"> Sort for child documents in the same block </param>
public BlockJoinComparerSource(Filter parentsFilter, Sort parentSort, Sort childSort)
{
this.parentsFilter = parentsFilter;
this.parentSort = parentSort;
this.childSort = childSort;
}
public override FieldComparer NewComparer(string fieldname, int numHits, int sortPos, bool reversed)
{
// we keep parallel slots: the parent ids and the child ids
int[] parentSlots = new int[numHits];
int[] childSlots = new int[numHits];
SortField[] parentFields = parentSort.GetSort();
int[] parentReverseMul = new int[parentFields.Length];
FieldComparer[] parentComparers = new FieldComparer[parentFields.Length];
for (int i = 0; i < parentFields.Length; i++)
{
parentReverseMul[i] = parentFields[i].IsReverse ? -1 : 1;
parentComparers[i] = parentFields[i].GetComparer(1, i);
}
SortField[] childFields = childSort.GetSort();
int[] childReverseMul = new int[childFields.Length];
FieldComparer[] childComparers = new FieldComparer[childFields.Length];
for (int i = 0; i < childFields.Length; i++)
{
childReverseMul[i] = childFields[i].IsReverse ? -1 : 1;
childComparers[i] = childFields[i].GetComparer(1, i);
}
// NOTE: we could return parent ID as value but really our sort "value" is more complex...
// So we throw UOE for now. At the moment you really should only use this at indexing time.
return new FieldComparerAnonymousClass(this, parentSlots,
childSlots, parentReverseMul, parentComparers, childReverseMul, childComparers);
}
private sealed class FieldComparerAnonymousClass : FieldComparer<J2N.Numerics.Int32>
{
private readonly BlockJoinComparerSource outerInstance;
private readonly int[] parentSlots;
private readonly int[] childSlots;
private readonly int[] parentReverseMul;
private readonly FieldComparer[] parentComparers;
private readonly int[] childReverseMul;
private readonly FieldComparer[] childComparers;
public FieldComparerAnonymousClass(BlockJoinComparerSource outerInstance,
int[] parentSlots, int[] childSlots, int[] parentReverseMul, FieldComparer[] parentComparers,
int[] childReverseMul, FieldComparer[] childComparers)
{
this.outerInstance = outerInstance;
this.parentSlots = parentSlots;
this.childSlots = childSlots;
this.parentReverseMul = parentReverseMul;
this.parentComparers = parentComparers;
this.childReverseMul = childReverseMul;
this.childComparers = childComparers;
}
internal int bottomParent;
internal int bottomChild;
internal FixedBitSet parentBits;
public override int Compare(int slot1, int slot2)
{
try
{
return Compare(childSlots[slot1], parentSlots[slot1], childSlots[slot2], parentSlots[slot2]);
}
catch (Exception e) when (e.IsIOException())
{
throw RuntimeException.Create(e);
}
}
public override void SetBottom(int slot)
{
bottomParent = parentSlots[slot];
bottomChild = childSlots[slot];
}
public override void SetTopValue(J2N.Numerics.Int32 value)
{
// we dont have enough information (the docid is needed)
throw UnsupportedOperationException.Create("this comparer cannot be used with deep paging");
}
public override int CompareBottom(int doc)
{
return Compare(bottomChild, bottomParent, doc, Parent(doc));
}
public override int CompareTop(int doc)
{
// we dont have enough information (the docid is needed)
throw UnsupportedOperationException.Create("this comparer cannot be used with deep paging");
}
public override void Copy(int slot, int doc)
{
childSlots[slot] = doc;
parentSlots[slot] = Parent(doc);
}
public override FieldComparer SetNextReader(AtomicReaderContext context)
{
DocIdSet parents = outerInstance.parentsFilter.GetDocIdSet(context, null);
if (parents is null)
{
throw IllegalStateException.Create("AtomicReader " + context.AtomicReader + " contains no parents!");
}
if (parents is not FixedBitSet fixedBitSet)
{
throw IllegalStateException.Create("parentFilter must return FixedBitSet; got " + parents);
}
parentBits = fixedBitSet;
for (int i = 0; i < parentComparers.Length; i++)
{
parentComparers[i] = parentComparers[i].SetNextReader(context);
}
for (int i = 0; i < childComparers.Length; i++)
{
childComparers[i] = childComparers[i].SetNextReader(context);
}
return this;
}
// LUCENENET NOTE: This was value(int) in Lucene.
public override J2N.Numerics.Int32 this[int slot] => throw
// really our sort "value" is more complex...
UnsupportedOperationException.Create("filling sort field values is not yet supported");
public override void SetScorer(Scorer scorer)
{
base.SetScorer(scorer);
foreach (FieldComparer comp in parentComparers)
{
comp.SetScorer(scorer);
}
foreach (FieldComparer comp in childComparers)
{
comp.SetScorer(scorer);
}
}
internal int Parent(int doc)
{
return parentBits.NextSetBit(doc);
}
internal int Compare(int docID1, int parent1, int docID2, int parent2)
{
if (parent1 == parent2) // both are in the same block
{
if (docID1 == parent1 || docID2 == parent2)
{
// keep parents at the end of blocks
return docID1 - docID2;
}
else
{
return Compare(docID1, docID2, childComparers, childReverseMul);
}
}
else
{
int cmp = Compare(parent1, parent2, parentComparers, parentReverseMul);
if (cmp == 0)
{
return parent1 - parent2;
}
else
{
return cmp;
}
}
}
internal static int Compare(int docID1, int docID2, FieldComparer[] comparers, int[] reverseMul) // LUCENENET: CA1822: Mark members as static
{
for (int i = 0; i < comparers.Length; i++)
{
// TODO: would be better if copy() didn't cause a term lookup in TermOrdVal & co,
// the segments are always the same here...
comparers[i].Copy(0, docID1);
comparers[i].SetBottom(0);
int comp = reverseMul[i] * comparers[i].CompareBottom(docID2);
if (comp != 0)
{
return comp;
}
}
return 0; // no need to docid tiebreak
}
}
public override string ToString()
{
return "blockJoin(parentSort=" + parentSort + ",childSort=" + childSort + ")";
}
}
}