/** * Reads the next entry into the provided {@link BytesRef}. The internal * storage is resized if needed. * * @return Returns <code>false</code> if EOF occurred when trying to read * the header of the next sequence. Returns <code>true</code> otherwise. * @throws EOFException if the file ends before the full sequence is read. */ public boolean read(BytesRef ref) throws IOException { short length; try { length = is.readShort(); } catch (EOFException e) { return false; } ref.grow(length); ref.offset = 0; ref.length = length; is.readFully(ref.bytes, 0, length); return true; }
/** * Appends a single suggestion and its weight to the internal buffers. * * @param utf8 * The suggestion (utf8 representation) to be added. The content is * copied and the object can be reused. * @param bucket * The bucket to place this suggestion in. Must be non-negative and * smaller than the number of buckets passed in the constructor. * Higher numbers indicate suggestions that should be presented * before suggestions placed in smaller buckets. */ public void add(BytesRef utf8, int bucket) throws IOException { if (bucket < 0 || bucket >= buckets) { throw new IllegalArgumentException( "Bucket outside of the allowed range [0, " + buckets + "): " + bucket); } if (scratch.bytes.length < utf8.length + 1) { scratch.grow(utf8.length + 10); } scratch.length = 1; scratch.bytes[0] = (byte) bucket; scratch.append(utf8); sorter.add(scratch); }
scratch.grow(estimatedSize); scratchOutput.reset(scratch.bytes, scratch.offset, scratch.bytes.length); assert scratch.offset == 0;