-
Notifications
You must be signed in to change notification settings - Fork 660
Expand file tree
/
Copy pathAbstractAppendingLongBuffer.cs
More file actions
291 lines (257 loc) · 10.3 KB
/
Copy pathAbstractAppendingLongBuffer.cs
File metadata and controls
291 lines (257 loc) · 10.3 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
using Lucene.Net.Diagnostics;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Lucene.Net.Util.Packed
{
/*
* 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>
/// Common functionality shared by <see cref="AppendingDeltaPackedInt64Buffer"/> and <see cref="MonotonicAppendingInt64Buffer"/>.
/// <para/>
/// NOTE: This was AbstractAppendingLongBuffer in Lucene
/// </summary>
public abstract class AbstractAppendingInt64Buffer : Int64Values, // LUCENENET NOTE: made public rather than internal because has public subclasses
IEnumerable<long> // LUCENENET specific
{
internal const int MIN_PAGE_SIZE = 64;
// More than 1M doesn't really makes sense with these appending buffers
// since their goal is to try to have small numbers of bits per value
internal const int MAX_PAGE_SIZE = 1 << 20;
internal readonly int pageShift, pageMask;
internal PackedInt32s.Reader[] values;
private long valuesBytes;
internal int valuesOff;
internal long[] pending;
internal int pendingOff;
internal readonly float acceptableOverheadRatio; // LUCENENET: marked readonly
private protected AbstractAppendingInt64Buffer(int initialBlockCount, int pageSize, float acceptableOverheadRatio) // LUCENENET: Changed from internal to private protected
{
values = new PackedInt32s.Reader[initialBlockCount];
pending = new long[pageSize];
pageShift = PackedInt32s.CheckBlockSize(pageSize, MIN_PAGE_SIZE, MAX_PAGE_SIZE);
pageMask = pageSize - 1;
valuesOff = 0;
pendingOff = 0;
this.acceptableOverheadRatio = acceptableOverheadRatio;
}
public int PageSize => pageMask + 1;
/// <summary>
/// Get the number of values that have been added to the buffer.
/// <para/>
/// NOTE: This was size() in Lucene.
/// </summary>
public long Count
{
get
{
long size = pendingOff;
if (valuesOff > 0)
{
size += values[valuesOff - 1].Count;
}
if (valuesOff > 1)
{
size += (long)(valuesOff - 1) * PageSize;
}
return size;
}
}
/// <summary>
/// Append a value to this buffer. </summary>
public void Add(long l)
{
if (pending is null)
{
throw IllegalStateException.Create("this buffer is frozen");
}
if (pendingOff == pending.Length)
{
// check size
if (values.Length == valuesOff)
{
int newLength = ArrayUtil.Oversize(valuesOff + 1, 8);
Grow(newLength);
}
PackPendingValues();
valuesBytes += values[valuesOff].RamBytesUsed();
++valuesOff;
// reset pending buffer
pendingOff = 0;
}
pending[pendingOff++] = l;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal virtual void Grow(int newBlockCount)
{
Array.Resize(ref values, newBlockCount);
}
internal abstract void PackPendingValues();
public override sealed long Get(long index)
{
if (Debugging.AssertsEnabled) Debugging.Assert(index >= 0 && index < Count);
int block = (int)(index >> pageShift);
int element = (int)(index & pageMask);
return Get(block, element);
}
/// <summary>
/// Bulk get: read at least one and at most <paramref name="len"/> <see cref="long"/>s starting
/// from <paramref name="index"/> into <c>arr[off:off+len]</c> and return
/// the actual number of values that have been read.
/// </summary>
public int Get(long index, long[] arr, int off, int len)
{
if (Debugging.AssertsEnabled)
{
Debugging.Assert(len > 0, "len must be > 0 (got {0})", len);
Debugging.Assert(index >= 0 && index < Count);
Debugging.Assert(off + len <= arr.Length);
}
int block = (int)(index >> pageShift);
int element = (int)(index & pageMask);
return Get(block, element, arr, off, len);
}
internal abstract long Get(int block, int element);
internal abstract int Get(int block, int element, long[] arr, int off, int len);
/// <summary>
/// Return an iterator over the values of this buffer.
/// </summary>
public virtual Enumerator GetEnumerator()
{
return new Enumerator(this);
}
IEnumerator<long> IEnumerable<long>.GetEnumerator() => GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public sealed class Enumerator : IEnumerator<long>
{
private readonly AbstractAppendingInt64Buffer outerInstance;
internal long[] currentValues;
internal int vOff, pOff;
internal int currentCount; // number of entries of the current page
internal Enumerator(AbstractAppendingInt64Buffer outerInstance)
{
this.outerInstance = outerInstance;
Reset(); // LUCENENET specific - moved from ctor
}
internal void FillValues()
{
if (vOff == outerInstance.valuesOff)
{
currentValues = outerInstance.pending;
currentCount = outerInstance.pendingOff;
}
else
{
currentCount = outerInstance.values[vOff].Count;
for (int k = 0; k < currentCount;)
{
k += outerInstance.Get(vOff, k, currentValues, k, currentCount - k);
}
}
}
/// <summary>
/// Gets the current value.
/// </summary>
public long Current { get; private set; }
object IEnumerator.Current => Current;
/// <summary>
/// Advances the enumerator to the next value.
/// </summary>
public bool MoveNext()
{
if (pOff >= currentCount)
{
return false;
}
Current = currentValues[pOff++];
if (pOff == currentCount)
{
vOff += 1;
pOff = 0;
if (vOff <= outerInstance.valuesOff)
{
FillValues();
}
else
{
currentCount = 0;
}
}
return true;
}
/// <summary>
/// Resets the enumerator to its initial position.
/// </summary>
public void Reset()
{
vOff = pOff = 0;
if (outerInstance.valuesOff == 0)
{
currentValues = outerInstance.pending;
currentCount = outerInstance.pendingOff;
}
else
{
currentValues = new long[outerInstance.values[0].Count];
FillValues();
}
}
/// <summary>
/// Dispose of the resources used by the <see cref="Enumerator"/>.
/// </summary>
public void Dispose()
{
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal virtual long BaseRamBytesUsed()
{
return RamUsageEstimator.NUM_BYTES_OBJECT_HEADER
+ 2 * RamUsageEstimator.NUM_BYTES_OBJECT_REF
+ 2 * RamUsageEstimator.NUM_BYTES_INT32
+ 2 * RamUsageEstimator.NUM_BYTES_INT32
+ RamUsageEstimator.NUM_BYTES_SINGLE
+ RamUsageEstimator.NUM_BYTES_INT64; // valuesBytes - acceptable overhead - pageShift, pageMask - the 2 offsets - the 2 arrays
}
/// <summary>
/// Return the number of bytes used by this instance. </summary>
public virtual long RamBytesUsed()
{
// TODO: this is called per-doc-per-norms/dv-field, can we optimize this?
long bytesUsed = RamUsageEstimator.AlignObjectSize(BaseRamBytesUsed()) + (pending != null ? RamUsageEstimator.SizeOf(pending) : 0L) + RamUsageEstimator.AlignObjectSize(RamUsageEstimator.NUM_BYTES_ARRAY_HEADER + (long)RamUsageEstimator.NUM_BYTES_OBJECT_REF * values.Length); // values
return bytesUsed + valuesBytes;
}
/// <summary>
/// Pack all pending values in this buffer. Subsequent calls to <see cref="Add(long)"/> will fail. </summary>
public virtual void Freeze()
{
if (pendingOff > 0)
{
if (values.Length == valuesOff)
{
Grow(valuesOff + 1); // don't oversize!
}
PackPendingValues();
valuesBytes += values[valuesOff].RamBytesUsed();
++valuesOff;
pendingOff = 0;
}
pending = null;
}
}
}