Clenup duplication in BKDWriter (#12469)

The logic for creating the writer runnable could be deduplicated.
Also, a couple of annonymous classes could be turned into lambdas.
This commit is contained in:
Armin Braun 2023-08-03 07:16:44 +02:00 committed by GitHub
parent 229dc7481e
commit e78feb7809
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 56 deletions

View File

@ -546,37 +546,7 @@ public class BKDWriter implements Closeable {
scratchBytesRef1.length = config.bytesPerDim; scratchBytesRef1.length = config.bytesPerDim;
scratchBytesRef1.bytes = splitPackedValues; scratchBytesRef1.bytes = splitPackedValues;
BKDTreeLeafNodes leafNodes = return makeWriter(metaOut, indexOut, splitDimensionValues, leafBlockFPs, dataStartFP);
new BKDTreeLeafNodes() {
@Override
public long getLeafLP(int index) {
return leafBlockFPs[index];
}
@Override
public BytesRef getSplitValue(int index) {
scratchBytesRef1.offset = index * config.bytesPerDim;
return scratchBytesRef1;
}
@Override
public int getSplitDimension(int index) {
return splitDimensionValues[index] & 0xff;
}
@Override
public int numLeaves() {
return leafBlockFPs.length;
}
};
return () -> {
try {
writeIndex(metaOut, indexOut, config.maxPointsInLeafNode, leafNodes, dataStartFP);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
} }
/* In the 1D case, we can simply sort points in ascending order and use the /* In the 1D case, we can simply sort points in ascending order and use the
@ -830,12 +800,9 @@ public class BKDWriter implements Closeable {
scratchBytesRef1.bytes = leafValues; scratchBytesRef1.bytes = leafValues;
final IntFunction<BytesRef> packedValues = final IntFunction<BytesRef> packedValues =
new IntFunction<BytesRef>() { i -> {
@Override scratchBytesRef1.offset = config.packedBytesLength * i;
public BytesRef apply(int i) { return scratchBytesRef1;
scratchBytesRef1.offset = config.packedBytesLength * i;
return scratchBytesRef1;
}
}; };
assert valuesInOrderAndBounds( assert valuesInOrderAndBounds(
config, config,
@ -999,6 +966,15 @@ public class BKDWriter implements Closeable {
scratchBytesRef1.bytes = splitPackedValues; scratchBytesRef1.bytes = splitPackedValues;
scratchBytesRef1.length = config.bytesPerDim; scratchBytesRef1.length = config.bytesPerDim;
return makeWriter(metaOut, indexOut, splitDimensionValues, leafBlockFPs, dataStartFP);
}
private Runnable makeWriter(
IndexOutput metaOut,
IndexOutput indexOut,
byte[] splitDimensionValues,
long[] leafBlockFPs,
long dataStartFP) {
BKDTreeLeafNodes leafNodes = BKDTreeLeafNodes leafNodes =
new BKDTreeLeafNodes() { new BKDTreeLeafNodes() {
@Override @Override
@ -1717,12 +1693,9 @@ public class BKDWriter implements Closeable {
// Write the full values: // Write the full values:
IntFunction<BytesRef> packedValues = IntFunction<BytesRef> packedValues =
new IntFunction<BytesRef>() { i -> {
@Override reader.getValue(from + i, scratchBytesRef1);
public BytesRef apply(int i) { return scratchBytesRef1;
reader.getValue(from + i, scratchBytesRef1);
return scratchBytesRef1;
}
}; };
assert valuesInOrderAndBounds( assert valuesInOrderAndBounds(
config, count, sortedDim, minPackedValue, maxPackedValue, packedValues, docIDs, 0); config, count, sortedDim, minPackedValue, maxPackedValue, packedValues, docIDs, 0);
@ -1973,19 +1946,7 @@ public class BKDWriter implements Closeable {
// Write the full values: // Write the full values:
IntFunction<BytesRef> packedValues = IntFunction<BytesRef> packedValues =
new IntFunction<BytesRef>() { i -> heapSource.getPackedValueSlice(from + i).packedValue();
final BytesRef scratch = new BytesRef();
{
scratch.length = config.packedBytesLength;
}
@Override
public BytesRef apply(int i) {
PointValue value = heapSource.getPackedValueSlice(from + i);
return value.packedValue();
}
};
assert valuesInOrderAndBounds( assert valuesInOrderAndBounds(
config, count, sortedDim, minPackedValue, maxPackedValue, packedValues, docIDs, 0); config, count, sortedDim, minPackedValue, maxPackedValue, packedValues, docIDs, 0);
writeLeafBlockPackedValues( writeLeafBlockPackedValues(