This is an automated email from the ASF dual-hosted git repository.
leerho pushed a commit to branch update_7.0.X
in repository https://gitbox.apache.org/repos/asf/datasketches-memory.git
The following commit(s) were added to refs/heads/update_7.0.X by this push:
new 5541e6f7 update 7.0.X
5541e6f7 is described below
commit 5541e6f7d67202d53ed8b04875265af9c1e11daf
Author: Lee Rhodes <[email protected]>
AuthorDate: Sat May 9 16:28:50 2026 -0700
update 7.0.X
---
.../memory/internal/AllocateDirectWritableMap.java | 9 +++--
.../apache/datasketches/memory/internal/Util.java | 37 +++++++++----------
.../internal/AllocateDirectMapMemoryTest.java | 43 +++++++++-------------
.../AllocateDirectWritableMapMemoryTest.java | 25 +++++--------
.../datasketches/memory/internal/MemoryTest.java | 10 -----
.../datasketches/memory/internal/ThreadTest.java | 9 ++---
.../datasketches/memory/internal/UtilTest.java | 25 +++++++------
7 files changed, 65 insertions(+), 93 deletions(-)
diff --git
a/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/internal/AllocateDirectWritableMap.java
b/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/internal/AllocateDirectWritableMap.java
index de99ef68..89fa821c 100644
---
a/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/internal/AllocateDirectWritableMap.java
+++
b/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/internal/AllocateDirectWritableMap.java
@@ -146,10 +146,6 @@ final class AllocateDirectWritableMap {
return deallocator.getValid();
}
- public static boolean isFileReadOnly(final File file) {
- return (!file.canWrite());
- }
-
public boolean isLoaded() {
try {
return (boolean) MAPPED_BYTE_BUFFER_ISLOADED0_METHOD
@@ -176,6 +172,11 @@ final class AllocateDirectWritableMap {
}
// Private methods
+
+ private static boolean isFileReadOnly(final File file) {
+ return (file.canRead() && !file.canWrite());
+ }
+
/**
* called by load(). Calls the native method load0 in MappedByteBuffer.java,
implemented
* in MappedByteBuffer.c. See reference at top of class. load0 allows
setting a mapping length
diff --git
a/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/internal/Util.java
b/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/internal/Util.java
index 1302a2d2..5769fcab 100644
---
a/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/internal/Util.java
+++
b/datasketches-memory-java11/src/main/java/org/apache/datasketches/memory/internal/Util.java
@@ -293,26 +293,8 @@ public final class Util {
}
/**
- * Checks if the given resourceName exists and sets it to Read-Only. This
is only used in test.
- * If the resource is in a JAR it will be copied into the File System as a
temporary file first.
- * This will not work if the file is currently memory-mapped.
- * If it is memory-mapped, close the mapping first.
- * @param resourceName the given resource.
- * @return File the read only file.
- */
- public static File ensureReadOnly(final String resourceName) {
- final File file = getResourceFile(resourceName);
- if (!file.exists()) {
- throw new IllegalArgumentException("File not found.");
- }
- // Works on Windows and POSIX)
- file.setReadOnly();
- return file;
- }
-
- /**
- * Returns a byte array of the contents of the file defined by the given
resourceName.
- * This is only used in test.
+ * Windows, POSIX, and JAR friendly, returns a byte array of the contents of
the file defined by the given
+ * resourceName. This is only used in test.
* If the resource is in a JAR it will be copied into the File System as a
temporary file first.
*
* @param resourceName the short name or the full path name.
@@ -383,4 +365,19 @@ public final class Util {
return tempFile;
}
+
+ /**
+ * Windows, POSIX, and JAR friendly, checks if the given resourceName exists
and sets it to Read-Only.
+ * This is only used in test.
+ * If the resource is in a JAR it will be copied into the File System as a
temporary file first.
+ * This will not work if the file is currently memory-mapped.
+ * If it is memory-mapped, close the mapping first.
+ * @param resourceName the given resource.
+ * @return the read only file.
+ */
+ public static File setResourceReadOnly(final String resourceName) {
+ final File file = getResourceFile(resourceName);
+ file.setReadOnly();
+ return file;
+ }
}
diff --git
a/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/AllocateDirectMapMemoryTest.java
b/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/AllocateDirectMapMemoryTest.java
index ce8b33ac..5ab35c0f 100644
---
a/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/AllocateDirectMapMemoryTest.java
+++
b/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/AllocateDirectMapMemoryTest.java
@@ -17,14 +17,9 @@
* under the License.
*/
-/*
- * Note: Lincoln's Gettysburg Address is in the public domain. See LICENSE.
- */
-
package org.apache.datasketches.memory.internal;
-import static org.apache.datasketches.memory.internal.Util.ensureReadOnly;
-import static org.apache.datasketches.memory.internal.Util.getResourceFile;
+import static org.apache.datasketches.memory.internal.Util.setResourceReadOnly;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
@@ -40,24 +35,26 @@ import org.testng.annotations.Test;
public class AllocateDirectMapMemoryTest {
private static final String LS = System.getProperty("line.separator");
-
+ private File gettyFile;
+ private long gettySize;
+
@BeforeClass
public void setReadOnly() {
- UtilTest.setGettysburgAddressFileToReadOnly();
+ gettyFile = setResourceReadOnly("GettysburgAddress.txt");
+ gettySize = gettyFile.length();
}
@Test(expectedExceptions = IllegalStateException.class)
public void simpleMap() throws IOException {
- File file = ensureReadOnly("GettysburgAddress.txt");
- try (Memory mem = Memory.map(file)) {
+
+ try (Memory mem = Memory.map(gettyFile)) {
mem.close(); //explicit close
} //The Try-With-Resources will throw
}
@Test
public void printGettysburgAddress() throws IOException {
- File file = getResourceFile("GettysburgAddress.txt");
- try (Memory mem = Memory.map(file))
+ try (Memory mem = Memory.map(gettyFile))
{
int len1 = (int)mem.getCapacity();
println("Mem Cap: " + len1);
@@ -86,14 +83,13 @@ public class AllocateDirectMapMemoryTest {
@Test
public void testIllegalArguments() throws IOException {
- File file = getResourceFile("GettysburgAddress.txt");
- try (Memory mem = Memory.map(file, -1, Integer.MAX_VALUE,
ByteOrder.nativeOrder())) {
+ try (Memory mem = Memory.map(gettyFile, -1, Integer.MAX_VALUE,
ByteOrder.nativeOrder())) {
fail("Failed: Position was negative.");
} catch (IllegalArgumentException e) {
//ok
}
- try (Memory mem = Memory.map(file, 0, -1, ByteOrder.nativeOrder())) {
+ try (Memory mem = Memory.map(gettyFile, 0, -1, ByteOrder.nativeOrder())) {
fail("Failed: Size was negative.");
} catch (IllegalArgumentException e) {
//ok
@@ -102,30 +98,27 @@ public class AllocateDirectMapMemoryTest {
@Test(expectedExceptions = IllegalStateException.class)
public void testAccessAfterClose() throws IOException {
- File file = getResourceFile("GettysburgAddress.txt");
- long memCapacity = file.length();
- try (Memory mem = Memory.map(file, 0, memCapacity,
ByteOrder.nativeOrder())) {
+ long memCapacity = gettySize;
+ try (Memory mem = Memory.map(gettyFile, 0, memCapacity,
ByteOrder.nativeOrder())) {
assertEquals(memCapacity, mem.getCapacity());
} //normal close via TWR
- Memory mem = Memory.map(file, 0, memCapacity, ByteOrder.nativeOrder());
+ Memory mem = Memory.map(gettyFile, 0, memCapacity,
ByteOrder.nativeOrder());
mem.close(); //normal manual close
mem.getCapacity(); //isLoaded(); //already closed, invalid
}
@Test(expectedExceptions = IllegalStateException.class)
public void testReadFailAfterClose() throws IOException {
- File file = getResourceFile("GettysburgAddress.txt");
- long memCapacity = file.length();
- Memory mem = Memory.map(file, 0, memCapacity, ByteOrder.nativeOrder());
+ long memCapacity = gettySize;
+ Memory mem = Memory.map(gettyFile, 0, memCapacity,
ByteOrder.nativeOrder());
mem.close();
mem.isLoaded();
}
@Test
public void testLoad() throws IOException {
- File file = getResourceFile("GettysburgAddress.txt");
- long memCapacity = file.length();
- try (Memory mem = Memory.map(file, 0, memCapacity,
ByteOrder.nativeOrder())) {
+ long memCapacity = gettySize;
+ try (Memory mem = Memory.map(gettyFile, 0, memCapacity,
ByteOrder.nativeOrder())) {
mem.load();
assertTrue(mem.isLoaded());
} //normal TWR close
diff --git
a/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/AllocateDirectWritableMapMemoryTest.java
b/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/AllocateDirectWritableMapMemoryTest.java
index 98d3c2a2..6f6a31c0 100644
---
a/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/AllocateDirectWritableMapMemoryTest.java
+++
b/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/AllocateDirectWritableMapMemoryTest.java
@@ -17,14 +17,10 @@
* under the License.
*/
-/*
- * Note: Lincoln's Gettysburg Address is in the public domain. See LICENSE.
- */
-
package org.apache.datasketches.memory.internal;
import static java.nio.charset.StandardCharsets.UTF_8;
-import static org.apache.datasketches.memory.internal.Util.getResourceFile;
+import static org.apache.datasketches.memory.internal.Util.setResourceReadOnly;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
@@ -44,16 +40,16 @@ import org.testng.annotations.Test;
public class AllocateDirectWritableMapMemoryTest {
private static final String LS = System.getProperty("line.separator");
-
+ private File gettyFile;
+
@BeforeClass
public void setReadOnly() {
- UtilTest.setGettysburgAddressFileToReadOnly();
+ gettyFile = setResourceReadOnly("GettysburgAddress.txt");
}
@Test
public void simpleMap() throws Exception {
- File file = getResourceFile("GettysburgAddress.txt");
- try (Memory mem = Memory.map(file)) {
+ try (Memory mem = Memory.map(gettyFile)) {
byte[] bytes = new byte[(int)mem.getCapacity()];
mem.getByteArray(0, bytes, 0, bytes.length);
String text = new String(bytes, UTF_8);
@@ -133,17 +129,14 @@ public class AllocateDirectWritableMapMemoryTest {
@Test(expectedExceptions = ReadOnlyException.class)
public void simpleMap2() throws IOException {
- File file = getResourceFile("GettysburgAddress.txt");
- assertTrue(file.canRead() && !file.canWrite());
- try (WritableMemory wmem = WritableMemory.writableMap(file)) { //throws
- //
+ try (WritableMemory wmem = WritableMemory.writableMap(gettyFile)) {
+ //throws
}
}
@Test(expectedExceptions = ReadOnlyException.class)
- public void checkOverLength() throws Exception {
- File file = getResourceFile("GettysburgAddress.txt");
- WritableMemory.writableMap(file, 0, 1 << 20, ByteOrder.nativeOrder());
+ public void checkOverLength() throws Exception {
+ WritableMemory.writableMap(gettyFile, 0, 1 << 20, ByteOrder.nativeOrder());
}
@Test
diff --git
a/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/MemoryTest.java
b/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/MemoryTest.java
index aacdba4b..818d0e3b 100644
---
a/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/MemoryTest.java
+++
b/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/MemoryTest.java
@@ -17,10 +17,6 @@
* under the License.
*/
-/*
- * Note: Lincoln's Gettysburg Address is in the public domain. See LICENSE.
- */
-
package org.apache.datasketches.memory.internal;
import static org.testng.Assert.assertEquals;
@@ -40,18 +36,12 @@ import org.apache.datasketches.memory.MemoryRequestServer;
import org.apache.datasketches.memory.Resource;
import org.apache.datasketches.memory.WritableBuffer;
import org.apache.datasketches.memory.WritableMemory;
-import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.collections.Lists;
public class MemoryTest {
private static final String LS = Util.LS;
- @BeforeClass
- public void setReadOnly() {
- UtilTest.setGettysburgAddressFileToReadOnly();
- }
-
@Test
public void checkDirectRoundTrip() throws Exception {
int n = 1024; //longs
diff --git
a/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/ThreadTest.java
b/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/ThreadTest.java
index e90b7e46..1d19bdd6 100644
---
a/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/ThreadTest.java
+++
b/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/ThreadTest.java
@@ -19,7 +19,6 @@
package org.apache.datasketches.memory.internal;
-import static org.apache.datasketches.memory.internal.Util.getResourceFile;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
@@ -33,20 +32,18 @@ import org.testng.annotations.Test;
public class ThreadTest {
- File file;
+ File gettyFile;
Memory mem;
WritableMemory wmem;
Thread altThread;
@BeforeClass
public void prepareFileAndMemory() throws IOException {
- UtilTest.setGettysburgAddressFileToReadOnly();
- file = getResourceFile("GettysburgAddress.txt");
- assertTrue(AllocateDirectWritableMap.isFileReadOnly(file));
+ gettyFile = Util.setResourceReadOnly("GettysburgAddress.txt");
}
void initMap() throws IOException {
- mem = Memory.map(file); assertTrue(mem.isAlive());
+ mem = Memory.map(gettyFile); assertTrue(mem.isAlive());
}
void initDirectMem() {
diff --git
a/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/UtilTest.java
b/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/UtilTest.java
index 68ae05a0..61abb2af 100644
---
a/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/UtilTest.java
+++
b/datasketches-memory-java11/src/test/java/org/apache/datasketches/memory/internal/UtilTest.java
@@ -20,7 +20,7 @@
package org.apache.datasketches.memory.internal;
import static org.apache.datasketches.memory.internal.Util.characterPad;
-import static org.apache.datasketches.memory.internal.Util.ensureReadOnly;
+import static org.apache.datasketches.memory.internal.Util.setResourceReadOnly;
import static org.apache.datasketches.memory.internal.Util.getResourceBytes;
import static org.apache.datasketches.memory.internal.Util.getResourceFile;
import static org.apache.datasketches.memory.internal.Util.negativeCheck;
@@ -39,11 +39,20 @@ import java.nio.file.attribute.PosixFilePermissions;
import org.apache.datasketches.memory.MemoryBoundsException;
import org.apache.datasketches.memory.WritableMemory;
+import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class UtilTest {
private static final String LS = System.getProperty("line.separator");
-
+ private File gettyFile;
+ private long gettySize;
+
+ @BeforeClass
+ public void setReadOnly() {
+ gettyFile = setResourceReadOnly("GettysburgAddress.txt");
+ gettySize = gettyFile.length();
+ }
+
//Binary Search
@Test
public void checkBinarySearch() {
@@ -128,17 +137,11 @@ public class UtilTest {
}
}
- static final void setGettysburgAddressFileToReadOnly() {
- ensureReadOnly("GettysburgAddress.txt");
- }
-
//Resources
@Test
public void resourceFileExits() {
- final String shortFileName = "GettysburgAddress.txt";
- final File file = getResourceFile(shortFileName);
- assertTrue(file.exists());
+ assertTrue(gettyFile.exists());
}
@Test
@@ -151,9 +154,7 @@ public class UtilTest {
@Test
public void resourceBytesCorrect() {
- final String shortFileName = "GettysburgAddress.txt";
- final byte[] bytes = getResourceBytes(shortFileName);
- assertTrue(bytes.length == 1541);
+ assertTrue(gettySize == 1541);
}
@Test
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]