Skip to content

Commit ccf1036

Browse files
committed
Remove the FSTWriter interface
1 parent 05877e2 commit ccf1036

File tree

4 files changed

+87
-125
lines changed

4 files changed

+87
-125
lines changed

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

Lines changed: 0 additions & 101 deletions
This file was deleted.

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,12 @@ public class FSTCompiler<T> {
134134
*/
135135
// TODO: remove this? Builder API should be the only entry point?
136136
public FSTCompiler(FST.INPUT_TYPE inputType, Outputs<T> outputs) {
137-
this(inputType, 32.0, outputs, true, getLegacyFSTWriter(DEFAULT_BLOCK_BITS), 1f);
137+
this(
138+
inputType, 32.0, outputs, true, new FSTWriter(getLegacyDataOutput(DEFAULT_BLOCK_BITS)), 1f);
138139
}
139140

140-
static FSTWriter getLegacyFSTWriter(int blockBits) {
141-
return new DataOutputFSTWriter(new BytesStore(blockBits));
141+
static DataOutput getLegacyDataOutput(int blockBits) {
142+
return new BytesStore(blockBits);
142143
}
143144

144145
private FSTCompiler(
@@ -158,7 +159,7 @@ private FSTCompiler(
158159
throw new RuntimeException(e);
159160
}
160161
this.fstWriter = fstWriter;
161-
this.fstReader = fstWriter.asReader();
162+
this.fstReader = fstWriter.getReader();
162163
fst =
163164
new FST<>(
164165
new FST.FSTMetadata<>(inputType, null, -1, VERSION_CURRENT, 0), outputs, fstReader);
@@ -191,7 +192,7 @@ public static class Builder<T> {
191192
private final Outputs<T> outputs;
192193
private double suffixRAMLimitMB = 32.0;
193194
private boolean allowFixedLengthArcs = true;
194-
private FSTWriter fstWriter = getLegacyFSTWriter(DEFAULT_BLOCK_BITS);
195+
private DataOutput dataOutput = getLegacyDataOutput(DEFAULT_BLOCK_BITS);
195196
private float directAddressingMaxOversizingFactor = DIRECT_ADDRESSING_MAX_OVERSIZING_FACTOR;
196197

197198
/**
@@ -255,8 +256,7 @@ public Builder<T> allowFixedLengthArcs(boolean allowFixedLengthArcs) {
255256
*/
256257
@Deprecated
257258
public Builder<T> bytesPageBits(int bytesPageBits) {
258-
this.fstWriter = getLegacyFSTWriter(bytesPageBits);
259-
return this;
259+
return dataOutput(getLegacyDataOutput(bytesPageBits));
260260
}
261261

262262
/**
@@ -271,7 +271,7 @@ public Builder<T> bytesPageBits(int bytesPageBits) {
271271
* @return this builder
272272
*/
273273
public Builder<T> dataOutput(DataOutput dataOutput) {
274-
this.fstWriter = new DataOutputFSTWriter(dataOutput);
274+
this.dataOutput = dataOutput;
275275
return this;
276276
}
277277

@@ -301,7 +301,7 @@ public FSTCompiler<T> build() {
301301
suffixRAMLimitMB,
302302
outputs,
303303
allowFixedLengthArcs,
304-
fstWriter,
304+
new FSTWriter(dataOutput),
305305
directAddressingMaxOversizingFactor);
306306
return fstCompiler;
307307
}

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

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,102 @@
1616
*/
1717
package org.apache.lucene.util.fst;
1818

19+
import java.io.Closeable;
1920
import java.io.IOException;
21+
import org.apache.lucene.store.DataOutput;
22+
import org.apache.lucene.util.Accountable;
23+
import org.apache.lucene.util.RamUsageEstimator;
2024

21-
/** Abstract class which provides low-level functionality to write to a FST */
22-
public interface FSTWriter {
25+
/**
26+
* Allow the FST to be written to a {@link DataOutput}. Generally it only supports writing to the
27+
* {@link DataOutput} and not reading from it. To read, you must either: 1. construct a
28+
* corresponding {@link org.apache.lucene.store.DataInput} and use the {@link FSTStore} to read pr
29+
* 2. use a DataOutput which also implements {@link FSTReader}
30+
*/
31+
final class FSTWriter {
32+
33+
private static final long BASE_RAM_BYTES_USED =
34+
RamUsageEstimator.shallowSizeOfInstance(FSTWriter.class);
35+
36+
/** the main DataOutput to store the FST bytes */
37+
private final DataOutput dataOutput;
38+
39+
private long size = 0L;
2340

2441
/**
25-
* Write a single byte to the end of this FSTWriter
42+
* ctor
43+
*
44+
* @param dataOutput the data output to write to
45+
*/
46+
public FSTWriter(DataOutput dataOutput) {
47+
this.dataOutput = dataOutput;
48+
}
49+
50+
/**
51+
* Write a single byte to the end of the DataOutput
2652
*
2753
* @param b the byte to write
2854
*/
29-
void writeByte(byte b) throws IOException;
55+
public void writeByte(byte b) throws IOException {
56+
size++;
57+
dataOutput.writeByte(b);
58+
}
3059

3160
/**
32-
* Write a byte array to the end of this FSTWriter
61+
* Write a byte array to the end of the DataOutput
3362
*
3463
* @param b the byte array to write
3564
* @param offset the offset of the array
3665
* @param length the number of bytes to write
3766
*/
38-
void writeBytes(byte[] b, int offset, int length) throws IOException;
67+
public void writeBytes(byte[] b, int offset, int length) throws IOException {
68+
size += length;
69+
dataOutput.writeBytes(b, offset, length);
70+
}
3971

4072
/**
41-
* Called when the FST construction is finished and no more node can be added to it. Freezing,
42-
* optimizing the datastructure can be done here
73+
* Called when the FST construction is finished and no more node can be added to it.
4374
*
4475
* @throws IOException if exception occurred during the operation
4576
*/
46-
default void finish() throws IOException {
47-
// do nothing by default
77+
public void finish() throws IOException {
78+
if (dataOutput instanceof Closeable) {
79+
((Closeable) dataOutput).close();
80+
}
4881
}
4982

5083
/**
5184
* @return The corresponding {@link FSTReader} of this writer
5285
*/
53-
FSTReader asReader();
86+
public FSTReader getReader() {
87+
if (dataOutput instanceof FSTReader) {
88+
return (FSTReader) dataOutput;
89+
}
90+
return new FSTReader() {
91+
@Override
92+
public long size() {
93+
return size;
94+
}
95+
96+
@Override
97+
public FST.BytesReader getReverseBytesReader() {
98+
return null;
99+
}
100+
101+
@Override
102+
public void writeTo(DataOutput out) {
103+
throw new UnsupportedOperationException(
104+
"writeTo(DataOutput) is not supported by FSTWriter");
105+
}
106+
107+
@Override
108+
public long ramBytesUsed() {
109+
long size = BASE_RAM_BYTES_USED;
110+
if (dataOutput instanceof Accountable) {
111+
size += ((Accountable) dataOutput).ramBytesUsed();
112+
}
113+
return size;
114+
}
115+
};
116+
}
54117
}

lucene/core/src/test/org/apache/lucene/util/fst/TestDataOutputFSTWriter.java renamed to lucene/core/src/test/org/apache/lucene/util/fst/TestFSTWriter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import org.apache.lucene.util.BytesRef;
3737
import org.apache.lucene.util.IntsRef;
3838

39-
public class TestDataOutputFSTWriter extends LuceneTestCase {
39+
public class TestFSTWriter extends LuceneTestCase {
4040

4141
private MockDirectoryWrapper dir;
4242

@@ -63,8 +63,8 @@ public void testRandom() throws Exception {
6363
final int numBytes = TestUtil.nextInt(random(), 1, maxBytes);
6464
final byte[] expected = new byte[numBytes];
6565
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
66-
final DataOutputFSTWriter fstWriter =
67-
new DataOutputFSTWriter(new OutputStreamDataOutput(baos));
66+
final FSTWriter fstWriter = new FSTWriter(new OutputStreamDataOutput(baos));
67+
FSTReader reader = fstWriter.getReader();
6868
if (VERBOSE) {
6969
System.out.println("TEST: iter=" + iter + " numBytes=" + numBytes);
7070
}
@@ -105,7 +105,7 @@ public void testRandom() throws Exception {
105105
break;
106106
}
107107

108-
assertEquals(pos, fstWriter.size());
108+
assertEquals(pos, reader.size());
109109
}
110110
for (int i = 0; i < numBytes; i++) {
111111
assertEquals("byte @ index=" + i, expected[i], baos.toByteArray()[i]);

0 commit comments

Comments
 (0)