This is an automated email from the ASF dual-hosted git repository.
leerho pushed a commit to branch frank_grimes_java-21-ffm
in repository https://gitbox.apache.org/repos/asf/datasketches-memory.git
The following commit(s) were added to refs/heads/frank_grimes_java-21-ffm by
this push:
new 34c661c Updating java 17 code to use java 21 API.
34c661c is described below
commit 34c661c29da67be4dfafb0b1e2bb06fe64c98ff2
Author: Lee Rhodes <[email protected]>
AuthorDate: Fri Dec 6 15:42:33 2024 -0800
Updating java 17 code to use java 21 API.
Had to upgrade my Java 21 JDK to Temurin 21.0.5+11 due to a discovered
bug in Java.
---
README.md | 4 +-
.../org/apache/datasketches/memory/Buffer.java | 25 +---
.../org/apache/datasketches/memory/Memory.java | 21 ---
.../org/apache/datasketches/memory/Resource.java | 40 +++--
.../memory/internal/CompareAndCopy.java | 70 ---------
.../datasketches/memory/internal/ResourceImpl.java | 143 ++++++++++--------
.../memory/internal/WritableBufferImpl.java | 9 +-
.../memory/internal/WritableMemoryImpl.java | 12 +-
.../memory/internal/CopyMemoryTest.java | 6 +-
.../datasketches/memory/internal/MemoryTest.java | 1 +
.../internal/NativeWritableBufferImplTest.java | 55 -------
.../internal/NativeWritableMemoryImplTest.java | 66 ---------
.../datasketches/memory/internal/ResourceTest.java | 164 +++++++++++++++------
.../memory/internal/WritableMemoryTest.java | 4 -
14 files changed, 249 insertions(+), 371 deletions(-)
diff --git a/README.md b/README.md
index 257281b..be71d4d 100644
--- a/README.md
+++ b/README.md
@@ -69,7 +69,9 @@ Note: *primitive* := *{byte, short, int, long, float, double}*
## Release 5.0.0 (inclusive) to 6.0.0 (exclusive)
Starting with release *datasketches-memory-5.0.0*, this Memory component
supports only Java 21 when compiling from source and should work with later
Java versions at runtime.
-### *NOTE: The DataSketches Java Memory Component is not thread-safe.*
+### *NOTES:*
+* The DataSketches Java Memory Component is not thread-safe.
+* I recommend Eclipse Adoptium/Temurin 21.0.5+11 or later as earlier releases
of 21 have bugs that affect this product.
## Build Instructions
__NOTES:__
diff --git a/src/main/java/org/apache/datasketches/memory/Buffer.java
b/src/main/java/org/apache/datasketches/memory/Buffer.java
index c388efd..f1bec23 100644
--- a/src/main/java/org/apache/datasketches/memory/Buffer.java
+++ b/src/main/java/org/apache/datasketches/memory/Buffer.java
@@ -383,29 +383,6 @@ public interface Buffer extends Positional, Resource {
int dstOffsetShorts,
int lengthShorts);
- //SPECIAL PRIMITIVE READ METHODS: compareTo.
- // No copyTo, No writeTo. Use Memory for that.
-
- /**
- * Compares the bytes of this Buffer to <i>that</i> Buffer.
- * This uses absolute offsets not the start, position and end.
- * Returns <i>(this < that) ? (some negative value) : (this > that) ?
(some positive value)
- * : 0;</i>.
- * If all bytes are equal up to the shorter of the two lengths, the shorter
length is
- * considered to be less than the other.
- * @param thisOffsetBytes the starting offset for <i>this Buffer</i>
- * @param thisLengthBytes the length of the region to compare from <i>this
Buffer</i>
- * @param that the other Buffer to compare with
- * @param thatOffsetBytes the starting offset for <i>that Buffer</i>
- * @param thatLengthBytes the length of the region to compare from <i>that
Buffer</i>
- * @return <i>(this < that) ? (some negative value) : (this > that) ?
(some positive value)
- * : 0;</i>
- */
- int compareTo(
- long thisOffsetBytes,
- long thisLengthBytes,
- Buffer that,
- long thatOffsetBytes,
- long thatLengthBytes);
+ //SPECIAL PRIMITIVE READ METHODS:none.
}
diff --git a/src/main/java/org/apache/datasketches/memory/Memory.java
b/src/main/java/org/apache/datasketches/memory/Memory.java
index 10ffbb7..071e816 100644
--- a/src/main/java/org/apache/datasketches/memory/Memory.java
+++ b/src/main/java/org/apache/datasketches/memory/Memory.java
@@ -445,27 +445,6 @@ public interface Memory extends Resource {
//SPECIAL PRIMITIVE READ METHODS: compareTo, copyTo, writeTo
- /**
- * Compares the bytes of this Memory to <i>that</i> Memory.
- * Returns <i>(this < that) ? (some negative value) : (this > that) ?
(some positive value)
- * : 0;</i>.
- * If all bytes are equal up to the shorter of the two lengths, the shorter
length is considered
- * to be less than the other.
- * @param thisOffsetBytes the starting offset for <i>this Memory</i>
- * @param thisLengthBytes the length of the region to compare from <i>this
Memory</i>
- * @param that the other Memory to compare with
- * @param thatOffsetBytes the starting offset for <i>that Memory</i>
- * @param thatLengthBytes the length of the region to compare from <i>that
Memory</i>
- * @return <i>(this < that) ? (some negative value) : (this > that) ?
(some positive value)
- * : 0;</i>
- */
- int compareTo(
- long thisOffsetBytes,
- long thisLengthBytes,
- Memory that,
- long thatOffsetBytes,
- long thatLengthBytes);
-
/**
* Copies bytes from a source range of this Memory to a destination range of
the given Memory
* with the same semantics when copying between overlapping ranges of bytes
as method
diff --git a/src/main/java/org/apache/datasketches/memory/Resource.java
b/src/main/java/org/apache/datasketches/memory/Resource.java
index c8a13f0..3dcdfef 100644
--- a/src/main/java/org/apache/datasketches/memory/Resource.java
+++ b/src/main/java/org/apache/datasketches/memory/Resource.java
@@ -74,7 +74,7 @@ public interface Resource extends AutoCloseable {
*/
void setMemoryRequestServer(MemoryRequestServer memReqSvr);
- //***
+ //*** Other
/**
* Returns a ByteBuffer view of this Memory object with the given ByteOrder.
@@ -119,11 +119,22 @@ public interface Resource extends AutoCloseable {
void close();
/**
- * Return true if this resource likely to be closeable, but not guaranteed.
- * There is no way to determine if the specific type of Arena is explicitly
closeable.
- * @return true if this resource likely to be closeable.
+ * Compares the bytes of this Resource to <i>that</i> Resource.
+ * Returns <i>(this < that) ? (some negative value) : (this > that) ?
(some positive value) : 0;</i>.
+ * If all bytes are equal up to the shorter of the two lengths, the shorter
length is considered
+ * to be less than the other.
+ * @param thisOffsetBytes the starting offset for <i>this Resource</i>
+ * @param thisLengthBytes the length of the region to compare from <i>this
Resource</i>
+ * @param that the other Memory to compare with
+ * @param thatOffsetBytes the starting offset for <i>that Resource</i>
+ * @param thatLengthBytes the length of the region to compare from <i>that
Resource</i>
+ * @return <i>(this < that) ? (some negative value) : (this > that) ?
(some positive value) : 0;</i>
*/
- boolean isCloseable();
+ int compareTo(long thisOffsetBytes,
+ long thisLengthBytes,
+ Resource that,
+ long thatOffsetBytes,
+ long thatLengthBytes);
/**
* Returns true if the given object is an instance of this class and has
equal contents to
@@ -187,6 +198,13 @@ public interface Resource extends AutoCloseable {
*/
boolean hasByteBuffer();
+ /**
+ * Return true if this resource is likely to be closeable, but not
guaranteed.
+ * There is no way to determine if the specific type of Arena is explicitly
closeable.
+ * @return true if this resource is likely to be closeable.
+ */
+ boolean isCloseable();
+
/**
* Is the underlying resource scope alive?
* @return true, if the underlying resource scope is alive.
@@ -327,6 +345,12 @@ mismatch(MemorySegment, long, long, MemorySegment, long,
long)</a>
*/
ByteBuffer toByteBuffer(ByteOrder order);
+ /**
+ * Returns a copy of the underlying MemorySegment.
+ * @return a copy of the underlying MemorySegment.
+ */
+ MemorySegment toMemorySegment();
+
/**
* Returns a brief description of this object.
* @return a brief description of this object.
@@ -349,12 +373,6 @@ mismatch(MemorySegment, long, long, MemorySegment, long,
long)</a>
int lengthBytes,
boolean withData);
- /**
- * Returns a copy of the underlying MemorySegment.
- * @return a copy of the underlying MemorySegment.
- */
- MemorySegment toMemorySegment();
-
/**
* Unloads the contents of this mapped segment from physical memory.
* @see <a
href="https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/foreign/MemorySegment.html#unload()">unload()</a>
diff --git
a/src/main/java/org/apache/datasketches/memory/internal/CompareAndCopy.java
b/src/main/java/org/apache/datasketches/memory/internal/CompareAndCopy.java
deleted file mode 100644
index 2a9a4cb..0000000
--- a/src/main/java/org/apache/datasketches/memory/internal/CompareAndCopy.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * 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.lang.foreign.MemorySegment;
-import java.lang.foreign.ValueLayout;
-
-/**
- * @author Lee Rhodes
- */
-final class CompareAndCopy {
-
- private CompareAndCopy() { }
-
- static int compare(
- final MemorySegment seg1, final long offsetBytes1, final long
lengthBytes1,
- final MemorySegment seg2, final long offsetBytes2, final long
lengthBytes2) {
- final MemorySegment slice1 = seg1.asSlice(offsetBytes1, lengthBytes1);
- final MemorySegment slice2 = seg2.asSlice(offsetBytes2, lengthBytes2);
- final long mm = slice1.mismatch(slice2);
- if (mm == -1) { return 0; }
- if ((lengthBytes1 > mm) && (lengthBytes2 > mm)) {
- return Integer.compare(
- slice1.get(ValueLayout.JAVA_BYTE, mm) & 0XFF,
- slice2.get(ValueLayout.JAVA_BYTE, mm) & 0XFF
- );
- }
- if (lengthBytes1 == mm) { return -1; }
- return +1;
- }
-
- static boolean equals(final MemorySegment seg1, final MemorySegment seg2) {
- final long cap1 = seg1.byteSize();
- final long cap2 = seg2.byteSize();
- return (cap1 == cap2) && equals(seg1, 0, seg2, 0, cap1);
- }
-
- static boolean equals(
- final MemorySegment seg1, final long offsetBytes1,
- final MemorySegment seg2, final long offsetBytes2, final long
lengthBytes) {
- final MemorySegment slice1 = seg1.asSlice(offsetBytes1, lengthBytes);
- final MemorySegment slice2 = seg2.asSlice(offsetBytes2, lengthBytes);
- return slice1.mismatch(slice2) == -1;
- }
-
- static void copy(final MemorySegment srcSegment, final long srcOffsetBytes,
- final MemorySegment dstSegment, final long dstOffsetBytes, final long
lengthBytes) {
- final MemorySegment srcSlice = srcSegment.asSlice(srcOffsetBytes,
lengthBytes);
- final MemorySegment dstSlice = dstSegment.asSlice(dstOffsetBytes,
lengthBytes);
- dstSlice.copyFrom(srcSlice);
- }
-
-}
diff --git
a/src/main/java/org/apache/datasketches/memory/internal/ResourceImpl.java
b/src/main/java/org/apache/datasketches/memory/internal/ResourceImpl.java
index f8f7346..e36704b 100644
--- a/src/main/java/org/apache/datasketches/memory/internal/ResourceImpl.java
+++ b/src/main/java/org/apache/datasketches/memory/internal/ResourceImpl.java
@@ -148,7 +148,7 @@ abstract class ResourceImpl implements Resource {
this.memReqSvr = memReqSvr;
}
- //***
+ //*** Other Statics
/**
* Check the requested offset and length against the allocated size.
@@ -174,6 +174,23 @@ abstract class ResourceImpl implements Resource {
}
}
+ static int compare(
+ final MemorySegment seg1, final long offsetBytes1, final long
lengthBytes1,
+ final MemorySegment seg2, final long offsetBytes2, final long
lengthBytes2) {
+ final long seg1EndOff = offsetBytes1 + lengthBytes1;
+ final long seg2EndOff = offsetBytes2 + lengthBytes2;
+ final long mm = MemorySegment.mismatch(seg1, offsetBytes1, seg1EndOff,
seg2, offsetBytes2, seg2EndOff);
+ if (mm == -1) {
+ return (lengthBytes2 > lengthBytes1) ? -1 : (lengthBytes2 ==
lengthBytes1) ? 0 : 1;
+ }
+ if (mm < Math.min(lengthBytes1, lengthBytes2)) {
+ final int c1 = seg1.get(ValueLayout.JAVA_BYTE, offsetBytes1 + mm) & 0XFF;
+ final int c2 = seg2.get(ValueLayout.JAVA_BYTE, offsetBytes2 + mm) & 0XFF;
+ return Integer.compare(c1, c2);
+ }
+ return (lengthBytes1 == mm) ? -1 : 1;
+ }
+
private static String pad(final String s, final int fieldLen) {
return characterPad(s, fieldLen, ' ' , true);
}
@@ -198,55 +215,6 @@ abstract class ResourceImpl implements Resource {
return new int[] {p0, p1};
}
- /**
- * Decodes the resource type. This is primarily for debugging.
- * @param typeId the given typeId
- * @return a human readable string.
- */
- static final String typeDecode(final int typeId) {
- final StringBuilder sb = new StringBuilder();
- final int group1 = typeId & 0x7;
- switch (group1) { // 0000 0XXX
- case 0 : sb.append(pad("Writable + ",32)); break;
- case 1 : sb.append(pad("ReadOnly + ",32)); break;
- case 2 : sb.append(pad("Writable + Region + ",32)); break;
- case 3 : sb.append(pad("ReadOnly + Region + ",32)); break;
- case 4 : sb.append(pad("Writable + Duplicate + ",32)); break;
- case 5 : sb.append(pad("ReadOnly + Duplicate + ",32)); break;
- case 6 : sb.append(pad("Writable + Region + Duplicate + ",32)); break;
- case 7 : sb.append(pad("ReadOnly + Region + Duplicate + ",32)); break;
- default: break;
- }
- final int group2 = (typeId >>> 3) & 0x3;
- switch (group2) { // 000X X000 43
- case 0 : sb.append(pad("Heap + ",15)); break; // 00
- case 1 : sb.append(pad("Direct + ",15)); break; // 01
- case 2 : sb.append(pad("Map + Direct + ",15)); break; //10
- case 3 : sb.append(pad("Map + Direct + ",15)); break; //11
- default: break;
- }
- final int group3 = (typeId >>> 5) & 0x1;
- switch (group3) { // 00X0 0000
- case 0 : sb.append(pad("NativeOrder + ",17)); break;
- case 1 : sb.append(pad("NonNativeOrder + ",17)); break;
- default: break;
- }
- final int group4 = (typeId >>> 6) & 0x1;
- switch (group4) { // 0X00 0000
- case 0 : sb.append(pad("Memory + ",9)); break;
- case 1 : sb.append(pad("Buffer + ",9)); break;
- default: break;
- }
- final int group5 = (typeId >>> 7) & 0x1;
- switch (group5) { // X000 0000
- case 0 : sb.append(pad("",10)); break;
- case 1 : sb.append(pad("ByteBuffer",10)); break;
- default: break;
- }
-
- return sb.toString();
- }
-
static final WritableBuffer selectBuffer(
final MemorySegment segment,
final int type,
@@ -333,6 +301,57 @@ abstract class ResourceImpl implements Resource {
return sb.toString();
}
+ /**
+ * Decodes the resource type. This is primarily for debugging.
+ * @param typeId the given typeId
+ * @return a human readable string.
+ */
+ static final String typeDecode(final int typeId) {
+ final StringBuilder sb = new StringBuilder();
+ final int group1 = typeId & 0x7;
+ switch (group1) { // 0000 0XXX
+ case 0 : sb.append(pad("Writable + ",32)); break;
+ case 1 : sb.append(pad("ReadOnly + ",32)); break;
+ case 2 : sb.append(pad("Writable + Region + ",32)); break;
+ case 3 : sb.append(pad("ReadOnly + Region + ",32)); break;
+ case 4 : sb.append(pad("Writable + Duplicate + ",32)); break;
+ case 5 : sb.append(pad("ReadOnly + Duplicate + ",32)); break;
+ case 6 : sb.append(pad("Writable + Region + Duplicate + ",32)); break;
+ case 7 : sb.append(pad("ReadOnly + Region + Duplicate + ",32)); break;
+ default: break;
+ }
+ final int group2 = (typeId >>> 3) & 0x3;
+ switch (group2) { // 000X X000 43
+ case 0 : sb.append(pad("Heap + ",15)); break; // 00
+ case 1 : sb.append(pad("Direct + ",15)); break; // 01
+ case 2 : sb.append(pad("Map + Direct + ",15)); break; //10
+ case 3 : sb.append(pad("Map + Direct + ",15)); break; //11
+ default: break;
+ }
+ final int group3 = (typeId >>> 5) & 0x1;
+ switch (group3) { // 00X0 0000
+ case 0 : sb.append(pad("NativeOrder + ",17)); break;
+ case 1 : sb.append(pad("NonNativeOrder + ",17)); break;
+ default: break;
+ }
+ final int group4 = (typeId >>> 6) & 0x1;
+ switch (group4) { // 0X00 0000
+ case 0 : sb.append(pad("Memory + ",9)); break;
+ case 1 : sb.append(pad("Buffer + ",9)); break;
+ default: break;
+ }
+ final int group5 = (typeId >>> 7) & 0x1;
+ switch (group5) { // X000 0000
+ case 0 : sb.append(pad("",10)); break;
+ case 1 : sb.append(pad("ByteBuffer",10)); break;
+ default: break;
+ }
+
+ return sb.toString();
+ }
+
+ //Other members
+
@Override
public final ByteBuffer asByteBufferView(final ByteOrder order) {
final ByteBuffer byteBuffer = seg.asByteBuffer().order(order);
@@ -352,10 +371,18 @@ abstract class ResourceImpl implements Resource {
}
@Override
- public final boolean equalTo(final long thisOffsetBytes, final Resource that,
- final long thatOffsetBytes, final long lengthBytes) {
+ public final int compareTo(final long thisOffsetBytes, final long
thisLengthBytes,
+ final Resource that, final long thatOffsetBytes, final long
thatLengthBytes) {
+ return ResourceImpl.compare(seg, thisOffsetBytes, thisLengthBytes,
+ ((ResourceImpl)that).seg, thatOffsetBytes, thatLengthBytes);
+ }
+
+ @Override
+ public final boolean equalTo(final long thisOffsetBytes, final Resource
that, final long thatOffsetBytes, final long lengthBytes) {
if (that == null) { return false; }
- return CompareAndCopy.equals(seg, thisOffsetBytes, ((ResourceImpl)
that).seg, thatOffsetBytes, lengthBytes);
+ final long thisEndOff = thisOffsetBytes + lengthBytes;
+ final long thatEndOff = thatOffsetBytes + lengthBytes;
+ return MemorySegment.mismatch(seg, thisOffsetBytes, thisEndOff,
((ResourceImpl)that).seg, thatOffsetBytes, thatEndOff) == -1;
}
@Override
@@ -387,14 +414,14 @@ abstract class ResourceImpl implements Resource {
public boolean isAlive() { return seg.scope().isAlive(); }
@Override
- public final boolean isByteOrderCompatible(final ByteOrder byteOrder) {
- final ByteOrder typeBO = getTypeByteOrder();
- return typeBO == ByteOrder.nativeOrder() && typeBO == byteOrder;
+ public final boolean isBuffer() {
+ return (typeId & BUFFER) > 0;
}
@Override
- public final boolean isBuffer() {
- return (typeId & BUFFER) > 0;
+ public final boolean isByteOrderCompatible(final ByteOrder byteOrder) {
+ final ByteOrder typeBO = getTypeByteOrder();
+ return typeBO == ByteOrder.nativeOrder() && typeBO == byteOrder;
}
@Override
diff --git
a/src/main/java/org/apache/datasketches/memory/internal/WritableBufferImpl.java
b/src/main/java/org/apache/datasketches/memory/internal/WritableBufferImpl.java
index a7afc4a..5818cd9 100644
---
a/src/main/java/org/apache/datasketches/memory/internal/WritableBufferImpl.java
+++
b/src/main/java/org/apache/datasketches/memory/internal/WritableBufferImpl.java
@@ -278,14 +278,7 @@ public abstract class WritableBufferImpl extends
PositionalImpl implements Writa
setPosition(pos + lengthBytes);
}
- //OTHER PRIMITIVE READ METHODS: e.g., copyTo, compareTo. No writeTo
-
- @Override
- public final int compareTo(final long thisOffsetBytes, final long
thisLengthBytes,
- final Buffer that, final long thatOffsetBytes, final long
thatLengthBytes) {
- return CompareAndCopy.compare(seg, thisOffsetBytes, thisLengthBytes,
- ((ResourceImpl)that).seg, thatOffsetBytes, thatLengthBytes);
- }
+ //OTHER PRIMITIVE READ METHODS:
/*
* Developer notes: There is no copyTo for Buffers because of the ambiguity
of what to do with
diff --git
a/src/main/java/org/apache/datasketches/memory/internal/WritableMemoryImpl.java
b/src/main/java/org/apache/datasketches/memory/internal/WritableMemoryImpl.java
index 1d7cf3f..953e8e8 100644
---
a/src/main/java/org/apache/datasketches/memory/internal/WritableMemoryImpl.java
+++
b/src/main/java/org/apache/datasketches/memory/internal/WritableMemoryImpl.java
@@ -323,20 +323,12 @@ public abstract class WritableMemoryImpl extends
ResourceImpl implements Writabl
MemorySegment.copy(seg, offsetBytes, dstSeg, dstOffsetBytes, lengthBytes);
}
- //OTHER PRIMITIVE READ METHODS: compareTo, copyTo, writeTo
-
- @Override
- public final int compareTo(final long thisOffsetBytes, final long
thisLengthBytes,
- final Memory that, final long thatOffsetBytes, final long
thatLengthBytes) {
- return CompareAndCopy.compare(seg, thisOffsetBytes, thisLengthBytes,
- ((ResourceImpl)that).seg, thatOffsetBytes, thatLengthBytes);
- }
+ //OTHER PRIMITIVE READ METHODS:
@Override
public final void copyTo(final long srcOffsetBytes,
final WritableMemory destination, final long dstOffsetBytes, final long
lengthBytes) {
- CompareAndCopy.copy(seg, srcOffsetBytes,
- ((ResourceImpl)destination).seg, dstOffsetBytes, lengthBytes);
+ MemorySegment.copy(seg, srcOffsetBytes, ((ResourceImpl)destination).seg,
dstOffsetBytes, lengthBytes);
}
@Override
diff --git
a/src/test/java/org/apache/datasketches/memory/internal/CopyMemoryTest.java
b/src/test/java/org/apache/datasketches/memory/internal/CopyMemoryTest.java
index f50cabe..61e61c0 100644
--- a/src/test/java/org/apache/datasketches/memory/internal/CopyMemoryTest.java
+++ b/src/test/java/org/apache/datasketches/memory/internal/CopyMemoryTest.java
@@ -20,6 +20,8 @@
package org.apache.datasketches.memory.internal;
import java.lang.foreign.Arena;
+import java.lang.foreign.MemorySegment;
+
import static org.testng.Assert.assertEquals;
import java.nio.ByteOrder;
@@ -174,7 +176,7 @@ public class CopyMemoryTest {
/**
* @param s value to print
*/
- static void println(String s) {
- //System.out.println(s); //disable here
+ static void println(Object o) {
+ //System.out.println(o); //disable here
}
}
diff --git
a/src/test/java/org/apache/datasketches/memory/internal/MemoryTest.java
b/src/test/java/org/apache/datasketches/memory/internal/MemoryTest.java
index 23fd593..8451839 100644
--- a/src/test/java/org/apache/datasketches/memory/internal/MemoryTest.java
+++ b/src/test/java/org/apache/datasketches/memory/internal/MemoryTest.java
@@ -422,6 +422,7 @@ public class MemoryTest {
int len = 64;
WritableMemory wmem = WritableMemory.allocate(len);
for (int i = 0; i < len; i++) { wmem.putByte(i, (byte) i); }
+ println(wmem.toString("",0,64, true));
assertTrue(wmem.equalTo(0, wmem, 0, len));
assertFalse(wmem.equalTo(0, wmem, len/2, len/2));
assertEquals(wmem.compareTo(0, len, wmem, 0, len), 0);
diff --git
a/src/test/java/org/apache/datasketches/memory/internal/NativeWritableBufferImplTest.java
b/src/test/java/org/apache/datasketches/memory/internal/NativeWritableBufferImplTest.java
index a783d9c..ddb5cd9 100644
---
a/src/test/java/org/apache/datasketches/memory/internal/NativeWritableBufferImplTest.java
+++
b/src/test/java/org/apache/datasketches/memory/internal/NativeWritableBufferImplTest.java
@@ -353,61 +353,6 @@ public class NativeWritableBufferImplTest {
ResourceImpl.checkBounds(50, 50, 100);
}
- @Test
- public void checkCompareToHeap() {
- byte[] arr1 = new byte[] {0, 1, 2, 3};
- byte[] arr2 = new byte[] {0, 1, 2, 4};
- byte[] arr3 = new byte[] {0, 1, 2, 3, 4};
-
- Buffer buf1 = Memory.wrap(arr1).asBuffer();
- Buffer buf2 = Memory.wrap(arr2).asBuffer();
- Buffer buf3 = Memory.wrap(arr3).asBuffer();
-
- int comp = buf1.compareTo(0, 3, buf2, 0, 3);
- assertEquals(comp, 0);
- comp = buf1.compareTo(0, 4, buf2, 0, 4);
- assertEquals(comp, -1);
- comp = buf2.compareTo(0, 4, buf1, 0, 4);
- assertEquals(comp, 1);
- //different lengths
- comp = buf1.compareTo(0, 4, buf3, 0, 5);
- assertEquals(comp, -1);
- comp = buf3.compareTo(0, 5, buf1, 0, 4);
- assertEquals(comp, 1);
- }
-
- @Test
- public void checkCompareToDirect() throws Exception {
- byte[] arr1 = new byte[] {0, 1, 2, 3};
- byte[] arr2 = new byte[] {0, 1, 2, 4};
- byte[] arr3 = new byte[] {0, 1, 2, 3, 4};
- try (Arena arena = Arena.ofConfined()) {
- WritableMemory mem1 = WritableMemory.allocateDirect(4, 1,
ByteOrder.nativeOrder(), memReqSvr, arena);
- WritableMemory mem2 = WritableMemory.allocateDirect(4, 1,
ByteOrder.nativeOrder(), memReqSvr, arena);
- WritableMemory mem3 = WritableMemory.allocateDirect(5, 1,
ByteOrder.nativeOrder(), memReqSvr, arena);
-
- mem1.putByteArray(0, arr1, 0, 4);
- mem2.putByteArray(0, arr2, 0, 4);
- mem3.putByteArray(0, arr3, 0, 5);
-
- Buffer buf1 = mem1.asBuffer();
- Buffer buf2 = mem2.asBuffer();
- Buffer buf3 = mem3.asBuffer();
-
- int comp = buf1.compareTo(0, 3, buf2, 0, 3);
- assertEquals(comp, 0);
- comp = buf1.compareTo(0, 4, buf2, 0, 4);
- assertEquals(comp, -1);
- comp = buf2.compareTo(0, 4, buf1, 0, 4);
- assertEquals(comp, 1);
- //different lengths
- comp = buf1.compareTo(0, 4, buf3, 0, 5);
- assertEquals(comp, -1);
- comp = buf3.compareTo(0, 5, buf1, 0, 4);
- assertEquals(comp, 1);
- }
- }
-
@Test
public void checkAsBuffer() {
WritableMemory wmem = WritableMemory.allocate(64);
diff --git
a/src/test/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImplTest.java
b/src/test/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImplTest.java
index 39ae895..ce3266e 100644
---
a/src/test/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImplTest.java
+++
b/src/test/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImplTest.java
@@ -545,72 +545,6 @@ public class NativeWritableMemoryImplTest {
ResourceImpl.checkBounds(50, 50, 100);
}
- @Test
- public void checkCompareToHeap() {
- byte[] arr1 = new byte[] {0, 1, 2, 3};
- byte[] arr2 = new byte[] {0, 1, 2, 4};
- byte[] arr3 = new byte[] {0, 1, 2, 3, 4};
-
- Memory mem1 = Memory.wrap(arr1);
- Memory mem2 = Memory.wrap(arr2);
- Memory mem3 = Memory.wrap(arr3);
- Memory mem4 = Memory.wrap(arr3); //same resource
-
- int comp = mem1.compareTo(0, 3, mem2, 0, 3);
- assertEquals(comp, 0);
- comp = mem1.compareTo(0, 4, mem2, 0, 4);
- assertEquals(comp, -1);
- comp = mem2.compareTo(0, 4, mem1, 0, 4);
- assertEquals(comp, 1);
- //different lengths
- comp = mem1.compareTo(0, 4, mem3, 0, 5);
- assertEquals(comp, -1);
- comp = mem3.compareTo(0, 5, mem1, 0, 4);
- assertEquals(comp, 1);
- comp = mem3.compareTo(0, 5, mem4, 0, 5);
- assertEquals(comp, 0);
- comp = mem3.compareTo(0, 4, mem4, 1, 4);
- assertEquals(comp, -1);
- ResourceImpl.checkBounds(0, 5, mem3.getCapacity());
- }
-
- @Test
- public void checkCompareToDirect() throws Exception {
- byte[] arr1 = new byte[] {0, 1, 2, 3};
- byte[] arr2 = new byte[] {0, 1, 2, 4};
- byte[] arr3 = new byte[] {0, 1, 2, 3, 4};
-
- try (Arena arena = Arena.ofConfined()) {
- WritableMemory mem1 = WritableMemory.allocateDirect(4, 1,
ByteOrder.nativeOrder(), memReqSvr, arena);
- WritableMemory mem2 = WritableMemory.allocateDirect(4, 1,
ByteOrder.nativeOrder(), memReqSvr, arena);
- WritableMemory mem3 = WritableMemory.allocateDirect(5, 1,
ByteOrder.nativeOrder(), memReqSvr, arena);
-
- mem1.putByteArray(0, arr1, 0, 4);
- mem2.putByteArray(0, arr2, 0, 4);
- mem3.putByteArray(0, arr3, 0, 5);
-
- int comp = mem1.compareTo(0, 3, mem2, 0, 3);
- assertEquals(comp, 0);
- comp = mem1.compareTo(0, 4, mem2, 0, 4);
- assertEquals(comp, -1);
- comp = mem2.compareTo(0, 4, mem1, 0, 4);
- assertEquals(comp, 1);
- //different lengths
- comp = mem1.compareTo(0, 4, mem3, 0, 5);
- assertEquals(comp, -1);
- comp = mem3.compareTo(0, 5, mem1, 0, 4);
- assertEquals(comp, 1);
- }
- }
-
- @Test
- public void testCompareToSameStart() {
- Memory mem = WritableMemory.allocate(3);
- assertEquals(-1, mem.compareTo(0, 1, mem, 0, 2));
- assertEquals(0, mem.compareTo(1, 1, mem, 1, 1));
- assertEquals(1, mem.compareTo(1, 2, mem, 1, 1));
- }
-
@Test
public void checkAsBuffer() {
WritableMemory wmem = WritableMemory.allocate(64);
diff --git
a/src/test/java/org/apache/datasketches/memory/internal/ResourceTest.java
b/src/test/java/org/apache/datasketches/memory/internal/ResourceTest.java
index 0a3d411..c9acfee 100644
--- a/src/test/java/org/apache/datasketches/memory/internal/ResourceTest.java
+++ b/src/test/java/org/apache/datasketches/memory/internal/ResourceTest.java
@@ -33,11 +33,114 @@ import java.nio.ByteOrder;
import org.apache.datasketches.memory.Buffer;
import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.MemoryRequestServer;
import org.apache.datasketches.memory.Resource;
import org.apache.datasketches.memory.WritableMemory;
import org.testng.annotations.Test;
public class ResourceTest {
+ private static final MemoryRequestServer memReqSvr =
Resource.defaultMemReqSvr;
+
+ @Test
+ public void checkCompare() {
+ byte[] arr1 = {1,2,3,4,5,6,7,8,9};
+ byte[] arr2 = {1,2,3,4,5,6,7,8};
+ MemorySegment seg1 = MemorySegment.ofArray(arr1);
+ MemorySegment seg2 = MemorySegment.ofArray(arr2);
+ int c = ResourceImpl.compare(seg1, 0, 9, seg2, 0, 8);
+ //println(c);
+ assertEquals(c, 1);
+ }
+
+ @Test
+ public void checkCompareToDirect() throws Exception {
+ byte[] arr1 = new byte[] {0, 1, 2, 3};
+ byte[] arr2 = new byte[] {0, 1, 2, 4};
+ byte[] arr3 = new byte[] {0, 1, 2, 3, 4};
+
+ try (Arena arena = Arena.ofConfined()) {
+ WritableMemory mem1 = WritableMemory.allocateDirect(4, 1,
ByteOrder.nativeOrder(), memReqSvr, arena);
+ WritableMemory mem2 = WritableMemory.allocateDirect(4, 1,
ByteOrder.nativeOrder(), memReqSvr, arena);
+ WritableMemory mem3 = WritableMemory.allocateDirect(5, 1,
ByteOrder.nativeOrder(), memReqSvr, arena);
+
+ mem1.putByteArray(0, arr1, 0, 4);
+ mem2.putByteArray(0, arr2, 0, 4);
+ mem3.putByteArray(0, arr3, 0, 5);
+
+ int comp = mem1.compareTo(0, 3, mem2, 0, 3);
+ assertEquals(comp, 0);
+ comp = mem1.compareTo(0, 4, mem2, 0, 4);
+ assertEquals(comp, -1);
+ comp = mem2.compareTo(0, 4, mem1, 0, 4);
+ assertEquals(comp, 1);
+ //different lengths
+ comp = mem1.compareTo(0, 4, mem3, 0, 5);
+ assertEquals(comp, -1);
+ comp = mem3.compareTo(0, 5, mem1, 0, 4);
+ assertEquals(comp, 1);
+ }
+ }
+
+
+
+ @Test
+ public void checkCompareToHeap() {
+ byte[] arr1 = new byte[] {0, 1, 2, 3};
+ byte[] arr2 = new byte[] {0, 1, 2, 4};
+ byte[] arr3 = new byte[] {0, 1, 2, 3, 4};
+
+ Memory mem1 = Memory.wrap(arr1);
+ Memory mem2 = Memory.wrap(arr2);
+ Memory mem3 = Memory.wrap(arr3);
+ Memory mem4 = Memory.wrap(arr3); //same resource
+
+ int comp = mem1.compareTo(0, 3, mem2, 0, 3);
+ assertEquals(comp, 0);
+ comp = mem1.compareTo(0, 4, mem2, 0, 4);
+ assertEquals(comp, -1);
+ comp = mem2.compareTo(0, 4, mem1, 0, 4);
+ assertEquals(comp, 1);
+ //different lengths
+ comp = mem1.compareTo(0, 4, mem3, 0, 5);
+ assertEquals(comp, -1);
+ comp = mem3.compareTo(0, 5, mem1, 0, 4);
+ assertEquals(comp, 1);
+ comp = mem3.compareTo(0, 5, mem4, 0, 5);
+ assertEquals(comp, 0);
+ comp = mem3.compareTo(0, 4, mem4, 1, 4);
+ assertEquals(comp, -1);
+ ResourceImpl.checkBounds(0, 5, mem3.getCapacity());
+ }
+
+ @Test
+ public void checkCompareToSamePrefix() {
+ WritableMemory wmem = WritableMemory.allocate(3); wmem.clear();
+ //The MemorySegment.mismatch(...) returns -1 in all 3 cases: but we need
to consider order.
+ assertEquals(wmem.compareTo(0, 1, wmem, 0, 2), -1); //{[0],x,x}, {[0,0],x}
+ assertEquals(wmem.compareTo(1, 1, wmem, 1, 1), 0); //{x,[0],x}, {{x,[0],x}
+ assertEquals(wmem.compareTo(1, 2, wmem, 1, 1), 1); //{x,[0,0]}, {{x,[0],x}
+ }
+
+ @Test
+ public void checkGetRelativeOffset() {
+ WritableMemory wmem = WritableMemory.allocateDirect(1024);
+ WritableMemory reg = wmem.writableRegion(512, 256);
+ long off = wmem.getRelativeOffset(reg);
+ assertEquals(off, 512);
+ }
+
+ @Test
+ public void checkIsByteOrderCompatible() {
+ WritableMemory wmem = WritableMemory.allocate(8);
+ assertTrue(wmem.isByteOrderCompatible(ByteOrder.nativeOrder()));
+ }
+
+ @Test
+ public void checkIsSameResource() {
+ WritableMemory wmem = WritableMemory.allocateDirect(1024);
+ WritableMemory reg = wmem.writableRegion(0, 1024);
+ assertTrue(wmem.isSameResource(reg));
+ }
@Test
public void checkNativeOverlap() {
@@ -92,24 +195,17 @@ public class ResourceTest {
}
@Test
- public void checkIsByteOrderCompatible() {
- WritableMemory wmem = WritableMemory.allocate(8);
- assertTrue(wmem.isByteOrderCompatible(ByteOrder.nativeOrder()));
- }
-
- @Test
- public void checkXxHash64() {
- WritableMemory mem = WritableMemory.allocate(8);
- long out = mem.xxHash64(mem.getLong(0), 1L);
- assertTrue(out != 0);
- }
-
- @Test
- public void checkTypeDecode() {
- for (int i = 0; i < 256; i++) {
- String str = ResourceImpl.typeDecode(i);
- println(i + "\t" + str);
- }
+ public void checkParseJavaVersion() {
+ try {
+ ResourceImpl.parseJavaVersion("15_1");
+ fail();
+ } catch (IllegalArgumentException e) { }
+ try {
+ ResourceImpl.parseJavaVersion("20");
+ fail();
+ } catch (IllegalArgumentException e) { }
+ ResourceImpl.parseJavaVersion("21");
+ ResourceImpl.parseJavaVersion("22");
}
@Test
@@ -156,32 +252,18 @@ public class ResourceTest {
}
@Test
- public void checkParseJavaVersion() {
- try {
- ResourceImpl.parseJavaVersion("15_1");
- fail();
- } catch (IllegalArgumentException e) { }
- try {
- ResourceImpl.parseJavaVersion("20");
- fail();
- } catch (IllegalArgumentException e) { }
- ResourceImpl.parseJavaVersion("21");
- ResourceImpl.parseJavaVersion("22");
- }
-
- @Test
- public void checkGetRelativeOffset() {
- WritableMemory wmem = WritableMemory.allocateDirect(1024);
- WritableMemory reg = wmem.writableRegion(512, 256);
- long off = wmem.getRelativeOffset(reg);
- assertEquals(off, 512);
+ public void checkTypeDecode() {
+ for (int i = 0; i < 256; i++) {
+ String str = ResourceImpl.typeDecode(i);
+ println(i + "\t" + str);
+ }
}
@Test
- public void checkIsSameResource() {
- WritableMemory wmem = WritableMemory.allocateDirect(1024);
- WritableMemory reg = wmem.writableRegion(0, 1024);
- assertTrue(wmem.isSameResource(reg));
+ public void checkXxHash64() {
+ WritableMemory mem = WritableMemory.allocate(8);
+ long out = mem.xxHash64(mem.getLong(0), 1L);
+ assertTrue(out != 0);
}
/********************/
diff --git
a/src/test/java/org/apache/datasketches/memory/internal/WritableMemoryTest.java
b/src/test/java/org/apache/datasketches/memory/internal/WritableMemoryTest.java
index 6cb2b03..310f258 100644
---
a/src/test/java/org/apache/datasketches/memory/internal/WritableMemoryTest.java
+++
b/src/test/java/org/apache/datasketches/memory/internal/WritableMemoryTest.java
@@ -71,10 +71,6 @@ public class WritableMemoryTest {
public void checkEquals() {
int len = 7;
WritableMemory wmem1 = WritableMemory.allocate(len);
- //@SuppressWarnings({"EqualsWithItself", "SelfEquals"}) //unsupported
- //SelfEquals for Plexus, EqualsWithItself for IntelliJ
- //boolean eq1 = wmem1.equals(wmem1); //strict profile complains
- //assertTrue(eq1);
WritableMemory wmem2 = WritableMemory.allocate(len + 1);
assertFalse(wmem1.equals(wmem2));
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]