private static long writeMessages(long address, long canWrite, int writeCount) { long length = 0; long count = 0; // writeCount = writeCount == 1 ? 1 : ThreadLocalRandom.current().nextInt(writeCount-1)+1; long fromAddress = nbs.addressForRead(0); while (writeCount > count && length + 4 + size <= canWrite) { UnsafeMemory.UNSAFE.copyMemory(fromAddress, address + 4, size); UnsafeMemory.UNSAFE.putOrderedInt(null, address, size); address += 4 + size; length += 4 + size; count++; } // System.out.println("w "+count+" "+length); return (count << 32) | length; } }
private static long writeMessages(long address, long canWrite, int writeCount) { long length = 0; long count = 0; // writeCount = writeCount == 1 ? 1 : ThreadLocalRandom.current().nextInt(writeCount-1)+1; long fromAddress = nbs.addressForRead(0); while (writeCount > count && length + 4 + size <= canWrite) { UnsafeMemory.UNSAFE.copyMemory(fromAddress, address + 4, size); UnsafeMemory.UNSAFE.putOrderedInt(null, address, size); address += 4 + size; length += 4 + size; count++; } // System.out.println("w "+count+" "+length); return (count << 32) | length; } }
@Override public long addressForRead(long offset) throws BufferUnderflowException { return bytesStore.addressForRead(offset); }
@Override @ForceInline public void nativeRead(long position, long address, long size) throws BufferUnderflowException { // TODO add bounds checking. memory.copyMemory(addressForRead(position), address, size); }
@Override public boolean isEqual(@Nullable String s) { if (s == null || s.length() != readRemaining()) return false; try { if (Jvm.isJava9Plus()) { byte[] bytes = StringUtils.extractBytes(s); byte coder = StringUtils.getStringCoder(s); if (bytesStore instanceof NativeBytesStore) { @NotNull NativeBytesStore bs = (NativeBytesStore) this.bytesStore; long address = bs.addressForRead(readPosition); return isEqual0(bytes, coder, bs, address); } else { return isEqual1(bytes, coder, bytesStore, readPosition); } } else { char[] chars = StringUtils.extractChars(s); if (bytesStore instanceof NativeBytesStore) { @NotNull NativeBytesStore bs = (NativeBytesStore) this.bytesStore; long address = bs.addressForRead(readPosition); return isEqual0(chars, bs, address); } else { return isEqual1(chars, bytesStore, readPosition); } } } catch (BufferUnderflowException e) { throw new AssertionError(e); } }
@Override public int byteCheckSum(int start, int end) throws IORuntimeException { byte b = 0; @Nullable NativeBytesStore bytesStore = (NativeBytesStore) bytesStore(); @Nullable Memory memory = bytesStore.memory; assert memory != null; long addr = bytesStore.addressForRead(start); int i = 0, len = end - start; for (; i < len - 3; i += 4) { b += memory.readByte(addr + i) + memory.readByte(addr + i + 1) + memory.readByte(addr + i + 2) + memory.readByte(addr + i + 3); } for (; i < len; i++) { b += memory.readByte(addr + i); } return b & 0xFF; }
static long applyAsLong9to16(@NotNull BytesStore store, int remaining) { @NotNull final NativeBytesStore bytesStore = (NativeBytesStore) store.bytesStore(); final long address = bytesStore.addressForRead(store.readPosition()); long h0 = (long) remaining * K0; int left = remaining; long addrI = address; long l0 = readIncompleteLong(addrI, left); int l0a = (int) (l0 >> 32); long l1 = readIncompleteLong(addrI + 8, left - 8); int l1a = (int) (l1 >> 32); final long l2 = 0; final int l2a = 0; final long l3 = 0; final int l3a = 0; h0 += (l0 + l1a - l2a) * M0; long h1 = (l1 + l2a - l3a) * M1; long h2 = (l2 + l3a - l0a) * M2; long h3 = (l3 + l0a - l1a) * M3; return agitate(h0) ^ agitate(h1) ^ agitate(h2) ^ agitate(h3); }
static long applyAsLong17to32(@NotNull BytesStore store, int remaining) { @NotNull final NativeBytesStore bytesStore = (NativeBytesStore) store.bytesStore(); final long address = bytesStore.addressForRead(store.readPosition()); long h0 = (long) remaining * K0; int left = remaining; long addrI = address; long l0 = MEMORY.readLong(addrI); int l0a = MEMORY.readInt(addrI + TOP_BYTES); long l1 = MEMORY.readLong(addrI + 8); int l1a = MEMORY.readInt(addrI + 8 + TOP_BYTES); long l2 = readIncompleteLong(addrI + 16, left - 16); int l2a = (int) (l2 >> 32); long l3 = readIncompleteLong(addrI + 24, left - 24); int l3a = (int) (l3 >> 32); h0 += (l0 + l1a - l2a) * M0; long h1 = (l1 + l2a - l3a) * M1; long h2 = (l2 + l3a - l0a) * M2; long h3 = (l3 + l0a - l1a) * M3; return agitate(h0) ^ agitate(h1) ^ agitate(h2) ^ agitate(h3); }
public static long applyAsLong32bytesMultiple(@NotNull BytesStore store, int remaining) { @NotNull final NativeBytesStore bytesStore = (NativeBytesStore) store.bytesStore(); final long address = bytesStore.addressForRead(store.readPosition()); long h0 = remaining * K0, h1 = 0, h2 = 0, h3 = 0; int i; for (i = 0; i < remaining - 31; i += 32) { if (i > 0) { h0 *= K0; h1 *= K1; h2 *= K2; h3 *= K3; } long addrI = address + i; long l0 = MEMORY.readLong(addrI); int l0a = MEMORY.readInt(addrI + TOP_BYTES); long l1 = MEMORY.readLong(addrI + 8); int l1a = MEMORY.readInt(addrI + 8 + TOP_BYTES); long l2 = MEMORY.readLong(addrI + 16); int l2a = MEMORY.readInt(addrI + 16 + TOP_BYTES); long l3 = MEMORY.readLong(addrI + 24); int l3a = MEMORY.readInt(addrI + 24 + TOP_BYTES); h0 += (l0 + l1a - l2a) * M0; h1 += (l1 + l2a - l3a) * M1; h2 += (l2 + l3a - l0a) * M2; h3 += (l3 + l0a - l1a) * M3; } return agitate(h0) ^ agitate(h1) ^ agitate(h2) ^ agitate(h3); }
public static long applyAsLongAny(@NotNull BytesStore store, long remaining) { @NotNull final NativeBytesStore bytesStore = (NativeBytesStore) store.bytesStore(); final long address = bytesStore.addressForRead(store.readPosition()); long h0 = remaining * K0, h1 = 0, h2 = 0, h3 = 0;