if (reader.read(scratch)) { final BytesRef bytes = scratch.get(); weight = decode(bytes, input); if (hasPayloads) { payload = decodePayload(bytes, input); contexts = decodeContexts(bytes, input); close(); success = done = true; return null; if (!success) { done = true; close();
@Override public int compare(BytesRef left, BytesRef right) { // Make shallow copy in case decode changes the BytesRef: leftScratch.bytes = left.bytes; leftScratch.offset = left.offset; leftScratch.length = left.length; rightScratch.bytes = right.bytes; rightScratch.offset = right.offset; rightScratch.length = right.length; long leftCost = decode(leftScratch, input); long rightCost = decode(rightScratch, input); if (hasPayloads) { decodePayload(leftScratch, input); decodePayload(rightScratch, input); } if (hasContexts) { decodeContexts(leftScratch, input); decodeContexts(rightScratch, input); } int cmp = comparator.compare(leftScratch, rightScratch); if (cmp != 0) { return cmp; } return Long.compare(leftCost, rightCost); } };
/** * Creates a new sorted wrapper, sorting by BytesRef * (ascending) then cost (ascending). */ public SortedInputIterator(InputIterator source, Comparator<BytesRef> comparator) throws IOException { this.hasPayloads = source.hasPayloads(); this.hasContexts = source.hasContexts(); this.source = source; this.comparator = comparator; this.reader = sort(); }
@Override public void build(InputIterator iterator) throws IOException { if (iterator.hasPayloads()) { throw new IllegalArgumentException("this suggester doesn't support payloads"); } if (iterator.hasContexts()) { throw new IllegalArgumentException("this suggester doesn't support contexts"); } root = new TernaryTreeNode(); // make sure it's sorted and the comparator uses UTF16 sort order iterator = new SortedInputIterator(iterator, BytesRef.getUTF8SortedAsUTF16Comparator()); count = 0; ArrayList<String> tokens = new ArrayList<>(); ArrayList<Number> vals = new ArrayList<>(); BytesRef spare; CharsRefBuilder charsSpare = new CharsRefBuilder(); while ((spare = iterator.next()) != null) { charsSpare.copyUTF8Bytes(spare); tokens.add(charsSpare.toString()); vals.add(Long.valueOf(iterator.weight())); count++; } autocomplete.balancedTree(tokens.toArray(), vals.toArray(), 0, tokens.size() - 1, root); }