@@ -134,38 +134,40 @@ public SynonymMap readSynonymMap(Directory directory) throws IOException {
134
134
synonymMetadata .fstMetadata ,
135
135
directory .openInput (FST_FILE , IOContext .DEFAULT ),
136
136
new OffHeapFSTStore ());
137
- IndexInput wordsInput = directory .openInput (WORDS_FILE , IOContext .READ );
138
- int [] bytesStartArray = new int [synonymMetadata .wordCount ];
139
- for (int i = 0 ; i < synonymMetadata .wordCount ; i ++) {
140
- bytesStartArray [i ] = Math .toIntExact (wordsInput .getFilePointer ());
141
- int length = wordsInput .readVInt ();
142
- wordsInput .seek (wordsInput .getFilePointer () + length );
137
+ OnHeapBytesRefHashLike words ;
138
+ try (IndexInput wordsInput = directory .openInput (WORDS_FILE , IOContext .DEFAULT )) {
139
+ words = new OnHeapBytesRefHashLike (synonymMetadata .wordCount , wordsInput );
143
140
}
144
- return new SynonymMap (
145
- fst ,
146
- new OffHeapBytesRefHashLike (bytesStartArray , wordsInput ),
147
- synonymMetadata .maxHorizontalContext );
141
+ return new SynonymMap (fst , words , synonymMetadata .maxHorizontalContext );
148
142
}
149
143
150
- private static class OffHeapBytesRefHashLike extends SynonymMap .BytesRefHashLike {
144
+ private static class OnHeapBytesRefHashLike extends SynonymMap .BytesRefHashLike {
151
145
private final int [] bytesStartArray ;
152
- private final IndexInput wordsFile ;
153
-
154
- public OffHeapBytesRefHashLike (int [] bytesStartArray , IndexInput wordsFile ) {
155
- this .bytesStartArray = bytesStartArray ;
156
- this .wordsFile = wordsFile ;
146
+ private final byte [] wordBytes ;
147
+
148
+ public OnHeapBytesRefHashLike (int wordCount , IndexInput wordsFile ) throws IOException {
149
+ bytesStartArray = new int [wordCount + 1 ];
150
+ int pos = 0 ;
151
+ for (int i = 0 ; i < wordCount ; i ++) {
152
+ bytesStartArray [i ] = pos ;
153
+ int size = wordsFile .readVInt ();
154
+ pos += size ;
155
+ wordsFile .seek (wordsFile .getFilePointer () + size );
156
+ }
157
+ bytesStartArray [wordCount ] = pos ;
158
+ wordsFile .seek (0 );
159
+ wordBytes = new byte [pos ];
160
+ for (int i = 0 ; i < wordCount ; i ++) {
161
+ int size = wordsFile .readVInt ();
162
+ wordsFile .readBytes (wordBytes , bytesStartArray [i ], size );
163
+ }
157
164
}
158
165
159
166
@ Override
160
- public void get (int id , BytesRef scratch ) throws IOException {
161
- wordsFile .seek (bytesStartArray [id ]);
162
- int length = wordsFile .readVInt ();
163
- if (scratch .bytes .length < length ) {
164
- scratch .bytes = new byte [length ];
165
- }
166
- wordsFile .readBytes (scratch .bytes , 0 , length );
167
- scratch .offset = 0 ;
168
- scratch .length = length ;
167
+ public void get (int id , BytesRef scratch ) {
168
+ scratch .bytes = wordBytes ;
169
+ scratch .offset = bytesStartArray [id ];
170
+ scratch .length = bytesStartArray [id + 1 ] - bytesStartArray [id ];
169
171
}
170
172
}
171
173
0 commit comments