public MappedNativeBytes(@NotNull final MappedFile mappedFile, boolean isSingleThreaded) { this.isSingleThreaded = isSingleThreaded; this.mappedFile = mappedFile; this.start = 0; this.position = start; this.limit = this.capacity = Long.MAX_VALUE; this.chronicleUnsafe = (isSingleThreaded) ? new ChronicleUnsafe(mappedFile) : null; }
private Bytes zeroOutDirty(long start, long end) { if (start < 0 || end > limit()) throw new IllegalArgumentException("start: " + start + ", end: " + end); if (start >= end) return this; // get unaligned leading bytes ChronicleUnsafe unsafe = getChronicleUnsafe(); while (start < end && (start & 7) != 0) { byte b = unsafe.getByte(this.start + start); if (b != 0) unsafe.putByte(this.start + start, (byte) 0); start++; } // check 64-bit aligned access while (start < end - 7) { long l = unsafe.getLong(this.start + start); if (l != 0) unsafe.putLong(this.start + start, 0L); start += 8; } // check unaligned tail while (start < end) { byte b = unsafe.getByte(this.start + start); if (b != 0) unsafe.putByte(this.start + start, (byte) 0); start++; } return this; }
@Override public char readChar(long offset) { return getChronicleUnsafe().getChar(start + offset); }
@Override public boolean compare(long offset, RandomDataInput input, long inputOffset, long len) { if (offset < 0 || inputOffset < 0 || len < 0) throw new IndexOutOfBoundsException(); if (offset + len < 0 || offset + len > capacity() || inputOffset + len < 0 || inputOffset + len > input.capacity()) { return false; } long i = 0L; ChronicleUnsafe unsafe = getChronicleUnsafe(); for (; i < len - 7L; i += 8L) { if (unsafe.getLong(start + offset + i) != input.readLong(inputOffset + i)) return false; } if (i < len - 3L) { if (unsafe.getInt(start + offset + i) != input.readInt(inputOffset + i)) return false; i += 4L; } if (i < len - 1L) { if (unsafe.getChar(start + offset + i) != input.readChar(inputOffset + i)) return false; i += 2L; } if (i < len) { if (unsafe.getByte(start + offset + i) != input.readByte(inputOffset + i)) return false; } return true; }
@Override public void write(long offset, byte[] bytes, int off, int len) { if (offset < 0 || off + len > bytes.length || offset + len > capacity()) throw new IllegalArgumentException(); getChronicleUnsafe().copyMemory(bytes, BYTES_OFFSET + off, null, start + offset, len); }
@Override public double readDouble(long offset) { return getChronicleUnsafe().getDouble(start + offset); }
@Override public int readInt(long offset) { return getChronicleUnsafe().getInt(start + offset); }
@Override public float readFloat(long offset) { return getChronicleUnsafe().getFloat(start + offset); }
@Override public int readVolatileInt(long offset) { return getChronicleUnsafe().getIntVolatile(null, start + offset); }
@Override public byte readByte(long offset) { return getChronicleUnsafe().getByte(start + offset); }
@Override public boolean compareAndSwapLong(long offset, long expected, long x) { offsetChecks(offset, 8L); return getChronicleUnsafe().compareAndSwapLong(null, start + offset, expected, x); }
@Override public boolean compareAndSwapInt(long offset, int expected, int x) { offsetChecks(offset, 4L); return getChronicleUnsafe().compareAndSwapInt(null, start + offset, expected, x); }
@Override public boolean compare(long offset, RandomDataInput input, long inputOffset, long len) { if (offset < 0 || inputOffset < 0 || len < 0) throw new IndexOutOfBoundsException(); if (offset + len < 0 || offset + len > capacity() || inputOffset + len < 0 || inputOffset + len > input.capacity()) { return false; } long i = 0L; ChronicleUnsafe unsafe = getChronicleUnsafe(); for (; i < len - 7L; i += 8L) { if (unsafe.getLong(start + offset + i) != input.readLong(inputOffset + i)) return false; } if (i < len - 3L) { if (unsafe.getInt(start + offset + i) != input.readInt(inputOffset + i)) return false; i += 4L; } if (i < len - 1L) { if (unsafe.getChar(start + offset + i) != input.readChar(inputOffset + i)) return false; i += 2L; } if (i < len) { if (unsafe.getByte(start + offset + i) != input.readByte(inputOffset + i)) return false; } return true; }
@Override public void write(long offset, byte[] bytes, int off, int len) { if (offset < 0 || off + len > bytes.length || offset + len > capacity()) throw new IllegalArgumentException(); getChronicleUnsafe().copyMemory(bytes, BYTES_OFFSET + off, null, start + offset, len); }
@Override public double readDouble(long offset) { return getChronicleUnsafe().getDouble(start + offset); }
@Override public int readInt(long offset) { return getChronicleUnsafe().getInt(start + offset); }
@Override public float readFloat(long offset) { return getChronicleUnsafe().getFloat(start + offset); }
@Override public int readVolatileInt(long offset) { return getChronicleUnsafe().getIntVolatile(null, start + offset); }