@@ -53,6 +53,8 @@ final class NodeHash<T> {
5353 private final FST .Arc <T > scratchArc = new FST .Arc <>();
5454 // store the last fallback table node length in getFallback()
5555 private int lastFallbackNodeLength ;
56+ // store the last fallback table hashtable position in getFallback()
57+ private long lastFallbackPos ;
5658
5759 /**
5860 * ramLimitMB is the max RAM we can use for recording suffixes. If we hit this limit, the least
@@ -92,6 +94,7 @@ private long getFallback(FSTCompiler.UnCompiledNode<T> nodeIn, long hash) throws
9294 if (length != -1 ) {
9395 // store the node length for further use
9496 this .lastFallbackNodeLength = length ;
97+ this .lastFallbackPos = pos ;
9598 // frozen version of this node is already here
9699 return node ;
97100 }
@@ -118,7 +121,7 @@ public long add(FSTCompiler.UnCompiledNode<T> nodeIn) throws IOException {
118121 if (node != 0 ) {
119122 // it was already in fallback -- promote to primary
120123 // TODO: Copy directly between 2 ByteBlockPool to avoid double-copy
121- primaryTable .set (pos , node , fallbackTable .getBytes (pos , lastFallbackNodeLength ));
124+ primaryTable .set (pos , node , fallbackTable .getBytes (lastFallbackPos , lastFallbackNodeLength ));
122125 } else {
123126 // not in fallback either -- freeze & add the incoming node
124127
@@ -244,6 +247,7 @@ public PagedGrowableHash(long lastNodeAddress, long size) {
244247
245248 public byte [] getBytes (long pos , int length ) {
246249 long address = copiedNodeAddress .get (pos );
250+ assert address - length + 1 >= 0 ;
247251 byte [] buf = new byte [length ];
248252 copiedNodes .readBytes (address - length + 1 , buf , 0 , length );
249253 return buf ;
@@ -375,7 +379,8 @@ private int getMatchedNodeLength(FSTCompiler.UnCompiledNode<T> node, long addres
375379
376380 if (scratchArc .isLast ()) {
377381 if (arcUpto == node .numArcs - 1 ) {
378- return Math .toIntExact (address - in .getPosition () + 1 );
382+ // position is 1 index past the starting address, as we are reading in backward
383+ return Math .toIntExact (address - in .getPosition ());
379384 } else {
380385 return -1 ;
381386 }
0 commit comments