Skip to content

Commit 76f78d2

Browse files
committed
Encapsulate address accumulation logic in separate class
1 parent d61633b commit 76f78d2

File tree

2 files changed

+87
-40
lines changed

2 files changed

+87
-40
lines changed

server/src/main/java/org/elasticsearch/index/codec/tsdb/es819/ES819TSDBDocValuesConsumer.java

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -562,46 +562,9 @@ private void writeSortedNumericField(FieldInfo field, TsdbDocValuesProducer valu
562562
writeField(field, valuesProducer, maxOrd, null);
563563
} else {
564564
assert numValues > numDocsWithField;
565-
566-
var addressMetaBuffer = new ByteBuffersDataOutput();
567-
String addressDataOutputName = null;
568-
try (
569-
var addressMetaOutput = new ByteBuffersIndexOutput(addressMetaBuffer, "meta-temp", "meta-temp");
570-
var addressDataOutput = dir.createTempOutput(data.getName(), "address-data", context)
571-
) {
572-
addressDataOutputName = addressDataOutput.getName();
573-
final DirectMonotonicWriter addressesWriter = DirectMonotonicWriter.getInstance(
574-
addressMetaOutput,
575-
addressDataOutput,
576-
numDocsWithField + 1L,
577-
DIRECT_MONOTONIC_BLOCK_SHIFT
578-
);
579-
// We write this initial zero because the offset for the first document is zero.
580-
addressesWriter.add(0);
581-
writeField(field, valuesProducer, maxOrd, new CheckedIntConsumer<>() {
582-
long addr = 0;
583-
584-
@Override
585-
public void accept(int docValueCount) throws IOException {
586-
addr += docValueCount;
587-
addressesWriter.add(addr);
588-
}
589-
});
590-
addressesWriter.finish();
591-
592-
long start = data.getFilePointer();
593-
meta.writeLong(start);
594-
meta.writeVInt(DIRECT_MONOTONIC_BLOCK_SHIFT);
595-
addressMetaBuffer.copyTo(meta);
596-
addressDataOutput.close();
597-
try (var addressDataInput = dir.openInput(addressDataOutput.getName(), context)) {
598-
data.copyBytes(addressDataInput, addressDataInput.length());
599-
meta.writeLong(data.getFilePointer() - start);
600-
}
601-
} finally {
602-
if (addressDataOutputName != null) {
603-
dir.deleteFile(addressDataOutputName);
604-
}
565+
try (var accumulator = new OffsetsAccumulator(dir, context, data, numDocsWithField)) {
566+
writeField(field, valuesProducer, maxOrd, accumulator::addDoc);
567+
accumulator.build(meta, data);
605568
}
606569
}
607570
} else {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)