|
| 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.codec.tsdb.es819; |
| 11 | + |
| 12 | +import org.apache.lucene.store.ByteBuffersDataOutput; |
| 13 | +import org.apache.lucene.store.ByteBuffersIndexOutput; |
| 14 | +import org.apache.lucene.store.Directory; |
| 15 | +import org.apache.lucene.store.IOContext; |
| 16 | +import org.apache.lucene.store.IndexOutput; |
| 17 | +import org.apache.lucene.util.IOUtils; |
| 18 | +import org.apache.lucene.util.packed.DirectMonotonicWriter; |
| 19 | +import org.elasticsearch.core.SuppressForbidden; |
| 20 | + |
| 21 | +import java.io.Closeable; |
| 22 | +import java.io.IOException; |
| 23 | + |
| 24 | +/** |
| 25 | + * Builds the doc values address offset table iteratively, one document at a time. Useful to avoid a separate docvalues iteration |
| 26 | + * to build the address offset table. |
| 27 | + */ |
| 28 | +final class OffsetsAccumulator implements Closeable { |
| 29 | + private final Directory dir; |
| 30 | + private final IOContext context; |
| 31 | + |
| 32 | + private final ByteBuffersDataOutput addressMetaBuffer; |
| 33 | + private final ByteBuffersIndexOutput addressMetaOutput; |
| 34 | + private final IndexOutput addressDataOutput; |
| 35 | + private final DirectMonotonicWriter addressesWriter; |
| 36 | + |
| 37 | + private final String addressOffsetsTempFileName; |
| 38 | + |
| 39 | + private long addr = 0; |
| 40 | + |
| 41 | + OffsetsAccumulator(Directory dir, IOContext context, IndexOutput data, long numDocsWithField) throws IOException { |
| 42 | + this.dir = dir; |
| 43 | + this.context = context; |
| 44 | + |
| 45 | + addressMetaBuffer = new ByteBuffersDataOutput(); |
| 46 | + addressMetaOutput = new ByteBuffersIndexOutput(addressMetaBuffer, "meta-temp", "meta-temp"); |
| 47 | + addressDataOutput = dir.createTempOutput(data.getName(), "address-data", context); |
| 48 | + addressOffsetsTempFileName = addressDataOutput.getName(); |
| 49 | + addressesWriter = DirectMonotonicWriter.getInstance( |
| 50 | + addressMetaOutput, |
| 51 | + addressDataOutput, |
| 52 | + numDocsWithField + 1L, |
| 53 | + ES819TSDBDocValuesFormat.DIRECT_MONOTONIC_BLOCK_SHIFT |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + public void addDoc(int docValueCount) throws IOException { |
| 58 | + addressesWriter.add(addr); |
| 59 | + addr += docValueCount; |
| 60 | + } |
| 61 | + |
| 62 | + public void build(IndexOutput meta, IndexOutput data) throws IOException { |
| 63 | + addressesWriter.add(addr); |
| 64 | + addressesWriter.finish(); |
| 65 | + long start = data.getFilePointer(); |
| 66 | + meta.writeLong(start); |
| 67 | + meta.writeVInt(ES819TSDBDocValuesFormat.DIRECT_MONOTONIC_BLOCK_SHIFT); |
| 68 | + addressMetaBuffer.copyTo(meta); |
| 69 | + addressDataOutput.close(); |
| 70 | + try (var addressDataInput = dir.openInput(addressOffsetsTempFileName, context)) { |
| 71 | + data.copyBytes(addressDataInput, addressDataInput.length()); |
| 72 | + meta.writeLong(data.getFilePointer() - start); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + @SuppressForbidden(reason = "require usage of Lucene's IOUtils#deleteFilesIgnoringExceptions(...)") |
| 78 | + public void close() throws IOException { |
| 79 | + IOUtils.close(addressMetaOutput, addressDataOutput); |
| 80 | + if (addressOffsetsTempFileName != null) { |
| 81 | + IOUtils.deleteFilesIgnoringExceptions(dir, addressOffsetsTempFileName); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments