Skip to content

Commit 74ace94

Browse files
committed
Clean up Freezeable
1 parent 5b05fe6 commit 74ace94

File tree

7 files changed

+56
-114
lines changed

7 files changed

+56
-114
lines changed

lucene/core/src/java/org/apache/lucene/util/fst/BytesStore.java

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
// let you read while writing which FST needs
2828

2929
// TODO: Separate the scratch writer and reader functionality
30-
class BytesStore extends DataOutput implements FSTReader, Freezeable {
30+
class BytesStore extends DataOutput implements FSTReader {
3131

3232
private static final long BASE_RAM_BYTES_USED =
3333
RamUsageEstimator.shallowSizeOfInstance(BytesStore.class)
@@ -41,7 +41,6 @@ class BytesStore extends DataOutput implements FSTReader, Freezeable {
4141

4242
private byte[] current;
4343
private int nextWrite;
44-
private boolean frozen;
4544

4645
public BytesStore(int blockBits) {
4746
this.blockBits = blockBits;
@@ -52,15 +51,13 @@ public BytesStore(int blockBits) {
5251

5352
/** Absolute write byte; you must ensure dest is < max position written so far. */
5453
public void writeByte(long dest, byte b) {
55-
assert frozen == false;
5654
int blockIndex = (int) (dest >> blockBits);
5755
byte[] block = blocks.get(blockIndex);
5856
block[(int) (dest & blockMask)] = b;
5957
}
6058

6159
@Override
6260
public void writeByte(byte b) {
63-
assert frozen == false;
6461
if (nextWrite == blockSize) {
6562
current = new byte[blockSize];
6663
blocks.add(current);
@@ -71,7 +68,6 @@ public void writeByte(byte b) {
7168

7269
@Override
7370
public void writeBytes(byte[] b, int offset, int len) {
74-
assert frozen == false;
7571
while (len > 0) {
7672
int chunk = blockSize - nextWrite;
7773
if (len <= chunk) {
@@ -102,7 +98,6 @@ int getBlockBits() {
10298
* so you must only call it on already written parts.
10399
*/
104100
void writeBytes(long dest, byte[] b, int offset, int len) {
105-
assert frozen == false;
106101
// System.out.println(" BS.writeBytes dest=" + dest + " offset=" + offset + " len=" + len);
107102
assert dest + len <= getPosition() : "dest=" + dest + " pos=" + getPosition() + " len=" + len;
108103

@@ -162,7 +157,6 @@ void writeBytes(long dest, byte[] b, int offset, int len) {
162157

163158
@Override
164159
public void copyBytes(DataInput input, long numBytes) throws IOException {
165-
assert frozen == false;
166160
assert numBytes >= 0 : "numBytes=" + numBytes;
167161
assert input != null;
168162
long len = numBytes;
@@ -187,7 +181,6 @@ public void copyBytes(DataInput input, long numBytes) throws IOException {
187181
* bytes, so must only call it on already written parts.
188182
*/
189183
public void copyBytes(long src, long dest, int len) {
190-
assert frozen == false;
191184
// System.out.println("BS.copyBytes src=" + src + " dest=" + dest + " len=" + len);
192185
assert src < dest;
193186

@@ -246,7 +239,6 @@ public void copyBytes(long src, long dest, int len) {
246239

247240
/** Copies bytes from this store to a target byte array. */
248241
public void copyBytes(long src, byte[] dest, int offset, int len) {
249-
assert frozen == false;
250242
int blockIndex = (int) (src >> blockBits);
251243
int upto = (int) (src & blockMask);
252244
byte[] block = blocks.get(blockIndex);
@@ -268,7 +260,6 @@ public void copyBytes(long src, byte[] dest, int offset, int len) {
268260

269261
/** Writes an int at the absolute position without changing the current pointer. */
270262
public void writeInt(long pos, int value) {
271-
assert frozen == false;
272263
int blockIndex = (int) (pos >> blockBits);
273264
int upto = (int) (pos & blockMask);
274265
byte[] block = blocks.get(blockIndex);
@@ -286,7 +277,6 @@ public void writeInt(long pos, int value) {
286277

287278
/** Reverse from srcPos, inclusive, to destPos, inclusive. */
288279
public void reverse(long srcPos, long destPos) {
289-
assert frozen == false;
290280
assert srcPos < destPos;
291281
assert destPos < getPosition();
292282
// System.out.println("reverse src=" + srcPos + " dest=" + destPos);
@@ -325,7 +315,6 @@ public void reverse(long srcPos, long destPos) {
325315
}
326316

327317
public void skipBytes(int len) {
328-
assert frozen == false;
329318
while (len > 0) {
330319
int chunk = blockSize - nextWrite;
331320
if (len <= chunk) {
@@ -351,7 +340,6 @@ public long size() {
351340

352341
/** Similar to {@link #truncate(long)} with newLen=0 but keep the first block to reduce GC. */
353342
public void reset() {
354-
assert frozen == false;
355343
if (blocks.isEmpty()) {
356344
return;
357345
}
@@ -365,7 +353,6 @@ public void reset() {
365353
* this!
366354
*/
367355
public void truncate(long newLen) {
368-
assert frozen == false;
369356
assert newLen <= getPosition();
370357
assert newLen >= 0;
371358
int blockIndex = (int) (newLen >> blockBits);
@@ -383,17 +370,6 @@ public void truncate(long newLen) {
383370
assert newLen == getPosition();
384371
}
385372

386-
@Override
387-
public void freeze() {
388-
this.frozen = true;
389-
if (current != null) {
390-
byte[] lastBuffer = new byte[nextWrite];
391-
System.arraycopy(current, 0, lastBuffer, 0, nextWrite);
392-
blocks.set(blocks.size() - 1, lastBuffer);
393-
current = null;
394-
}
395-
}
396-
397373
/** Writes all of our bytes to the target {@link FSTDataOutputWriter}. */
398374
public void writeTo(FSTDataOutputWriter out) throws IOException {
399375
reverse(0, getPosition() - 1);
@@ -409,9 +385,12 @@ public void writeTo(FSTDataOutputWriter out) throws IOException {
409385
/** Writes all of our bytes to the target {@link DataOutput}. */
410386
@Override
411387
public void writeTo(DataOutput out) throws IOException {
412-
assert frozen;
413388
for (byte[] block : blocks) {
414-
out.writeBytes(block, 0, block.length);
389+
if (block == current) { // last block
390+
out.writeBytes(block, 0, nextWrite);
391+
} else {
392+
out.writeBytes(block, 0, block.length);
393+
}
415394
}
416395
}
417396

0 commit comments

Comments
 (0)