This is an automated email from the ASF dual-hosted git repository. leerho pushed a commit to branch master_tempfix in repository https://gitbox.apache.org/repos/asf/datasketches-memory.git
commit 1d717867acca9b2985cf780535c70234e5f61ea9 Author: Lee Rhodes <[email protected]> AuthorDate: Thu Jan 12 17:59:48 2023 -0800 Interim -- still not working. --- .../memory/internal/AccessByteBuffer.java | 16 +-- .../internal/BBNonNativeWritableBufferImpl.java | 119 +++++++++++++------ .../internal/BBNonNativeWritableMemoryImpl.java | 100 +++++++++++----- .../memory/internal/BBWritableBufferImpl.java | 119 +++++++++++++------ .../memory/internal/BBWritableMemoryImpl.java | 100 +++++++++++----- .../memory/internal/BaseStateImpl.java | 13 +- .../memory/internal/BaseWritableBufferImpl.java | 7 +- .../memory/internal/BaseWritableMemoryImpl.java | 52 ++++++-- .../DirectNonNativeWritableBufferImpl.java | 130 ++++++++++++++------ .../DirectNonNativeWritableMemoryImpl.java | 112 +++++++++++++----- .../memory/internal/DirectWritableBufferImpl.java | 131 +++++++++++++++------ .../memory/internal/DirectWritableMemoryImpl.java | 112 +++++++++++++----- .../internal/HeapNonNativeWritableBufferImpl.java | 50 +++++--- .../internal/HeapNonNativeWritableMemoryImpl.java | 30 +++-- .../memory/internal/HeapWritableBufferImpl.java | 48 +++++--- .../memory/internal/HeapWritableMemoryImpl.java | 26 ++-- .../internal/MapNonNativeWritableBufferImpl.java | 122 ++++++++++++++----- .../internal/MapNonNativeWritableMemoryImpl.java | 103 ++++++++++++---- .../memory/internal/MapWritableBufferImpl.java | 122 ++++++++++++++----- .../memory/internal/MapWritableMemoryImpl.java | 103 ++++++++++++---- .../apache/datasketches/memory/internal/Utf8.java | 1 - .../apache/datasketches/memory/internal/Util.java | 19 +-- .../memory/internal/AaByteBufferTest.java | 53 +++++++++ .../internal/AllocateDirectMapMemoryTest.java | 18 +-- .../memory/internal/BaseStateTest.java | 4 +- .../datasketches/memory/internal/Buffer2Test.java | 4 +- .../memory/internal/SpecificLeafTest.java | 68 ++++++----- 27 files changed, 1283 insertions(+), 499 deletions(-) diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/AccessByteBuffer.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/AccessByteBuffer.java index 2ec9020..71f30a7 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/AccessByteBuffer.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/AccessByteBuffer.java @@ -48,7 +48,7 @@ final class AccessByteBuffer { final long nativeBaseOffset; final long capacityBytes; - final long regionOffset; + final long offsetBytes; final Object unsafeObj; final boolean resourceReadOnly; final ByteOrder byteOrder; //not used externally, here for reference. @@ -65,13 +65,13 @@ final class AccessByteBuffer { if (direct) { nativeBaseOffset = ((sun.nio.ch.DirectBuffer) byteBuf).address(); unsafeObj = null; - regionOffset = 0L; //address() is already adjusted for direct slices, so regionOffset = 0 + offsetBytes = 0L; //address() is already adjusted for direct slices, so regionOffset = 0 } else { nativeBaseOffset = 0L; // ByteBuffer.arrayOffset() and ByteBuffer.array() throw ReadOnlyBufferException if // ByteBuffer is read-only. This uses reflection for both writable and read-only cases. // Includes the slice() offset for heap. - regionOffset = unsafe.getInt(byteBuf, BYTE_BUFFER_OFFSET_FIELD_OFFSET); + offsetBytes = unsafe.getInt(byteBuf, BYTE_BUFFER_OFFSET_FIELD_OFFSET); unsafeObj = unsafe.getObject(byteBuf, BYTE_BUFFER_HB_FIELD_OFFSET); } } @@ -82,11 +82,11 @@ final class AccessByteBuffer { * : wrap(...). See LICENSE. */ static ByteBuffer getDummyReadOnlyDirectByteBuffer(final long address, final int capacity) { - final ByteBuffer buf = ZERO_READ_ONLY_DIRECT_BYTE_BUFFER.duplicate(); - unsafe.putLong(buf, NIO_BUFFER_ADDRESS_FIELD_OFFSET, address); - unsafe.putInt(buf, NIO_BUFFER_CAPACITY_FIELD_OFFSET, capacity); - buf.limit(capacity); - return buf; + final ByteBuffer byteBuf = ZERO_READ_ONLY_DIRECT_BYTE_BUFFER.duplicate(); + unsafe.putLong(byteBuf, NIO_BUFFER_ADDRESS_FIELD_OFFSET, address); + unsafe.putInt(byteBuf, NIO_BUFFER_CAPACITY_FIELD_OFFSET, capacity); + byteBuf.limit(capacity); + return byteBuf; } } diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BBNonNativeWritableBufferImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BBNonNativeWritableBufferImpl.java index 8fbce11..cc4e834 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BBNonNativeWritableBufferImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BBNonNativeWritableBufferImpl.java @@ -32,80 +32,127 @@ import org.apache.datasketches.memory.WritableBuffer; * @author Lee Rhodes */ final class BBNonNativeWritableBufferImpl extends NonNativeWritableBufferImpl { - private static final int id = BUFFER | NONNATIVE | BYTEBUF; - private final Object unsafeObj; - private final long nativeBaseOffset; //used to compute cumBaseOffset private final ByteBuffer byteBuf; //holds a reference to a ByteBuffer until we are done with it. + private final Object unsafeObj; + private final long nativeBaseOffset; + private final long offsetBytes; + private final long capacityBytes; + private final int typeId; + private long cumOffsetBytes; + private long regionOffsetBytes; private final MemoryRequestServer memReqSvr; - private final byte typeId; BBNonNativeWritableBufferImpl( final Object unsafeObj, final long nativeBaseOffset, - final long regionOffset, + final long offsetBytes, final long capacityBytes, final int typeId, - final ByteBuffer byteBuf, - final MemoryRequestServer memReqSvr) { - super(unsafeObj, nativeBaseOffset, regionOffset, capacityBytes); + final long cumOffsetBytes, + final MemoryRequestServer memReqSvr, + final ByteBuffer byteBuf) { + super(capacityBytes); this.unsafeObj = unsafeObj; this.nativeBaseOffset = nativeBaseOffset; - this.byteBuf = byteBuf; + this.offsetBytes = offsetBytes; + this.capacityBytes = capacityBytes; + this.typeId = removeNnBuf(typeId) | BYTEBUF | BUFFER | NONNATIVE; + this.cumOffsetBytes = cumOffsetBytes; + this.regionOffsetBytes = 0; this.memReqSvr = memReqSvr; - this.typeId = (byte) (id | (typeId & 0x7)); + this.byteBuf = byteBuf; + } + + @Override + BaseWritableBufferImpl toWritableRegion( + final long regionOffsetBytes, + final long capacityBytes, + final boolean readOnly, + final ByteOrder byteOrder) { + this.regionOffsetBytes = regionOffsetBytes; + final long newOffsetBytes = offsetBytes + regionOffsetBytes; + cumOffsetBytes += regionOffsetBytes; + int typeIdOut = removeNnBuf(typeId) | BUFFER | REGION | (readOnly ? READONLY : 0); + + if (Util.isNativeByteOrder(byteOrder)) { + typeIdOut |= NATIVE; + return new BBWritableBufferImpl( + unsafeObj, nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } else { + typeIdOut |= NONNATIVE; + return new BBNonNativeWritableBufferImpl( + unsafeObj, nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } } @Override - BaseWritableBufferImpl toWritableRegion(final long offsetBytes, final long capacityBytes, - final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | REGION; - return Util.isNativeByteOrder(byteOrder) - ? new BBWritableBufferImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, byteBuf, memReqSvr) - : new BBNonNativeWritableBufferImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, byteBuf, memReqSvr); + BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final ByteOrder byteOrder) { + int typeIdOut = removeNnBuf(typeId) | MEMORY | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new BBWritableMemoryImpl( + unsafeObj, nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } else { + typeIdOut |= NONNATIVE; + return new BBNonNativeWritableMemoryImpl( + unsafeObj, nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } } @Override BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | DUPLICATE; - return Util.isNativeByteOrder(byteOrder) - ? new BBWritableBufferImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(), getCapacity(), type, byteBuf, memReqSvr) - : new BBNonNativeWritableBufferImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(), getCapacity(), type, byteBuf, memReqSvr); + int typeIdOut = removeNnBuf(typeId) | BUFFER | DUPLICATE | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new BBWritableBufferImpl( + unsafeObj, nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } else { + typeIdOut |= NONNATIVE; + return new BBNonNativeWritableBufferImpl( + unsafeObj, nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } } @Override - BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly); - return Util.isNativeByteOrder(byteOrder) - ? new BBWritableMemoryImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(), getCapacity(), type, byteBuf, memReqSvr) - : new BBNonNativeWritableMemoryImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(), getCapacity(), type, byteBuf, memReqSvr); + public long getCapacity() { + assertValid(); + return capacityBytes; } @Override - public ByteBuffer getByteBuffer() { + public long getCumulativeOffset() { assertValid(); - return byteBuf; + return cumOffsetBytes; } @Override public MemoryRequestServer getMemoryRequestServer() { - assertValid(); return memReqSvr; } @Override - long getNativeBaseOffset() { + public long getNativeBaseOffset() { return nativeBaseOffset; } + @Override + public long getOffset() { + assertValid(); + return offsetBytes; + } + + @Override + public long getRegionOffset() { + assertValid(); + return regionOffsetBytes; + } + @Override int getTypeId() { - return typeId & 0xff; + assertValid(); + return typeId; } @Override diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BBNonNativeWritableMemoryImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BBNonNativeWritableMemoryImpl.java index 60ed085..6ea83f9 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BBNonNativeWritableMemoryImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BBNonNativeWritableMemoryImpl.java @@ -32,70 +32,112 @@ import org.apache.datasketches.memory.WritableMemory; * @author Lee Rhodes */ final class BBNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImpl { - private static final int id = MEMORY | NONNATIVE | BYTEBUF; - private final Object unsafeObj; - private final long nativeBaseOffset; //used to compute cumBaseOffset private final ByteBuffer byteBuf; //holds a reference to a ByteBuffer until we are done with it. + private final Object unsafeObj; + private final long nativeBaseOffset; + private final long offsetBytes; + private final long capacityBytes; + private final int typeId; + private long cumOffsetBytes; + private long regionOffsetBytes; private final MemoryRequestServer memReqSvr; - private final byte typeId; BBNonNativeWritableMemoryImpl( final Object unsafeObj, final long nativeBaseOffset, - final long regionOffset, + final long offsetBytes, final long capacityBytes, final int typeId, - final ByteBuffer byteBuf, - final MemoryRequestServer memReqSvr) { - super(unsafeObj, nativeBaseOffset, regionOffset, capacityBytes); + final long cumOffsetBytes, + final MemoryRequestServer memReqSvr, + final ByteBuffer byteBuf) { + super(); this.unsafeObj = unsafeObj; this.nativeBaseOffset = nativeBaseOffset; - this.byteBuf = byteBuf; + this.offsetBytes = offsetBytes; + this.capacityBytes = capacityBytes; + this.typeId = removeNnBuf(typeId) | BYTEBUF | MEMORY | NONNATIVE; + this.cumOffsetBytes = cumOffsetBytes; + this.regionOffsetBytes = 0; this.memReqSvr = memReqSvr; - this.typeId = (byte) (id | (typeId & 0x7)); + this.byteBuf = byteBuf; } @Override - BaseWritableMemoryImpl toWritableRegion(final long offsetBytes, final long capacityBytes, - final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | REGION; - return Util.isNativeByteOrder(byteOrder) - ? new BBWritableMemoryImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, getByteBuffer(), memReqSvr) - : new BBNonNativeWritableMemoryImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, getByteBuffer(), memReqSvr); + BaseWritableMemoryImpl toWritableRegion( + final long regionOffsetBytes, + final long capacityBytes, + final boolean readOnly, + final ByteOrder byteOrder) { + this.regionOffsetBytes = regionOffsetBytes; + final long newOffsetBytes = offsetBytes + regionOffsetBytes; + cumOffsetBytes += regionOffsetBytes; + int typeIdOut = removeNnBuf(typeId) | MEMORY | REGION | (readOnly ? READONLY : 0); + + if (Util.isNativeByteOrder(byteOrder)) { + typeIdOut |= NATIVE; + return new BBWritableMemoryImpl( + unsafeObj, nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } else { + typeIdOut |= NONNATIVE; + return new BBNonNativeWritableMemoryImpl( + unsafeObj, nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } } @Override BaseWritableBufferImpl toWritableBuffer(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly); - return Util.isNativeByteOrder(byteOrder) - ? new BBWritableBufferImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(), getCapacity(), type, byteBuf, memReqSvr) - : new BBNonNativeWritableBufferImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(), getCapacity(), type, byteBuf, memReqSvr); + int typeIdOut = removeNnBuf(typeId) | BUFFER | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new BBWritableBufferImpl( + unsafeObj, nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } else { + typeIdOut |= NONNATIVE; + return new BBNonNativeWritableBufferImpl( + unsafeObj, nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } } @Override - public ByteBuffer getByteBuffer() { + public long getCapacity() { assertValid(); - return byteBuf; + return capacityBytes; } @Override - public MemoryRequestServer getMemoryRequestServer() { + public long getCumulativeOffset() { assertValid(); + return cumOffsetBytes; + } + + @Override + public MemoryRequestServer getMemoryRequestServer() { return memReqSvr; } @Override - long getNativeBaseOffset() { + public long getNativeBaseOffset() { return nativeBaseOffset; } + @Override + public long getOffset() { + assertValid(); + return offsetBytes; + } + + @Override + public long getRegionOffset() { + assertValid(); + return regionOffsetBytes; + } + @Override int getTypeId() { - return typeId & 0xff; + assertValid(); + return typeId; } @Override diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BBWritableBufferImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BBWritableBufferImpl.java index 0288488..a05eedc 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BBWritableBufferImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BBWritableBufferImpl.java @@ -32,80 +32,127 @@ import org.apache.datasketches.memory.WritableBuffer; * @author Lee Rhodes */ final class BBWritableBufferImpl extends NativeWritableBufferImpl { - private static final int id = BUFFER | NATIVE | BYTEBUF; - private final Object unsafeObj; - private final long nativeBaseOffset; //used to compute cumBaseOffset private final ByteBuffer byteBuf; //holds a reference to a ByteBuffer until we are done with it. + private final Object unsafeObj; + private final long nativeBaseOffset; + private final long offsetBytes; + private final long capacityBytes; + private final int typeId; + private long cumOffsetBytes; + private long regionOffsetBytes; private final MemoryRequestServer memReqSvr; - private final byte typeId; BBWritableBufferImpl( final Object unsafeObj, final long nativeBaseOffset, - final long regionOffset, + final long offsetBytes, final long capacityBytes, final int typeId, - final ByteBuffer byteBuf, - final MemoryRequestServer memReqSvr) { - super(unsafeObj, nativeBaseOffset, regionOffset, capacityBytes); + final long cumOffsetBytes, + final MemoryRequestServer memReqSvr, + final ByteBuffer byteBuf) { + super(capacityBytes); this.unsafeObj = unsafeObj; this.nativeBaseOffset = nativeBaseOffset; - this.byteBuf = byteBuf; + this.offsetBytes = offsetBytes; + this.capacityBytes = capacityBytes; + this.typeId = removeNnBuf(typeId) | BYTEBUF | BUFFER | NATIVE; + this.cumOffsetBytes = cumOffsetBytes; + this.regionOffsetBytes = 0; this.memReqSvr = memReqSvr; - this.typeId = (byte) (id | (typeId & 0x7)); + this.byteBuf = byteBuf; + } + + @Override + BaseWritableBufferImpl toWritableRegion( + final long regionOffsetBytes, + final long capacityBytes, + final boolean readOnly, + final ByteOrder byteOrder) { + this.regionOffsetBytes = regionOffsetBytes; + final long newOffsetBytes = offsetBytes + regionOffsetBytes; + cumOffsetBytes += regionOffsetBytes; + int typeIdOut = removeNnBuf(typeId) | BUFFER | REGION | (readOnly ? READONLY : 0); + + if (Util.isNativeByteOrder(byteOrder)) { + typeIdOut |= NATIVE; + return new BBWritableBufferImpl( + unsafeObj, nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } else { + typeIdOut |= NONNATIVE; + return new BBNonNativeWritableBufferImpl( + unsafeObj, nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } } @Override - BaseWritableBufferImpl toWritableRegion(final long offsetBytes, final long capacityBytes, - final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | REGION; - return Util.isNativeByteOrder(byteOrder) - ? new BBWritableBufferImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, byteBuf, memReqSvr) - : new BBNonNativeWritableBufferImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, byteBuf, memReqSvr); + BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final ByteOrder byteOrder) { + int typeIdOut = removeNnBuf(typeId) | MEMORY | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new BBWritableMemoryImpl( + unsafeObj, nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } else { + typeIdOut |= NONNATIVE; + return new BBNonNativeWritableMemoryImpl( + unsafeObj, nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } } @Override BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | DUPLICATE; - return Util.isNativeByteOrder(byteOrder) - ? new BBWritableBufferImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(), getCapacity(), type, byteBuf, memReqSvr) - : new BBNonNativeWritableBufferImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(), getCapacity(), type, byteBuf, memReqSvr); + int typeIdOut = removeNnBuf(typeId) | BUFFER | DUPLICATE | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new BBWritableBufferImpl( + unsafeObj, nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } else { + typeIdOut |= NONNATIVE; + return new BBNonNativeWritableBufferImpl( + unsafeObj, nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } } @Override - BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly); - return Util.isNativeByteOrder(byteOrder) - ? new BBWritableMemoryImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(), getCapacity(), type, byteBuf, memReqSvr) - : new BBNonNativeWritableMemoryImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(), getCapacity(), type, byteBuf, memReqSvr); + public long getCapacity() { + assertValid(); + return capacityBytes; } @Override - public ByteBuffer getByteBuffer() { + public long getCumulativeOffset() { assertValid(); - return byteBuf; + return cumOffsetBytes; } @Override public MemoryRequestServer getMemoryRequestServer() { - assertValid(); return memReqSvr; } @Override - long getNativeBaseOffset() { + public long getNativeBaseOffset() { return nativeBaseOffset; } + @Override + public long getOffset() { + assertValid(); + return offsetBytes; + } + + @Override + public long getRegionOffset() { + assertValid(); + return regionOffsetBytes; + } + @Override int getTypeId() { - return typeId & 0xff; + assertValid(); + return typeId; } @Override diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BBWritableMemoryImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BBWritableMemoryImpl.java index ad06dc9..9c0cf14 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BBWritableMemoryImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BBWritableMemoryImpl.java @@ -32,70 +32,112 @@ import org.apache.datasketches.memory.WritableMemory; * @author Lee Rhodes */ final class BBWritableMemoryImpl extends NativeWritableMemoryImpl { - private static final int id = MEMORY | NATIVE | BYTEBUF; - private final Object unsafeObj; - private final long nativeBaseOffset; //used to compute cumBaseOffset private final ByteBuffer byteBuf; //holds a reference to a ByteBuffer until we are done with it. + private final Object unsafeObj; + private final long nativeBaseOffset; + private final long offsetBytes; + private final long capacityBytes; + private final int typeId; + private long cumOffsetBytes; + private long regionOffsetBytes; private final MemoryRequestServer memReqSvr; - private final byte typeId; BBWritableMemoryImpl( final Object unsafeObj, final long nativeBaseOffset, - final long regionOffset, + final long offsetBytes, final long capacityBytes, final int typeId, - final ByteBuffer byteBuf, - final MemoryRequestServer memReqSvr) { - super(unsafeObj, nativeBaseOffset, regionOffset, capacityBytes); + final long cumOffsetBytes, + final MemoryRequestServer memReqSvr, + final ByteBuffer byteBuf) { + super(); this.unsafeObj = unsafeObj; this.nativeBaseOffset = nativeBaseOffset; - this.byteBuf = byteBuf; + this.offsetBytes = offsetBytes; + this.capacityBytes = capacityBytes; + this.typeId = removeNnBuf(typeId) | BYTEBUF | MEMORY | NATIVE; + this.cumOffsetBytes = cumOffsetBytes; + this.regionOffsetBytes = 0; this.memReqSvr = memReqSvr; - this.typeId = (byte) (id | (typeId & 0x7)); + this.byteBuf = byteBuf; } @Override - BaseWritableMemoryImpl toWritableRegion(final long offsetBytes, final long capacityBytes, - final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | REGION; - return Util.isNativeByteOrder(byteOrder) - ? new BBWritableMemoryImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, getByteBuffer(), memReqSvr) - : new BBNonNativeWritableMemoryImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, getByteBuffer(), memReqSvr); + BaseWritableMemoryImpl toWritableRegion( + final long regionOffsetBytes, + final long capacityBytes, + final boolean readOnly, + final ByteOrder byteOrder) { + this.regionOffsetBytes = regionOffsetBytes; + final long newOffsetBytes = offsetBytes + regionOffsetBytes; + cumOffsetBytes += regionOffsetBytes; + int typeIdOut = removeNnBuf(typeId) | MEMORY | REGION | (readOnly ? READONLY : 0); + + if (Util.isNativeByteOrder(byteOrder)) { + typeIdOut |= NATIVE; + return new BBWritableMemoryImpl( + unsafeObj, nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } else { + typeIdOut |= NONNATIVE; + return new BBNonNativeWritableMemoryImpl( + unsafeObj, nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } } @Override BaseWritableBufferImpl toWritableBuffer(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly); - return Util.isNativeByteOrder(byteOrder) - ? new BBWritableBufferImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(), getCapacity(), type, byteBuf, memReqSvr) - : new BBNonNativeWritableBufferImpl( - unsafeObj, nativeBaseOffset, getRegionOffset(), getCapacity(), type, byteBuf, memReqSvr); + int typeIdOut = removeNnBuf(typeId) | BUFFER | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new BBWritableBufferImpl( + unsafeObj, nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } else { + typeIdOut |= NONNATIVE; + return new BBNonNativeWritableBufferImpl( + unsafeObj, nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, byteBuf); + } } @Override - public ByteBuffer getByteBuffer() { + public long getCapacity() { assertValid(); - return byteBuf; + return capacityBytes; } @Override - public MemoryRequestServer getMemoryRequestServer() { + public long getCumulativeOffset() { assertValid(); + return cumOffsetBytes; + } + + @Override + public MemoryRequestServer getMemoryRequestServer() { return memReqSvr; } @Override - long getNativeBaseOffset() { + public long getNativeBaseOffset() { return nativeBaseOffset; } + @Override + public long getOffset() { + assertValid(); + return offsetBytes; + } + + @Override + public long getRegionOffset() { + assertValid(); + return regionOffsetBytes; + } + @Override int getTypeId() { - return typeId & 0xff; + assertValid(); + return typeId; } @Override diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BaseStateImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BaseStateImpl.java index 8c64e58..a63343f 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BaseStateImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BaseStateImpl.java @@ -183,6 +183,8 @@ public abstract class BaseStateImpl implements BaseState { //Overridden by ByteBuffer, Direct and Map leafs abstract long getNativeBaseOffset(); + abstract long getOffset(); + @Override public final ByteOrder getTypeByteOrder() { return isNonNativeType(getTypeId()) ? Util.NON_NATIVE_BYTE_ORDER : ByteOrder.nativeOrder(); @@ -394,6 +396,7 @@ public abstract class BaseStateImpl implements BaseState { */ public static final String typeDecode(final int typeId) { final StringBuilder sb = new StringBuilder(); + sb.append(typeId + ": "); final int group1 = typeId & 0x7; switch (group1) { case 1 : sb.append("ReadOnly, "); break; @@ -410,7 +413,7 @@ public abstract class BaseStateImpl implements BaseState { case 0 : sb.append("Heap, "); break; case 1 : sb.append("Direct, "); break; case 2 : sb.append("Map, "); break; - case 3 : sb.append("ByteBuffer, "); break; + case 3 : sb.append("Map Direct, "); break; default: break; } final int group3 = (typeId >>> 5) & 0x1; @@ -421,10 +424,14 @@ public abstract class BaseStateImpl implements BaseState { } final int group4 = (typeId >>> 6) & 0x1; switch (group4) { - case 0 : sb.append("Memory"); break; - case 1 : sb.append("Buffer"); break; + case 0 : sb.append("Memory, "); break; + case 1 : sb.append("Buffer, "); break; default: break; } + final int group5 = (typeId >>> 7) & 0x1; + switch (group5) { + case 1 : sb.append("ByteBuffer"); break; + } return sb.toString(); } diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BaseWritableBufferImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BaseWritableBufferImpl.java index d52371a..abbb9ac 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BaseWritableBufferImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BaseWritableBufferImpl.java @@ -77,11 +77,12 @@ public abstract class BaseWritableBufferImpl extends BaseBufferImpl implements W final MemoryRequestServer memReqSvr) { final AccessByteBuffer abb = new AccessByteBuffer(byteBuf); final int typeId = (abb.resourceReadOnly || localReadOnly) ? READONLY : 0; + final long cumOffsetBytes = 0; final BaseWritableBufferImpl bwbi = Util.isNativeByteOrder(byteOrder) ? new BBWritableBufferImpl(abb.unsafeObj, abb.nativeBaseOffset, - abb.regionOffset, abb.capacityBytes, typeId, byteBuf, memReqSvr) + abb.offsetBytes, abb.capacityBytes, typeId, cumOffsetBytes, memReqSvr, byteBuf) : new BBNonNativeWritableBufferImpl(abb.unsafeObj, abb.nativeBaseOffset, - abb.regionOffset, abb.capacityBytes, typeId, byteBuf, memReqSvr); + abb.offsetBytes, abb.capacityBytes, typeId, cumOffsetBytes, memReqSvr, byteBuf); bwbi.setStartPositionEnd(0, byteBuf.position(), byteBuf.limit()); return bwbi; } @@ -294,7 +295,7 @@ public abstract class BaseWritableBufferImpl extends BaseBufferImpl implements W } /* - * Develper notes: There is no copyTo for Buffers because of the ambiguity of what to do with + * Developer notes: There is no copyTo for Buffers because of the ambiguity of what to do with * the positional values. Switch to MemoryImpl view to do copyTo. */ diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BaseWritableMemoryImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BaseWritableMemoryImpl.java index 8a44c71..24226e6 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BaseWritableMemoryImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/BaseWritableMemoryImpl.java @@ -92,8 +92,8 @@ public abstract class BaseWritableMemoryImpl extends BaseStateImpl implements Wr final long cumOffsetBytes = UnsafeUtil.getArrayBaseOffset(arr.getClass()) + offsetBytes; final int typeId = (localReadOnly ? READONLY : 0); return Util.isNativeByteOrder(byteOrder) - ? new HeapWritableMemoryImpl(arr, offsetBytes, lengthBytes, typeId, cumOffsetBytes) - : new HeapNonNativeWritableMemoryImpl(arr, offsetBytes, lengthBytes, typeId, cumOffsetBytes); + ? new HeapWritableMemoryImpl(arr, offsetBytes, lengthBytes, typeId, cumOffsetBytes, memReqSvr) + : new HeapNonNativeWritableMemoryImpl(arr, offsetBytes, lengthBytes, typeId, cumOffsetBytes, memReqSvr); } /** @@ -109,11 +109,14 @@ public abstract class BaseWritableMemoryImpl extends BaseStateImpl implements Wr final MemoryRequestServer memReqSvr) { final AccessByteBuffer abb = new AccessByteBuffer(byteBuf); final int typeId = (abb.resourceReadOnly || localReadOnly) ? READONLY : 0; + final long cumOffsetBytes = abb.offsetBytes + (abb.unsafeObj == null + ? abb.nativeBaseOffset + : UnsafeUtil.getArrayBaseOffset(abb.unsafeObj.getClass())); return Util.isNativeByteOrder(byteOrder) ? new BBWritableMemoryImpl(abb.unsafeObj, abb.nativeBaseOffset, - abb.regionOffset, abb.capacityBytes, typeId, byteBuf, memReqSvr) + abb.offsetBytes, abb.capacityBytes, typeId, cumOffsetBytes, memReqSvr, byteBuf) : new BBNonNativeWritableMemoryImpl(abb.unsafeObj, abb.nativeBaseOffset, - abb.regionOffset, abb.capacityBytes, typeId, byteBuf, memReqSvr); + abb.offsetBytes, abb.capacityBytes, typeId, cumOffsetBytes, memReqSvr, byteBuf); } /** @@ -130,11 +133,22 @@ public abstract class BaseWritableMemoryImpl extends BaseStateImpl implements Wr final AllocateDirectWritableMap dirWMap = new AllocateDirectWritableMap(file, fileOffsetBytes, capacityBytes, localReadOnly); final int typeId = (dirWMap.resourceReadOnly || localReadOnly) ? READONLY : 0; + final long cumOffsetBytes = dirWMap.nativeBaseOffset; final BaseWritableMemoryImpl wmem = Util.isNativeByteOrder(byteOrder) - ? new MapWritableMemoryImpl(dirWMap.nativeBaseOffset, 0L, capacityBytes, - typeId, dirWMap.getValid()) - : new MapNonNativeWritableMemoryImpl(dirWMap.nativeBaseOffset, 0L, capacityBytes, - typeId, dirWMap.getValid()); + ? new MapWritableMemoryImpl( + dirWMap.nativeBaseOffset, + 0L, + capacityBytes, + typeId, + cumOffsetBytes, + dirWMap.getValid()) + : new MapNonNativeWritableMemoryImpl( + dirWMap.nativeBaseOffset, + 0L, + capacityBytes, + typeId, + cumOffsetBytes, + dirWMap.getValid()); return new WritableMapHandleImpl(dirWMap, wmem); } @@ -149,11 +163,25 @@ public abstract class BaseWritableMemoryImpl extends BaseStateImpl implements Wr final ByteOrder byteOrder, final MemoryRequestServer memReqSvr) { final AllocateDirect direct = new AllocateDirect(capacityBytes); final int typeId = 0; //direct is never read-only on construction + final long nativeBaseOffset = direct.getNativeBaseOffset(); + final long cumOffsetBytes = nativeBaseOffset; final BaseWritableMemoryImpl wmem = Util.isNativeByteOrder(byteOrder) - ? new DirectWritableMemoryImpl(direct.getNativeBaseOffset(), 0L, capacityBytes, - typeId, direct.getValid(), memReqSvr) - : new DirectNonNativeWritableMemoryImpl(direct.getNativeBaseOffset(), 0L, capacityBytes, - typeId, direct.getValid(), memReqSvr); + ? new DirectWritableMemoryImpl( + nativeBaseOffset, + 0L, + capacityBytes, + typeId, + cumOffsetBytes, + memReqSvr, + direct.getValid()) + : new DirectNonNativeWritableMemoryImpl( + nativeBaseOffset, + 0L, + capacityBytes, + typeId, + cumOffsetBytes, + memReqSvr, + direct.getValid()); final WritableHandle handle = new WritableDirectHandleImpl(direct, wmem); return handle; diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/DirectNonNativeWritableBufferImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/DirectNonNativeWritableBufferImpl.java index 3b0a183..7b07234 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/DirectNonNativeWritableBufferImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/DirectNonNativeWritableBufferImpl.java @@ -31,76 +31,134 @@ import org.apache.datasketches.memory.WritableBuffer; * @author Lee Rhodes */ final class DirectNonNativeWritableBufferImpl extends NonNativeWritableBufferImpl { - private static final int id = BUFFER | NONNATIVE | DIRECT; - private final long nativeBaseOffset; //used to compute cumBaseOffset - private final StepBoolean valid; //a reference only + private final long nativeBaseOffset; + private final long offsetBytes; + private final long capacityBytes; + private final int typeId; + private long cumOffsetBytes; + private long regionOffsetBytes; private final MemoryRequestServer memReqSvr; - private final byte typeId; + private final StepBoolean valid; //a reference only DirectNonNativeWritableBufferImpl( final long nativeBaseOffset, - final long regionOffset, + final long offsetBytes, final long capacityBytes, final int typeId, - final StepBoolean valid, - final MemoryRequestServer memReqSvr) { - super(null, nativeBaseOffset, regionOffset, capacityBytes); + final long cumOffsetBytes, + final MemoryRequestServer memReqSvr, + final StepBoolean valid) { + super(capacityBytes); this.nativeBaseOffset = nativeBaseOffset; - this.valid = valid; + this.offsetBytes = offsetBytes; + this.capacityBytes = capacityBytes; + this.typeId = removeNnBuf(typeId) | DIRECT | BUFFER | NONNATIVE; //initially cannot be ReadOnly + this.cumOffsetBytes = cumOffsetBytes; + this.regionOffsetBytes = 0; this.memReqSvr = memReqSvr; - this.typeId = (byte) (id | (typeId & 0x7)); + this.valid = valid; } @Override - BaseWritableBufferImpl toWritableRegion(final long offsetBytes, final long capacityBytes, - final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | REGION; - return Util.isNativeByteOrder(byteOrder) - ? new DirectWritableBufferImpl( - nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, valid, memReqSvr) - : new DirectNonNativeWritableBufferImpl( - nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, valid, memReqSvr); + BaseWritableBufferImpl toWritableRegion( + final long regionOffsetBytes, + final long capacityBytes, + final boolean readOnly, + final ByteOrder byteOrder) { + this.regionOffsetBytes = regionOffsetBytes; + final long newOffsetBytes = offsetBytes + regionOffsetBytes; + cumOffsetBytes += regionOffsetBytes; + int typeIdOut = removeNnBuf(typeId) | BUFFER | REGION | (readOnly ? READONLY : 0); + if (Util.isNativeByteOrder(byteOrder)) { + typeIdOut |= NATIVE; + return new DirectWritableBufferImpl( + nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } else { + typeIdOut |= NONNATIVE; + return new DirectNonNativeWritableBufferImpl( + nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } + } + + @Override + BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final ByteOrder byteOrder) { + int typeIdOut = removeNnBuf(typeId) | MEMORY | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new DirectWritableMemoryImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } else { + typeIdOut |= NONNATIVE; + return new DirectNonNativeWritableMemoryImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } } @Override BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | DUPLICATE; - return Util.isNativeByteOrder(byteOrder) - ? new DirectWritableBufferImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid, memReqSvr) - : new DirectNonNativeWritableBufferImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid, memReqSvr); + int typeIdOut = removeNnBuf(typeId) | BUFFER | DUPLICATE | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new DirectWritableBufferImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } else { + typeIdOut |= NONNATIVE; + return new DirectNonNativeWritableBufferImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } } @Override - BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly); - return Util.isNativeByteOrder(byteOrder) - ? new DirectWritableMemoryImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid, memReqSvr) - : new DirectNonNativeWritableMemoryImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid, memReqSvr); + public boolean isValid() { + return valid.get(); } @Override - public MemoryRequestServer getMemoryRequestServer() { + public long getCapacity() { assertValid(); + return capacityBytes; + } + + @Override + public long getCumulativeOffset() { + assertValid(); + return cumOffsetBytes; + } + + @Override + public MemoryRequestServer getMemoryRequestServer() { return memReqSvr; } @Override - long getNativeBaseOffset() { + public long getNativeBaseOffset() { return nativeBaseOffset; } + @Override + public long getOffset() { + assertValid(); + return offsetBytes; + } + + @Override + public long getRegionOffset() { + assertValid(); + return regionOffsetBytes; + } + @Override int getTypeId() { - return typeId & 0xff; + assertValid(); + return typeId; } @Override - public boolean isValid() { - return valid.get(); + Object getUnsafeObject() { + assertValid(); + return null; } } diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/DirectNonNativeWritableMemoryImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/DirectNonNativeWritableMemoryImpl.java index 7be7e74..f5bb988 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/DirectNonNativeWritableMemoryImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/DirectNonNativeWritableMemoryImpl.java @@ -31,66 +31,120 @@ import org.apache.datasketches.memory.WritableMemory; * @author Lee Rhodes */ final class DirectNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImpl { - private static final int id = MEMORY | NONNATIVE | DIRECT; - private final long nativeBaseOffset; //used to compute cumBaseOffset - private final StepBoolean valid; //a reference only + private final long nativeBaseOffset; + private final long offsetBytes; + private final long capacityBytes; + private final int typeId; + private long cumOffsetBytes; + private long regionOffsetBytes; private final MemoryRequestServer memReqSvr; - private final byte typeId; + private final StepBoolean valid; //a reference only DirectNonNativeWritableMemoryImpl( final long nativeBaseOffset, - final long regionOffset, + final long offsetBytes, final long capacityBytes, final int typeId, - final StepBoolean valid, - final MemoryRequestServer memReqSvr) { - super(null, nativeBaseOffset, regionOffset, capacityBytes); + final long cumOffsetBytes, + final MemoryRequestServer memReqSvr, + final StepBoolean valid) { + super(); this.nativeBaseOffset = nativeBaseOffset; - this.valid = valid; + this.offsetBytes = offsetBytes; + this.capacityBytes = capacityBytes; + this.typeId = removeNnBuf(typeId) | DIRECT | MEMORY | NONNATIVE; //initially cannot be ReadOnly + this.cumOffsetBytes = cumOffsetBytes; + this.regionOffsetBytes = 0; this.memReqSvr = memReqSvr; - this.typeId = (byte) (id | (typeId & 0x7)); + this.valid = valid; } @Override - BaseWritableMemoryImpl toWritableRegion(final long offsetBytes, final long capacityBytes, - final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | REGION; - return Util.isNativeByteOrder(byteOrder) - ? new DirectWritableMemoryImpl( - nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, valid, memReqSvr) - : new DirectNonNativeWritableMemoryImpl( - nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, valid, memReqSvr); + BaseWritableMemoryImpl toWritableRegion( + final long regionOffsetBytes, + final long capacityBytes, + final boolean readOnly, + final ByteOrder byteOrder) { + this.regionOffsetBytes = regionOffsetBytes; + final long newOffsetBytes = offsetBytes + regionOffsetBytes; + cumOffsetBytes += regionOffsetBytes; + int typeIdOut = removeNnBuf(typeId) | MEMORY | REGION | (readOnly ? READONLY : 0); + + if (Util.isNativeByteOrder(byteOrder)) { + typeIdOut |= NATIVE; + return new DirectWritableMemoryImpl( + nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } else { + typeIdOut |= NONNATIVE; + return new DirectNonNativeWritableMemoryImpl( + nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } } @Override BaseWritableBufferImpl toWritableBuffer(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly); - return Util.isNativeByteOrder(byteOrder) - ? new DirectWritableBufferImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid, memReqSvr) - : new DirectNonNativeWritableBufferImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid, memReqSvr); + int typeIdOut = removeNnBuf(typeId) | BUFFER | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new DirectWritableBufferImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } else { + typeIdOut |= NONNATIVE; + return new DirectNonNativeWritableBufferImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } } @Override - public MemoryRequestServer getMemoryRequestServer() { + public boolean isValid() { + return valid.get(); + } + + @Override + public long getCapacity() { + assertValid(); + return capacityBytes; + } + + @Override + public long getCumulativeOffset() { assertValid(); + return cumOffsetBytes; + } + + @Override + public MemoryRequestServer getMemoryRequestServer() { return memReqSvr; } @Override - long getNativeBaseOffset() { + public long getNativeBaseOffset() { return nativeBaseOffset; } + @Override + public long getOffset() { + assertValid(); + return offsetBytes; + } + + @Override + public long getRegionOffset() { + assertValid(); + return regionOffsetBytes; + } + @Override int getTypeId() { - return typeId & 0xff; + assertValid(); + return typeId; } @Override - public boolean isValid() { - return valid.get(); + Object getUnsafeObject() { + assertValid(); + return null; } } diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/DirectWritableBufferImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/DirectWritableBufferImpl.java index 84e7d5e..c8147e5 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/DirectWritableBufferImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/DirectWritableBufferImpl.java @@ -31,76 +31,135 @@ import org.apache.datasketches.memory.WritableBuffer; * @author Lee Rhodes */ final class DirectWritableBufferImpl extends NativeWritableBufferImpl { - private static final int id = BUFFER | NATIVE | DIRECT; - private final long nativeBaseOffset; //used to compute cumBaseOffset - private final StepBoolean valid; //a reference only + private final long nativeBaseOffset; + private final long offsetBytes; + private final long capacityBytes; + private final int typeId; + private long cumOffsetBytes; + private long regionOffsetBytes; private final MemoryRequestServer memReqSvr; - private final byte typeId; + private final StepBoolean valid; //a reference only DirectWritableBufferImpl( final long nativeBaseOffset, - final long regionOffset, + final long offsetBytes, final long capacityBytes, final int typeId, - final StepBoolean valid, - final MemoryRequestServer memReqSvr) { - super(null, nativeBaseOffset, regionOffset, capacityBytes); + final long cumOffsetBytes, + final MemoryRequestServer memReqSvr, + final StepBoolean valid) { + super(capacityBytes); this.nativeBaseOffset = nativeBaseOffset; - this.valid = valid; + this.offsetBytes = offsetBytes; + this.capacityBytes = capacityBytes; + this.typeId = removeNnBuf(typeId) | DIRECT | BUFFER | NATIVE; //initially cannot be ReadOnly + this.cumOffsetBytes = cumOffsetBytes; + this.regionOffsetBytes = 0; this.memReqSvr = memReqSvr; - this.typeId = (byte) (id | (typeId & 0x7)); + this.valid = valid; + } + + @Override + BaseWritableBufferImpl toWritableRegion( + final long regionOffsetBytes, + final long capacityBytes, + final boolean readOnly, + final ByteOrder byteOrder) { + this.regionOffsetBytes = regionOffsetBytes; + final long newOffsetBytes = offsetBytes + regionOffsetBytes; + cumOffsetBytes += regionOffsetBytes; + int typeIdOut = removeNnBuf(typeId) | BUFFER | REGION | (readOnly ? READONLY : 0); + + if (Util.isNativeByteOrder(byteOrder)) { + typeIdOut |= NATIVE; + return new DirectWritableBufferImpl( + nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } else { + typeIdOut |= NONNATIVE; + return new DirectNonNativeWritableBufferImpl( + nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } } @Override - BaseWritableBufferImpl toWritableRegion(final long offsetBytes, final long capacityBytes, - final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | REGION; - return Util.isNativeByteOrder(byteOrder) - ? new DirectWritableBufferImpl( - nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, valid, memReqSvr) - : new DirectNonNativeWritableBufferImpl( - nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, valid, memReqSvr); + BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final ByteOrder byteOrder) { + int typeIdOut = removeNnBuf(typeId) | MEMORY | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new DirectWritableMemoryImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } else { + typeIdOut |= NONNATIVE; + return new DirectNonNativeWritableMemoryImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } } @Override BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | DUPLICATE; - return Util.isNativeByteOrder(byteOrder) - ? new DirectWritableBufferImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid, memReqSvr) - : new DirectNonNativeWritableBufferImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid, memReqSvr); + int typeIdOut = removeNnBuf(typeId) | BUFFER | DUPLICATE | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new DirectWritableBufferImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } else { + typeIdOut |= NONNATIVE; + return new DirectNonNativeWritableBufferImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } } @Override - BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly); - return Util.isNativeByteOrder(byteOrder) - ? new DirectWritableMemoryImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid, memReqSvr) - : new DirectNonNativeWritableMemoryImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid, memReqSvr); + public boolean isValid() { + return valid.get(); } @Override - public MemoryRequestServer getMemoryRequestServer() { + public long getCapacity() { + assertValid(); + return capacityBytes; + } + + @Override + public long getCumulativeOffset() { assertValid(); + return cumOffsetBytes; + } + + @Override + public MemoryRequestServer getMemoryRequestServer() { return memReqSvr; } @Override - long getNativeBaseOffset() { + public long getNativeBaseOffset() { return nativeBaseOffset; } + @Override + public long getOffset() { + assertValid(); + return offsetBytes; + } + + @Override + public long getRegionOffset() { + assertValid(); + return regionOffsetBytes; + } + @Override int getTypeId() { - return typeId & 0xff; + assertValid(); + return typeId; } @Override - public boolean isValid() { - return valid.get(); + Object getUnsafeObject() { + assertValid(); + return null; } } diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/DirectWritableMemoryImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/DirectWritableMemoryImpl.java index 68a3461..8ff0961 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/DirectWritableMemoryImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/DirectWritableMemoryImpl.java @@ -31,66 +31,120 @@ import org.apache.datasketches.memory.WritableMemory; * @author Lee Rhodes */ final class DirectWritableMemoryImpl extends NativeWritableMemoryImpl { - private static final int id = MEMORY | NATIVE | DIRECT; - private final long nativeBaseOffset; //used to compute cumBaseOffset - private final StepBoolean valid; //a reference only + private final long nativeBaseOffset; + private final long offsetBytes; + private final long capacityBytes; + private final int typeId; + private long cumOffsetBytes; + private long regionOffsetBytes; private final MemoryRequestServer memReqSvr; - private final byte typeId; + private final StepBoolean valid; //a reference only DirectWritableMemoryImpl( final long nativeBaseOffset, - final long regionOffset, + final long offsetBytes, final long capacityBytes, final int typeId, - final StepBoolean valid, - final MemoryRequestServer memReqSvr) { - super(null, nativeBaseOffset, regionOffset, capacityBytes); + final long cumOffsetBytes, + final MemoryRequestServer memReqSvr, + final StepBoolean valid) { + super(); this.nativeBaseOffset = nativeBaseOffset; - this.valid = valid; + this.offsetBytes = offsetBytes; + this.capacityBytes = capacityBytes; + this.typeId = removeNnBuf(typeId) | DIRECT | MEMORY | NATIVE; //initially cannot be ReadOnly + this.cumOffsetBytes = cumOffsetBytes; + this.regionOffsetBytes = 0; this.memReqSvr = memReqSvr; - this.typeId = (byte) (id | (typeId & 0x7)); + this.valid = valid; } @Override - BaseWritableMemoryImpl toWritableRegion(final long offsetBytes, final long capacityBytes, - final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | REGION; - return Util.isNativeByteOrder(byteOrder) - ? new DirectWritableMemoryImpl( - nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, valid, memReqSvr) - : new DirectNonNativeWritableMemoryImpl( - nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, valid, memReqSvr); + BaseWritableMemoryImpl toWritableRegion( + final long regionOffsetBytes, + final long capacityBytes, + final boolean readOnly, + final ByteOrder byteOrder) { + //this.regionOffsetBytes = regionOffsetBytes; + final long newOffsetBytes = offsetBytes + regionOffsetBytes; + cumOffsetBytes += regionOffsetBytes; + int typeIdOut = removeNnBuf(typeId) | MEMORY | REGION | (readOnly ? READONLY : 0); + + if (Util.isNativeByteOrder(byteOrder)) { + typeIdOut |= NATIVE; + return new DirectWritableMemoryImpl( + nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } else { + typeIdOut |= NONNATIVE; + return new DirectNonNativeWritableMemoryImpl( + nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } } @Override BaseWritableBufferImpl toWritableBuffer(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly); - return Util.isNativeByteOrder(byteOrder) - ? new DirectWritableBufferImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid, memReqSvr) - : new DirectNonNativeWritableBufferImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid, memReqSvr); + int typeIdOut = removeNnBuf(typeId) | BUFFER | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new DirectWritableBufferImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } else { + typeIdOut |= NONNATIVE; + return new DirectNonNativeWritableBufferImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr, valid); + } } @Override - public MemoryRequestServer getMemoryRequestServer() { + public boolean isValid() { + return valid.get(); + } + + @Override + public long getCapacity() { + assertValid(); + return capacityBytes; + } + + @Override + public long getCumulativeOffset() { assertValid(); + return cumOffsetBytes; + } + + @Override + public MemoryRequestServer getMemoryRequestServer() { return memReqSvr; } @Override - long getNativeBaseOffset() { + public long getNativeBaseOffset() { return nativeBaseOffset; } + @Override + public long getOffset() { + assertValid(); + return offsetBytes; + } + + @Override + public long getRegionOffset() { + assertValid(); + return regionOffsetBytes; + } + @Override int getTypeId() { - return typeId & 0xff; + assertValid(); + return typeId; } @Override - public boolean isValid() { - return valid.get(); + Object getUnsafeObject() { + assertValid(); + return null; } } diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/HeapNonNativeWritableBufferImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/HeapNonNativeWritableBufferImpl.java index b3e9492..91d98fa 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/HeapNonNativeWritableBufferImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/HeapNonNativeWritableBufferImpl.java @@ -37,13 +37,15 @@ final class HeapNonNativeWritableBufferImpl extends NonNativeWritableBufferImpl private final int typeId; private long cumOffsetBytes; private long regionOffsetBytes; + private final MemoryRequestServer memReqSvr; HeapNonNativeWritableBufferImpl( final Object unsafeObj, final long offsetBytes, final long capacityBytes, final int typeId, - final long cumOffsetBytes) { + final long cumOffsetBytes, + final MemoryRequestServer memReqSvr) { super(capacityBytes); this.unsafeObj = unsafeObj; this.offsetBytes = offsetBytes; @@ -51,6 +53,7 @@ final class HeapNonNativeWritableBufferImpl extends NonNativeWritableBufferImpl this.typeId = removeNnBuf(typeId) | HEAP | BUFFER | NONNATIVE; this.cumOffsetBytes = cumOffsetBytes; this.regionOffsetBytes = 0; + this.memReqSvr = memReqSvr; } @Override @@ -59,46 +62,49 @@ final class HeapNonNativeWritableBufferImpl extends NonNativeWritableBufferImpl final long capacityBytes, final boolean readOnly, final ByteOrder byteOrder) { - final Object unsafeObj = this.unsafeObj; - final long newOffsetBytes = this.offsetBytes + this.regionOffsetBytes; - this.cumOffsetBytes += this.regionOffsetBytes; + this.regionOffsetBytes = regionOffsetBytes; + final long newOffsetBytes = offsetBytes + regionOffsetBytes; + this.cumOffsetBytes += regionOffsetBytes; int typeIdOut = removeNnBuf(typeId) | BUFFER | REGION | (readOnly ? READONLY : 0); + if (Util.isNativeByteOrder(byteOrder)) { typeIdOut |= NATIVE; - return new HeapWritableBufferImpl(unsafeObj, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + return new HeapWritableBufferImpl( + unsafeObj, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } else { typeIdOut |= NONNATIVE; - return new HeapNonNativeWritableBufferImpl(unsafeObj, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + return new HeapNonNativeWritableBufferImpl( + unsafeObj, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } } @Override - BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) { - int typeIdOut = removeNnBuf(typeId) | BUFFER | DUPLICATE | (readOnly ? READONLY : 0); + BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final ByteOrder byteOrder) { + int typeIdOut = removeNnBuf(typeId) | MEMORY | (readOnly ? READONLY : 0); if (byteOrder == ByteOrder.nativeOrder()) { typeIdOut |= NATIVE; - return new HeapWritableBufferImpl( - unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + return new HeapWritableMemoryImpl( + unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } else { typeIdOut |= NONNATIVE; - return new HeapNonNativeWritableBufferImpl( - unsafeObj, regionOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + return new HeapNonNativeWritableMemoryImpl( + unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } } @Override - BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final ByteOrder byteOrder) { - int typeIdOut = removeNnBuf(typeId) | MEMORY | (readOnly ? READONLY : 0); + BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) { + int typeIdOut = removeNnBuf(typeId) | BUFFER | DUPLICATE | (readOnly ? READONLY : 0); if (byteOrder == ByteOrder.nativeOrder()) { typeIdOut |= NATIVE; - return new HeapWritableMemoryImpl( - unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + return new HeapWritableBufferImpl( + unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } else { typeIdOut |= NONNATIVE; - return new HeapNonNativeWritableMemoryImpl( - unsafeObj, regionOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + return new HeapNonNativeWritableBufferImpl( + unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } } @@ -116,7 +122,7 @@ final class HeapNonNativeWritableBufferImpl extends NonNativeWritableBufferImpl @Override public MemoryRequestServer getMemoryRequestServer() { - return null; + return memReqSvr; } @Override @@ -124,6 +130,12 @@ final class HeapNonNativeWritableBufferImpl extends NonNativeWritableBufferImpl return 0; } + @Override + public long getOffset() { + assertValid(); + return offsetBytes; + } + @Override public long getRegionOffset() { assertValid(); diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/HeapNonNativeWritableMemoryImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/HeapNonNativeWritableMemoryImpl.java index 7ca8627..5ebe8db 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/HeapNonNativeWritableMemoryImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/HeapNonNativeWritableMemoryImpl.java @@ -37,13 +37,15 @@ final class HeapNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImpl private final int typeId; private long cumOffsetBytes; private long regionOffsetBytes; + private final MemoryRequestServer memReqSvr; HeapNonNativeWritableMemoryImpl( final Object unsafeObj, final long offsetBytes, final long capacityBytes, final int typeId, - final long cumOffsetBytes) { + final long cumOffsetBytes, + final MemoryRequestServer memReqSvr) { super(); this.unsafeObj = unsafeObj; this.offsetBytes = offsetBytes; @@ -51,6 +53,7 @@ final class HeapNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImpl this.typeId = removeNnBuf(typeId) | HEAP | MEMORY | NONNATIVE; this.cumOffsetBytes = cumOffsetBytes; this.regionOffsetBytes = 0; + this.memReqSvr = memReqSvr; } @Override @@ -59,16 +62,19 @@ final class HeapNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImpl final long capacityBytes, final boolean readOnly, final ByteOrder byteOrder) { - final Object unsafeObj = this.unsafeObj; - final long newOffsetBytes = this.offsetBytes + this.regionOffsetBytes; - this.cumOffsetBytes += this.regionOffsetBytes; + this.regionOffsetBytes = regionOffsetBytes; + final long newOffsetBytes = offsetBytes + regionOffsetBytes; + this.cumOffsetBytes += regionOffsetBytes; int typeIdOut = removeNnBuf(typeId) | MEMORY | REGION | (readOnly ? READONLY : 0); + if (Util.isNativeByteOrder(byteOrder)) { typeIdOut |= NATIVE; - return new HeapWritableMemoryImpl(unsafeObj, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + return new HeapWritableMemoryImpl( + unsafeObj, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } else { typeIdOut |= NONNATIVE; - return new HeapNonNativeWritableMemoryImpl(unsafeObj, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + return new HeapNonNativeWritableMemoryImpl( + unsafeObj, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } } @@ -79,11 +85,11 @@ final class HeapNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImpl if (byteOrder == ByteOrder.nativeOrder()) { typeIdOut |= NATIVE; return new HeapWritableBufferImpl( - unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } else { typeIdOut |= NONNATIVE; return new HeapNonNativeWritableBufferImpl( - unsafeObj, regionOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } } @@ -101,7 +107,7 @@ final class HeapNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImpl @Override public MemoryRequestServer getMemoryRequestServer() { - return null; + return memReqSvr; } @Override @@ -109,6 +115,12 @@ final class HeapNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImpl return 0; } + @Override + public long getOffset() { + assertValid(); + return offsetBytes; + } + @Override public long getRegionOffset() { assertValid(); diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/HeapWritableBufferImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/HeapWritableBufferImpl.java index d57ae2d..e54a3cf 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/HeapWritableBufferImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/HeapWritableBufferImpl.java @@ -37,13 +37,15 @@ final class HeapWritableBufferImpl extends NativeWritableBufferImpl { private final int typeId; private long cumOffsetBytes; private long regionOffsetBytes; + private final MemoryRequestServer memReqSvr; HeapWritableBufferImpl( final Object unsafeObj, final long offsetBytes, final long capacityBytes, final int typeId, - final long cumOffsetBytes) { + final long cumOffsetBytes, + final MemoryRequestServer memReqSvr) { super(capacityBytes); this.unsafeObj = unsafeObj; this.offsetBytes = offsetBytes; @@ -51,6 +53,7 @@ final class HeapWritableBufferImpl extends NativeWritableBufferImpl { this.typeId = removeNnBuf(typeId) | HEAP | BUFFER | NATIVE; this.cumOffsetBytes = cumOffsetBytes; this.regionOffsetBytes = 0; + this.memReqSvr = memReqSvr; } @Override @@ -59,46 +62,49 @@ final class HeapWritableBufferImpl extends NativeWritableBufferImpl { final long capacityBytes, final boolean readOnly, final ByteOrder byteOrder) { - final Object unsafeObj = this.unsafeObj; - final long newOffsetBytes = this.offsetBytes + regionOffsetBytes; + this.regionOffsetBytes = regionOffsetBytes; + final long newOffsetBytes = offsetBytes + regionOffsetBytes; this.cumOffsetBytes += regionOffsetBytes; int typeIdOut = removeNnBuf(typeId) | BUFFER | REGION | (readOnly ? READONLY : 0); + if (Util.isNativeByteOrder(byteOrder)) { typeIdOut |= NATIVE; - return new HeapWritableBufferImpl(unsafeObj, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + return new HeapWritableBufferImpl( + unsafeObj, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } else { typeIdOut |= NONNATIVE; - return new HeapNonNativeWritableBufferImpl(unsafeObj, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + return new HeapNonNativeWritableBufferImpl( + unsafeObj, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } } @Override - BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) { - int typeIdOut = removeNnBuf(typeId) | BUFFER | DUPLICATE | (readOnly ? READONLY : 0); + BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final ByteOrder byteOrder) { + int typeIdOut = removeNnBuf(typeId) | MEMORY | (readOnly ? READONLY : 0); if (byteOrder == ByteOrder.nativeOrder()) { typeIdOut |= NATIVE; - return new HeapWritableBufferImpl( - unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + return new HeapWritableMemoryImpl( + unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } else { typeIdOut |= NONNATIVE; - return new HeapNonNativeWritableBufferImpl( - unsafeObj, regionOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + return new HeapNonNativeWritableMemoryImpl( + unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } } @Override - BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final ByteOrder byteOrder) { - int typeIdOut = removeNnBuf(typeId) | MEMORY | (readOnly ? READONLY : 0); + BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) { + int typeIdOut = removeNnBuf(typeId) | BUFFER | DUPLICATE | (readOnly ? READONLY : 0); if (byteOrder == ByteOrder.nativeOrder()) { typeIdOut |= NATIVE; - return new HeapWritableMemoryImpl( - unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + return new HeapWritableBufferImpl( + unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } else { typeIdOut |= NONNATIVE; - return new HeapNonNativeWritableMemoryImpl( - unsafeObj, regionOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + return new HeapNonNativeWritableBufferImpl( + unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } } @@ -116,7 +122,7 @@ final class HeapWritableBufferImpl extends NativeWritableBufferImpl { @Override public MemoryRequestServer getMemoryRequestServer() { - return null; + return memReqSvr; } @Override @@ -124,6 +130,12 @@ final class HeapWritableBufferImpl extends NativeWritableBufferImpl { return 0; } + @Override + public long getOffset() { + assertValid(); + return offsetBytes; + } + @Override public long getRegionOffset() { assertValid(); diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/HeapWritableMemoryImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/HeapWritableMemoryImpl.java index 85c75e7..2da356c 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/HeapWritableMemoryImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/HeapWritableMemoryImpl.java @@ -37,13 +37,15 @@ final class HeapWritableMemoryImpl extends NativeWritableMemoryImpl { private final int typeId; private long cumOffsetBytes; private long regionOffsetBytes; + private final MemoryRequestServer memReqSvr; HeapWritableMemoryImpl( final Object unsafeObj, final long offsetBytes, final long capacityBytes, - final int typeId, //if this is RO it stays RO - final long cumOffsetBytes) { + final int typeId, + final long cumOffsetBytes, + final MemoryRequestServer memReqSvr) { super(); this.unsafeObj = unsafeObj; this.offsetBytes = offsetBytes; @@ -51,6 +53,7 @@ final class HeapWritableMemoryImpl extends NativeWritableMemoryImpl { this.typeId = removeNnBuf(typeId) | HEAP | MEMORY | NATIVE; this.cumOffsetBytes = cumOffsetBytes; this.regionOffsetBytes = 0; + this.memReqSvr = memReqSvr; } @Override @@ -63,12 +66,15 @@ final class HeapWritableMemoryImpl extends NativeWritableMemoryImpl { final long newOffsetBytes = offsetBytes + regionOffsetBytes; cumOffsetBytes += regionOffsetBytes; int typeIdOut = removeNnBuf(typeId) | MEMORY | REGION | (readOnly ? READONLY : 0); + if (Util.isNativeByteOrder(byteOrder)) { typeIdOut |= NATIVE; - return new HeapWritableMemoryImpl(unsafeObj, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + return new HeapWritableMemoryImpl( + unsafeObj, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } else { typeIdOut |= NONNATIVE; - return new HeapNonNativeWritableMemoryImpl(unsafeObj, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + return new HeapNonNativeWritableMemoryImpl( + unsafeObj, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } } @@ -79,11 +85,11 @@ final class HeapWritableMemoryImpl extends NativeWritableMemoryImpl { if (byteOrder == ByteOrder.nativeOrder()) { typeIdOut |= NATIVE; return new HeapWritableBufferImpl( - unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } else { typeIdOut |= NONNATIVE; return new HeapNonNativeWritableBufferImpl( - unsafeObj, regionOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes); + unsafeObj, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, memReqSvr); } } @@ -101,7 +107,7 @@ final class HeapWritableMemoryImpl extends NativeWritableMemoryImpl { @Override public MemoryRequestServer getMemoryRequestServer() { - return null; + return memReqSvr; } @Override @@ -109,6 +115,12 @@ final class HeapWritableMemoryImpl extends NativeWritableMemoryImpl { return 0; } + @Override + public long getOffset() { + assertValid(); + return offsetBytes; + } + @Override public long getRegionOffset() { assertValid(); diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableBufferImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableBufferImpl.java index 31a4f1f..b5333a2 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableBufferImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableBufferImpl.java @@ -31,52 +31,98 @@ import org.apache.datasketches.memory.WritableBuffer; * @author Lee Rhodes */ final class MapNonNativeWritableBufferImpl extends NonNativeWritableBufferImpl { - private static final int id = BUFFER | NONNATIVE | MAP; - private final long nativeBaseOffset; //used to compute cumBaseOffset + private final long nativeBaseOffset; + private final long offsetBytes; + private final long capacityBytes; + private final int typeId; + private long cumOffsetBytes; + private long regionOffsetBytes; private final StepBoolean valid; //a reference only - private final byte typeId; MapNonNativeWritableBufferImpl( final long nativeBaseOffset, - final long regionOffset, + final long offsetBytes, final long capacityBytes, final int typeId, + final long cumOffsetBytes, final StepBoolean valid) { - super(null, nativeBaseOffset, regionOffset, capacityBytes); + super(capacityBytes); this.nativeBaseOffset = nativeBaseOffset; + this.offsetBytes = offsetBytes; + this.capacityBytes = capacityBytes; + this.typeId = removeNnBuf(typeId) | MAP | BUFFER | NONNATIVE; + this.cumOffsetBytes = cumOffsetBytes; + this.regionOffsetBytes = 0; this.valid = valid; - this.typeId = (byte) (id | (typeId & 0x7)); } @Override - BaseWritableBufferImpl toWritableRegion(final long offsetBytes, final long capacityBytes, - final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | REGION; - return Util.isNativeByteOrder(byteOrder) - ? new MapWritableBufferImpl( - nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, valid) - : new MapNonNativeWritableBufferImpl( - nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, valid); + BaseWritableBufferImpl toWritableRegion( + final long regionOffsetBytes, + final long capacityBytes, + final boolean readOnly, + final ByteOrder byteOrder) { + this.regionOffsetBytes = regionOffsetBytes; + final long newOffsetBytes = offsetBytes + regionOffsetBytes; + cumOffsetBytes += regionOffsetBytes; + int typeIdOut = removeNnBuf(typeId) | MAP | REGION | (readOnly ? READONLY : 0); + + if (Util.isNativeByteOrder(byteOrder)) { + typeIdOut |= NATIVE; + return new MapWritableBufferImpl( + nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } else { + typeIdOut |= NONNATIVE; + return new MapNonNativeWritableBufferImpl( + nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } + } + + @Override + BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final ByteOrder byteOrder) { + int typeIdOut = removeNnBuf(typeId) | MEMORY | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new MapWritableMemoryImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } else { + typeIdOut |= NONNATIVE; + return new MapNonNativeWritableMemoryImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } } @Override BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | DUPLICATE; - return Util.isNativeByteOrder(byteOrder) - ? new MapWritableBufferImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid) - : new MapNonNativeWritableBufferImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid); + int typeIdOut = removeNnBuf(typeId) | BUFFER | DUPLICATE | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new MapWritableBufferImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } else { + typeIdOut |= NONNATIVE; + return new MapNonNativeWritableBufferImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } } @Override - BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly); - return Util.isNativeByteOrder(byteOrder) - ? new MapWritableMemoryImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid) - : new MapNonNativeWritableMemoryImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid); + public boolean isValid() { + return valid.get(); + } + + @Override + public long getCapacity() { + assertValid(); + return capacityBytes; + } + + @Override + public long getCumulativeOffset() { + assertValid(); + return cumOffsetBytes; } @Override @@ -85,18 +131,32 @@ final class MapNonNativeWritableBufferImpl extends NonNativeWritableBufferImpl { } @Override - long getNativeBaseOffset() { + public long getNativeBaseOffset() { return nativeBaseOffset; } + @Override + public long getOffset() { + assertValid(); + return offsetBytes; + } + + @Override + public long getRegionOffset() { + assertValid(); + return regionOffsetBytes; + } + @Override int getTypeId() { - return typeId & 0xff; + assertValid(); + return typeId; } @Override - public boolean isValid() { - return valid.get(); + Object getUnsafeObject() { + assertValid(); + return null; } } diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableMemoryImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableMemoryImpl.java index 5305e5f..ce53e6f 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableMemoryImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableMemoryImpl.java @@ -31,42 +31,83 @@ import org.apache.datasketches.memory.WritableMemory; * @author Lee Rhodes */ final class MapNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImpl { - private static final int id = MEMORY | NONNATIVE | MAP; - private final long nativeBaseOffset; //used to compute cumBaseOffset + private final long nativeBaseOffset; + private final long offsetBytes; + private final long capacityBytes; + private final int typeId; + private long cumOffsetBytes; + private long regionOffsetBytes; private final StepBoolean valid; //a reference only - private final byte typeId; MapNonNativeWritableMemoryImpl( final long nativeBaseOffset, - final long regionOffset, + final long offsetBytes, final long capacityBytes, final int typeId, + final long cumOffsetBytes, final StepBoolean valid) { - super(null, nativeBaseOffset, regionOffset, capacityBytes); + super(); this.nativeBaseOffset = nativeBaseOffset; + this.offsetBytes = offsetBytes; + this.capacityBytes = capacityBytes; + this.typeId = removeNnBuf(typeId) | MAP | MEMORY | NONNATIVE; + this.cumOffsetBytes = cumOffsetBytes; + this.regionOffsetBytes = 0; this.valid = valid; - this.typeId = (byte) (id | (typeId & 0x7)); } @Override - BaseWritableMemoryImpl toWritableRegion(final long offsetBytes, final long capacityBytes, - final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | REGION; - return Util.isNativeByteOrder(byteOrder) - ? new MapWritableMemoryImpl( - nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, valid) - : new MapNonNativeWritableMemoryImpl( - nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, valid); + BaseWritableMemoryImpl toWritableRegion( + final long regionOffsetBytes, + final long capacityBytes, + final boolean readOnly, + final ByteOrder byteOrder) { + this.regionOffsetBytes = regionOffsetBytes; + final long newOffsetBytes = offsetBytes + regionOffsetBytes; + cumOffsetBytes += regionOffsetBytes; + int typeIdOut = removeNnBuf(typeId) | MAP | REGION | (readOnly ? READONLY : 0); + + if (Util.isNativeByteOrder(byteOrder)) { + typeIdOut |= NATIVE; + return new MapWritableMemoryImpl( + nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } else { + typeIdOut |= NONNATIVE; + return new MapNonNativeWritableMemoryImpl( + nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } } @Override BaseWritableBufferImpl toWritableBuffer(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly); - return Util.isNativeByteOrder(byteOrder) - ? new MapWritableBufferImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid) - : new MapNonNativeWritableBufferImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid); + int typeIdOut = removeNnBuf(typeId) | BUFFER | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new MapWritableBufferImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } else { + typeIdOut |= NONNATIVE; + return new MapNonNativeWritableBufferImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } + } + + @Override + public boolean isValid() { + return valid.get(); + } + + @Override + public long getCapacity() { + assertValid(); + return capacityBytes; + } + + @Override + public long getCumulativeOffset() { + assertValid(); + return cumOffsetBytes; } @Override @@ -75,18 +116,32 @@ final class MapNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImpl { } @Override - long getNativeBaseOffset() { + public long getNativeBaseOffset() { return nativeBaseOffset; } + @Override + public long getOffset() { + assertValid(); + return offsetBytes; + } + + @Override + public long getRegionOffset() { + assertValid(); + return regionOffsetBytes; + } + @Override int getTypeId() { - return typeId & 0xff; + assertValid(); + return typeId; } @Override - public boolean isValid() { - return valid.get(); + Object getUnsafeObject() { + assertValid(); + return null; } } diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapWritableBufferImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapWritableBufferImpl.java index 9f7c35c..dfc6017 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapWritableBufferImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapWritableBufferImpl.java @@ -31,52 +31,98 @@ import org.apache.datasketches.memory.WritableBuffer; * @author Lee Rhodes */ final class MapWritableBufferImpl extends NativeWritableBufferImpl { - private static final int id = BUFFER | NATIVE | MAP; - private final long nativeBaseOffset; //used to compute cumBaseOffset + private final long nativeBaseOffset; + private final long offsetBytes; + private final long capacityBytes; + private final int typeId; + private long cumOffsetBytes; + private long regionOffsetBytes; private final StepBoolean valid; //a reference only - private final byte typeId; MapWritableBufferImpl( final long nativeBaseOffset, - final long regionOffset, + final long offsetBytes, final long capacityBytes, final int typeId, + final long cumOffsetBytes, final StepBoolean valid) { - super(null, nativeBaseOffset, regionOffset, capacityBytes); + super(capacityBytes); this.nativeBaseOffset = nativeBaseOffset; + this.offsetBytes = offsetBytes; + this.capacityBytes = capacityBytes; + this.typeId = removeNnBuf(typeId) | MAP | BUFFER | NATIVE; + this.cumOffsetBytes = cumOffsetBytes; + this.regionOffsetBytes = 0; this.valid = valid; - this.typeId = (byte) (id | (typeId & 0x7)); } @Override - BaseWritableBufferImpl toWritableRegion(final long offsetBytes, final long capacityBytes, - final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | REGION; - return Util.isNativeByteOrder(byteOrder) - ? new MapWritableBufferImpl( - nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, valid) - : new MapNonNativeWritableBufferImpl( - nativeBaseOffset, getRegionOffset(offsetBytes), capacityBytes, type, valid); + BaseWritableBufferImpl toWritableRegion( + final long regionOffsetBytes, + final long capacityBytes, + final boolean readOnly, + final ByteOrder byteOrder) { + this.regionOffsetBytes = regionOffsetBytes; + final long newOffsetBytes = offsetBytes + regionOffsetBytes; + cumOffsetBytes += regionOffsetBytes; + int typeIdOut = removeNnBuf(typeId) | MAP | REGION | (readOnly ? READONLY : 0); + + if (Util.isNativeByteOrder(byteOrder)) { + typeIdOut |= NATIVE; + return new MapWritableBufferImpl( + nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } else { + typeIdOut |= NONNATIVE; + return new MapNonNativeWritableBufferImpl( + nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } + } + + @Override + BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final ByteOrder byteOrder) { + int typeIdOut = removeNnBuf(typeId) | MEMORY | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new MapWritableMemoryImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } else { + typeIdOut |= NONNATIVE; + return new MapNonNativeWritableMemoryImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } } @Override BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | DUPLICATE; - return Util.isNativeByteOrder(byteOrder) - ? new MapWritableBufferImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid) - : new MapNonNativeWritableBufferImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid); + int typeIdOut = removeNnBuf(typeId) | BUFFER | DUPLICATE | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new MapWritableBufferImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } else { + typeIdOut |= NONNATIVE; + return new MapNonNativeWritableBufferImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } } @Override - BaseWritableMemoryImpl toWritableMemory(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly); - return Util.isNativeByteOrder(byteOrder) - ? new MapWritableMemoryImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid) - : new MapNonNativeWritableMemoryImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid); + public boolean isValid() { + return valid.get(); + } + + @Override + public long getCapacity() { + assertValid(); + return capacityBytes; + } + + @Override + public long getCumulativeOffset() { + assertValid(); + return cumOffsetBytes; } @Override @@ -85,18 +131,32 @@ final class MapWritableBufferImpl extends NativeWritableBufferImpl { } @Override - long getNativeBaseOffset() { + public long getNativeBaseOffset() { return nativeBaseOffset; } + @Override + public long getOffset() { + assertValid(); + return offsetBytes; + } + + @Override + public long getRegionOffset() { + assertValid(); + return regionOffsetBytes; + } + @Override int getTypeId() { - return typeId & 0xff; + assertValid(); + return typeId; } @Override - public boolean isValid() { - return valid.get(); + Object getUnsafeObject() { + assertValid(); + return null; } } diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapWritableMemoryImpl.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapWritableMemoryImpl.java index 4dbbc42..3216a4f 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapWritableMemoryImpl.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/MapWritableMemoryImpl.java @@ -31,42 +31,83 @@ import org.apache.datasketches.memory.WritableMemory; * @author Lee Rhodes */ final class MapWritableMemoryImpl extends NativeWritableMemoryImpl { - private static final int id = MEMORY | NATIVE | MAP; - private final long nativeBaseOffset; //used to compute cumBaseOffset + private final long nativeBaseOffset; + private final long offsetBytes; + private final long capacityBytes; + private final int typeId; + private long cumOffsetBytes; + private long regionOffsetBytes; private final StepBoolean valid; //a reference only - private final byte typeId; MapWritableMemoryImpl( final long nativeBaseOffset, - final long regionOffset, + final long offsetBytes, final long capacityBytes, final int typeId, + final long cumOffsetBytes, final StepBoolean valid) { - super(null, nativeBaseOffset, regionOffset, capacityBytes); + super(); this.nativeBaseOffset = nativeBaseOffset; + this.offsetBytes = offsetBytes; + this.capacityBytes = capacityBytes; + this.typeId = removeNnBuf(typeId) | MAP | MEMORY | NATIVE; + this.cumOffsetBytes = cumOffsetBytes; + this.regionOffsetBytes = cumOffsetBytes - nativeBaseOffset; this.valid = valid; - this.typeId = (byte) (id | (typeId & 0x7)); } @Override - BaseWritableMemoryImpl toWritableRegion(final long regionOffsetBytes, final long capacityBytes, - final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly) | REGION; - return Util.isNativeByteOrder(byteOrder) - ? new MapWritableMemoryImpl( - nativeBaseOffset, getRegionOffset(regionOffsetBytes), capacityBytes, type, valid) - : new MapNonNativeWritableMemoryImpl( - nativeBaseOffset, getRegionOffset(regionOffsetBytes), capacityBytes, type, valid); + BaseWritableMemoryImpl toWritableRegion( + final long regionOffsetBytes, + final long capacityBytes, + final boolean readOnly, + final ByteOrder byteOrder) { + //this.regionOffsetBytes = regionOffsetBytes; + final long newOffsetBytes = offsetBytes + regionOffsetBytes; + cumOffsetBytes += regionOffsetBytes; + int typeIdOut = removeNnBuf(typeId) | MAP | REGION | (readOnly ? READONLY : 0); + + if (Util.isNativeByteOrder(byteOrder)) { + typeIdOut |= NATIVE; + return new MapWritableMemoryImpl( + nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } else { + typeIdOut |= NONNATIVE; + return new MapNonNativeWritableMemoryImpl( + nativeBaseOffset, newOffsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } } @Override BaseWritableBufferImpl toWritableBuffer(final boolean readOnly, final ByteOrder byteOrder) { - final int type = setReadOnlyType(typeId, readOnly); - return Util.isNativeByteOrder(byteOrder) - ? new MapWritableBufferImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid) - : new MapNonNativeWritableBufferImpl( - nativeBaseOffset, getRegionOffset(), getCapacity(), type, valid); + int typeIdOut = removeNnBuf(typeId) | BUFFER | (readOnly ? READONLY : 0); + + if (byteOrder == ByteOrder.nativeOrder()) { + typeIdOut |= NATIVE; + return new MapWritableBufferImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } else { + typeIdOut |= NONNATIVE; + return new MapNonNativeWritableBufferImpl( + nativeBaseOffset, offsetBytes, capacityBytes, typeIdOut, cumOffsetBytes, valid); + } + } + + @Override + public boolean isValid() { + return valid.get(); + } + + @Override + public long getCapacity() { + assertValid(); + return capacityBytes; + } + + @Override + public long getCumulativeOffset() { + assertValid(); + return cumOffsetBytes; } @Override @@ -75,18 +116,32 @@ final class MapWritableMemoryImpl extends NativeWritableMemoryImpl { } @Override - long getNativeBaseOffset() { + public long getNativeBaseOffset() { return nativeBaseOffset; } + @Override + public long getOffset() { + assertValid(); + return offsetBytes; + } + + @Override + public long getRegionOffset() { + assertValid(); + return regionOffsetBytes; + } + @Override int getTypeId() { - return typeId & 0xff; + assertValid(); + return typeId; } @Override - public boolean isValid() { - return valid.get(); + Object getUnsafeObject() { + assertValid(); + return null; } } diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/Utf8.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/Utf8.java index d8fb52d..aee5eaf 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/Utf8.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/Utf8.java @@ -67,7 +67,6 @@ final class Utf8 { //Decode Direct CharBuffers and all other Appendables final long address = cumBaseOffset + offsetBytes; - // Optimize for 100% ASCII (Hotspot loves small simple top-level loops like this). // This simple loop stops when we encounter a byte >= 0x80 (i.e. non-ASCII). // Need to keep this loop int-indexed, because it's faster for Hotspot JIT, it doesn't insert diff --git a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/Util.java b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/Util.java index 7660dca..182053a 100644 --- a/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/Util.java +++ b/datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/Util.java @@ -36,15 +36,10 @@ import org.apache.datasketches.memory.Memory; * @author Lee Rhodes */ public final class Util { - public static final String LS = System.getProperty("line.separator"); - //Byte Order related - public static final ByteOrder NON_NATIVE_BYTE_ORDER = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN - ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN; + private Util() { } - public static ByteOrder otherByteOrder(final ByteOrder order) { - return (order == ByteOrder.nativeOrder()) ? NON_NATIVE_BYTE_ORDER : ByteOrder.nativeOrder(); - } + public static final String LS = System.getProperty("line.separator"); /** * Don't use sun.misc.Unsafe#copyMemory to copy blocks of memory larger than this @@ -58,9 +53,15 @@ public final class Util { */ public static final int UNSAFE_COPY_THRESHOLD_BYTES = 1024 * 1024; - private Util() { } + //Byte Order related + public static final ByteOrder NATIVE_BYTE_ORDER = ByteOrder.nativeOrder(); + + public static final ByteOrder NON_NATIVE_BYTE_ORDER = NATIVE_BYTE_ORDER == ByteOrder.LITTLE_ENDIAN + ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN; - //Byte Order Related + public static ByteOrder otherByteOrder(final ByteOrder order) { + return (order == NATIVE_BYTE_ORDER) ? NON_NATIVE_BYTE_ORDER : NATIVE_BYTE_ORDER; + } /** * Returns true if the given byteOrder is the same as the native byte order. diff --git a/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/AaByteBufferTest.java b/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/AaByteBufferTest.java new file mode 100644 index 0000000..46ee5af --- /dev/null +++ b/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/AaByteBufferTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.datasketches.memory.internal; + +import java.nio.ByteBuffer; + +import org.apache.datasketches.memory.Memory; +import org.testng.annotations.Test; + +public class AaByteBufferTest { + + @Test + public void checkBB() { + byte[] byteArr = new byte[32]; + int len = byteArr.length; + for (byte i = 0; i < len; i++) { byteArr[i] = i; } + ByteBuffer bb = ByteBuffer.wrap(byteArr); + Memory mem = Memory.wrap(bb); + for (int i = 0; i < len; i++) { + //System.out.println(mem.getByte(i)); + } + } + + @Test + public void checkHeap() { + byte[] byteArr = new byte[32]; + int len = byteArr.length; + for (byte i = 0; i < len; i++) { byteArr[i] = i; } + Memory mem = Memory.wrap(byteArr); + for (int i = 0; i < len; i++) { + //System.out.println(mem.getByte(i)); + } + } + +} + diff --git a/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/AllocateDirectMapMemoryTest.java b/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/AllocateDirectMapMemoryTest.java index 1331781..455ad3d 100644 --- a/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/AllocateDirectMapMemoryTest.java +++ b/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/AllocateDirectMapMemoryTest.java @@ -59,29 +59,29 @@ public class AllocateDirectMapMemoryTest { @Test public void simpleMap2() throws Exception { File file = getResourceFile("GettysburgAddress.txt"); - try ( - MapHandle rh = - Memory.map(file) - ) + try (MapHandle rh = Memory.map(file)) { Memory mem = rh.get(); - println("Mem Cap: " + mem.getCapacity()); + println("Mem Cap: " + mem.getCapacity()); + println("Native Off: " + ((BaseStateImpl)mem).getNativeBaseOffset()); + println("Offset: " + ((BaseStateImpl)mem).getOffset()); println("Cum Offset: " + mem.getCumulativeOffset(0)); println("Region Offset: " + mem.getRegionOffset()); StringBuilder sb = new StringBuilder(); mem.getCharsFromUtf8(43, 176, sb); println(sb.toString()); - Memory mem2 = mem.region(38, 12); + println(""); + Memory mem2 = mem.region(43+76, 20); println("Mem Cap: " + mem2.getCapacity()); + println("Native Off: " + ((BaseStateImpl)mem).getNativeBaseOffset()); + println("Offset: " + ((BaseStateImpl)mem).getOffset()); println("Cum Offset: " + mem2.getCumulativeOffset(0)); println("Region Offset: " + mem2.getRegionOffset()); StringBuilder sb2 = new StringBuilder(); mem2.getCharsFromUtf8(0, 12, sb2); println(sb2.toString()); - - rh.close(); } } @@ -194,7 +194,7 @@ public class AllocateDirectMapMemoryTest { */ static void print(final Object o) { if (o != null) { - System.out.print(o.toString()); //disable here + //System.out.print(o.toString()); //disable here } } diff --git a/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/BaseStateTest.java b/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/BaseStateTest.java index 430dfa1..b4a193a 100644 --- a/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/BaseStateTest.java +++ b/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/BaseStateTest.java @@ -105,9 +105,9 @@ public class BaseStateTest { @Test public void checkIsNativeByteOrder() { - assertTrue(BaseStateImpl.isNativeByteOrder(ByteOrder.nativeOrder())); + assertTrue(Util.isNativeByteOrder(ByteOrder.nativeOrder())); try { - BaseStateImpl.isNativeByteOrder(null); + Util.isNativeByteOrder(null); fail(); } catch (final IllegalArgumentException e) {} } diff --git a/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/Buffer2Test.java b/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/Buffer2Test.java index e625cb4..8aef3db 100644 --- a/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/Buffer2Test.java +++ b/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/Buffer2Test.java @@ -50,7 +50,9 @@ public class Buffer2Test { Buffer buffer = Buffer.wrap(bb.asReadOnlyBuffer().order(ByteOrder.nativeOrder())); while (buffer.hasRemaining()) { - assertEquals(bb.get(), buffer.getByte()); + byte a1 = bb.get(); + byte b1 = buffer.getByte(); + assertEquals(a1, b1); } assertEquals(true, buffer.hasArray()); diff --git a/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/SpecificLeafTest.java b/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/SpecificLeafTest.java index 2b96543..2c67d7f 100644 --- a/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/SpecificLeafTest.java +++ b/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/SpecificLeafTest.java @@ -46,8 +46,10 @@ public class SpecificLeafTest { bb.order(ByteOrder.nativeOrder()); Memory mem = Memory.wrap(bb).region(0, bytes, ByteOrder.nativeOrder()); - - assertTrue(((BaseStateImpl)mem).isByteBufferType(((BaseStateImpl)mem).getTypeId())); + BaseStateImpl bsi = (BaseStateImpl)mem; + int typeId = bsi.getTypeId(); + assertTrue(bsi.isByteBufferType(typeId)); + assertTrue(bsi.isNativeType(typeId)); assertTrue(mem.isReadOnly()); checkCrossLeafTypeIds(mem); Buffer buf = mem.asBuffer().region(0, bytes, ByteOrder.nativeOrder()); @@ -90,6 +92,33 @@ public class SpecificLeafTest { } } + @Test + public void checkHeapLeafs() { + int bytes = 128; + Memory mem = Memory.wrap(new byte[bytes]); + BaseStateImpl bsi = (BaseStateImpl)mem; + int typeId = bsi.getTypeId(); + assertTrue(bsi.isHeapType(typeId)); + assertTrue(bsi.isReadOnlyType(typeId)); + checkCrossLeafTypeIds(mem); + Memory nnreg = mem.region(0, bytes, Util.NON_NATIVE_BYTE_ORDER); + + Memory reg = mem.region(0, bytes, ByteOrder.nativeOrder()); + Buffer buf = reg.asBuffer().region(0, bytes, ByteOrder.nativeOrder()); + Buffer buf4 = buf.duplicate(); + + Memory reg2 = nnreg.region(0, bytes, Util.NON_NATIVE_BYTE_ORDER); + Buffer buf2 = reg2.asBuffer().region(0, bytes, Util.NON_NATIVE_BYTE_ORDER); + Buffer buf3 = buf2.duplicate(); + + assertFalse(((BaseStateImpl)mem).isRegionType(((BaseStateImpl)mem).getTypeId())); + assertTrue(((BaseStateImpl)reg2).isRegionType(((BaseStateImpl)reg2).getTypeId())); + assertTrue(((BaseStateImpl)buf).isRegionType(((BaseStateImpl)buf).getTypeId())); + assertTrue(((BaseStateImpl)buf2).isRegionType(((BaseStateImpl)buf2).getTypeId())); + assertTrue(((BaseStateImpl)buf3).isDuplicateType(((BaseStateImpl)buf3).getTypeId())); + assertTrue(((BaseStateImpl)buf4).isDuplicateType(((BaseStateImpl)buf4).getTypeId())); + } + @Test public void checkMapLeafs() throws Exception { File file = new File("TestFile2.bin"); @@ -108,7 +137,7 @@ public class SpecificLeafTest { final long bytes = 128; try (WritableMapHandle h = WritableMemory.writableMap(file, 0L, bytes, ByteOrder.nativeOrder())) { - WritableMemory mem = h.getWritable(); //native mem + WritableMemory mem = h.getWritable(); assertTrue(((BaseStateImpl)mem).isMapType(((BaseStateImpl)mem).getTypeId())); assertFalse(mem.isReadOnly()); checkCrossLeafTypeIds(mem); @@ -131,35 +160,18 @@ public class SpecificLeafTest { } } - @Test - public void checkHeapLeafs() { - int bytes = 128; - Memory mem = Memory.wrap(new byte[bytes]); - assertTrue(((BaseStateImpl)mem).isHeapType(((BaseStateImpl)mem).getTypeId())); - assertTrue(((BaseStateImpl)mem).isReadOnlyType(((BaseStateImpl)mem).getTypeId())); - checkCrossLeafTypeIds(mem); - Memory nnreg = mem.region(0, bytes, Util.NON_NATIVE_BYTE_ORDER); - - Memory reg = mem.region(0, bytes, ByteOrder.nativeOrder()); - Buffer buf = reg.asBuffer().region(0, bytes, ByteOrder.nativeOrder()); - Buffer buf4 = buf.duplicate(); - - Memory reg2 = nnreg.region(0, bytes, Util.NON_NATIVE_BYTE_ORDER); - Buffer buf2 = reg2.asBuffer().region(0, bytes, Util.NON_NATIVE_BYTE_ORDER); - Buffer buf3 = buf2.duplicate(); - - assertFalse(((BaseStateImpl)mem).isRegionType(((BaseStateImpl)mem).getTypeId())); - assertTrue(((BaseStateImpl)reg2).isRegionType(((BaseStateImpl)reg2).getTypeId())); - assertTrue(((BaseStateImpl)buf).isRegionType(((BaseStateImpl)buf).getTypeId())); - assertTrue(((BaseStateImpl)buf2).isRegionType(((BaseStateImpl)buf2).getTypeId())); - assertTrue(((BaseStateImpl)buf3).isDuplicateType(((BaseStateImpl)buf3).getTypeId())); - assertTrue(((BaseStateImpl)buf4).isDuplicateType(((BaseStateImpl)buf4).getTypeId())); - } +// static void theId(Memory mem) { +// int typeId = ((BaseStateImpl)mem).getTypeId(); +// System.out.println(BaseStateImpl.typeDecode(typeId)); +// } +// static void theId(Buffer buf) { +// int typeId = ((BaseStateImpl)buf).getTypeId(); +// System.out.println(BaseStateImpl.typeDecode(typeId)); +// } private static void checkCrossLeafTypeIds(Memory mem) { Memory reg1 = mem.region(0, mem.getCapacity()); assertTrue(((BaseStateImpl)reg1).isRegionType(((BaseStateImpl)reg1).getTypeId())); - Buffer buf1 = reg1.asBuffer(); assertTrue(((BaseStateImpl)buf1).isRegionType(((BaseStateImpl)buf1).getTypeId())); assertTrue(((BaseStateImpl)buf1).isBufferType(((BaseStateImpl)buf1).getTypeId())); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
