mikemccand commented on code in PR #13054: URL: https://github.com/apache/lucene/pull/13054#discussion_r1687872625
########## lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymMapDirectory.java: ########## @@ -0,0 +1,191 @@ +/* + * 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. + */ + +package org.apache.lucene.analysis.synonym; + +import java.io.Closeable; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.FSDirectory; +import org.apache.lucene.store.IOContext; +import org.apache.lucene.store.IndexInput; +import org.apache.lucene.store.IndexOutput; +import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.fst.ByteSequenceOutputs; +import org.apache.lucene.util.fst.FST; +import org.apache.lucene.util.fst.OffHeapFSTStore; + +/** + * Wraps an {@link FSDirectory} to read and write a compiled {@link SynonymMap}. When reading, the + * FST and output words are kept off-heap. + */ +public class SynonymMapDirectory implements Closeable { + private final SynonymMapFormat synonymMapFormat = + new SynonymMapFormat(); // TODO -- Should this be more flexible/codec-like? Less? + private final Directory directory; + + public SynonymMapDirectory(Path path) throws IOException { Review Comment: Is it possible to store several `SynonymMap`s in one `Directory`? Or one must make a separate `Directory` for each? That's maybe fine ... e.g. one could make a `FilterDirectory` impl that can share a single underlying filesystem directory and distinguish the files by e.g. a unique filename prefix or so. ########## lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymMap.java: ########## @@ -291,11 +306,35 @@ public SynonymMap build() throws IOException { fstCompiler.add(Util.toUTF32(input, scratchIntsRef), scratch.toBytesRef()); } - FST<BytesRef> fst = FST.fromFSTReader(fstCompiler.compile(), fstCompiler.getFSTReader()); - return new SynonymMap(fst, words, maxHorizontalContext); + FST.FSTMetadata<BytesRef> fstMetaData = fstCompiler.compile(); + if (directory != null) { + fstOutput.close(); // TODO -- Should fstCompiler.compile take care of this? + try (SynonymMapDirectory.WordsOutput wordsOutput = directory.wordsOutput()) { + BytesRef scratchRef = new BytesRef(); + for (int i = 0; i < words.size(); i++) { + words.get(i, scratchRef); + wordsOutput.addWord(scratchRef); + } + } + directory.writeMetadata(words.size(), maxHorizontalContext, fstMetaData); + return directory.readMap(); + } + FST<BytesRef> fst = FST.fromFSTReader(fstMetaData, fstCompiler.getFSTReader()); + BytesRefHashLike wordsLike = + new BytesRefHashLike() { + @Override + public void get(int id, BytesRef scratch) { + words.get(id, scratch); + } + }; + return new SynonymMap(fst, wordsLike, maxHorizontalContext); } } + abstract static class BytesRefHashLike { Review Comment: Also, maybe rename to remove any reference to `BytesRefHash`? E.g. `WordProvider` or `IDToWord` or something? ########## lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymMapDirectory.java: ########## @@ -0,0 +1,191 @@ +/* + * 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. + */ + +package org.apache.lucene.analysis.synonym; + +import java.io.Closeable; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.FSDirectory; +import org.apache.lucene.store.IOContext; +import org.apache.lucene.store.IndexInput; +import org.apache.lucene.store.IndexOutput; +import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.fst.ByteSequenceOutputs; +import org.apache.lucene.util.fst.FST; +import org.apache.lucene.util.fst.OffHeapFSTStore; + +/** + * Wraps an {@link FSDirectory} to read and write a compiled {@link SynonymMap}. When reading, the + * FST and output words are kept off-heap. + */ +public class SynonymMapDirectory implements Closeable { Review Comment: Maybe mark this with `@lucene.experimental` so we are free to change the API within non-major releases? Or: could this be package private? Does the user need to create this wrapper themselves for some reason? ########## lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymMapDirectory.java: ########## @@ -0,0 +1,191 @@ +/* + * 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. + */ + +package org.apache.lucene.analysis.synonym; + +import java.io.Closeable; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.FSDirectory; +import org.apache.lucene.store.IOContext; +import org.apache.lucene.store.IndexInput; +import org.apache.lucene.store.IndexOutput; +import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.fst.ByteSequenceOutputs; +import org.apache.lucene.util.fst.FST; +import org.apache.lucene.util.fst.OffHeapFSTStore; + +/** + * Wraps an {@link FSDirectory} to read and write a compiled {@link SynonymMap}. When reading, the + * FST and output words are kept off-heap. + */ +public class SynonymMapDirectory implements Closeable { + private final SynonymMapFormat synonymMapFormat = + new SynonymMapFormat(); // TODO -- Should this be more flexible/codec-like? Less? + private final Directory directory; + + public SynonymMapDirectory(Path path) throws IOException { + directory = FSDirectory.open(path); + } + + public IndexOutput fstOutput() throws IOException { + return synonymMapFormat.getFSTOutput(directory); + } + + public WordsOutput wordsOutput() throws IOException { + return synonymMapFormat.getWordsOutput(directory); + } + + public void writeMetadata( + int wordCount, int maxHorizontalContext, FST.FSTMetadata<BytesRef> fstMetadata) + throws IOException { + synonymMapFormat.writeMetadata(directory, wordCount, maxHorizontalContext, fstMetadata); + } + + public SynonymMap readMap() throws IOException { + return synonymMapFormat.readSynonymMap(directory); Review Comment: Hmm, this will hold open `IndexInput` file handles right? How do these get closed? (`SynonymMap` doesn't have a `close` I think?). ########## lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymMapDirectory.java: ########## @@ -0,0 +1,191 @@ +/* + * 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. + */ + +package org.apache.lucene.analysis.synonym; + +import java.io.Closeable; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.FSDirectory; +import org.apache.lucene.store.IOContext; +import org.apache.lucene.store.IndexInput; +import org.apache.lucene.store.IndexOutput; +import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.fst.ByteSequenceOutputs; +import org.apache.lucene.util.fst.FST; +import org.apache.lucene.util.fst.OffHeapFSTStore; + +/** + * Wraps an {@link FSDirectory} to read and write a compiled {@link SynonymMap}. When reading, the + * FST and output words are kept off-heap. Review Comment: How does the user control separately whether FST and output words are off heap or not? ########## lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymMapDirectory.java: ########## @@ -0,0 +1,191 @@ +/* + * 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. + */ + +package org.apache.lucene.analysis.synonym; + +import java.io.Closeable; +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.FSDirectory; +import org.apache.lucene.store.IOContext; +import org.apache.lucene.store.IndexInput; +import org.apache.lucene.store.IndexOutput; +import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.fst.ByteSequenceOutputs; +import org.apache.lucene.util.fst.FST; +import org.apache.lucene.util.fst.OffHeapFSTStore; + +/** + * Wraps an {@link FSDirectory} to read and write a compiled {@link SynonymMap}. When reading, the + * FST and output words are kept off-heap. + */ +public class SynonymMapDirectory implements Closeable { + private final SynonymMapFormat synonymMapFormat = + new SynonymMapFormat(); // TODO -- Should this be more flexible/codec-like? Less? + private final Directory directory; + + public SynonymMapDirectory(Path path) throws IOException { + directory = FSDirectory.open(path); + } + + public IndexOutput fstOutput() throws IOException { + return synonymMapFormat.getFSTOutput(directory); + } + + public WordsOutput wordsOutput() throws IOException { + return synonymMapFormat.getWordsOutput(directory); + } + + public void writeMetadata( + int wordCount, int maxHorizontalContext, FST.FSTMetadata<BytesRef> fstMetadata) + throws IOException { + synonymMapFormat.writeMetadata(directory, wordCount, maxHorizontalContext, fstMetadata); + } + + public SynonymMap readMap() throws IOException { + return synonymMapFormat.readSynonymMap(directory); + } + + public boolean hasSynonyms() throws IOException { + // TODO should take the path to the synonyms file to compare file hash against file used to + // build the directory + return directory.listAll().length > 0; + } + + @Override + public void close() throws IOException { + directory.close(); + } + + /** + * Abstraction to support writing individual output words to the directory. Should be closed after + * the last word is written. + */ + public abstract static class WordsOutput implements Closeable { + public abstract void addWord(BytesRef word) throws IOException; + } + + private static class SynonymMapFormat { + private static final String FST_FILE = "synonyms.fst"; + private static final String WORDS_FILE = "synonyms.wrd"; + private static final String METADATA_FILE = "synonyms.mdt"; + + public IndexOutput getFSTOutput(Directory directory) throws IOException { + return directory.createOutput(FST_FILE, IOContext.DEFAULT); + } + + public WordsOutput getWordsOutput(Directory directory) throws IOException { + IndexOutput wordsOutput = directory.createOutput(WORDS_FILE, IOContext.DEFAULT); + return new WordsOutput() { + @Override + public void close() throws IOException { + wordsOutput.close(); + } + + @Override + public void addWord(BytesRef word) throws IOException { + wordsOutput.writeVInt(word.length); + wordsOutput.writeBytes(word.bytes, word.offset, word.length); + } + }; + } + ; + + public void writeMetadata( + Directory directory, + int wordCount, + int maxHorizontalContext, + FST.FSTMetadata<BytesRef> fstMetadata) + throws IOException { + try (IndexOutput metadataOutput = directory.createOutput(METADATA_FILE, IOContext.DEFAULT)) { + metadataOutput.writeVInt(wordCount); + metadataOutput.writeVInt(maxHorizontalContext); + fstMetadata.save(metadataOutput); + } + directory.sync(List.of(FST_FILE, WORDS_FILE, METADATA_FILE)); + } + + private SynonymMetadata readMetadata(Directory directory) throws IOException { + try (IndexInput metadataInput = directory.openInput(METADATA_FILE, IOContext.READONCE)) { + int wordCount = metadataInput.readVInt(); + int maxHorizontalContext = metadataInput.readVInt(); + FST.FSTMetadata<BytesRef> fstMetadata = + FST.readMetadata(metadataInput, ByteSequenceOutputs.getSingleton()); + return new SynonymMetadata(wordCount, maxHorizontalContext, fstMetadata); + } + } + + public SynonymMap readSynonymMap(Directory directory) throws IOException { + SynonymMetadata synonymMetadata = readMetadata(directory); + FST<BytesRef> fst = + new FST<>( + synonymMetadata.fstMetadata, + directory.openInput(FST_FILE, IOContext.DEFAULT), + new OffHeapFSTStore()); + OnHeapBytesRefHashLike words; + try (IndexInput wordsInput = directory.openInput(WORDS_FILE, IOContext.DEFAULT)) { + words = new OnHeapBytesRefHashLike(synonymMetadata.wordCount, wordsInput); + } + return new SynonymMap(fst, words, synonymMetadata.maxHorizontalContext); + } + + private static class OnHeapBytesRefHashLike extends SynonymMap.BytesRefHashLike { Review Comment: Maybe rename to `OnHeapDictionary` or something (remove `BytesRefHash`)? I feel one should not try to be like a `BytesRefHash`, it is so hairy :) ########## lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymMap.java: ########## @@ -291,11 +306,35 @@ public SynonymMap build() throws IOException { fstCompiler.add(Util.toUTF32(input, scratchIntsRef), scratch.toBytesRef()); } - FST<BytesRef> fst = FST.fromFSTReader(fstCompiler.compile(), fstCompiler.getFSTReader()); - return new SynonymMap(fst, words, maxHorizontalContext); + FST.FSTMetadata<BytesRef> fstMetaData = fstCompiler.compile(); + if (directory != null) { + fstOutput.close(); // TODO -- Should fstCompiler.compile take care of this? + try (SynonymMapDirectory.WordsOutput wordsOutput = directory.wordsOutput()) { + BytesRef scratchRef = new BytesRef(); + for (int i = 0; i < words.size(); i++) { + words.get(i, scratchRef); + wordsOutput.addWord(scratchRef); + } + } + directory.writeMetadata(words.size(), maxHorizontalContext, fstMetaData); + return directory.readMap(); + } + FST<BytesRef> fst = FST.fromFSTReader(fstMetaData, fstCompiler.getFSTReader()); + BytesRefHashLike wordsLike = + new BytesRefHashLike() { + @Override + public void get(int id, BytesRef scratch) { + words.get(id, scratch); + } + }; + return new SynonymMap(fst, wordsLike, maxHorizontalContext); } } + abstract static class BytesRefHashLike { Review Comment: Does this need to be `public` since it's used in the `public` ctor for `SynonymMap`? I thought we had static checking for this though... -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org