@Override @ForceInline public void write(long offsetInRDO, @NotNull ByteBuffer bytes, int offset, int length) { if (bytes.isDirect()) { memory.copyMemory(((DirectBuffer) bytes).address(), address + translate(offsetInRDO), length); } else { memory.copyMemory(bytes.array(), offset, address + translate(offsetInRDO), length); } }
public int byteCheckSum() throws IORuntimeException { if (readLimit() >= Integer.MAX_VALUE || start() != 0) throw new AssertionError(); byte b = 0; NativeBytesStore bytesStore = (NativeBytesStore) bytesStore(); Memory memory = bytesStore.memory; assert memory != null; for (int i = (int) readPosition(), lim = (int) readLimit(); i < lim; i++) { b += memory.readByte(bytesStore.address + i); } return b & 0xFF; }
@NotNull private static NativeBytesStore<Void> of(long capacity, boolean zeroOut, boolean elastic) throws IllegalArgumentException { Memory memory = OS.memory(); long address = memory.allocate(capacity); if (zeroOut || capacity < MEMORY_MAPPED_SIZE) { memory.setMemory(address, capacity, (byte) 0); memory.storeFence(); } Deallocator deallocator = new Deallocator(address, capacity); return new NativeBytesStore<>(address, capacity, deallocator, elastic); }
@NotNull @Override @ForceInline public NativeBytesStore<Underlying> writeFloat(long offset, float f) { memory.writeFloat(address + translate(offset), f); return this; }
@Override @ForceInline public byte readByte(long offset) { if (Jvm.isDebug()) checkReleased(); return memory.readByte(address + translate(offset)); }
@NotNull @Override @ForceInline public NativeBytesStore<Underlying> writeDouble(long offset, double d) { memory.writeDouble(address + translate(offset), d); return this; }
@NotNull @Override @ForceInline public NativeBytesStore<Underlying> writeVolatileShort(long offset, short i16) { memory.writeVolatileShort(address + translate(offset), i16); return this; }
@NotNull @Override @ForceInline public NativeBytesStore<Underlying> writeVolatileLong(long offset, long i64) { memory.writeVolatileLong(address + translate(offset), i64); return this; }
public long appendUTF(long pos, char[] chars, int offset, int length) { long address = this.address + translate(0); Memory memory = this.memory; int i; ascii: { for (i = 0; i < length; i++) { char c = chars[offset + i]; if (c > 0x007F) break ascii; memory.writeByte(address + pos++, (byte) c); } return pos; } return appendUTF0(pos, chars, offset, length, i); }
@NotNull @Override @ForceInline public NativeBytesStore<Underlying> writeOrderedInt(long offset, int i) { memory.writeOrderedInt(address + translate(offset), i); return this; }
@NotNull @Override @ForceInline public NativeBytesStore<Underlying> write( long offsetInRDO, byte[] bytes, int offset, int length) { memory.copyMemory(bytes, offset, address + translate(offsetInRDO), length); return this; }
public long readIncompleteLong(long offset) { int remaining = (int) Math.min(8, readRemaining() - offset); long l = 0; for (int i = 0; i < remaining; i++) { byte b = memory.readByte(address + offset + i); l |= (long) (b & 0xFF) << (i * 8); } return l; }
@NotNull public Bytes<Underlying> append8bit(@NotNull CharSequence cs) throws BufferOverflowException, BufferUnderflowException, IORuntimeException { if (cs instanceof BytesStore) { return write((BytesStore) cs); } int length = cs.length(); long offset = writeOffsetPositionMoved(length); long address = bytesStore.address(offset); Memory memory = bytesStore.memory; assert memory != null; for (int i = 0; i < length; i++) { char c = cs.charAt(i); if (c > 255) c = '?'; memory.writeByte(address + i, (byte) c); } return this; }
public static void entries(long address, long entries) { if (entries >= (1L << 32)) { throw new IllegalStateException( "tier entries overflow: up to " + UNSIGNED_INT_MASK + " supported, " + entries + " given"); } memory.writeInt(address + ENTRIES_OFFSET, (int) entries); }
public static void deleted(long address, long deleted) { if (deleted >= (1L << 32)) { throw new IllegalStateException( "tier deleted entries count overflow: up to " + UNSIGNED_INT_MASK + " supported, " + deleted + " given"); } memory.writeInt(address + DELETED_OFFSET, (int) deleted); }
@Override public boolean isEqual(String s) { if (s == null || s.length() != readRemaining()) return false; char[] chars = StringUtils.extractChars(s); if (bytesStore instanceof NativeBytesStore) { NativeBytesStore bs = (NativeBytesStore) this.bytesStore; long address = bs.address + bs.translate(readPosition); Memory memory = bs.memory; for (int i = 0; i < chars.length; i++) { int b = memory.readByte(address + i) & 0xFF; if (b != chars[i]) return false; } } else { try { for (int i = 0; i < chars.length; i++) { int b = bytesStore.readByte(readPosition + i) & 0xFF; if (b != chars[i]) return false; } } catch (IORuntimeException e) { throw new AssertionError(e); } } return true; }
@NotNull @Override @ForceInline public NativeBytesStore<Underlying> zeroOut(long start, long end) throws IllegalArgumentException { if (start < writePosition() || end > writeLimit()) throw new IllegalArgumentException( "position: " + writePosition() + ", start: " + start + ", end: " + end + ", limit: " + writeLimit()); if (start >= end) return this; memory.setMemory(address + translate(start), end - start, (byte) 0); return this; }
@Override @ForceInline public byte readVolatileByte(long offset) { return memory.readVolatileByte(address + translate(offset)); }
public static int tier(long address) { return memory.readInt(address + TIER_OFFSET); }
public static void tier(long address, int tier) { memory.writeInt(address + TIER_OFFSET, tier); }
void read8bit(long position, char[] chars, int length) { long addr = address + translate(position); Memory memory = OS.memory(); for (int i = 0; i < length; i++) chars[i] = (char) (memory.readByte(addr + i) & 0xFF); }
void write8bit(long position, char[] chars, int offset, int length) { long addr = address + translate(position); Memory memory = this.memory; for (int i = 0; i < length; i++) memory.writeByte(addr + i, (byte) chars[offset + i]); }
public static long entries(long address) { return memory.readInt(address + ENTRIES_OFFSET) & UNSIGNED_INT_MASK; }
public static long deleted(long address) { return memory.readInt(address + DELETED_OFFSET) & UNSIGNED_INT_MASK; }
@Override @ForceInline public boolean compareAndSwapLong(long offset, long expected, long value) { return memory.compareAndSwapLong(address + translate(offset), expected, value); }
@Override @ForceInline public float readFloat(long offset) { return memory.readFloat(address + translate(offset)); }
@Override @ForceInline public double readDouble(long offset) { return memory.readDouble(address + translate(offset)); }
@Override @ForceInline public long readVolatileLong(long offset) { return memory.readVolatileLong(address + translate(offset)); }
@Override @ForceInline public short readVolatileShort(long offset) { return memory.readVolatileShort(address + translate(offset)); }
public static void segmentIndex(long address, int segmentIndex) { memory.writeInt(address + SEGMENT_INDEX_OFFSET, segmentIndex); }