Skip to content

Commit 49b622f

Browse files
committed
Resolve merge conflicts
Moved FST metadata saving into FSTMetadata class per suggestion from @dungba88.
1 parent f8c1f0e commit 49b622f

File tree

3 files changed

+53
-53
lines changed

3 files changed

+53
-53
lines changed

lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymMap.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public SynonymMap build(SynonymMapDirectory directory) throws IOException {
306306
fstCompiler.add(Util.toUTF32(input, scratchIntsRef), scratch.toBytesRef());
307307
}
308308

309-
FST<BytesRef> fst = FST.fromFSTReader(fstCompiler.compile(), fstCompiler.getFSTReader());
309+
FST.FSTMetadata<BytesRef> fstMetaData = fstCompiler.compile();
310310
if (directory != null) {
311311
fstOutput.close(); // TODO -- Should fstCompiler.compile take care of this?
312312
try (SynonymMapDirectory.WordsOutput wordsOutput = directory.wordsOutput()) {
@@ -316,9 +316,10 @@ public SynonymMap build(SynonymMapDirectory directory) throws IOException {
316316
wordsOutput.addWord(scratchRef);
317317
}
318318
}
319-
directory.writeMetadata(words.size(), maxHorizontalContext, fst);
319+
directory.writeMetadata(words.size(), maxHorizontalContext, fstMetaData);
320320
return directory.readMap();
321321
}
322+
FST<BytesRef> fst = FST.fromFSTReader(fstMetaData, fstCompiler.getFSTReader());
322323
BytesRefHashLike wordsLike =
323324
new BytesRefHashLike() {
324325
@Override

lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymMapDirectory.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ public WordsOutput wordsOutput() throws IOException {
5252
return synonymMapFormat.getWordsOutput(directory);
5353
}
5454

55-
public void writeMetadata(int wordCount, int maxHorizontalContext, FST<BytesRef> fst)
55+
public void writeMetadata(
56+
int wordCount, int maxHorizontalContext, FST.FSTMetadata<BytesRef> fstMetadata)
5657
throws IOException {
57-
synonymMapFormat.writeMetadata(directory, wordCount, maxHorizontalContext, fst);
58+
synonymMapFormat.writeMetadata(directory, wordCount, maxHorizontalContext, fstMetadata);
5859
}
5960

6061
public SynonymMap readMap() throws IOException {
@@ -107,12 +108,15 @@ public void addWord(BytesRef word) throws IOException {
107108
;
108109

109110
public void writeMetadata(
110-
Directory directory, int wordCount, int maxHorizontalContext, FST<BytesRef> fst)
111+
Directory directory,
112+
int wordCount,
113+
int maxHorizontalContext,
114+
FST.FSTMetadata<BytesRef> fstMetadata)
111115
throws IOException {
112116
try (IndexOutput metadataOutput = directory.createOutput(METADATA_FILE, IOContext.DEFAULT)) {
113117
metadataOutput.writeVInt(wordCount);
114118
metadataOutput.writeVInt(maxHorizontalContext);
115-
fst.saveMetadata(metadataOutput);
119+
fstMetadata.save(metadataOutput);
116120
}
117121
directory.sync(List.of(FST_FILE, WORDS_FILE, METADATA_FILE));
118122
}

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

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -537,56 +537,10 @@ public FSTMetadata<T> getMetadata() {
537537
* @param out the DataOutput to write the FST bytes to
538538
*/
539539
public void save(DataOutput metaOut, DataOutput out) throws IOException {
540-
saveMetadata(metaOut);
540+
metadata.save(metaOut);
541541
fstReader.writeTo(out);
542542
}
543543

544-
/**
545-
* Save the metadata to a DataOutput
546-
*
547-
* @param metaOut the DataOutput to write the metadata to
548-
*/
549-
public void saveMetadata(DataOutput metaOut) throws IOException {
550-
CodecUtil.writeHeader(metaOut, FILE_FORMAT_NAME, VERSION_CURRENT);
551-
// TODO: really we should encode this as an arc, arriving
552-
// to the root node, instead of special casing here:
553-
if (metadata.emptyOutput != null) {
554-
// Accepts empty string
555-
metaOut.writeByte((byte) 1);
556-
557-
// Serialize empty-string output:
558-
ByteBuffersDataOutput ros = new ByteBuffersDataOutput();
559-
outputs.writeFinalOutput(metadata.emptyOutput, ros);
560-
byte[] emptyOutputBytes = ros.toArrayCopy();
561-
int emptyLen = emptyOutputBytes.length;
562-
563-
// reverse
564-
final int stopAt = emptyLen / 2;
565-
int upto = 0;
566-
while (upto < stopAt) {
567-
final byte b = emptyOutputBytes[upto];
568-
emptyOutputBytes[upto] = emptyOutputBytes[emptyLen - upto - 1];
569-
emptyOutputBytes[emptyLen - upto - 1] = b;
570-
upto++;
571-
}
572-
metaOut.writeVInt(emptyLen);
573-
metaOut.writeBytes(emptyOutputBytes, 0, emptyLen);
574-
} else {
575-
metaOut.writeByte((byte) 0);
576-
}
577-
final byte t;
578-
if (metadata.inputType == INPUT_TYPE.BYTE1) {
579-
t = 0;
580-
} else if (metadata.inputType == INPUT_TYPE.BYTE2) {
581-
t = 1;
582-
} else {
583-
t = 2;
584-
}
585-
metaOut.writeByte(t);
586-
metaOut.writeVLong(metadata.startNode);
587-
metaOut.writeVLong(numBytes());
588-
}
589-
590544
/** Writes an automaton to a file. */
591545
public void save(final Path path) throws IOException {
592546
try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(path))) {
@@ -1258,5 +1212,46 @@ public FSTMetadata(
12581212
public int getVersion() {
12591213
return version;
12601214
}
1215+
1216+
public void save(DataOutput metaOut) throws IOException {
1217+
CodecUtil.writeHeader(metaOut, FILE_FORMAT_NAME, VERSION_CURRENT);
1218+
// TODO: really we should encode this as an arc, arriving
1219+
// to the root node, instead of special casing here:
1220+
if (emptyOutput != null) {
1221+
// Accepts empty string
1222+
metaOut.writeByte((byte) 1);
1223+
1224+
// Serialize empty-string output:
1225+
ByteBuffersDataOutput ros = new ByteBuffersDataOutput();
1226+
outputs.writeFinalOutput(emptyOutput, ros);
1227+
byte[] emptyOutputBytes = ros.toArrayCopy();
1228+
int emptyLen = emptyOutputBytes.length;
1229+
1230+
// reverse
1231+
final int stopAt = emptyLen / 2;
1232+
int upto = 0;
1233+
while (upto < stopAt) {
1234+
final byte b = emptyOutputBytes[upto];
1235+
emptyOutputBytes[upto] = emptyOutputBytes[emptyLen - upto - 1];
1236+
emptyOutputBytes[emptyLen - upto - 1] = b;
1237+
upto++;
1238+
}
1239+
metaOut.writeVInt(emptyLen);
1240+
metaOut.writeBytes(emptyOutputBytes, 0, emptyLen);
1241+
} else {
1242+
metaOut.writeByte((byte) 0);
1243+
}
1244+
final byte t;
1245+
if (inputType == INPUT_TYPE.BYTE1) {
1246+
t = 0;
1247+
} else if (inputType == INPUT_TYPE.BYTE2) {
1248+
t = 1;
1249+
} else {
1250+
t = 2;
1251+
}
1252+
metaOut.writeByte(t);
1253+
metaOut.writeVLong(startNode);
1254+
metaOut.writeVLong(numBytes);
1255+
}
12611256
}
12621257
}

0 commit comments

Comments
 (0)