diff --git a/server/src/main/java/org/elasticsearch/index/codec/ForUtil.java b/server/src/main/java/org/elasticsearch/index/codec/ForUtil.java index 722894be91c79..00e6001f07f2a 100644 --- a/server/src/main/java/org/elasticsearch/index/codec/ForUtil.java +++ b/server/src/main/java/org/elasticsearch/index/codec/ForUtil.java @@ -21,8 +21,10 @@ // else we pack 2 ints per long public final class ForUtil { - public static final int BLOCK_SIZE = 128; - private static final int BLOCK_SIZE_LOG2 = 7; + public static final int BLOCK_SIZE_SHIFT = 7; + public static final int BLOCK_SIZE = 1 << BLOCK_SIZE_SHIFT; + private static final int BLOCK_SIZE_MASK = BLOCK_SIZE - 1; + private static final ThreadLocal scratch = ThreadLocal.withInitial(() -> new long[BLOCK_SIZE / 2]); private ForUtil() {} @@ -51,95 +53,103 @@ private static long mask8(int bitsPerValue) { return expandMask8((1L << bitsPerValue) - 1); } - private static void expand8(long[] arr) { + private static void expand8(long[] arr, int offset) { for (int i = 0; i < 16; ++i) { - long l = arr[i]; - arr[i] = (l >>> 56) & 0xFFL; - arr[16 + i] = (l >>> 48) & 0xFFL; - arr[32 + i] = (l >>> 40) & 0xFFL; - arr[48 + i] = (l >>> 32) & 0xFFL; - arr[64 + i] = (l >>> 24) & 0xFFL; - arr[80 + i] = (l >>> 16) & 0xFFL; - arr[96 + i] = (l >>> 8) & 0xFFL; - arr[112 + i] = l & 0xFFL; + long l = arr[i + offset]; + arr[i + offset] = (l >>> 56) & 0xFFL; + arr[16 + i + offset] = (l >>> 48) & 0xFFL; + arr[32 + i + offset] = (l >>> 40) & 0xFFL; + arr[48 + i + offset] = (l >>> 32) & 0xFFL; + arr[64 + i + offset] = (l >>> 24) & 0xFFL; + arr[80 + i + offset] = (l >>> 16) & 0xFFL; + arr[96 + i + offset] = (l >>> 8) & 0xFFL; + arr[112 + i + offset] = l & 0xFFL; } } - private static void expand8To32(long[] arr) { + private static void expand8To32(long[] arr, int offset) { for (int i = 0; i < 16; ++i) { - long l = arr[i]; - arr[i] = (l >>> 24) & 0x000000FF000000FFL; - arr[16 + i] = (l >>> 16) & 0x000000FF000000FFL; - arr[32 + i] = (l >>> 8) & 0x000000FF000000FFL; - arr[48 + i] = l & 0x000000FF000000FFL; + long l = arr[i + offset]; + arr[i + offset] = (l >>> 24) & 0x000000FF000000FFL; + arr[16 + i + offset] = (l >>> 16) & 0x000000FF000000FFL; + arr[32 + i + offset] = (l >>> 8) & 0x000000FF000000FFL; + arr[48 + i + offset] = l & 0x000000FF000000FFL; } } - private static void collapse8(long[] arr) { + private static void collapse8(long[] arr, int offset) { for (int i = 0; i < 16; ++i) { - arr[i] = (arr[i] << 56) | (arr[16 + i] << 48) | (arr[32 + i] << 40) | (arr[48 + i] << 32) | (arr[64 + i] << 24) | (arr[80 + i] - << 16) | (arr[96 + i] << 8) | arr[112 + i]; + arr[i + offset] = (arr[i + offset] << 56) | (arr[16 + i + offset] << 48) | (arr[32 + i + offset] << 40) | (arr[48 + i + offset] + << 32) | (arr[64 + i + offset] << 24) | (arr[80 + i + offset] << 16) | (arr[96 + i + offset] << 8) | arr[112 + i + offset]; } } - private static void expand16(long[] arr) { + private static void expand16(long[] arr, int offset) { for (int i = 0; i < 32; ++i) { - long l = arr[i]; - arr[i] = (l >>> 48) & 0xFFFFL; - arr[32 + i] = (l >>> 32) & 0xFFFFL; - arr[64 + i] = (l >>> 16) & 0xFFFFL; - arr[96 + i] = l & 0xFFFFL; + long l = arr[i + offset]; + arr[i + offset] = (l >>> 48) & 0xFFFFL; + arr[32 + i + offset] = (l >>> 32) & 0xFFFFL; + arr[64 + i + offset] = (l >>> 16) & 0xFFFFL; + arr[96 + i + offset] = l & 0xFFFFL; } } - private static void expand16To32(long[] arr) { + private static void expand16To32(long[] arr, int offset) { for (int i = 0; i < 32; ++i) { - long l = arr[i]; - arr[i] = (l >>> 16) & 0x0000FFFF0000FFFFL; - arr[32 + i] = l & 0x0000FFFF0000FFFFL; + long l = arr[i + offset]; + arr[i + offset] = (l >>> 16) & 0x0000FFFF0000FFFFL; + arr[32 + i + offset] = l & 0x0000FFFF0000FFFFL; } } - private static void collapse16(long[] arr) { + private static void collapse16(long[] arr, int offset) { for (int i = 0; i < 32; ++i) { - arr[i] = (arr[i] << 48) | (arr[32 + i] << 32) | (arr[64 + i] << 16) | arr[96 + i]; + arr[i + offset] = (arr[i + offset] << 48) | (arr[32 + i + offset] << 32) | (arr[64 + i + offset] << 16) | arr[96 + i + offset]; } } - private static void expand32(long[] arr) { + private static void expand32(long[] arr, int offset) { for (int i = 0; i < 64; ++i) { - long l = arr[i]; - arr[i] = l >>> 32; - arr[64 + i] = l & 0xFFFFFFFFL; + long l = arr[i + offset]; + arr[i + offset] = l >>> 32; + arr[64 + i + offset] = l & 0xFFFFFFFFL; } } - private static void collapse32(long[] arr) { + private static void collapse32(long[] arr, int offset) { for (int i = 0; i < 64; ++i) { - arr[i] = (arr[i] << 32) | arr[64 + i]; + arr[i + offset] = (arr[i + offset] << 32) | arr[64 + i + offset]; } } - /** Encode 128 integers from {@code longs} into {@code out}. */ + /** Encode an array of longs into {@code out}. The array size is expected to be a multiple of {@code BLOCK_SIZE}. */ public static void encode(long[] longs, int bitsPerValue, DataOutput out) throws IOException { + assert longs.length >= BLOCK_SIZE && (longs.length & BLOCK_SIZE_MASK) == 0 + : "expected to get an array that a multiple of " + BLOCK_SIZE + ", got" + longs.length; + for (int i = 0; i < longs.length >> BLOCK_SIZE_SHIFT; i++) { + encode(longs, BLOCK_SIZE * i, bitsPerValue, out); + } + } + + private static void encode(long[] longs, int offset, int bitsPerValue, DataOutput out) throws IOException { final int nextPrimitive; final int numLongs; if (bitsPerValue <= 8) { nextPrimitive = 8; numLongs = BLOCK_SIZE / 8; - collapse8(longs); + collapse8(longs, offset); } else if (bitsPerValue <= 16) { nextPrimitive = 16; numLongs = BLOCK_SIZE / 4; - collapse16(longs); + collapse16(longs, offset); } else { nextPrimitive = 32; numLongs = BLOCK_SIZE / 2; - collapse32(longs); + collapse32(longs, offset); } final int numLongsPerShift = bitsPerValue * 2; - int idx = 0; + int idx = offset; int shift = nextPrimitive - bitsPerValue; final long[] tmp = scratch.get(); for (int i = 0; i < numLongsPerShift; ++i) { @@ -163,7 +173,7 @@ public static void encode(long[] longs, int bitsPerValue, DataOutput out) throws int tmpIdx = 0; int remainingBitsPerValue = bitsPerValue; - while (idx < numLongs) { + while (idx < numLongs + offset) { if (remainingBitsPerValue >= remainingBitsPerLong) { remainingBitsPerValue -= remainingBitsPerLong; tmp[tmpIdx++] |= (longs[idx] >>> remainingBitsPerValue) & maskRemainingBitsPerLong; @@ -194,16 +204,16 @@ public static void encode(long[] longs, int bitsPerValue, DataOutput out) throws } } - /** Number of bytes required to encode 128 integers of {@code bitsPerValue} bits per value. */ + /** Number of bytes required to encode an array of {@code BLOCK_SIZE} longs with {@code bitsPerValue} bits per value. */ public static int numBytes(int bitsPerValue) { - return bitsPerValue << (BLOCK_SIZE_LOG2 - 3); + return bitsPerValue << (BLOCK_SIZE_SHIFT - 3); } - private static void decodeSlow(int bitsPerValue, DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decodeSlow(int bitsPerValue, DataInput in, long[] tmp, long[] longs, int offset) throws IOException { final int numLongs = bitsPerValue << 1; in.readLongs(tmp, 0, numLongs); final long mask = MASKS32[bitsPerValue]; - int longsIdx = 0; + int longsIdx = offset; int shift = 32 - bitsPerValue; for (; shift >= 0; shift -= bitsPerValue) { shiftLongs(tmp, numLongs, longs, longsIdx, shift, mask); @@ -213,7 +223,7 @@ private static void decodeSlow(int bitsPerValue, DataInput in, long[] tmp, long[ final long mask32RemainingBitsPerLong = MASKS32[remainingBitsPerLong]; int tmpIdx = 0; int remainingBits = remainingBitsPerLong; - for (; longsIdx < BLOCK_SIZE / 2; ++longsIdx) { + for (; (longsIdx & BLOCK_SIZE_MASK) < BLOCK_SIZE / 2; ++longsIdx) { int b = bitsPerValue - remainingBits; long l = (tmp[tmpIdx++] & MASKS32[remainingBits]) << b; while (b >= remainingBitsPerLong) { @@ -302,241 +312,261 @@ private static void shiftLongs(long[] a, int count, long[] b, int bi, int shift, private static final long MASK32_23 = MASKS32[23]; private static final long MASK32_24 = MASKS32[24]; - /** Decode 128 integers into {@code longs}. */ + /** Decode an encoded input stream into an array of longs. The array size is expected to be a multiple of {@code BLOCK_SIZE}. */ public static void decode(int bitsPerValue, DataInput in, long[] longs) throws IOException { + int alignedSize = longs.length; + alignedSize = (alignedSize & ~1); // Postings reader uses a buffer with size (BLOCK_SIZE + 1). + assert alignedSize >= BLOCK_SIZE && (alignedSize & BLOCK_SIZE_MASK) == 0 + : "expected to get an array that a multiple of " + BLOCK_SIZE + ", got" + alignedSize; + for (int i = 0; i < alignedSize >> BLOCK_SIZE_SHIFT; i++) { + decode(bitsPerValue, in, longs, BLOCK_SIZE * i); + } + } + + private static void decode(int bitsPerValue, DataInput in, long[] longs, int offset) throws IOException { final long[] tmp = scratch.get(); switch (bitsPerValue) { case 1: - decode1(in, tmp, longs); - expand8(longs); + decode1(in, tmp, longs, offset); + expand8(longs, offset); break; case 2: - decode2(in, tmp, longs); - expand8(longs); + decode2(in, tmp, longs, offset); + expand8(longs, offset); break; case 3: - decode3(in, tmp, longs); - expand8(longs); + decode3(in, tmp, longs, offset); + expand8(longs, offset); break; case 4: - decode4(in, tmp, longs); - expand8(longs); + decode4(in, tmp, longs, offset); + expand8(longs, offset); break; case 5: - decode5(in, tmp, longs); - expand8(longs); + decode5(in, tmp, longs, offset); + expand8(longs, offset); break; case 6: - decode6(in, tmp, longs); - expand8(longs); + decode6(in, tmp, longs, offset); + expand8(longs, offset); break; case 7: - decode7(in, tmp, longs); - expand8(longs); + decode7(in, tmp, longs, offset); + expand8(longs, offset); break; case 8: - decode8(in, tmp, longs); - expand8(longs); + decode8(in, tmp, longs, offset); + expand8(longs, offset); break; case 9: - decode9(in, tmp, longs); - expand16(longs); + decode9(in, tmp, longs, offset); + expand16(longs, offset); break; case 10: - decode10(in, tmp, longs); - expand16(longs); + decode10(in, tmp, longs, offset); + expand16(longs, offset); break; case 11: - decode11(in, tmp, longs); - expand16(longs); + decode11(in, tmp, longs, offset); + expand16(longs, offset); break; case 12: - decode12(in, tmp, longs); - expand16(longs); + decode12(in, tmp, longs, offset); + expand16(longs, offset); break; case 13: - decode13(in, tmp, longs); - expand16(longs); + decode13(in, tmp, longs, offset); + expand16(longs, offset); break; case 14: - decode14(in, tmp, longs); - expand16(longs); + decode14(in, tmp, longs, offset); + expand16(longs, offset); break; case 15: - decode15(in, tmp, longs); - expand16(longs); + decode15(in, tmp, longs, offset); + expand16(longs, offset); break; case 16: - decode16(in, tmp, longs); - expand16(longs); + decode16(in, tmp, longs, offset); + expand16(longs, offset); break; case 17: - decode17(in, tmp, longs); - expand32(longs); + decode17(in, tmp, longs, offset); + expand32(longs, offset); break; case 18: - decode18(in, tmp, longs); - expand32(longs); + decode18(in, tmp, longs, offset); + expand32(longs, offset); break; case 19: - decode19(in, tmp, longs); - expand32(longs); + decode19(in, tmp, longs, offset); + expand32(longs, offset); break; case 20: - decode20(in, tmp, longs); - expand32(longs); + decode20(in, tmp, longs, offset); + expand32(longs, offset); break; case 21: - decode21(in, tmp, longs); - expand32(longs); + decode21(in, tmp, longs, offset); + expand32(longs, offset); break; case 22: - decode22(in, tmp, longs); - expand32(longs); + decode22(in, tmp, longs, offset); + expand32(longs, offset); break; case 23: - decode23(in, tmp, longs); - expand32(longs); + decode23(in, tmp, longs, offset); + expand32(longs, offset); break; case 24: - decode24(in, tmp, longs); - expand32(longs); + decode24(in, tmp, longs, offset); + expand32(longs, offset); break; default: - decodeSlow(bitsPerValue, in, tmp, longs); - expand32(longs); + decodeSlow(bitsPerValue, in, tmp, longs, offset); + expand32(longs, offset); break; } } /** - * Decodes 128 integers into 64 {@code longs} such that each long contains two values, each - * represented with 32 bits. Values [0..63] are encoded in the high-order bits of {@code longs} - * [0..63], and values [64..127] are encoded in the low-order bits of {@code longs} [0..63]. This - * representation may allow subsequent operations to be performed on two values at a time. + * Decodes an encoded input stream into an array of longs, such that each long contains two values, each represented with 32 bits. + * Values [0..63] are encoded in the high-order bits of {@code longs} [0..63], and values [64..127] are encoded in the low-order + * bits of {@code longs} [0..63]. This representation allows subsequent operations to be performed on two values at a time. + * The size of the output array is expected to be a multiple of {@code BLOCK_SIZE}. */ public static void decodeTo32(int bitsPerValue, DataInput in, long[] longs) throws IOException { + int alignedSize = longs.length; + alignedSize = (alignedSize & ~1); // Postings reader uses a buffer with size (BLOCK_SIZE + 1). + assert alignedSize >= BLOCK_SIZE && (alignedSize & BLOCK_SIZE_MASK) == 0 + : "expected to get an array that a multiple of " + BLOCK_SIZE + ", got " + alignedSize; + for (int i = 0; i < alignedSize >> BLOCK_SIZE_SHIFT; ++i) { + decodeTo32(bitsPerValue, in, longs, BLOCK_SIZE * i); + } + } + + private static void decodeTo32(int bitsPerValue, DataInput in, long[] longs, int offset) throws IOException { final long[] tmp = scratch.get(); switch (bitsPerValue) { case 1: - decode1(in, tmp, longs); - expand8To32(longs); + decode1(in, tmp, longs, offset); + expand8To32(longs, offset); break; case 2: - decode2(in, tmp, longs); - expand8To32(longs); + decode2(in, tmp, longs, offset); + expand8To32(longs, offset); break; case 3: - decode3(in, tmp, longs); - expand8To32(longs); + decode3(in, tmp, longs, offset); + expand8To32(longs, offset); break; case 4: - decode4(in, tmp, longs); - expand8To32(longs); + decode4(in, tmp, longs, offset); + expand8To32(longs, offset); break; case 5: - decode5(in, tmp, longs); - expand8To32(longs); + decode5(in, tmp, longs, offset); + expand8To32(longs, offset); break; case 6: - decode6(in, tmp, longs); - expand8To32(longs); + decode6(in, tmp, longs, offset); + expand8To32(longs, offset); break; case 7: - decode7(in, tmp, longs); - expand8To32(longs); + decode7(in, tmp, longs, offset); + expand8To32(longs, offset); break; case 8: - decode8(in, tmp, longs); - expand8To32(longs); + decode8(in, tmp, longs, offset); + expand8To32(longs, offset); break; case 9: - decode9(in, tmp, longs); - expand16To32(longs); + decode9(in, tmp, longs, offset); + expand16To32(longs, offset); break; case 10: - decode10(in, tmp, longs); - expand16To32(longs); + decode10(in, tmp, longs, offset); + expand16To32(longs, offset); break; case 11: - decode11(in, tmp, longs); - expand16To32(longs); + decode11(in, tmp, longs, offset); + expand16To32(longs, offset); break; case 12: - decode12(in, tmp, longs); - expand16To32(longs); + decode12(in, tmp, longs, offset); + expand16To32(longs, offset); break; case 13: - decode13(in, tmp, longs); - expand16To32(longs); + decode13(in, tmp, longs, offset); + expand16To32(longs, offset); break; case 14: - decode14(in, tmp, longs); - expand16To32(longs); + decode14(in, tmp, longs, offset); + expand16To32(longs, offset); break; case 15: - decode15(in, tmp, longs); - expand16To32(longs); + decode15(in, tmp, longs, offset); + expand16To32(longs, offset); break; case 16: - decode16(in, tmp, longs); - expand16To32(longs); + decode16(in, tmp, longs, offset); + expand16To32(longs, offset); break; case 17: - decode17(in, tmp, longs); + decode17(in, tmp, longs, offset); break; case 18: - decode18(in, tmp, longs); + decode18(in, tmp, longs, offset); break; case 19: - decode19(in, tmp, longs); + decode19(in, tmp, longs, offset); break; case 20: - decode20(in, tmp, longs); + decode20(in, tmp, longs, offset); break; case 21: - decode21(in, tmp, longs); + decode21(in, tmp, longs, offset); break; case 22: - decode22(in, tmp, longs); + decode22(in, tmp, longs, offset); break; case 23: - decode23(in, tmp, longs); + decode23(in, tmp, longs, offset); break; case 24: - decode24(in, tmp, longs); + decode24(in, tmp, longs, offset); break; default: - decodeSlow(bitsPerValue, in, tmp, longs); + decodeSlow(bitsPerValue, in, tmp, longs, offset); break; } } - private static void decode1(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode1(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 2); - shiftLongs(tmp, 2, longs, 0, 7, MASK8_1); - shiftLongs(tmp, 2, longs, 2, 6, MASK8_1); - shiftLongs(tmp, 2, longs, 4, 5, MASK8_1); - shiftLongs(tmp, 2, longs, 6, 4, MASK8_1); - shiftLongs(tmp, 2, longs, 8, 3, MASK8_1); - shiftLongs(tmp, 2, longs, 10, 2, MASK8_1); - shiftLongs(tmp, 2, longs, 12, 1, MASK8_1); - shiftLongs(tmp, 2, longs, 14, 0, MASK8_1); + shiftLongs(tmp, 2, longs, offset, 7, MASK8_1); + shiftLongs(tmp, 2, longs, offset + 2, 6, MASK8_1); + shiftLongs(tmp, 2, longs, offset + 4, 5, MASK8_1); + shiftLongs(tmp, 2, longs, offset + 6, 4, MASK8_1); + shiftLongs(tmp, 2, longs, offset + 8, 3, MASK8_1); + shiftLongs(tmp, 2, longs, offset + 10, 2, MASK8_1); + shiftLongs(tmp, 2, longs, offset + 12, 1, MASK8_1); + shiftLongs(tmp, 2, longs, offset + 14, 0, MASK8_1); } - private static void decode2(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode2(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 4); - shiftLongs(tmp, 4, longs, 0, 6, MASK8_2); - shiftLongs(tmp, 4, longs, 4, 4, MASK8_2); - shiftLongs(tmp, 4, longs, 8, 2, MASK8_2); - shiftLongs(tmp, 4, longs, 12, 0, MASK8_2); + shiftLongs(tmp, 4, longs, offset, 6, MASK8_2); + shiftLongs(tmp, 4, longs, offset + 4, 4, MASK8_2); + shiftLongs(tmp, 4, longs, offset + 8, 2, MASK8_2); + shiftLongs(tmp, 4, longs, offset + 12, 0, MASK8_2); } - private static void decode3(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode3(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 6); - shiftLongs(tmp, 6, longs, 0, 5, MASK8_3); - shiftLongs(tmp, 6, longs, 6, 2, MASK8_3); - for (int iter = 0, tmpIdx = 0, longsIdx = 12; iter < 2; ++iter, tmpIdx += 3, longsIdx += 2) { + shiftLongs(tmp, 6, longs, offset, 5, MASK8_3); + shiftLongs(tmp, 6, longs, offset + 6, 2, MASK8_3); + for (int iter = 0, tmpIdx = 0, longsIdx = 12 + offset; iter < 2; ++iter, tmpIdx += 3, longsIdx += 2) { long l0 = (tmp[tmpIdx + 0] & MASK8_2) << 1; l0 |= (tmp[tmpIdx + 1] >>> 1) & MASK8_1; longs[longsIdx + 0] = l0; @@ -546,16 +576,16 @@ private static void decode3(DataInput in, long[] tmp, long[] longs) throws IOExc } } - private static void decode4(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode4(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 8); - shiftLongs(tmp, 8, longs, 0, 4, MASK8_4); - shiftLongs(tmp, 8, longs, 8, 0, MASK8_4); + shiftLongs(tmp, 8, longs, offset, 4, MASK8_4); + shiftLongs(tmp, 8, longs, offset + 8, 0, MASK8_4); } - private static void decode5(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode5(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 10); - shiftLongs(tmp, 10, longs, 0, 3, MASK8_5); - for (int iter = 0, tmpIdx = 0, longsIdx = 10; iter < 2; ++iter, tmpIdx += 5, longsIdx += 3) { + shiftLongs(tmp, 10, longs, offset, 3, MASK8_5); + for (int iter = 0, tmpIdx = 0, longsIdx = 10 + offset; iter < 2; ++iter, tmpIdx += 5, longsIdx += 3) { long l0 = (tmp[tmpIdx + 0] & MASK8_3) << 2; l0 |= (tmp[tmpIdx + 1] >>> 1) & MASK8_2; longs[longsIdx + 0] = l0; @@ -569,11 +599,11 @@ private static void decode5(DataInput in, long[] tmp, long[] longs) throws IOExc } } - private static void decode6(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode6(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 12); - shiftLongs(tmp, 12, longs, 0, 2, MASK8_6); + shiftLongs(tmp, 12, longs, offset, 2, MASK8_6); shiftLongs(tmp, 12, tmp, 0, 0, MASK8_2); - for (int iter = 0, tmpIdx = 0, longsIdx = 12; iter < 4; ++iter, tmpIdx += 3, longsIdx += 1) { + for (int iter = 0, tmpIdx = 0, longsIdx = 12 + offset; iter < 4; ++iter, tmpIdx += 3, longsIdx += 1) { long l0 = tmp[tmpIdx + 0] << 4; l0 |= tmp[tmpIdx + 1] << 2; l0 |= tmp[tmpIdx + 2] << 0; @@ -581,11 +611,11 @@ private static void decode6(DataInput in, long[] tmp, long[] longs) throws IOExc } } - private static void decode7(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode7(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 14); - shiftLongs(tmp, 14, longs, 0, 1, MASK8_7); + shiftLongs(tmp, 14, longs, offset, 1, MASK8_7); shiftLongs(tmp, 14, tmp, 0, 0, MASK8_1); - for (int iter = 0, tmpIdx = 0, longsIdx = 14; iter < 2; ++iter, tmpIdx += 7, longsIdx += 1) { + for (int iter = 0, tmpIdx = 0, longsIdx = 14 + offset; iter < 2; ++iter, tmpIdx += 7, longsIdx += 1) { long l0 = tmp[tmpIdx + 0] << 6; l0 |= tmp[tmpIdx + 1] << 5; l0 |= tmp[tmpIdx + 2] << 4; @@ -597,14 +627,14 @@ private static void decode7(DataInput in, long[] tmp, long[] longs) throws IOExc } } - private static void decode8(DataInput in, long[] tmp, long[] longs) throws IOException { - in.readLongs(longs, 0, 16); + private static void decode8(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { + in.readLongs(longs, offset, 16); } - private static void decode9(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode9(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 18); - shiftLongs(tmp, 18, longs, 0, 7, MASK16_9); - for (int iter = 0, tmpIdx = 0, longsIdx = 18; iter < 2; ++iter, tmpIdx += 9, longsIdx += 7) { + shiftLongs(tmp, 18, longs, offset, 7, MASK16_9); + for (int iter = 0, tmpIdx = 0, longsIdx = 18 + offset; iter < 2; ++iter, tmpIdx += 9, longsIdx += 7) { long l0 = (tmp[tmpIdx + 0] & MASK16_7) << 2; l0 |= (tmp[tmpIdx + 1] >>> 5) & MASK16_2; longs[longsIdx + 0] = l0; @@ -630,10 +660,10 @@ private static void decode9(DataInput in, long[] tmp, long[] longs) throws IOExc } } - private static void decode10(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode10(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 20); - shiftLongs(tmp, 20, longs, 0, 6, MASK16_10); - for (int iter = 0, tmpIdx = 0, longsIdx = 20; iter < 4; ++iter, tmpIdx += 5, longsIdx += 3) { + shiftLongs(tmp, 20, longs, offset, 6, MASK16_10); + for (int iter = 0, tmpIdx = 0, longsIdx = 20 + offset; iter < 4; ++iter, tmpIdx += 5, longsIdx += 3) { long l0 = (tmp[tmpIdx + 0] & MASK16_6) << 4; l0 |= (tmp[tmpIdx + 1] >>> 2) & MASK16_4; longs[longsIdx + 0] = l0; @@ -647,10 +677,10 @@ private static void decode10(DataInput in, long[] tmp, long[] longs) throws IOEx } } - private static void decode11(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode11(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 22); - shiftLongs(tmp, 22, longs, 0, 5, MASK16_11); - for (int iter = 0, tmpIdx = 0, longsIdx = 22; iter < 2; ++iter, tmpIdx += 11, longsIdx += 5) { + shiftLongs(tmp, 22, longs, offset, 5, MASK16_11); + for (int iter = 0, tmpIdx = 0, longsIdx = 22 + offset; iter < 2; ++iter, tmpIdx += 11, longsIdx += 5) { long l0 = (tmp[tmpIdx + 0] & MASK16_5) << 6; l0 |= (tmp[tmpIdx + 1] & MASK16_5) << 1; l0 |= (tmp[tmpIdx + 2] >>> 4) & MASK16_1; @@ -674,11 +704,11 @@ private static void decode11(DataInput in, long[] tmp, long[] longs) throws IOEx } } - private static void decode12(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode12(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 24); - shiftLongs(tmp, 24, longs, 0, 4, MASK16_12); + shiftLongs(tmp, 24, longs, offset, 4, MASK16_12); shiftLongs(tmp, 24, tmp, 0, 0, MASK16_4); - for (int iter = 0, tmpIdx = 0, longsIdx = 24; iter < 8; ++iter, tmpIdx += 3, longsIdx += 1) { + for (int iter = 0, tmpIdx = 0, longsIdx = 24 + offset; iter < 8; ++iter, tmpIdx += 3, longsIdx += 1) { long l0 = tmp[tmpIdx + 0] << 8; l0 |= tmp[tmpIdx + 1] << 4; l0 |= tmp[tmpIdx + 2] << 0; @@ -686,10 +716,10 @@ private static void decode12(DataInput in, long[] tmp, long[] longs) throws IOEx } } - private static void decode13(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode13(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 26); - shiftLongs(tmp, 26, longs, 0, 3, MASK16_13); - for (int iter = 0, tmpIdx = 0, longsIdx = 26; iter < 2; ++iter, tmpIdx += 13, longsIdx += 3) { + shiftLongs(tmp, 26, longs, offset, 3, MASK16_13); + for (int iter = 0, tmpIdx = 0, longsIdx = 26 + offset; iter < 2; ++iter, tmpIdx += 13, longsIdx += 3) { long l0 = (tmp[tmpIdx + 0] & MASK16_3) << 10; l0 |= (tmp[tmpIdx + 1] & MASK16_3) << 7; l0 |= (tmp[tmpIdx + 2] & MASK16_3) << 4; @@ -711,11 +741,11 @@ private static void decode13(DataInput in, long[] tmp, long[] longs) throws IOEx } } - private static void decode14(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode14(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 28); - shiftLongs(tmp, 28, longs, 0, 2, MASK16_14); + shiftLongs(tmp, 28, longs, offset, 2, MASK16_14); shiftLongs(tmp, 28, tmp, 0, 0, MASK16_2); - for (int iter = 0, tmpIdx = 0, longsIdx = 28; iter < 4; ++iter, tmpIdx += 7, longsIdx += 1) { + for (int iter = 0, tmpIdx = 0, longsIdx = 28 + offset; iter < 4; ++iter, tmpIdx += 7, longsIdx += 1) { long l0 = tmp[tmpIdx + 0] << 12; l0 |= tmp[tmpIdx + 1] << 10; l0 |= tmp[tmpIdx + 2] << 8; @@ -727,11 +757,11 @@ private static void decode14(DataInput in, long[] tmp, long[] longs) throws IOEx } } - private static void decode15(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode15(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 30); - shiftLongs(tmp, 30, longs, 0, 1, MASK16_15); + shiftLongs(tmp, 30, longs, offset, 1, MASK16_15); shiftLongs(tmp, 30, tmp, 0, 0, MASK16_1); - for (int iter = 0, tmpIdx = 0, longsIdx = 30; iter < 2; ++iter, tmpIdx += 15, longsIdx += 1) { + for (int iter = 0, tmpIdx = 0, longsIdx = 30 + offset; iter < 2; ++iter, tmpIdx += 15, longsIdx += 1) { long l0 = tmp[tmpIdx + 0] << 14; l0 |= tmp[tmpIdx + 1] << 13; l0 |= tmp[tmpIdx + 2] << 12; @@ -751,14 +781,14 @@ private static void decode15(DataInput in, long[] tmp, long[] longs) throws IOEx } } - private static void decode16(DataInput in, long[] tmp, long[] longs) throws IOException { - in.readLongs(longs, 0, 32); + private static void decode16(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { + in.readLongs(longs, offset, 32); } - private static void decode17(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode17(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 34); - shiftLongs(tmp, 34, longs, 0, 15, MASK32_17); - for (int iter = 0, tmpIdx = 0, longsIdx = 34; iter < 2; ++iter, tmpIdx += 17, longsIdx += 15) { + shiftLongs(tmp, 34, longs, offset, 15, MASK32_17); + for (int iter = 0, tmpIdx = 0, longsIdx = 34 + offset; iter < 2; ++iter, tmpIdx += 17, longsIdx += 15) { long l0 = (tmp[tmpIdx + 0] & MASK32_15) << 2; l0 |= (tmp[tmpIdx + 1] >>> 13) & MASK32_2; longs[longsIdx + 0] = l0; @@ -808,10 +838,10 @@ private static void decode17(DataInput in, long[] tmp, long[] longs) throws IOEx } } - private static void decode18(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode18(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 36); - shiftLongs(tmp, 36, longs, 0, 14, MASK32_18); - for (int iter = 0, tmpIdx = 0, longsIdx = 36; iter < 4; ++iter, tmpIdx += 9, longsIdx += 7) { + shiftLongs(tmp, 36, longs, offset, 14, MASK32_18); + for (int iter = 0, tmpIdx = 0, longsIdx = 36 + offset; iter < 4; ++iter, tmpIdx += 9, longsIdx += 7) { long l0 = (tmp[tmpIdx + 0] & MASK32_14) << 4; l0 |= (tmp[tmpIdx + 1] >>> 10) & MASK32_4; longs[longsIdx + 0] = l0; @@ -837,10 +867,10 @@ private static void decode18(DataInput in, long[] tmp, long[] longs) throws IOEx } } - private static void decode19(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode19(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 38); - shiftLongs(tmp, 38, longs, 0, 13, MASK32_19); - for (int iter = 0, tmpIdx = 0, longsIdx = 38; iter < 2; ++iter, tmpIdx += 19, longsIdx += 13) { + shiftLongs(tmp, 38, longs, offset, 13, MASK32_19); + for (int iter = 0, tmpIdx = 0, longsIdx = 38 + offset; iter < 2; ++iter, tmpIdx += 19, longsIdx += 13) { long l0 = (tmp[tmpIdx + 0] & MASK32_13) << 6; l0 |= (tmp[tmpIdx + 1] >>> 7) & MASK32_6; longs[longsIdx + 0] = l0; @@ -888,10 +918,10 @@ private static void decode19(DataInput in, long[] tmp, long[] longs) throws IOEx } } - private static void decode20(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode20(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 40); - shiftLongs(tmp, 40, longs, 0, 12, MASK32_20); - for (int iter = 0, tmpIdx = 0, longsIdx = 40; iter < 8; ++iter, tmpIdx += 5, longsIdx += 3) { + shiftLongs(tmp, 40, longs, offset, 12, MASK32_20); + for (int iter = 0, tmpIdx = 0, longsIdx = 40 + offset; iter < 8; ++iter, tmpIdx += 5, longsIdx += 3) { long l0 = (tmp[tmpIdx + 0] & MASK32_12) << 8; l0 |= (tmp[tmpIdx + 1] >>> 4) & MASK32_8; longs[longsIdx + 0] = l0; @@ -905,10 +935,10 @@ private static void decode20(DataInput in, long[] tmp, long[] longs) throws IOEx } } - private static void decode21(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode21(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 42); - shiftLongs(tmp, 42, longs, 0, 11, MASK32_21); - for (int iter = 0, tmpIdx = 0, longsIdx = 42; iter < 2; ++iter, tmpIdx += 21, longsIdx += 11) { + shiftLongs(tmp, 42, longs, offset, 11, MASK32_21); + for (int iter = 0, tmpIdx = 0, longsIdx = 42 + offset; iter < 2; ++iter, tmpIdx += 21, longsIdx += 11) { long l0 = (tmp[tmpIdx + 0] & MASK32_11) << 10; l0 |= (tmp[tmpIdx + 1] >>> 1) & MASK32_10; longs[longsIdx + 0] = l0; @@ -954,10 +984,10 @@ private static void decode21(DataInput in, long[] tmp, long[] longs) throws IOEx } } - private static void decode22(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode22(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 44); - shiftLongs(tmp, 44, longs, 0, 10, MASK32_22); - for (int iter = 0, tmpIdx = 0, longsIdx = 44; iter < 4; ++iter, tmpIdx += 11, longsIdx += 5) { + shiftLongs(tmp, 44, longs, offset, 10, MASK32_22); + for (int iter = 0, tmpIdx = 0, longsIdx = 44 + offset; iter < 4; ++iter, tmpIdx += 11, longsIdx += 5) { long l0 = (tmp[tmpIdx + 0] & MASK32_10) << 12; l0 |= (tmp[tmpIdx + 1] & MASK32_10) << 2; l0 |= (tmp[tmpIdx + 2] >>> 8) & MASK32_2; @@ -981,10 +1011,10 @@ private static void decode22(DataInput in, long[] tmp, long[] longs) throws IOEx } } - private static void decode23(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode23(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 46); - shiftLongs(tmp, 46, longs, 0, 9, MASK32_23); - for (int iter = 0, tmpIdx = 0, longsIdx = 46; iter < 2; ++iter, tmpIdx += 23, longsIdx += 9) { + shiftLongs(tmp, 46, longs, offset, 9, MASK32_23); + for (int iter = 0, tmpIdx = 0, longsIdx = 46 + offset; iter < 2; ++iter, tmpIdx += 23, longsIdx += 9) { long l0 = (tmp[tmpIdx + 0] & MASK32_9) << 14; l0 |= (tmp[tmpIdx + 1] & MASK32_9) << 5; l0 |= (tmp[tmpIdx + 2] >>> 4) & MASK32_5; @@ -1028,11 +1058,11 @@ private static void decode23(DataInput in, long[] tmp, long[] longs) throws IOEx } } - private static void decode24(DataInput in, long[] tmp, long[] longs) throws IOException { + private static void decode24(DataInput in, long[] tmp, long[] longs, int offset) throws IOException { in.readLongs(tmp, 0, 48); - shiftLongs(tmp, 48, longs, 0, 8, MASK32_24); + shiftLongs(tmp, 48, longs, offset, 8, MASK32_24); shiftLongs(tmp, 48, tmp, 0, 0, MASK32_8); - for (int iter = 0, tmpIdx = 0, longsIdx = 48; iter < 16; ++iter, tmpIdx += 3, longsIdx += 1) { + for (int iter = 0, tmpIdx = 0, longsIdx = 48 + offset; iter < 16; ++iter, tmpIdx += 3, longsIdx += 1) { long l0 = tmp[tmpIdx + 0] << 16; l0 |= tmp[tmpIdx + 1] << 8; l0 |= tmp[tmpIdx + 2] << 0; diff --git a/server/src/main/java/org/elasticsearch/index/codec/tsdb/DocValuesForUtil.java b/server/src/main/java/org/elasticsearch/index/codec/tsdb/DocValuesForUtil.java index 04ac6fff604a9..3fbf897a3af89 100644 --- a/server/src/main/java/org/elasticsearch/index/codec/tsdb/DocValuesForUtil.java +++ b/server/src/main/java/org/elasticsearch/index/codec/tsdb/DocValuesForUtil.java @@ -21,11 +21,15 @@ public final class DocValuesForUtil { private static final int BITS_IN_FIVE_BYTES = 5 * Byte.SIZE; private static final int BITS_IN_SIX_BYTES = 6 * Byte.SIZE; private static final int BITS_IN_SEVEN_BYTES = 7 * Byte.SIZE; + private final int blockSize; - private final byte[] encoded = new byte[1024]; + private final byte[] encoded; public DocValuesForUtil(int numericBlockSize) { + assert numericBlockSize >= ForUtil.BLOCK_SIZE && (numericBlockSize & (ForUtil.BLOCK_SIZE - 1)) == 0 + : "expected to get a block size that a multiple of " + ForUtil.BLOCK_SIZE + ", got " + numericBlockSize; this.blockSize = numericBlockSize; + this.encoded = new byte[numericBlockSize * Long.BYTES]; } public static int roundBits(int bitsPerValue) { @@ -47,9 +51,11 @@ public void encode(long[] in, int bitsPerValue, final DataOutput out) throws IOE if (bitsPerValue <= 24) { // these bpvs are handled efficiently by ForUtil ForUtil.encode(in, bitsPerValue, out); } else if (bitsPerValue <= 32) { - collapse32(in); - for (int i = 0; i < blockSize / 2; ++i) { - out.writeLong(in[i]); + for (int k = 0; k < blockSize >> ForUtil.BLOCK_SIZE_SHIFT; k++) { + collapse32(in, k * ForUtil.BLOCK_SIZE); + for (int i = 0; i < ForUtil.BLOCK_SIZE / 2; i++) { + out.writeLong(in[k * ForUtil.BLOCK_SIZE + i]); + } } } else if (bitsPerValue == BITS_IN_FIVE_BYTES || bitsPerValue == BITS_IN_SIX_BYTES || bitsPerValue == BITS_IN_SEVEN_BYTES) { encodeFiveSixOrSevenBytesPerValue(in, bitsPerValue, out); @@ -73,8 +79,10 @@ public void decode(int bitsPerValue, final DataInput in, long[] out) throws IOEx if (bitsPerValue <= 24) { ForUtil.decode(bitsPerValue, in, out); } else if (bitsPerValue <= 32) { - in.readLongs(out, 0, blockSize / 2); - expand32(out); + for (int k = 0; k < blockSize >> ForUtil.BLOCK_SIZE_SHIFT; k++) { + in.readLongs(out, k * ForUtil.BLOCK_SIZE, ForUtil.BLOCK_SIZE / 2); + expand32(out, k * ForUtil.BLOCK_SIZE); + } } else if (bitsPerValue == BITS_IN_FIVE_BYTES || bitsPerValue == BITS_IN_SIX_BYTES || bitsPerValue == BITS_IN_SEVEN_BYTES) { decodeFiveSixOrSevenBytesPerValue(bitsPerValue, in, out); } else { @@ -94,17 +102,17 @@ private void decodeFiveSixOrSevenBytesPerValue(int bitsPerValue, final DataInput } } - private static void collapse32(long[] arr) { + private static void collapse32(long[] arr, int offset) { for (int i = 0; i < 64; ++i) { - arr[i] = (arr[i] << 32) | arr[64 + i]; + arr[i + offset] = (arr[i + offset] << 32) | arr[64 + i + offset]; } } - private static void expand32(long[] arr) { + private static void expand32(long[] arr, int offset) { for (int i = 0; i < 64; ++i) { - long l = arr[i]; - arr[i] = l >>> 32; - arr[64 + i] = l & 0xFFFFFFFFL; + long l = arr[i + offset]; + arr[i + offset] = l >>> 32; + arr[64 + i + offset] = l & 0xFFFFFFFFL; } } } diff --git a/server/src/test/java/org/elasticsearch/index/codec/ForUtilTests.java b/server/src/test/java/org/elasticsearch/index/codec/ForUtilTests.java index 55ad8fabd044f..25c8ce0970a6e 100644 --- a/server/src/test/java/org/elasticsearch/index/codec/ForUtilTests.java +++ b/server/src/test/java/org/elasticsearch/index/codec/ForUtilTests.java @@ -38,12 +38,13 @@ public class ForUtilTests extends LuceneTestCase { public void testEncodeDecode() throws IOException { final int iterations = RandomNumbers.randomIntBetween(random(), 50, 1000); - final int[] values = new int[iterations * ForUtil.BLOCK_SIZE]; + final int blockSize = ForUtil.BLOCK_SIZE << random().nextInt(0, 5); + final int[] values = new int[iterations * blockSize]; for (int i = 0; i < iterations; ++i) { final int bpv = TestUtil.nextInt(random(), 1, 31); - for (int j = 0; j < ForUtil.BLOCK_SIZE; ++j) { - values[i * ForUtil.BLOCK_SIZE + j] = RandomNumbers.randomIntBetween(random(), 0, (int) PackedInts.maxValue(bpv)); + for (int j = 0; j < blockSize; ++j) { + values[i * blockSize + j] = RandomNumbers.randomIntBetween(random(), 0, (int) PackedInts.maxValue(bpv)); } } @@ -55,10 +56,10 @@ public void testEncodeDecode() throws IOException { IndexOutput out = d.createOutput("test.bin", IOContext.DEFAULT); for (int i = 0; i < iterations; ++i) { - long[] source = new long[ForUtil.BLOCK_SIZE]; + long[] source = new long[blockSize]; long or = 0; - for (int j = 0; j < ForUtil.BLOCK_SIZE; ++j) { - source[j] = values[i * ForUtil.BLOCK_SIZE + j]; + for (int j = 0; j < blockSize; ++j) { + source[j] = values[i * blockSize + j]; or |= source[j]; } final int bpv = PackedInts.bitsRequired(or); @@ -75,18 +76,14 @@ public void testEncodeDecode() throws IOException { for (int i = 0; i < iterations; ++i) { final int bitsPerValue = in.readByte(); final long currentFilePointer = in.getFilePointer(); - final long[] restored = new long[ForUtil.BLOCK_SIZE]; + final long[] restored = new long[blockSize]; ForUtil.decode(bitsPerValue, in, restored); - int[] ints = new int[ForUtil.BLOCK_SIZE]; - for (int j = 0; j < ForUtil.BLOCK_SIZE; ++j) { + int[] ints = new int[blockSize]; + for (int j = 0; j < blockSize; ++j) { ints[j] = Math.toIntExact(restored[j]); } - assertArrayEquals( - Arrays.toString(ints), - ArrayUtil.copyOfSubArray(values, i * ForUtil.BLOCK_SIZE, (i + 1) * ForUtil.BLOCK_SIZE), - ints - ); - assertEquals(ForUtil.numBytes(bitsPerValue), in.getFilePointer() - currentFilePointer); + assertArrayEquals(Arrays.toString(ints), ArrayUtil.copyOfSubArray(values, i * blockSize, (i + 1) * blockSize), ints); + assertEquals(ForUtil.numBytes(bitsPerValue) * blockSize / ForUtil.BLOCK_SIZE, in.getFilePointer() - currentFilePointer); } assertEquals(endPointer, in.getFilePointer()); in.close(); diff --git a/server/src/test/java/org/elasticsearch/index/codec/tsdb/DocValuesForUtilTests.java b/server/src/test/java/org/elasticsearch/index/codec/tsdb/DocValuesForUtilTests.java index 62474113d73d2..aeae3d56125ff 100644 --- a/server/src/test/java/org/elasticsearch/index/codec/tsdb/DocValuesForUtilTests.java +++ b/server/src/test/java/org/elasticsearch/index/codec/tsdb/DocValuesForUtilTests.java @@ -31,7 +31,7 @@ import java.util.Random; public class DocValuesForUtilTests extends LuceneTestCase { - int NUMERIC_BLOCK_SIZE = 1 << 7; + int NUMERIC_BLOCK_SIZE = 1 << random().nextInt(7, 14); public void testEncodeDecode() throws IOException { final int iterations = RandomNumbers.randomIntBetween(random(), 50, 1000); diff --git a/server/src/test/java/org/elasticsearch/index/codec/tsdb/ES87TSDBDocValuesEncoderTests.java b/server/src/test/java/org/elasticsearch/index/codec/tsdb/ES87TSDBDocValuesEncoderTests.java index 0010c25179b69..a6a4fefd98f40 100644 --- a/server/src/test/java/org/elasticsearch/index/codec/tsdb/ES87TSDBDocValuesEncoderTests.java +++ b/server/src/test/java/org/elasticsearch/index/codec/tsdb/ES87TSDBDocValuesEncoderTests.java @@ -258,7 +258,7 @@ public void testEncodeOrdinalsNoRepetitions() throws IOException { for (int i = 0; i < blockSize; ++i) { arr[i] = i; } - doTestOrdinals(arr, 113); + doTestOrdinals(arr, -1); } public void testEncodeOrdinalsBitPack3Bits() throws IOException { @@ -267,7 +267,7 @@ public void testEncodeOrdinalsBitPack3Bits() throws IOException { for (int i = 0; i < 4; i++) { arr[i] = i; } - doTestOrdinals(arr, 49); + doTestOrdinals(arr, -1); } public void testEncodeOrdinalsCycle2() throws IOException { @@ -292,20 +292,20 @@ public void testEncodeOrdinalsCycleTooLong() throws IOException { long[] arr = new long[blockSize]; Arrays.setAll(arr, i -> i % 33); // the cycle is too long and the vales are bit-packed - doTestOrdinals(arr, 97); + doTestOrdinals(arr, -1); } public void testEncodeOrdinalsAlmostCycle() throws IOException { long[] arr = new long[blockSize]; Arrays.setAll(arr, i -> i % 3); arr[arr.length - 1] = 4; - doTestOrdinals(arr, 49); + doTestOrdinals(arr, -1); } public void testEncodeOrdinalsDifferentCycles() throws IOException { long[] arr = new long[blockSize]; Arrays.setAll(arr, i -> i > 64 ? i % 4 : i % 3); - doTestOrdinals(arr, 33); + doTestOrdinals(arr, -1); } private void doTestOrdinals(long[] arr, long expectedNumBytes) throws IOException { @@ -318,7 +318,9 @@ private void doTestOrdinals(long[] arr, long expectedNumBytes) throws IOExceptio try (Directory dir = newDirectory()) { try (IndexOutput out = dir.createOutput("tests.bin", IOContext.DEFAULT)) { encoder.encodeOrdinals(arr, out, bitsPerOrd); - assertEquals(expectedNumBytes, out.getFilePointer()); + if (expectedNumBytes >= 0) { + assertEquals(expectedNumBytes, out.getFilePointer()); + } } try (IndexInput in = dir.openInput("tests.bin", IOContext.DEFAULT)) { long[] decoded = new long[blockSize]; diff --git a/server/src/test/java/org/elasticsearch/index/codec/tsdb/ES87TSDBDocValuesFormatTests.java b/server/src/test/java/org/elasticsearch/index/codec/tsdb/ES87TSDBDocValuesFormatTests.java index 9556c949f0fd9..6d538f2f0b601 100644 --- a/server/src/test/java/org/elasticsearch/index/codec/tsdb/ES87TSDBDocValuesFormatTests.java +++ b/server/src/test/java/org/elasticsearch/index/codec/tsdb/ES87TSDBDocValuesFormatTests.java @@ -160,7 +160,8 @@ public void testOneDocManyValues() throws Exception { IndexWriterConfig config = new IndexWriterConfig(); config.setCodec(getCodec()); try (Directory dir = newDirectory(); IndexWriter writer = new IndexWriter(dir, config)) { - int numValues = 128 + random().nextInt(1024); // > 2^7 to require two blocks + // requires two blocks + int numValues = ES87TSDBDocValuesFormat.NUMERIC_BLOCK_SIZE + random().nextInt(ES87TSDBDocValuesFormat.NUMERIC_BLOCK_SIZE * 4); Document d = new Document(); for (int i = 0; i < numValues; i++) { d.add(new SortedSetDocValuesField("dv", new BytesRef("v-" + i)));