@Override public void copyNormalizedKey(MemorySegment target, int offset, int len) { // see IntValue for an explanation of the logic if (len == 8) { // default case, full normalized key target.putLongBigEndian(offset, value - Long.MIN_VALUE); } else if (len <= 0) { } else if (len < 8) { long value = this.value - Long.MIN_VALUE; for (int i = 0; len > 0; len--, i++) { target.put(offset + i, (byte) (value >>> ((7-i)<<3))); } } else { target.putLongBigEndian(offset, value - Long.MIN_VALUE); for (int i = 8; i < len; i++) { target.put(offset + i, (byte) 0); } } }
@Override public void copyNormalizedKey(MemorySegment target, int offset, int len) { // take out value and add the integer min value. This gets an offset // representation when interpreted as an unsigned integer (as is the case // with normalized keys). write this value as big endian to ensure the // most significant byte comes first. if (len == 4) { target.putIntBigEndian(offset, value - Integer.MIN_VALUE); } else if (len <= 0) { } else if (len < 4) { int value = this.value - Integer.MIN_VALUE; for (int i = 0; len > 0; len--, i++) { target.put(offset + i, (byte) ((value >>> ((3-i)<<3)) & 0xff)); } } else { target.putIntBigEndian(offset, value - Integer.MIN_VALUE); for (int i = 4; i < len; i++) { target.put(offset + i, (byte) 0); } } }
@Override public void putNormalizedKey(Integer iValue, MemorySegment target, int offset, int numBytes) { int value = iValue.intValue() - Integer.MIN_VALUE; // see IntValue for an explanation of the logic if (numBytes == 4) { // default case, full normalized key target.putIntBigEndian(offset, value); } else if (numBytes <= 0) { } else if (numBytes < 4) { for (int i = 0; numBytes > 0; numBytes--, i++) { target.put(offset + i, (byte) (value >>> ((3-i)<<3))); } } else { target.putLongBigEndian(offset, value); for (int i = 4; i < numBytes; i++) { target.put(offset + i, (byte) 0); } } }
@Override public void putNormalizedKey(T record, MemorySegment target, int offset, int numBytes) { if (numBytes > 0) { // write a null byte with padding if (record == null) { target.putBoolean(offset, false); // write padding for (int j = 0; j < numBytes - 1; j++) { target.put(offset + 1 + j, (byte) 0); } } // write a non-null byte with key else { target.putBoolean(offset, true); // write key wrappedComparator.putNormalizedKey(record, target, offset + 1, numBytes - 1); } } }
@Override public void copyNormalizedKey(MemorySegment target, int offset, int len) { for (int i = offset; i < offset + len; i++) { target.put(i, (byte) 0); } }
@Override public void putNormalizedKey(T iValue, MemorySegment target, int offset, int numBytes) { int value = iValue.ordinal() - Integer.MIN_VALUE; // see IntValue for an explanation of the logic if (numBytes == 4) { // default case, full normalized key target.putIntBigEndian(offset, value); } else if (numBytes <= 0) { } else if (numBytes < 4) { for (int i = 0; numBytes > 0; numBytes--, i++) { target.put(offset + i, (byte) (value >>> ((3-i)<<3))); } } else { target.putLongBigEndian(offset, value); for (int i = 4; i < numBytes; i++) { target.put(offset + i, (byte) 0); } } }
public static void putNormalizedKeyDate(Date record, MemorySegment target, int offset, int numBytes) { final long value = record.getTime() - Long.MIN_VALUE; // see IntValue for an explanation of the logic if (numBytes == 8) { // default case, full normalized key target.putLongBigEndian(offset, value); } else if (numBytes < 8) { for (int i = 0; numBytes > 0; numBytes--, i++) { target.put(offset + i, (byte) (value >>> ((7-i)<<3))); } } else { target.putLongBigEndian(offset, value); for (int i = 8; i < numBytes; i++) { target.put(offset + i, (byte) 0); } } } }
@Override public void putNormalizedKey(Long lValue, MemorySegment target, int offset, int numBytes) { long value = lValue.longValue() - Long.MIN_VALUE; // see IntValue for an explanation of the logic if (numBytes == 8) { // default case, full normalized key target.putLongBigEndian(offset, value); } else if (numBytes <= 0) { } else if (numBytes < 8) { for (int i = 0; numBytes > 0; numBytes--, i++) { target.put(offset + i, (byte) (value >>> ((7-i)<<3))); } } else { target.putLongBigEndian(offset, value); for (int i = 8; i < numBytes; i++) { target.put(offset + i, (byte) 0); } } }
@Override public void copyNormalizedKey(MemorySegment target, int offset, int len) { if (len == 1) { // default case, full normalized key. need to explicitly convert to int to // avoid false results due to implicit type conversion to int when subtracting // the min byte value int highByte = this.value & 0xff; highByte -= Byte.MIN_VALUE; target.put(offset, (byte) highByte); } else if (len > 1) { int highByte = this.value & 0xff; highByte -= Byte.MIN_VALUE; target.put(offset, (byte) highByte); for (int i = 1; i < len; i++) { target.put(offset + i, (byte) 0); } } }
@Override public void putNormalizedKey(Byte value, MemorySegment target, int offset, int numBytes) { if (numBytes == 1) { // default case, full normalized key. need to explicitly convert to int to // avoid false results due to implicit type conversion to int when subtracting // the min byte value int highByte = value & 0xff; highByte -= Byte.MIN_VALUE; target.put(offset, (byte) highByte); } else if (numBytes <= 0) { } else { int highByte = value & 0xff; highByte -= Byte.MIN_VALUE; target.put(offset, (byte) highByte); for (int i = 1; i < numBytes; i++) { target.put(offset + i, (byte) 0); } } }
@Override public void putNormalizedKey(Character value, MemorySegment target, int offset, int numBytes) { // note that the char is an unsigned data type in java and consequently needs // no code that transforms the signed representation to an offset representation // that is equivalent to unsigned, when compared byte by byte if (numBytes == 2) { // default case, full normalized key target.put(offset, (byte) ((value >>> 8) & 0xff)); target.put(offset + 1, (byte) ((value ) & 0xff)); } else if (numBytes <= 0) { } else if (numBytes == 1) { target.put(offset, (byte) ((value >>> 8) & 0xff)); } else { target.put(offset, (byte) ((value >>> 8) & 0xff)); target.put(offset + 1, (byte) ((value ) & 0xff)); for (int i = 2; i < numBytes; i++) { target.put(offset + i, (byte) 0); } } }
@Override public void copyNormalizedKey(MemorySegment target, int offset, int len) { // note that the char is an unsigned data type in java and consequently needs // no code that transforms the signed representation to an offset representation // that is equivalent to unsigned, when compared byte by byte if (len == 2) { // default case, full normalized key target.put(offset, (byte) ((value >>> 8) & 0xff)); target.put(offset + 1, (byte) ((value ) & 0xff)); } else if (len <= 0) { } else if (len == 1) { target.put(offset, (byte) ((value >>> 8) & 0xff)); } else { target.put(offset, (byte) ((value >>> 8) & 0xff)); target.put(offset + 1, (byte) ((value ) & 0xff)); for (int i = 2; i < len; i++) { target.put(offset + i, (byte) 0); } } }
private void testByteBufferPut(boolean directBuffer) { byte[] bytes = new byte[pageSize]; random.nextBytes(bytes); ByteBuffer source = directBuffer ? ByteBuffer.allocateDirect(pageSize) : ByteBuffer.allocate(pageSize); source.put(bytes); source.clear(); MemorySegment seg = createSegment(3 * pageSize); int offset = 2 * pageSize; // transfer the segment in chunks into the byte buffer int pos = 0; while (pos < pageSize) { int len = random.nextInt(pageSize / 10); len = Math.min(len, pageSize - pos); seg.put(offset + pos, source, len); pos += len; } // verify that we read the same bytes byte[] result = new byte[pageSize]; seg.get(offset, result); assertArrayEquals(bytes, result); }
private void testSlicedByteBufferGet(boolean directBuffer) { MemorySegment seg = createSegment(pageSize); byte[] bytes = new byte[pageSize]; random.nextBytes(bytes); seg.put(0, bytes); ByteBuffer target = directBuffer ? ByteBuffer.allocateDirect(pageSize + 49) : ByteBuffer.allocate(pageSize + 49); target.position(19).limit(19 + pageSize); ByteBuffer slicedTarget = target.slice(); // transfer the segment in chunks into the byte buffer int pos = 0; while (pos < pageSize) { int len = random.nextInt(pageSize / 10); len = Math.min(len, pageSize - pos); seg.get(pos, slicedTarget, len); pos += len; } // verify that we wrote the same bytes byte[] result = new byte[pageSize]; target.position(19); target.get(result); assertArrayEquals(bytes, result); }
private void testByteBufferGet(boolean directBuffer) { MemorySegment seg = createSegment(pageSize); byte[] bytes = new byte[pageSize]; random.nextBytes(bytes); seg.put(0, bytes); ByteBuffer target = directBuffer ? ByteBuffer.allocateDirect(3 * pageSize) : ByteBuffer.allocate(3 * pageSize); target.position(2 * pageSize); // transfer the segment in chunks into the byte buffer int pos = 0; while (pos < pageSize) { int len = random.nextInt(pageSize / 10); len = Math.min(len, pageSize - pos); seg.get(pos, target, len); pos += len; } // verify that we wrote the same bytes byte[] result = new byte[pageSize]; target.position(2 * pageSize); target.get(result); assertArrayEquals(bytes, result); }
private static BufferOrEvent createBuffer(int channel, int pageSize) { final int size = sizeCounter++; byte[] bytes = new byte[size]; RND.nextBytes(bytes); MemorySegment memory = MemorySegmentFactory.allocateUnpooledSegment(pageSize); memory.put(0, bytes); Buffer buf = new NetworkBuffer(memory, FreeingBufferRecycler.INSTANCE); buf.setSize(size); // retain an additional time so it does not get disposed after being read by the input gate buf.retainBuffer(); return new BufferOrEvent(buf, channel); }
private static BufferOrEvent createBuffer(int channel, int size) { byte[] bytes = new byte[size]; RND.nextBytes(bytes); MemorySegment memory = MemorySegmentFactory.allocateUnpooledSegment(PAGE_SIZE); memory.put(0, bytes); Buffer buf = new NetworkBuffer(memory, FreeingBufferRecycler.INSTANCE); buf.setSize(size); // retain an additional time so it does not get disposed after being read by the input gate buf.retainBuffer(); return new BufferOrEvent(buf, channel); }
public static BufferOrEvent generateRandomBuffer(int size, int channelIndex) { MemorySegment seg = MemorySegmentFactory.allocateUnpooledSegment(PAGE_SIZE); for (int i = 0; i < size; i++) { seg.put(i, (byte) i); } Buffer buf = new NetworkBuffer(seg, FreeingBufferRecycler.INSTANCE); buf.setSize(size); return new BufferOrEvent(buf, channelIndex); }