Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions lucene/core/src/java/org/apache/lucene/util/fst/FSTCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private FSTCompiler(
if (suffixRAMLimitMB < 0) {
throw new IllegalArgumentException("ramLimitMB must be >= 0; got: " + suffixRAMLimitMB);
} else if (suffixRAMLimitMB > 0) {
dedupHash = new NodeHash<>(this, suffixRAMLimitMB, bytes.getReverseReader(false));
dedupHash = new NodeHash<>(this, suffixRAMLimitMB);
} else {
dedupHash = null;
}
Expand Down Expand Up @@ -292,13 +292,13 @@ private CompiledNode compileNode(UnCompiledNode<T> nodeIn, int tailLength) throw
long bytesPosStart = bytes.getPosition();
if (dedupHash != null) {
if (nodeIn.numArcs == 0) {
node = addNode(nodeIn);
node = addNode(nodeIn, false).nodeAddress;
lastFrozenNode = node;
} else {
node = dedupHash.add(nodeIn);
}
} else {
node = addNode(nodeIn);
node = addNode(nodeIn, false).nodeAddress;
}
assert node != -2;

Expand All @@ -318,13 +318,13 @@ private CompiledNode compileNode(UnCompiledNode<T> nodeIn, int tailLength) throw

// serializes new node by appending its bytes to the end
// of the current byte[]
long addNode(FSTCompiler.UnCompiledNode<T> nodeIn) throws IOException {
NodeAndBuffer addNode(FSTCompiler.UnCompiledNode<T> nodeIn, boolean needCopy) throws IOException {
// System.out.println("FST.addNode pos=" + bytes.getPosition() + " numArcs=" + nodeIn.numArcs);
if (nodeIn.numArcs == 0) {
if (nodeIn.isFinal) {
return FINAL_END_NODE;
return new NodeAndBuffer(FINAL_END_NODE, null);
} else {
return NON_FINAL_END_NODE;
return new NodeAndBuffer(NON_FINAL_END_NODE, null);
}
}
final long startAddress = bytes.getPosition();
Expand Down Expand Up @@ -461,7 +461,23 @@ long addNode(FSTCompiler.UnCompiledNode<T> nodeIn) throws IOException {
final long thisNodeAddress = bytes.getPosition() - 1;
bytes.reverse(startAddress, thisNodeAddress);
nodeCount++;
return thisNodeAddress;
if (needCopy) {
byte[] buf = new byte[Math.toIntExact(thisNodeAddress - startAddress + 1)];
bytes.copyBytes(startAddress, buf, 0, buf.length);
return new NodeAndBuffer(thisNodeAddress, buf);
}
return new NodeAndBuffer(thisNodeAddress, null);
}

class NodeAndBuffer {

final long nodeAddress;
final byte[] bytes;

NodeAndBuffer(long nodeAddress, byte[] bytes) {
this.nodeAddress = nodeAddress;
this.bytes = bytes;
}
}

private void writeLabel(DataOutput out, int v) throws IOException {
Expand Down
Loading