|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.index.mapper.blockloader.docvalues; |
| 11 | + |
| 12 | +import org.apache.lucene.index.BinaryDocValues; |
| 13 | +import org.apache.lucene.index.LeafReaderContext; |
| 14 | +import org.apache.lucene.util.BytesRef; |
| 15 | +import org.elasticsearch.core.Nullable; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | + |
| 19 | +public class BytesRefsFromBinaryBlockLoader extends BlockDocValuesReader.DocValuesBlockLoader { |
| 20 | + |
| 21 | + private final String fieldName; |
| 22 | + |
| 23 | + public BytesRefsFromBinaryBlockLoader(String fieldName) { |
| 24 | + this.fieldName = fieldName; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public Builder builder(BlockFactory factory, int expectedCount) { |
| 29 | + return factory.bytesRefs(expectedCount); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public AllReader reader(LeafReaderContext context) throws IOException { |
| 34 | + BinaryDocValues docValues = context.reader().getBinaryDocValues(fieldName); |
| 35 | + return createReader(docValues); |
| 36 | + } |
| 37 | + |
| 38 | + public static AllReader createReader(@Nullable BinaryDocValues docValues) { |
| 39 | + if (docValues == null) { |
| 40 | + return new ConstantNullsReader(); |
| 41 | + } |
| 42 | + return new BytesRefsFromBinary(docValues); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Read BinaryDocValues with no additional structure in the BytesRefs. |
| 47 | + * Each BytesRef from the doc values maps directly to a value in the block loader. |
| 48 | + */ |
| 49 | + static class BytesRefsFromBinary extends BytesRefsFromCustomBinaryBlockLoader.AbstractBytesRefsFromBinary { |
| 50 | + |
| 51 | + BytesRefsFromBinary(BinaryDocValues docValues) { |
| 52 | + super(docValues); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void read(int doc, BytesRefBuilder builder) throws IOException { |
| 57 | + if (false == docValues.advanceExact(doc)) { |
| 58 | + builder.appendNull(); |
| 59 | + return; |
| 60 | + } |
| 61 | + BytesRef bytes = docValues.binaryValue(); |
| 62 | + builder.appendBytesRef(bytes); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public String toString() { |
| 67 | + return "BlockDocValuesReader.Bytes"; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments