You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by le...@apache.org on 2022/05/25 01:23:25 UTC
[datasketches-memory17] branch main updated: First commit that has no compile errors.
This is an automated email from the ASF dual-hosted git repository.
leerho pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datasketches-memory17.git
The following commit(s) were added to refs/heads/main by this push:
new 96b22f5 First commit that has no compile errors.
96b22f5 is described below
commit 96b22f5dcbd2058598482e9c271679c244ad249c
Author: Lee Rhodes <le...@users.noreply.github.com>
AuthorDate: Tue May 24 18:23:19 2022 -0700
First commit that has no compile errors.
---
.classpath | 28 ++
.project | 6 +
bin/.asf.yaml | 2 +
bin/.github/workflows/.toolchains.xml | 63 +++
bin/.github/workflows/codeql-analysis.yml | 69 +++
bin/.github/workflows/maven.yml | 99 ++++
.project => bin/.project | 6 +
bin/.settings/edu.umd.cs.findbugs.core.prefs | 146 ++++++
bin/.settings/org.eclipse.core.resources.prefs | 2 +
bin/.settings/org.eclipse.jdt.core.prefs | 131 +++++
bin/.settings/org.eclipse.jdt.launching.prefs | 3 +
bin/.settings/org.eclipse.m2e.core.prefs | 4 +
bin/.settings/org.testng.eclipse.maven.prefs | 7 +
bin/LICENSE | 350 +++++++++++++
bin/NOTICE | 12 +
bin/README.md | 1 +
bin/pom.xml | 542 +++++++++++++++++++++
.../org/apache/datasketches/memory/BaseState.class | Bin 0 -> 2560 bytes
.../memory/DefaultMemoryRequestServer.class | Bin 0 -> 1751 bytes
.../org/apache/datasketches/memory/Memory.class | Bin 0 -> 6867 bytes
.../datasketches/memory/MemoryCloseException.class | Bin 0 -> 1030 bytes
.../datasketches/memory/MemoryException.class | Bin 0 -> 831 bytes
.../datasketches/memory/MemoryRequestServer.class | Bin 0 -> 821 bytes
.../apache/datasketches/memory/MurmurHash3v2.class | Bin 0 -> 4636 bytes
.../datasketches/memory/ReadOnlyException.class | Bin 0 -> 871 bytes
.../datasketches/memory/Utf8CodingException.class | Bin 0 -> 3886 bytes
.../datasketches/memory/WritableMemory.class | Bin 0 -> 8749 bytes
.../org/apache/datasketches/memory/XxHash.class | Bin 0 -> 3799 bytes
.../apache/datasketches/memory/package-info.class | Bin 0 -> 150 bytes
bin/tools/CloverConfig.txt | 49 ++
bin/tools/FindBugsExcludeFilter.xml | 96 ++++
bin/tools/MemoryCheckstyle.xml | 398 +++++++++++++++
bin/tools/scripts/CheckMemoryJar.class | Bin 0 -> 7443 bytes
bin/tools/scripts/LoremIpsum.txt | 3 +
bin/tools/scripts/compile-package-jar.sh | 163 +++++++
bin/tools/scripts/getGitProperties.sh | 66 +++
bin/tools/toolchains.xml | 53 ++
src/main/java/module-info.java | 26 +
.../org/apache/datasketches/memory/BaseBuffer.java | 136 ++++++
.../org/apache/datasketches/memory/BaseState.java | 147 ++----
.../org/apache/datasketches/memory/Buffer.java | 387 +++++++++++++++
.../org/apache/datasketches/memory/Memory.java | 176 ++-----
.../datasketches/memory/Utf8CodingException.java | 129 -----
.../apache/datasketches/memory/WritableBuffer.java | 367 ++++++++++++++
.../apache/datasketches/memory/WritableMemory.java | 314 +++++-------
.../org/apache/datasketches/memory/XxHash.java | 49 +-
.../internal/BBNonNativeWritableBufferImpl.java | 52 ++
.../internal/BBNonNativeWritableMemoryImpl.java | 40 ++
.../memory/internal/BBWritableBufferImpl.java | 52 ++
.../memory/internal/BBWritableMemoryImpl.java | 40 ++
.../memory/internal/BaseBufferImpl.java | 217 +++++++++
.../memory/internal/BaseStateImpl.java | 407 ++++++++++++++++
.../memory/internal/BaseWritableBufferImpl.java | 450 +++++++++++++++++
.../memory/internal/BaseWritableMemoryImpl.java | 375 ++++++++++++++
.../memory/internal/CompareAndCopy.java | 70 +++
.../DirectNonNativeWritableBufferImpl.java | 54 ++
.../DirectNonNativeWritableMemoryImpl.java | 43 ++
.../memory/internal/DirectWritableBufferImpl.java | 53 ++
.../memory/internal/DirectWritableMemoryImpl.java | 43 ++
.../internal/HeapNonNativeWritableBufferImpl.java | 53 ++
.../internal/HeapNonNativeWritableMemoryImpl.java | 43 ++
.../memory/internal/HeapWritableBufferImpl.java | 53 ++
.../memory/internal/HeapWritableMemoryImpl.java | 43 ++
.../apache/datasketches/memory/internal/Ints.java | 39 ++
.../internal/MapNonNativeWritableBufferImpl.java | 50 ++
.../internal/MapNonNativeWritableMemoryImpl.java | 40 ++
.../memory/internal/MapWritableBufferImpl.java | 50 ++
.../memory/internal/MapWritableMemoryImpl.java | 40 ++
.../MurmurHash3v3.java} | 86 ++--
.../memory/internal/NativeWritableBufferImpl.java | 324 ++++++++++++
.../memory/internal/NativeWritableMemoryImpl.java | 224 +++++++++
.../internal/NonNativeWritableBufferImpl.java | 363 ++++++++++++++
.../internal/NonNativeWritableMemoryImpl.java | 260 ++++++++++
.../apache/datasketches/memory/internal/Util.java | 349 +++++++++++++
.../datasketches/memory/internal/XxHash64.java | 308 ++++++++++++
.../datasketches/memory/internal/package-info.java | 20 +
.../apache/datasketches/memory/package-info.java | 72 +--
target/.gitignore | 1 +
78 files changed, 7634 insertions(+), 710 deletions(-)
diff --git a/.classpath b/.classpath
new file mode 100644
index 0000000..14ca02c
--- /dev/null
+++ b/.classpath
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" output="target/classes" path="src/main/java">
+ <attributes>
+ <attribute name="optional" value="true"/>
+ <attribute name="maven.pomderived" value="true"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="src" output="target/test-classes" path="src/test/java">
+ <attributes>
+ <attribute name="test" value="true"/>
+ <attribute name="optional" value="true"/>
+ <attribute name="maven.pomderived" value="true"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
+ <attributes>
+ <attribute name="module" value="true"/>
+ <attribute name="maven.pomderived" value="true"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
+ <attributes>
+ <attribute name="maven.pomderived" value="true"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="output" path="target/classes"/>
+</classpath>
diff --git a/.project b/.project
index 2a426a7..3fd6cca 100644
--- a/.project
+++ b/.project
@@ -5,6 +5,11 @@
<projects>
</projects>
<buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
@@ -13,5 +18,6 @@
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
diff --git a/bin/.asf.yaml b/bin/.asf.yaml
new file mode 100644
index 0000000..15e33a5
--- /dev/null
+++ b/bin/.asf.yaml
@@ -0,0 +1,2 @@
+github:
+ homepage: https://datasketches.apache.org
\ No newline at end of file
diff --git a/bin/.github/workflows/.toolchains.xml b/bin/.github/workflows/.toolchains.xml
new file mode 100644
index 0000000..c415d5d
--- /dev/null
+++ b/bin/.github/workflows/.toolchains.xml
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF8"?>
+<toolchains>
+ <toolchain>
+ <type>jdk</type>
+ <provides>
+ <version>1.8</version>
+ <vendor>openjdk</vendor>
+ </provides>
+ <configuration>
+ <jdkHome>${env.JAVA8_HOME}</jdkHome>
+ </configuration>
+ </toolchain>
+ <toolchain>
+ <type>jdk</type>
+ <provides>
+ <version>8</version>
+ <vendor>openjdk</vendor>
+ </provides>
+ <configuration>
+ <jdkHome>${env.JAVA8_HOME}</jdkHome>
+ </configuration>
+ </toolchain>
+ <toolchain>
+ <type>jdk</type>
+ <provides>
+ <version>11</version>
+ <vendor>openjdk</vendor>
+ </provides>
+ <configuration>
+ <jdkHome>${env.JAVA11_HOME}</jdkHome>
+ </configuration>
+ </toolchain>
+ <toolchain>
+ <type>jdk</type>
+ <provides>
+ <version>12</version>
+ <vendor>openjdk</vendor>
+ </provides>
+ <configuration>
+ <jdkHome>${env.JAVA12_HOME}</jdkHome>
+ </configuration>
+ </toolchain>
+ <toolchain>
+ <type>jdk</type>
+ <provides>
+ <version>13</version>
+ <vendor>openjdk</vendor>
+ </provides>
+ <configuration>
+ <jdkHome>${env.JAVA13_HOME}</jdkHome>
+ </configuration>
+ </toolchain>
+ <toolchain>
+ <type>jdk</type>
+ <provides>
+ <version>17</version>
+ <vendor>openjdk</vendor>
+ </provides>
+ <configuration>
+ <jdkHome>${env.JAVA17_HOME}</jdkHome>
+ </configuration>
+ </toolchain>
+</toolchains>
\ No newline at end of file
diff --git a/bin/.github/workflows/codeql-analysis.yml b/bin/.github/workflows/codeql-analysis.yml
new file mode 100644
index 0000000..9beda7d
--- /dev/null
+++ b/bin/.github/workflows/codeql-analysis.yml
@@ -0,0 +1,69 @@
+# For most projects, this workflow file will not need changing; you simply need
+# to commit it to your repository.
+#
+# You may wish to alter this file to override the set of languages analyzed,
+# or to provide custom queries or build logic.
+#
+# ******** NOTE ********
+# We have attempted to detect the languages in your repository. Please check
+# the `language` matrix defined below to confirm you have the correct set of
+# supported CodeQL languages.
+#
+name: "CodeQL"
+
+on:
+ push:
+ branches: [ master ]
+ pull_request:
+ # The branches below must be a subset of the branches above
+ branches: [ master ]
+ workflow_dispatch:
+
+jobs:
+ analyze:
+ name: Analyze
+ runs-on: ubuntu-latest
+ permissions:
+ actions: read
+ contents: read
+ security-events: write
+
+ strategy:
+ fail-fast: false
+ matrix:
+ language: [ 'java' ]
+ # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
+ # Learn more about CodeQL language support at https://git.io/codeql-language-support
+
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v3
+
+ # Initializes the CodeQL tools for scanning.
+ - name: Initialize CodeQL
+ uses: github/codeql-action/init@v2
+ with:
+ languages: ${{ matrix.language }}
+ # If you wish to specify custom queries, you can do so here or in a config file.
+ # By default, queries listed here will override any specified in a config file.
+ # Prefix the list here with "+" to use these queries and those in the config file.
+ # queries: ./path/to/local/query, your-org/your-repo/queries@main
+
+ # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
+ # If this step fails, then you should remove it and run the build manually (see below)
+ - name: Autobuild
+ uses: github/codeql-action/autobuild@v2
+
+ # âšī¸ Command-line programs to run using the OS shell.
+ # đ https://git.io/JvXDl
+
+ # âī¸ If the Autobuild fails above, remove it and uncomment the following three lines
+ # and modify them (or add more) to build your code if your project
+ # uses a compiled language
+
+ #- run: |
+ # make bootstrap
+ # make release
+
+ - name: Perform CodeQL Analysis
+ uses: github/codeql-action/analyze@v2
diff --git a/bin/.github/workflows/maven.yml b/bin/.github/workflows/maven.yml
new file mode 100644
index 0000000..d961c5f
--- /dev/null
+++ b/bin/.github/workflows/maven.yml
@@ -0,0 +1,99 @@
+name: Java Test Coverage with Maven, Coveralls
+
+on:
+ pull_request:
+ push:
+ branches: [ master ]
+ workflow_dispatch:
+
+env:
+ MAVEN_OPTS: -Xmx4g -Xms1g
+ repo_token: ${{secrets.coveralls_token}}
+ RUNNER_TEMP: /tmp
+
+jobs:
+ build:
+ name: Build, Test, Coverage
+ runs-on: ubuntu-latest
+ strategy:
+ fail-fast: false
+ matrix:
+ jdk: [17]
+
+ # All JDKs are installed per build machine which is inefficient
+
+ env:
+ JDK_VERSION: ${{ matrix.jdk }}
+
+ steps:
+ - name: "Checkout ${{ github.ref }} ( ${{ github.sha }} )"
+ uses: actions/checkout@v2
+ with:
+ persist-credentials: false
+
+ - name: Cache local Maven repository
+ uses: actions/cache@v2
+ with:
+ path: ~/.m2/repository
+ key: build-${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
+ restore-keys: build-${{ runner.os }}-maven-
+
+ - name: Install JDK 17
+ uses: AdoptOpenJDK/install-jdk@v1
+ with:
+ version: '17'
+ architecture: x64
+ impl: hotspot
+ targets: 'JAVA17_HOME'
+
+ - name: Install Matrix JDK
+ uses: AdoptOpenJDK/install-jdk@v1
+ with:
+ version: ${{ matrix.jdk }}
+ architecture: x64
+ impl: hotspot
+ targets: 'JAVA_HOME'
+
+ - name: Echo Java Version
+ run: >
+ java -version
+
+ - name: Compile
+ run: >
+ mvn clean compile
+ -Dmaven.javadoc.skip=true
+ -Dgpg.skip=true
+ --toolchains .github/workflows/.toolchains.xml
+
+ - name: Install Dependencies
+ run: >
+ mvn clean install
+ -DskipTests=true
+ -Dmaven.javadoc.skip=true
+ -Dgpg.skip=true
+ --toolchains .github/workflows/.toolchains.xml
+
+ - name: Package
+ run: >
+ mvn package
+ -Dmaven.javadoc.skip=true
+ -Dgpg.skip=true
+ -Denvironment=ci
+ -Dmatrix.jdk.version=${{matrix.jdk}}
+ --toolchains .github/workflows/.toolchains.xml
+
+ # The GitTag for CI purposes is irrelevant
+ - name: Custom build script
+ run: |
+ ./tools/scripts/compile-package-jar.sh $JAVA_HOME x.y.z .
+ shell: bash
+
+ - name: Test & Report
+ if: ${{ matrix.jdk == 17 && success() }}
+ run: >
+ mvn verify coveralls:report -B -V
+ -Dcoveralls-repo-token=${repo_token}
+ -Dmaven.javadoc.skip=true
+ -Dgpg.skip=true
+ -Dmatrix.jdk.version=${{matrix.jdk}}
+ --toolchains .github/workflows/.toolchains.xml
\ No newline at end of file
diff --git a/.project b/bin/.project
similarity index 69%
copy from .project
copy to bin/.project
index 2a426a7..3fd6cca 100644
--- a/.project
+++ b/bin/.project
@@ -5,6 +5,11 @@
<projects>
</projects>
<buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
@@ -13,5 +18,6 @@
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
diff --git a/bin/.settings/edu.umd.cs.findbugs.core.prefs b/bin/.settings/edu.umd.cs.findbugs.core.prefs
new file mode 100644
index 0000000..9ef7dcd
--- /dev/null
+++ b/bin/.settings/edu.umd.cs.findbugs.core.prefs
@@ -0,0 +1,146 @@
+#SpotBugs User Preferences
+#Mon Nov 15 10:10:51 PST 2021
+detectorExplicitSerialization=ExplicitSerialization|true
+detectorMultithreadedInstanceAccess=MultithreadedInstanceAccess|true
+detectorConfusionBetweenInheritedAndOuterMethod=ConfusionBetweenInheritedAndOuterMethod|true
+detectorWrongMapIterator=WrongMapIterator|true
+detectorUnnecessaryMath=UnnecessaryMath|true
+detectorUselessSubclassMethod=UselessSubclassMethod|false
+filter_settings=Medium|BAD_PRACTICE,CORRECTNESS,MT_CORRECTNESS,PERFORMANCE,STYLE|false|15
+detectorURLProblems=URLProblems|true
+detectorIteratorIdioms=IteratorIdioms|true
+detectorMutableEnum=MutableEnum|true
+detectorFindNonShortCircuit=FindNonShortCircuit|true
+detectorSynchronizeAndNullCheckField=SynchronizeAndNullCheckField|true
+detectorVolatileUsage=VolatileUsage|true
+detectorFindNakedNotify=FindNakedNotify|true
+detectorFindUninitializedGet=FindUninitializedGet|true
+detectorFindUseOfNonSerializableValue=FindUseOfNonSerializableValue|true
+detectorFindJSR166LockMonitorenter=FindJSR166LockMonitorenter|true
+detectorQuestionableBooleanAssignment=QuestionableBooleanAssignment|true
+detectorSwitchFallthrough=SwitchFallthrough|true
+detectorFindLocalSelfAssignment2=FindLocalSelfAssignment2|true
+detectorConfusedInheritance=ConfusedInheritance|true
+detectorSynchronizationOnSharedBuiltinConstant=SynchronizationOnSharedBuiltinConstant|true
+detectorMutableStaticFields=MutableStaticFields|true
+detectorInvalidJUnitTest=InvalidJUnitTest|true
+detectorInfiniteLoop=InfiniteLoop|true
+detectorFindRunInvocations=FindRunInvocations|true
+detectorBadSyntaxForRegularExpression=BadSyntaxForRegularExpression|true
+detectorXMLFactoryBypass=XMLFactoryBypass|true
+detectorFindOpenStream=FindOpenStream|true
+detectorCheckExpectedWarnings=CheckExpectedWarnings|false
+detectorHugeSharedStringConstants=HugeSharedStringConstants|true
+detectorLostLoggerDueToWeakReference=LostLoggerDueToWeakReference|true
+detectorStringConcatenation=StringConcatenation|true
+detectorLoadOfKnownNullValue=LoadOfKnownNullValue|true
+detectorFinalizerNullsFields=FinalizerNullsFields|true
+detectorFindFieldSelfAssignment=FindFieldSelfAssignment|true
+detectorInefficientToArray=InefficientToArray|false
+detectorDontCatchIllegalMonitorStateException=DontCatchIllegalMonitorStateException|true
+detectorInconsistentAnnotations=InconsistentAnnotations|true
+detectorBadlyOverriddenAdapter=BadlyOverriddenAdapter|true
+detectorInstantiateStaticClass=InstantiateStaticClass|true
+detectorCheckRelaxingNullnessAnnotation=CheckRelaxingNullnessAnnotation|true
+detectorMethodReturnCheck=MethodReturnCheck|true
+detectorEqualsOperandShouldHaveClassCompatibleWithThis=EqualsOperandShouldHaveClassCompatibleWithThis|true
+detectorFindDoubleCheck=FindDoubleCheck|true
+detectorFindBadForLoop=FindBadForLoop|true
+detectorDefaultEncodingDetector=DefaultEncodingDetector|true
+detectorFindInconsistentSync2=FindInconsistentSync2|true
+detectorFindSpinLoop=FindSpinLoop|true
+detectorFindMaskedFields=FindMaskedFields|true
+detectorBooleanReturnNull=BooleanReturnNull|true
+detectorFindUnsyncGet=FindUnsyncGet|true
+detectorCrossSiteScripting=CrossSiteScripting|true
+detectorDroppedException=DroppedException|true
+detectorFindDeadLocalStores=FindDeadLocalStores|true
+detectorCheckImmutableAnnotation=CheckImmutableAnnotation|true
+detectorInfiniteRecursiveLoop=InfiniteRecursiveLoop|true
+detectorFindRefComparison=FindRefComparison|true
+detectorFindRoughConstants=FindRoughConstants|true
+detectorMutableLock=MutableLock|true
+detectorFindNullDeref=FindNullDeref|true
+detectorFindReturnRef=FindReturnRef|true
+detectorSynchronizeOnClassLiteralNotGetClass=SynchronizeOnClassLiteralNotGetClass|true
+detectorFindUselessControlFlow=FindUselessControlFlow|true
+detectorOverridingEqualsNotSymmetrical=OverridingEqualsNotSymmetrical|true
+detectorIDivResultCastToDouble=IDivResultCastToDouble|true
+detectorReadOfInstanceFieldInMethodInvokedByConstructorInSuperclass=ReadOfInstanceFieldInMethodInvokedByConstructorInSuperclass|true
+detectorFindSelfComparison=FindSelfComparison|true
+detectorFindFloatEquality=FindFloatEquality|true
+detectorFindComparatorProblems=FindComparatorProblems|true
+detectorRepeatedConditionals=RepeatedConditionals|true
+filter_settings_neg=MALICIOUS_CODE,SECURITY,EXPERIMENTAL,NOISE,I18N|
+detectorInefficientMemberAccess=InefficientMemberAccess|false
+detectorFindUncalledPrivateMethods=FindUncalledPrivateMethods|true
+detectorNumberConstructor=NumberConstructor|true
+detectorDontAssertInstanceofInTests=DontAssertInstanceofInTests|true
+detectorFindFinalizeInvocations=FindFinalizeInvocations|true
+detectorFindBadEndOfStreamCheck=FindBadEndOfStreamCheck|true
+detectorFindNullDerefsInvolvingNonShortCircuitEvaluation=FindNullDerefsInvolvingNonShortCircuitEvaluation|true
+detectorDontIgnoreResultOfPutIfAbsent=DontIgnoreResultOfPutIfAbsent|true
+detectorFindUnconditionalWait=FindUnconditionalWait|true
+detectorFindTwoLockWait=FindTwoLockWait|true
+detectorFindSleepWithLockHeld=FindSleepWithLockHeld|true
+detectorFindUnreleasedLock=FindUnreleasedLock|true
+detectorInefficientIndexOf=InefficientIndexOf|false
+detectorDoInsideDoPrivileged=DoInsideDoPrivileged|true
+detectorFindEmptySynchronizedBlock=FindEmptySynchronizedBlock|true
+detectorOverridingMethodsMustInvokeSuperDetector=OverridingMethodsMustInvokeSuperDetector|true
+detectorWaitInLoop=WaitInLoop|true
+detectorIntCast2LongAsInstant=IntCast2LongAsInstant|true
+detectorBadUseOfReturnValue=BadUseOfReturnValue|true
+detectorFindSqlInjection=FindSqlInjection|true
+detectorUnreadFields=UnreadFields|true
+detectorSynchronizingOnContentsOfFieldToProtectField=SynchronizingOnContentsOfFieldToProtectField|true
+detectorFindUselessObjects=FindUselessObjects|true
+detectorBadAppletConstructor=BadAppletConstructor|false
+detectorInheritanceUnsafeGetResource=InheritanceUnsafeGetResource|true
+detectorSerializableIdiom=SerializableIdiom|true
+detectorNaming=Naming|true
+detectorNoteUnconditionalParamDerefs=NoteUnconditionalParamDerefs|true
+detectorFormatStringChecker=FormatStringChecker|true
+detectorSuspiciousThreadInterrupted=SuspiciousThreadInterrupted|true
+detectorEmptyZipFileEntry=EmptyZipFileEntry|false
+detectorFindCircularDependencies=FindCircularDependencies|false
+detectorPreferZeroLengthArrays=PreferZeroLengthArrays|true
+detectorAtomicityProblem=AtomicityProblem|true
+detectorRuntimeExceptionCapture=RuntimeExceptionCapture|true
+detectorInitializationChain=InitializationChain|true
+detectorInitializeNonnullFieldsInConstructor=InitializeNonnullFieldsInConstructor|true
+detectorOptionalReturnNull=OptionalReturnNull|true
+detectorStartInConstructor=StartInConstructor|true
+detectorFindUnsatisfiedObligation=FindUnsatisfiedObligation|true
+detectorRedundantConditions=RedundantConditions|true
+effort=default
+detectorRedundantInterfaces=RedundantInterfaces|true
+detectorDuplicateBranches=DuplicateBranches|true
+detectorCheckTypeQualifiers=CheckTypeQualifiers|true
+detectorComparatorIdiom=ComparatorIdiom|true
+detectorFindBadCast2=FindBadCast2|true
+detectorFindMismatchedWaitOrNotify=FindMismatchedWaitOrNotify|true
+excludefilter0=tools/FindBugsExcludeFilter.xml|true
+detectorBadResultSetAccess=BadResultSetAccess|true
+detectorIncompatMask=IncompatMask|true
+detectorCovariantArrayAssignment=CovariantArrayAssignment|false
+detectorDumbMethodInvocations=DumbMethodInvocations|true
+run_at_full_build=false
+detectorStaticCalendarDetector=StaticCalendarDetector|true
+detectorUncallableMethodOfAnonymousClass=UncallableMethodOfAnonymousClass|true
+detectorVarArgsProblems=VarArgsProblems|true
+detectorInefficientInitializationInsideLoop=InefficientInitializationInsideLoop|false
+detectorCloneIdiom=CloneIdiom|true
+detectorFindHEmismatch=FindHEmismatch|true
+detectorAppendingToAnObjectOutputStream=AppendingToAnObjectOutputStream|true
+detectorFindSelfComparison2=FindSelfComparison2|true
+detectorLazyInit=LazyInit|true
+detectorFindUnrelatedTypesInGenericContainer=FindUnrelatedTypesInGenericContainer|true
+detectorDontUseEnum=DontUseEnum|true
+detectorFindPuzzlers=FindPuzzlers|true
+detectorCallToUnsupportedMethod=CallToUnsupportedMethod|false
+detectorSuperfluousInstanceOf=SuperfluousInstanceOf|true
+detectorReadReturnShouldBeChecked=ReadReturnShouldBeChecked|true
+detector_threshold=2
+detectorPublicSemaphores=PublicSemaphores|false
+detectorDumbMethods=DumbMethods|true
diff --git a/bin/.settings/org.eclipse.core.resources.prefs b/bin/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/bin/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/<project>=UTF-8
diff --git a/bin/.settings/org.eclipse.jdt.core.prefs b/bin/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..07c54ac
--- /dev/null
+++ b/bin/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,131 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.builder.annotationPath.allLocations=disabled
+org.eclipse.jdt.core.builder.cleanOutputFolder=clean
+org.eclipse.jdt.core.builder.duplicateResourceTask=warning
+org.eclipse.jdt.core.builder.invalidClasspath=abort
+org.eclipse.jdt.core.builder.recreateModifiedClassFileInOutputFolder=ignore
+org.eclipse.jdt.core.builder.resourceCopyExclusionFilter=*.launch
+org.eclipse.jdt.core.circularClasspath=error
+org.eclipse.jdt.core.classpath.exclusionPatterns=enabled
+org.eclipse.jdt.core.classpath.mainOnlyProjectHasTestOnlyDependency=error
+org.eclipse.jdt.core.classpath.multipleOutputLocations=enabled
+org.eclipse.jdt.core.classpath.outputOverlappingAnotherSource=error
+org.eclipse.jdt.core.compiler.annotation.inheritNullAnnotations=disabled
+org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore
+org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull
+org.eclipse.jdt.core.compiler.annotation.nonnull.secondary=
+org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault
+org.eclipse.jdt.core.compiler.annotation.nonnullbydefault.secondary=
+org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable
+org.eclipse.jdt.core.compiler.annotation.nullable.secondary=
+org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=17
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.maxProblemPerUnit=100
+org.eclipse.jdt.core.compiler.problem.APILeak=warning
+org.eclipse.jdt.core.compiler.problem.annotatedTypeArgumentToUnannotated=info
+org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.autoboxing=ignore
+org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning
+org.eclipse.jdt.core.compiler.problem.deadCode=warning
+org.eclipse.jdt.core.compiler.problem.deprecation=warning
+org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled
+org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=enabled
+org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
+org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=warning
+org.eclipse.jdt.core.compiler.problem.fallthroughCase=warning
+org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
+org.eclipse.jdt.core.compiler.problem.fieldHiding=warning
+org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning
+org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning
+org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
+org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning
+org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=disabled
+org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning
+org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=warning
+org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=warning
+org.eclipse.jdt.core.compiler.problem.localVariableHiding=ignore
+org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning
+org.eclipse.jdt.core.compiler.problem.missingDefaultCase=ignore
+org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=warning
+org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled
+org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=warning
+org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=warning
+org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled
+org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning
+org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=warning
+org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning
+org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning
+org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore
+org.eclipse.jdt.core.compiler.problem.nonnullParameterAnnotationDropped=warning
+org.eclipse.jdt.core.compiler.problem.nonnullTypeVariableFromLegacyInvocation=warning
+org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error
+org.eclipse.jdt.core.compiler.problem.nullReference=warning
+org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error
+org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning
+org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning
+org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore
+org.eclipse.jdt.core.compiler.problem.pessimisticNullAnalysisForFreeTypeVariables=warning
+org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=warning
+org.eclipse.jdt.core.compiler.problem.potentialNullReference=warning
+org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=warning
+org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning
+org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning
+org.eclipse.jdt.core.compiler.problem.redundantNullCheck=warning
+org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=info
+org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=warning
+org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore
+org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=warning
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
+org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled
+org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning
+org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=enabled
+org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled
+org.eclipse.jdt.core.compiler.problem.suppressWarningsNotFullyAnalysed=info
+org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=disabled
+org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=warning
+org.eclipse.jdt.core.compiler.problem.terminalDeprecation=warning
+org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning
+org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=disabled
+org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning
+org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning
+org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore
+org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning
+org.eclipse.jdt.core.compiler.problem.unlikelyCollectionMethodArgumentType=warning
+org.eclipse.jdt.core.compiler.problem.unlikelyCollectionMethodArgumentTypeStrict=disabled
+org.eclipse.jdt.core.compiler.problem.unlikelyEqualsArgumentType=info
+org.eclipse.jdt.core.compiler.problem.unnecessaryElse=info
+org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=warning
+org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore
+org.eclipse.jdt.core.compiler.problem.unstableAutoModuleName=warning
+org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=warning
+org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled
+org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled
+org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled
+org.eclipse.jdt.core.compiler.problem.unusedExceptionParameter=ignore
+org.eclipse.jdt.core.compiler.problem.unusedImport=warning
+org.eclipse.jdt.core.compiler.problem.unusedLabel=warning
+org.eclipse.jdt.core.compiler.problem.unusedLocal=warning
+org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=warning
+org.eclipse.jdt.core.compiler.problem.unusedParameter=warning
+org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled
+org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled
+org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled
+org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning
+org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=warning
+org.eclipse.jdt.core.compiler.problem.unusedWarningToken=ignore
+org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=17
+org.eclipse.jdt.core.incompatibleJDKLevel=ignore
+org.eclipse.jdt.core.incompleteClasspath=error
diff --git a/bin/.settings/org.eclipse.jdt.launching.prefs b/bin/.settings/org.eclipse.jdt.launching.prefs
new file mode 100644
index 0000000..f8a131b
--- /dev/null
+++ b/bin/.settings/org.eclipse.jdt.launching.prefs
@@ -0,0 +1,3 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.launching.PREF_COMPILER_COMPLIANCE_DOES_NOT_MATCH_JRE=warning
+org.eclipse.jdt.launching.PREF_STRICTLY_COMPATIBLE_JRE_NOT_AVAILABLE=warning
diff --git a/bin/.settings/org.eclipse.m2e.core.prefs b/bin/.settings/org.eclipse.m2e.core.prefs
new file mode 100644
index 0000000..f897a7f
--- /dev/null
+++ b/bin/.settings/org.eclipse.m2e.core.prefs
@@ -0,0 +1,4 @@
+activeProfiles=
+eclipse.preferences.version=1
+resolveWorkspaceProjects=true
+version=1
diff --git a/bin/.settings/org.testng.eclipse.maven.prefs b/bin/.settings/org.testng.eclipse.maven.prefs
new file mode 100644
index 0000000..246d09f
--- /dev/null
+++ b/bin/.settings/org.testng.eclipse.maven.prefs
@@ -0,0 +1,7 @@
+eclipse.preferences.version=1
+org.testng.eclipse.maven.additionalClasspath=true
+org.testng.eclipse.maven.argline=true
+org.testng.eclipse.maven.environ=true
+org.testng.eclipse.maven.properties=true
+org.testng.eclipse.maven.sysproperties=true
+userprojectsettings=true
diff --git a/bin/LICENSE b/bin/LICENSE
new file mode 100644
index 0000000..61f8cad
--- /dev/null
+++ b/bin/LICENSE
@@ -0,0 +1,350 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+
+
+APPENDIX A: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+ -------------------------------------------------------------
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed 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.
+ -------------------------------------------------------------
+
+
+
+APPENDIX B: Additional licenses relevant to this product:
+
+ This product includes a number of source files with code that has been
+ adapted from 3rd party sources including sources that may be subject
+ to different copyright notices and license terms. Your use of
+ the source code for these subcomponents is subject to the terms and
+ conditions of the following licenses.
+
+
+
+ =============================================================
+ Apache License version 2.0 (see above)
+ =============================================================
+ DirectMemory
+ Copyright 2015 Odnoklassniki Ltd, Mail.Ru Group
+
+ Code locations:
+ -------------------------------------------------------------
+ This product contains a method to create a dummy read-only ByteBuffer using unsafe:
+ * datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/AccessByteBuffer.java,
+ Method: getDummyReadOnlyDirectByteBuffer(...)
+ and adapted from Java source code located at:
+ * src/one/nio/mem/DirectMemory.java,
+ Method: wrap(...).
+
+
+
+ =============================================================
+ Apache License version 2.0 (see above)
+ =============================================================
+ Zero-Allocation-Hashing
+ Copyright 2015 Higher Frequency Trading http://www.higherfrequencytrading.com
+
+ Code locations:
+ -------------------------------------------------------------
+ This product contains code to implement and test the xxHash function:
+ * datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/XxHash64.java
+ * datasketches-memory-java8-tests/src/test/java/org/apache/datasketches/memory/test/XxHash64Test.java
+ Method: collisionTest(),
+ * datasketches-memory-java8-tests/src/test/java/org/apache/datasketches/memory/test/XxHash64LoopingTest.java
+ Method: testWithSeed() and HASHES_OF_LOOPING_BYTES_WITH_SEED_42 test data,
+ and adapted from Java source code located at:
+ * https://github.com/OpenHFT/Zero-Allocation-Hashing
+
+
+
+ =============================================================
+ Google Protobuf License:
+ =============================================================
+ Copyright 2008 Google Inc.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following disclaimer
+ in the documentation and/or other materials provided with the
+ distribution.
+ * Neither the name of Google Inc. nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ Code generated by the Protocol Buffer compiler is owned by the owner
+ of the input file used when generating it. This code is not
+ standalone and requires a support library to be linked with it. This
+ support library is itself covered by the above license.
+
+ Code locations:
+ -------------------------------------------------------------
+ This product contains code for encoding, decoding and testing UTF8:
+ * datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/Utf8.java
+ * datasketches-memory-java8-tests/src/test/java/org/apache/datasketches/memory/test/Utf8Test.java
+ * datasketches-memory-java8-tests/src/test/java/org/apache/datasketches/memory/test/IsValidUtf8TestUtil.java
+ and adapted from Java source code located at:
+ * https://github.com/protocolbuffers/protobuf/blob/master/java/core/src/main/java/com/google/protobuf/Utf8.java,
+ * https://github.com/protocolbuffers/protobuf/blob/master/java/core/src/test/java/com/google/protobuf/Utf8Test.java,
+ * https://github.com/protocolbuffers/protobuf/blob/master/java/core/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java,
+
+
+
+ =============================================================
+ BSD-2-Clause License
+ =============================================================
+ Copyright (C) 2012-2016, Yann Collet.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ Code locations:
+ -------------------------------------------------------------
+ This product contains code for encoding, decoding and testing UTF8:
+ * datasketches-memory-java8/src/main/java/org/apache/datasketches/memory/internal/XxHash64.java
+ and adapted from C++ code located at:
+ * https://github.com/Cyan4973/xxHash/blob/dev/xxhash.c,
+ * https://github.com/Cyan4973/xxHash/blob/dev/xxhash.h
+
+
+
+ =============================================================
+ Public Domain (optional notice)
+ =============================================================
+ This product contains the text of Lincoln's Gettysburg Address, which is in the public domain,
+ and is used in various file tests. This file is read-only and tested for its exact character
+ sequence and cannot be appended with any additional text.
+ * datasketches-memory-java8-tests/src/test/java/org/apache/datasketches/memory/test/AllocateDirectMapMemoryTest.java
+ * datasketches-memory-java8-tests/src/test/java/org/apache/datasketches/memory/test/AllocateDirectWritableMapMemoryTest.java
+ * datasketches-memory-java8-tests/src/test/java/org/apache/datasketches/memory/test/MemoryTest.java
+ * datasketches-memory-java8-tests/src/test/java/org/apache/datasketches/memory/test/UtilTest.java
diff --git a/bin/NOTICE b/bin/NOTICE
new file mode 100644
index 0000000..d7ec45b
--- /dev/null
+++ b/bin/NOTICE
@@ -0,0 +1,12 @@
+Apache DataSketches Memory
+
+Copyright 2021 - The Apache Software Foundation
+
+Copyright 2015-2018 Yahoo
+Copyright 2019 Verizon Media
+
+This product includes software developed at
+The Apache Software Foundation (http://www.apache.org/).
+
+Prior to moving to ASF, the software for this project was developed at
+Yahoo (now Verizon Media) (https://developer.yahoo.com).
diff --git a/bin/README.md b/bin/README.md
new file mode 100644
index 0000000..076825a
--- /dev/null
+++ b/bin/README.md
@@ -0,0 +1 @@
+# datasketches-memory17
diff --git a/bin/pom.xml b/bin/pom.xml
new file mode 100644
index 0000000..eba2337
--- /dev/null
+++ b/bin/pom.xml
@@ -0,0 +1,542 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache</groupId>
+ <artifactId>apache</artifactId>
+ <version>23</version>
+ </parent>
+
+ <groupId>org.apache.datasketches</groupId>
+ <artifactId>datasketches-memory17</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ <!-- Required for multi-module project -->
+ <packaging>pom</packaging>
+
+ <name>${project.artifactId}</name>
+ <description>High-performance native memory access with Panama.</description>
+ <url>https://datasketches.apache.org/</url>
+ <inceptionYear>2015</inceptionYear>
+
+ <mailingLists>
+ <mailingList>
+ <name>DataSketches Developers</name>
+ <subscribe>dev-subscribe@datasketches.apache.org</subscribe>
+ <unsubscribe>dev-unsubscribe@datasketches.apache.org</unsubscribe>
+ <post>dev@datasketches.apache.org</post>
+ <archive>https://mail-archives.apache.org/mod_mbox/datasketches-dev</archive>
+ </mailingList>
+ <mailingList>
+ <name>sketches-user</name>
+ <archive>https://groups.google.com/forum/#!forum/sketches-user</archive>
+ <subscribe>mailto:sketches-user%2Bsubscribe@googlegroups.com</subscribe>
+ <unsubscribe>mailto:sketches-user%2Bunsubscribe@googlegroups.com</unsubscribe>
+ <post>mailto:sketches-user@googlegroups.com</post>
+ </mailingList>
+ </mailingLists>
+
+ <scm>
+ <connection>scm:git:ssh://git@github.com/apache/${project.artifactId}.git</connection>
+ <developerConnection>scm:git:ssh://git@github.com/apache/${project.artifactId}.git</developerConnection>
+ <url>https://github.com/apache/${project.artifactId}</url>
+ <tag>HEAD</tag>
+ </scm>
+
+ <issueManagement>
+ <!-- <system>jira</system>
+ <url>https://issues.apache.org/jira/browse/DATASKETCHES</url> -->
+ <system>GitHub</system>
+ <url>https://github.com/apache/${project.artifactId}/issues</url>
+ </issueManagement>
+
+ <developers>
+ <developer>
+ <name>The Apache DataSketches Team</name>
+ <email>dev@datasketches.apache.org</email>
+ <url>https://datasketches.apache.org</url>
+ <organization>Apache Software Foundation</organization>
+ <organizationUrl>http://www.apache.org</organizationUrl>
+ </developer>
+ </developers>
+
+ <!--
+ NOTE:
+ The datasketches-memory submodule uses the Maven project-aggregation feature and does not inherit
+ from this root module as a parent; so that there is no runtime dependency on the parent project (root module).
+ As a result, some properties from this POM (including the version) are duplicated in the datasketches-memory
+ module for inclusion in the assembled artifacts. For more information, see:
+ https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Aggregation -->
+
+
+ <properties>
+ <!-- UNIQUE FOR THIS JAVA COMPONENT -->
+ <protobuf-java.version>4.0.0-rc-2</protobuf-java.version>
+ <!-- Used for UTF8 testing -->
+ <zero-allocation-hashing.version>0.15</zero-allocation-hashing.version>
+ <!-- END:UNIQUE FOR THIS JAVA COMPONENT -->
+
+ <!-- Test -->
+ <!-- Version 7.4.0 fails to locate jquery.min.js for reporting on debian systems -->
+ <testng.version>7.5</testng.version>
+
+ <!-- System-wide properties -->
+ <maven.version>3.5.0</maven.version>
+ <java.version>17</java.version>
+ <jdk-toolchain.version>8</jdk-toolchain.version>
+ <maven.compiler.source>${java.version}</maven.compiler.source>
+ <maven.compiler.target>${java.version}</maven.compiler.target>
+ <argLine>-Xmx4g -Duser.language=en -Duser.country=US -Dfile.encoding=UTF-8</argLine>
+ <charset.encoding>UTF-8</charset.encoding>
+ <project.build.sourceEncoding>${charset.encoding}</project.build.sourceEncoding>
+ <project.build.resourceEncoding>${charset.encoding}</project.build.resourceEncoding>
+ <project.reporting.outputEncoding>${charset.encoding}</project.reporting.outputEncoding>
+
+ <!-- org.codehaus plugins -->
+ <!-- used for strict profile testing-->
+ <plexus-compiler-javac-errorprone.version>2.8.5</plexus-compiler-javac-errorprone.version>
+ <versions-maven-plugin.version>2.8.1</versions-maven-plugin.version>
+
+ <!-- Maven Plugins -->
+ <maven-assembly-plugin.version>3.3.0</maven-assembly-plugin.version> <!-- overrides parent -->
+ <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version> <!-- overrides parent -->
+ <maven-deploy-plugin.version>3.0.0-M1</maven-deploy-plugin.version> <!-- overrides parent -->
+ <maven-enforcer-plugin.version>3.0.0-M3</maven-enforcer-plugin.version> <!-- overrides parent -->
+ <maven-jar-plugin.version>3.2.0</maven-jar-plugin.version> <!-- overrides parent -->
+ <maven-javadoc-plugin.version>3.3.0</maven-javadoc-plugin.version> <!-- overrides parent -->
+ <maven-release-plugin.version>3.0.0-M1</maven-release-plugin.version> <!-- overrides parent -->
+ <maven-remote-resources-plugin.version>1.7.0</maven-remote-resources-plugin.version> <!-- overrides parent -->
+ <maven-source-plugin.version>3.2.1</maven-source-plugin.version> <!-- overrides parent -->
+ <maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version> <!-- overrides parent -->
+ <maven-toolchains-plugin.version>3.0.0</maven-toolchains-plugin.version>
+ <maven-install-plugin.version>3.0.0-M1</maven-install-plugin.version>
+ <!-- Apache Plugins -->
+ <apache-rat-plugin.version>0.13</apache-rat-plugin.version> <!-- overrides parent -->
+ <!-- org.jacoco Maven Plugins -->
+ <jacoco-maven-plugin.version>0.8.8</jacoco-maven-plugin.version>
+ <!-- org.eluder Maven Plugins -->
+ <coveralls-repo-token></coveralls-repo-token>
+ <coveralls-maven-plugin.version>4.3.0</coveralls-maven-plugin.version>
+ <!-- other -->
+ <lifecycle-mapping.version>1.0.0</lifecycle-mapping.version>
+ <maven.deploy.skip>true</maven.deploy.skip>
+ <maven.install.skip>true</maven.install.skip>
+ <maven.javadoc.skip>true</maven.javadoc.skip>
+ </properties>
+
+ <repositories>
+ <repository>
+ <id>apache.snapshots</id>
+ <name>Apache Snapshot Repository</name>
+ <url>https://repository.apache.org/content/groups/snapshots/org/apache/datasketches/</url>
+ <releases>
+ <enabled>false</enabled>
+ </releases>
+ <snapshots>
+ <enabled>true</enabled>
+ </snapshots>
+ </repository>
+ <repository>
+ <id>apache</id>
+ <name>Apache Releases Repository</name>
+ <url>https://repository.apache.org/content/repositories/releases/org/apache/datasketches/</url>
+ <releases>
+ <enabled>true</enabled>
+ </releases>
+ <snapshots>
+ <enabled>false</enabled>
+ </snapshots>
+ </repository>
+ </repositories>
+
+ <dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.testng</groupId>
+ <artifactId>testng</artifactId>
+ <version>${testng.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <!-- Used for UTF8 testing -->
+ <groupId>com.google.protobuf</groupId>
+ <artifactId>protobuf-java</artifactId>
+ <version>${protobuf-java.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <!-- Used for xxHash testing -->
+ <groupId>net.openhft</groupId>
+ <artifactId>zero-allocation-hashing</artifactId>
+ <version>${zero-allocation-hashing.version}</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+
+ <build>
+ <pluginManagement>
+ <plugins>
+
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>versions-maven-plugin</artifactId>
+ <version>${versions-maven-plugin.version}</version>
+ </plugin>
+
+ <plugin>
+ <!-- We want to deploy the artifacts to a staging location for perusal -->
+ <!-- Apache Parent pom: apache-release profile -->
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-deploy-plugin</artifactId>
+ <version>${maven-deploy-plugin.version}</version>
+ </plugin>
+
+ <plugin>
+ <!-- Apache Parent pom, pluginManagement-->
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-release-plugin</artifactId>
+ <version>${maven-release-plugin.version}</version>
+ </plugin>
+
+ <plugin>
+ <!-- Extends Apache Parent pom, pluginManagement-->
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <version>${maven-jar-plugin.version}</version>
+ <executions>
+ <execution>
+ <id>default-jar</id>
+ <phase>package</phase>
+ <goals>
+ <goal>jar</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>default-test-jar</id>
+ <phase>package</phase>
+ <goals>
+ <goal>test-jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ <plugin>
+ <!-- Extends Apache Parent pom, apache-release profile -->
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-javadoc-plugin</artifactId>
+ <version>${maven-javadoc-plugin.version}</version>
+ <configuration>
+ <docfilessubdirs>true</docfilessubdirs>
+ <doclint>none</doclint>
+ <jdkToolchain>
+ <version>${jdk-toolchain.version}</version>
+ </jdkToolchain>
+ <excludePackageNames>org.apache.datasketches.memory.internal</excludePackageNames>
+ </configuration>
+ <executions>
+ <execution>
+ <id>attach-javadocs</id>
+ <phase>package</phase>
+ <goals>
+ <goal>jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-enforcer-plugin</artifactId>
+ <version>${maven-enforcer-plugin.version}</version>
+ <executions>
+ <execution>
+ <id>enforce-banned-dependencies</id>
+ <goals>
+ <goal>enforce</goal>
+ </goals>
+ <configuration>
+ <rules>
+ <requireJavaVersion>
+ <version>[1.8,1.9),[8],[11,14),[17,18)</version>
+ </requireJavaVersion>
+ <requireMavenVersion>
+ <version>[${maven.version},)</version>
+ </requireMavenVersion>
+ <bannedDependencies>
+ <excludes>
+ <!--LGPL licensed library-->
+ <exclude>com.google.code.findbugs:annotations</exclude>
+ </excludes>
+ </bannedDependencies>
+ </rules>
+ <fail>true</fail>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+
+ <plugin>
+ <!-- Apache Parent pom, pluginManagement-->
+ <groupId>org.apache.rat</groupId>
+ <artifactId>apache-rat-plugin</artifactId>
+ <version>${apache-rat-plugin.version}</version>
+ <executions>
+ <execution>
+ <phase>verify</phase>
+ <goals>
+ <goal>check</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <outputDirectory>${project.basedir}/rat</outputDirectory>
+ <consoleOutput>true</consoleOutput>
+ <useDefaultExcludes>true</useDefaultExcludes>
+ <excludes>
+ <exclude>**/*.yaml</exclude>
+ <exclude>**/*.yml</exclude>
+ <exclude>**/.*/**/*</exclude>
+ <exclude>**/.clover/**/*</exclude>
+ <exclude>**/test/resources/**/*.txt</exclude>
+ <exclude>**/test-output/**/*</exclude>
+ <exclude>**/img/**/*.png</exclude>
+ <exclude>**/git.properties</exclude>
+ <exclude>**/scripts/LoremIpsum.txt</exclude>
+ <exclude>LICENSE</exclude>
+ <exclude>NOTICE</exclude>
+ </excludes>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <!-- Extends Apache Parent pom, apache-release profile -->
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-source-plugin</artifactId>
+ <version>${maven-source-plugin.version}</version>
+ <executions>
+ <execution>
+ <id>attach-sources</id>
+ <phase>package</phase>
+ <goals>
+ <goal>jar-no-fork</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>attach-test-sources</id>
+ <phase>package</phase>
+ <goals>
+ <goal>test-jar-no-fork</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ <plugin>
+ <!-- Apache Parent pom, pluginManagement-->
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>${maven-surefire-plugin.version}</version>
+ <configuration>
+ <trimStackTrace>false</trimStackTrace>
+ <useManifestOnlyJar>false</useManifestOnlyJar>
+ <redirectTestOutputToFile>true</redirectTestOutputToFile>
+ <jdkToolchain>
+ <version>${jdk-toolchain.version}</version>
+ </jdkToolchain>
+ <reportsDirectory>${project.build.directory}/test-output/${maven.build.timestamp}</reportsDirectory>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-toolchains-plugin</artifactId>
+ <version>${maven-toolchains-plugin.version}</version>
+ <configuration>
+ <toolchains>
+ <jdk>
+ <version>[1.8,1.9),[8],[11,14),[17,18)</version>
+ </jdk>
+ </toolchains>
+ </configuration>
+ <executions>
+ <execution>
+ <goals><goal>toolchain</goal></goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ <plugin>
+ <!-- Generates code coverage report from website. -->
+ <groupId>org.jacoco</groupId>
+ <artifactId>jacoco-maven-plugin</artifactId>
+ <version>${jacoco-maven-plugin.version}</version>
+ <executions>
+ <execution>
+ <id>default-prepare-agent</id>
+ <goals>
+ <goal>prepare-agent</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ <plugin>
+ <!-- Submit code coverage report to Coveralls.io. -->
+ <groupId>org.eluder.coveralls</groupId>
+ <artifactId>coveralls-maven-plugin</artifactId>
+ <version>${coveralls-maven-plugin.version}</version>
+ <configuration>
+ <!--suppress UnresolvedMavenProperty -->
+ <repoToken>${coveralls-repo-token}</repoToken>
+ <!--suppress UnresolvedMavenProperty -->
+ <jacocoReports>${maven.multiModuleProjectDirectory}/datasketches-memory/target/site/jacoco-aggregate/jacoco.xml</jacocoReports>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>${maven-compiler-plugin.version}</version>
+ <configuration>
+ <jdkToolchain>
+ <version>${jdk-toolchain.version}</version>
+ </jdkToolchain>
+ </configuration>
+ </plugin>
+
+ </plugins>
+ </pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-deploy-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-release-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-javadoc-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-enforcer-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.rat</groupId>
+ <artifactId>apache-rat-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-source-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.jacoco</groupId>
+ <artifactId>jacoco-maven-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.eluder.coveralls</groupId>
+ <artifactId>coveralls-maven-plugin</artifactId>
+ </plugin>
+ </plugins>
+ </build>
+
+ <profiles>
+ <!-- Disable source release assembly for 'apache-release' profile.
+ This is performed from a script outside Maven
+ -->
+ <profile>
+ <id>apache-release</id>
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-assembly-plugin</artifactId>
+ <version>${maven-assembly-plugin.version}</version>
+ <executions>
+ <execution>
+ <id>source-release-assembly</id>
+ <phase>none</phase>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
+
+ <profile>
+ <id>only-eclipse</id>
+ <activation>
+ <property>
+ <name>m2e.version</name>
+ </property>
+ </activation>
+ <build>
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-remote-resources-plugin</artifactId>
+ <version>${maven-remote-resources-plugin.version}</version>
+ <executions>
+ <execution>
+ <id>process-resource-bundles</id>
+ <phase>none</phase>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+ </build>
+ </profile>
+
+
+ </profiles>
+
+ <!-- This Maven project is divided into an aggregator project (the root), that manages a group of submodules that
+ inherit configuration from this root module.
+ A multi-module project is used to target different version-specific features of the Java language APIs.
+ Furthermore, it is a structure that is supported in most IDEs allowing minimal disruption to developers that
+ contribute to this project
+ <modules>
+ <module>datasketches-memory-java8</module>
+ <module>datasketches-memory-java8-tests</module>
+ <module>datasketches-memory-java11</module>
+ <module>datasketches-memory</module>
+ <module>datasketches-memory-resources</module>
+ </modules>
+ -->
+
+</project>
diff --git a/bin/src/main/java/org/apache/datasketches/memory/BaseState.class b/bin/src/main/java/org/apache/datasketches/memory/BaseState.class
new file mode 100644
index 0000000..db1716f
Binary files /dev/null and b/bin/src/main/java/org/apache/datasketches/memory/BaseState.class differ
diff --git a/bin/src/main/java/org/apache/datasketches/memory/DefaultMemoryRequestServer.class b/bin/src/main/java/org/apache/datasketches/memory/DefaultMemoryRequestServer.class
new file mode 100644
index 0000000..bf6c8e8
Binary files /dev/null and b/bin/src/main/java/org/apache/datasketches/memory/DefaultMemoryRequestServer.class differ
diff --git a/bin/src/main/java/org/apache/datasketches/memory/Memory.class b/bin/src/main/java/org/apache/datasketches/memory/Memory.class
new file mode 100644
index 0000000..61fc1ef
Binary files /dev/null and b/bin/src/main/java/org/apache/datasketches/memory/Memory.class differ
diff --git a/bin/src/main/java/org/apache/datasketches/memory/MemoryCloseException.class b/bin/src/main/java/org/apache/datasketches/memory/MemoryCloseException.class
new file mode 100644
index 0000000..9988496
Binary files /dev/null and b/bin/src/main/java/org/apache/datasketches/memory/MemoryCloseException.class differ
diff --git a/bin/src/main/java/org/apache/datasketches/memory/MemoryException.class b/bin/src/main/java/org/apache/datasketches/memory/MemoryException.class
new file mode 100644
index 0000000..65db8cf
Binary files /dev/null and b/bin/src/main/java/org/apache/datasketches/memory/MemoryException.class differ
diff --git a/bin/src/main/java/org/apache/datasketches/memory/MemoryRequestServer.class b/bin/src/main/java/org/apache/datasketches/memory/MemoryRequestServer.class
new file mode 100644
index 0000000..30d4f85
Binary files /dev/null and b/bin/src/main/java/org/apache/datasketches/memory/MemoryRequestServer.class differ
diff --git a/bin/src/main/java/org/apache/datasketches/memory/MurmurHash3v2.class b/bin/src/main/java/org/apache/datasketches/memory/MurmurHash3v2.class
new file mode 100644
index 0000000..2c0babb
Binary files /dev/null and b/bin/src/main/java/org/apache/datasketches/memory/MurmurHash3v2.class differ
diff --git a/bin/src/main/java/org/apache/datasketches/memory/ReadOnlyException.class b/bin/src/main/java/org/apache/datasketches/memory/ReadOnlyException.class
new file mode 100644
index 0000000..9acce5a
Binary files /dev/null and b/bin/src/main/java/org/apache/datasketches/memory/ReadOnlyException.class differ
diff --git a/bin/src/main/java/org/apache/datasketches/memory/Utf8CodingException.class b/bin/src/main/java/org/apache/datasketches/memory/Utf8CodingException.class
new file mode 100644
index 0000000..d68ec9c
Binary files /dev/null and b/bin/src/main/java/org/apache/datasketches/memory/Utf8CodingException.class differ
diff --git a/bin/src/main/java/org/apache/datasketches/memory/WritableMemory.class b/bin/src/main/java/org/apache/datasketches/memory/WritableMemory.class
new file mode 100644
index 0000000..340a03f
Binary files /dev/null and b/bin/src/main/java/org/apache/datasketches/memory/WritableMemory.class differ
diff --git a/bin/src/main/java/org/apache/datasketches/memory/XxHash.class b/bin/src/main/java/org/apache/datasketches/memory/XxHash.class
new file mode 100644
index 0000000..ef92cad
Binary files /dev/null and b/bin/src/main/java/org/apache/datasketches/memory/XxHash.class differ
diff --git a/bin/src/main/java/org/apache/datasketches/memory/package-info.class b/bin/src/main/java/org/apache/datasketches/memory/package-info.class
new file mode 100644
index 0000000..5ccce46
Binary files /dev/null and b/bin/src/main/java/org/apache/datasketches/memory/package-info.class differ
diff --git a/bin/tools/CloverConfig.txt b/bin/tools/CloverConfig.txt
new file mode 100644
index 0000000..77ea2ce
--- /dev/null
+++ b/bin/tools/CloverConfig.txt
@@ -0,0 +1,49 @@
+# 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.
+
+Clover Config for Eclipse:
+
+At Project Level Properties:
+ Instrumentation:
+ Initstring: Default value
+ Output Folder: ...project output dir(s)
+ Flush Policy: At JVM shutdown ...
+ Misc: Fully qualify ... , Instrument and compile at statement level
+ Contexts:
+ Check: assert statements
+ Add Custom Coverage Context Filter:
+ private-constructor: also see link below
+ Method
+ (.* )?private +[a-zA-Z0-9_$]+ *\( *\).*
+ Source Files
+ Only look ...
+ [check] src/main/java[includes=**/*.java][excludes=]
+ [check] src/test/java[includes=**/*.java][excludes=]
+ Test Classes
+ Assume all source in the specified folders are tests or test utility classes
+ [check] src/test/java
+
+At Clover "down-triangle" menu:
+ Columns:
+ Element
+ % TOTAL Coverage
+ Uncovered Elements: Custom: %UncoveredElements * TotalElements / 100
+ Total Elements
+
+
+
+http://alexruizlog.blogspot.com/2009/04/how-to-make-clover-ignore-private_21.html
\ No newline at end of file
diff --git a/bin/tools/FindBugsExcludeFilter.xml b/bin/tools/FindBugsExcludeFilter.xml
new file mode 100644
index 0000000..ae68389
--- /dev/null
+++ b/bin/tools/FindBugsExcludeFilter.xml
@@ -0,0 +1,96 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<FindBugsFilter> <!-- memory -->
+
+ <!-- Too many false positives to be useful. I could not make it happy :( -->
+ <Match>
+ <Bug pattern="SF_SWITCH_NO_DEFAULT" />
+ </Match>
+
+ <Match> <!-- Exclude for test classes; too many False Positives. -->
+ <Bug pattern="NP_NULL_PARAM_DEREF_NONVIRTUAL" />
+ <Class name="~.*\.*Test" />
+ </Match>
+
+ <Match> <!-- Exclude for test classes; too many False Positives. -->
+ <Bug pattern="DLS_DEAD_LOCAL_STORE" />
+ <Class name="~.*\.*Test" />
+ </Match>
+
+</FindBugsFilter>
+
+
+
+<!-- Examples: -->
+
+<!-- Exclude java.* classes -->
+ <!--
+ <Match>
+ <Package name="java\.*" />
+ </Match>
+-->
+
+ <!-- Exclude test classes -->
+<!--
+ <Match>
+ <Class name="~.*\.*Test" />
+ </Match>
+-->
+
+<!--
+ <Match>
+ <Class name="com.foobar.ClassNotToBeAnalyzed" />
+ </Match>
+-->
+<!--
+ <Match>
+ <Class name="com.foobar.ClassWithSomeBugsMatched" />
+ <Bug code="DE,UrF,SIC" />
+ </Match>
+-->
+ <!-- Match all XYZ violations. -->
+<!--
+ <Match>
+ <Bug code="XYZ" />
+ </Match>
+-->
+ <!-- Match all doublecheck violations in these methods of "AnotherClass". -->
+<!--
+ <Match>
+ <Class name="com.foobar.AnotherClass" />
+ <Or>
+ <Method name="nonOverloadedMethod" />
+ <Method name="frob" params="int,java.lang.String" returns="void" />
+ <Method name="blat" params="" returns="boolean" />
+ </Or>
+ <Bug code="DC" />
+ </Match>
+-->
+ <!-- A method with a dead local store false positive (medium priority). -->
+<!--
+ <Match>
+ <Class name="com.foobar.MyClass" />
+ <Method name="someMethod" />
+ <Bug pattern="DLS_DEAD_LOCAL_STORE" />
+ <Priority value="2" />
+ </Match>
+-->
diff --git a/bin/tools/MemoryCheckstyle.xml b/bin/tools/MemoryCheckstyle.xml
new file mode 100644
index 0000000..93c3bf3
--- /dev/null
+++ b/bin/tools/MemoryCheckstyle.xml
@@ -0,0 +1,398 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE module PUBLIC
+ "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
+ "https://checkstyle.org/dtds/configuration_1_3.dtd">
+
+<!--
+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.
+-->
+
+<!--
+ MemoryCheckstyle.xml
+
+ Checkstyle is very configurable. Be sure to read the documentation at
+ http://checkstyle.sourceforge.net (or in your downloaded distribution).
+
+ To completely disable a check, comment it out, delete it, or set
+ property name="severity" value="ignore".
+-->
+
+<module name="Checker">
+ <property name="charset" value="UTF-8"/>
+ <property name="severity" value="warning"/>
+ <property name="fileExtensions" value="java"/>
+
+ <module name="BeforeExecutionExclusionFileFilter">
+ <property name="fileNamePattern" value="src[\\/]test[\\/]java[\\/].+$|module\-info\.java.+$"/>
+ </module>
+
+ <module name="FileTabCharacter">
+ <property name="eachLine" value="true"/>
+ </module>
+
+ <module name="JavadocPackage"/>
+
+ <module name="NewlineAtEndOfFile">
+ <property name="lineSeparator" value="lf"/>
+ </module>
+
+ <!-- Size Violations -->
+ <module name="LineLength">
+ <property name="severity" value="warning"/>
+ <property name="max" value="120"/>
+ <property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
+ <!-- <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/> -->
+ </module>
+
+ <module name="SuppressWithPlainTextCommentFilter">
+ <property name="offCommentFormat" value="//CHECKSTYLE.OFF\: ([\w\|]+)"/>
+ <property name="onCommentFormat" value="//CHECKSTYLE.ON\: ([\w\|]+)"/>
+ <property name="checkFormat" value="$1"/>
+ </module>
+
+ <!-- ******************************************************** -->
+
+ <module name="TreeWalker">
+
+ <!-- Annotations -->
+ <module name="AnnotationLocation">
+ <property name="tokens" value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF"/>
+ </module>
+
+ <module name="AnnotationLocation">
+ <property name="tokens" value="VARIABLE_DEF"/>
+ <property name="allowSamelineMultipleAnnotations" value="true"/>
+ </module>
+
+ <!-- Block Checks -->
+ <module name="EmptyBlock">
+ <property name="severity" value="warning"/>
+ <property name="option" value="TEXT"/>
+ <property name="tokens" value="LITERAL_TRY, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE, LITERAL_SWITCH"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="EmptyCatchBlock">
+ <property name="severity" value="ignore"/>
+ <property name="exceptionVariableName" value="expected"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="LeftCurly">
+ <!-- doesn't allow for if (n == 0) { return 0.0; }, which is readable and not corruptable -->
+ <property name="severity" value="ignore"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="NeedBraces">
+ <property name="allowSingleLineStatement" value="false"/> <!-- default = false -->
+ <property name="allowEmptyLoopBody" value="false"/> <!-- default = false -->
+ </module>
+
+ <module name="RightCurly">
+ <!-- requires right curly start a new line; alone: on a line alone -->
+ <property name="severity" value="ignore"/>
+ <property name="option" value="alone"/>
+ <property name="tokens" value="CLASS_DEF, METHOD_DEF, CTOR_DEF, LITERAL_FOR, LITERAL_WHILE, LITERAL_DO, STATIC_INIT, INSTANCE_INIT"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <!-- Coding -->
+ <module name="FallThrough">
+ <property name="severity" value="warning"/>
+ <property name="reliefPattern" value="fallthru|falls? ?through|FALL[- ]?THROUGH"/>
+ <!-- <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/> -->
+ </module>
+
+ <module name="FinalLocalVariable">
+ <property name="severity" value="warning"/>
+ <property name="tokens" value="VARIABLE_DEF,PARAMETER_DEF"/>
+ </module>
+
+ <module name="IllegalTokenText">
+ <property name="severity" value="warning"/>
+ <property name="tokens" value="STRING_LITERAL, CHAR_LITERAL"/>
+ <property name="format" value="\\u00(08|09|0(a|A)|0(c|C)|0(d|D)|22|27|5(C|c))|\\(0(10|11|12|14|15|42|47)|134)"/>
+ <property name="message" value="Avoid using corresponding octal or Unicode escape."/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="MissingSwitchDefault">
+ <!-- Too many false positives -->
+ <property name="severity" value="ignore"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="MultipleVariableDeclarations">
+ <property name="severity" value="ignore"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="NoFinalizer">
+ <property name="severity" value="ignore"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="OneStatementPerLine">
+ <property name="severity" value="ignore"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="OverloadMethodsDeclarationOrder">
+ <property name="severity" value="ignore"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="VariableDeclarationUsageDistance">
+ <property name="severity" value="ignore"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <!-- Class Design -->
+ <module name="OneTopLevelClass"/>
+
+ <module name="FinalClass"/>
+
+ <!-- Imports -->
+ <module name="AvoidStarImport"/>
+
+ <module name="CustomImportOrder">
+ <property name="specialImportsRegExp" value="org"/>
+ <property name="sortImportsInGroupAlphabetically" value="true"/>
+ <property name="separateLineBetweenGroups" value="true"/>
+ <!-- Google: "STATIC###SPECIAL_IMPORTS###THIRD_PARTY_PACKAGE###STANDARD_JAVA_PACKAGE" -->
+ <property name="customImportOrderRules" value="STATIC###STANDARD_JAVA_PACKAGE###SPECIAL_IMPORTS###THIRD_PARTY_PACKAGE"/>
+ </module>
+
+ <module name="RedundantImport"/>
+
+ <module name="UnusedImports"/>
+
+ <!-- Filters -->
+ <!-- Enable suppression using comments: //CHECKSTYLE.OFF: "RULE" and //CHECKSTYLE.ON: "RULE"
+ You must specify the specific rule, as in: //CHECKSTYLE.OFF: LineLength -->
+ <module name="SuppressionCommentFilter">
+ <property name="offCommentFormat" value="//CHECKSTYLE.OFF\: ([\w\|]+)"/>
+ <property name="onCommentFormat" value="//CHECKSTYLE.ON\: ([\w\|]+)"/>
+ <property name="checkFormat" value="$1"/>
+ </module>
+
+ <!-- Javadoc Comments -->
+ <!-- JavadocPackage under Checker -->
+ <module name="AtclauseOrder">
+ <property name="tagOrder" value="@param, @return, @throws, @deprecated"/>
+ <property name="target" value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF, VARIABLE_DEF"/>
+ </module>
+
+ <module name="JavadocMethod">
+ <property name="scope" value="public"/>
+ <property name="allowMissingParamTags" value="false"/> <!-- default -->
+ <property name="allowMissingReturnTag" value="false"/> <!-- default -->
+ <property name="allowedAnnotations" value="Override, Test"/>
+ </module>
+
+ <module name="JavadocParagraph"/>
+
+ <module name="JavadocTagContinuationIndentation">
+ <property name="severity" value="ignore"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="NonEmptyAtclauseDescription"/>
+
+ <module name="SingleLineJavadoc">
+ <property name="ignoreInlineTags" value="false"/>
+ </module>
+
+ <module name="SummaryJavadocCheck">
+ <property name="severity" value="ignore"/>
+ <property name="forbiddenSummaryFragments" value="^@return the *|^This method returns |^A [{]@code [a-zA-Z0-9]+[}]( is a )"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <!-- Miscellaneous -->
+ <module name="ArrayTypeStyle">
+ <property name="severity" value="ignore"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="AvoidEscapedUnicodeCharacters">
+ <property name="severity" value="warning"/>
+ <property name="allowEscapesForControlCharacters" value="true"/>
+ <property name="allowByTailComment" value="true"/>
+ <property name="allowNonPrintableEscapes" value="true"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="CommentsIndentation">
+ <property name="severity" value="warning"/>
+ <!-- <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/> -->
+ </module>
+
+ <module name="Indentation">
+ <property name="severity" value="ignore"/>
+ <property name="basicOffset" value="2"/>
+ <property name="braceAdjustment" value="0"/> <!-- default -->
+ <property name="caseIndent" value="2"/>
+ <property name="throwsIndent" value="4"/>
+ <property name="lineWrappingIndentation" value="4"/>
+ <property name="arrayInitIndent" value="2"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="OuterTypeFilename">
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="TodoComment">
+ <property name="severity" value="ignore"/>
+ <property name="format" value="(//TODO)|(//FIXME)"/>
+ </module>
+
+ <module name="UpperEll">
+ <property name="severity" value="warning"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <!-- Modifiers -->
+ <module name="ModifierOrder">
+ <property name="severity" value="ignore"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <!-- Naming Conventions -->
+ <module name="AbbreviationAsWordInName">
+ <property name="severity" value="ignore"/>
+ <property name="ignoreFinal" value="false"/>
+ <property name="allowedAbbreviationLength" value="1"/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="ClassTypeParameterName">
+ <property name="format" value="(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]*[T]$)"/>
+ <message key="name.invalidPattern"
+ value="Class type name ''{0}'' must match pattern ''{1}''."/>
+ </module>
+
+ <module name="LocalVariableName">
+ <property name="severity" value="ignore"/>
+ <property name="tokens" value="VARIABLE_DEF"/>
+ <property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
+ <property name="allowOneCharVarInForLoop" value="true"/>
+ <message key="name.invalidPattern"
+ value="Local variable name ''{0}'' must match pattern ''{1}''."/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="MemberName">
+ <property name="severity" value="ignore"/>
+ <property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
+ <message key="name.invalidPattern"
+ value="Member name ''{0}'' must match pattern ''{1}''."/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="MethodName">
+ <property name="severity" value="ignore"/>
+ <property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9_]*$"/>
+ <message key="name.invalidPattern"
+ value="Method name ''{0}'' must match pattern ''{1}''."/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="MethodTypeParameterName">
+ <property name="format" value="(^[A-Z][0-9]?)$|([A-Z][a-zA-Z0-9]*[T]$)"/>
+ <message key="name.invalidPattern"
+ value="Method type name ''{0}'' must match pattern ''{1}''."/>
+ </module>
+
+ <module name="PackageName">
+ <property name="format" value="^[a-z]+(\.[a-z][a-z0-9]*)*$"/>
+ <message key="name.invalidPattern"
+ value="Package name ''{0}'' must match pattern ''{1}''."/>
+ </module>
+
+ <module name="ParameterName">
+ <property name="severity" value="ignore"/>
+ <property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
+ <message key="name.invalidPattern"
+ value="Parameter name ''{0}'' must match pattern ''{1}''."/>
+ <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
+ </module>
+
+ <module name="TypeName">
+ <message key="name.invalidPattern"
+ value="Type name ''{0}'' must match pattern ''{1}''."/>
+ </module>
+
+ <!-- Regexp -->
+ <module name="Regexp">
+ <property name="severity" value="ignore"/>
+ <property name="format" value="[ \t]+$"/>
+ <property name="illegalPattern" value="true"/>
+ <property name="message" value="Trailing whitespace"/>
+ <property name="ignoreComments" value="true"/>
+ </module>
+
+ <!-- Whitespace -->
+ <module name="EmptyLineSeparator">
+ <property name="allowNoEmptyLineBetweenFields" value="true"/>
+ </module>
+
+ <module name="GenericWhitespace">
+ <message key="ws.followed"
+ value="GenericWhitespace ''{0}'' is followed by whitespace."/>
+ <message key="ws.preceded"
+ value="GenericWhitespace ''{0}'' is preceded with whitespace."/>
+ <message key="ws.illegalFollow"
+ value="GenericWhitespace ''{0}'' should followed by whitespace."/>
+ <message key="ws.notPreceded"
+ value="GenericWhitespace ''{0}'' is not preceded with whitespace."/>
+ </module>
+
+ <module name="NoLineWrap"/> <!-- Only for import and package statements -->
+
+ <module name="MethodParamPad"/>
+
+ <module name="OperatorWrap">
+ <property name="option" value="NL"/>
+ <property name="tokens" value="BAND, BOR, BSR, BXOR, DIV, EQUAL, GE, GT, LAND, LE, LITERAL_INSTANCEOF, LOR, LT, MINUS, MOD, NOT_EQUAL, PLUS, QUESTION, SL, SR, STAR "/>
+ </module>
+
+ <module name="SeparatorWrap">
+ <property name="tokens" value="DOT"/>
+ <property name="option" value="nl"/>
+ </module>
+
+ <module name="SeparatorWrap">
+ <property name="tokens" value="COMMA"/>
+ <property name="option" value="EOL"/>
+ </module>
+
+ <module name="WhitespaceAround">
+ <property name="allowEmptyConstructors" value="true"/>
+ <property name="allowEmptyMethods" value="true"/>
+ <property name="allowEmptyTypes" value="true"/>
+ <property name="allowEmptyLoops" value="true"/>
+ <message key="ws.notPreceded" value="WhitespaceAround: ''{0}'' is not preceded with whitespace."/>
+ <message key="ws.notFollowed" value="WhitespaceAround: ''{0}'' is not followed by whitespace. Empty blocks may only be represented as '{}' when not part of a multi-block statement (4.1.3)"/>
+ </module>
+
+ </module> <!-- End of TreeWalker -->
+
+</module>
diff --git a/bin/tools/scripts/CheckMemoryJar.class b/bin/tools/scripts/CheckMemoryJar.class
new file mode 100644
index 0000000..1b34fc3
Binary files /dev/null and b/bin/tools/scripts/CheckMemoryJar.class differ
diff --git a/bin/tools/scripts/LoremIpsum.txt b/bin/tools/scripts/LoremIpsum.txt
new file mode 100644
index 0000000..5c0dbb2
--- /dev/null
+++ b/bin/tools/scripts/LoremIpsum.txt
@@ -0,0 +1,3 @@
+Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas id fermentum sem. Donec malesuada tristique erat vitae molestie. In urna eros, scelerisque ut finibus a, commodo et felis. Sed lacinia lacus ex, sit amet imperdiet tortor rutrum dictum. Nunc turpis dolor, placerat non condimentum quis, suscipit ut libero. Morbi suscipit porta nibh, sit amet bibendum erat posuere maximus. Pellentesque egestas magna sed purus scelerisque aliquam. Vestibulum placerat odio risus, nec semper sa [...]
+
+Suspendisse vitae elit urna. Donec orci dolor, ullamcorper ut felis eget, porta facilisis ante. Vivamus odio quam, porta sed sapien quis, sodales tincidunt ligula. Donec porttitor.
\ No newline at end of file
diff --git a/bin/tools/scripts/compile-package-jar.sh b/bin/tools/scripts/compile-package-jar.sh
new file mode 100755
index 0000000..c9d0162
--- /dev/null
+++ b/bin/tools/scripts/compile-package-jar.sh
@@ -0,0 +1,163 @@
+#!/bin/bash -e
+
+# 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.
+
+# This is a general bash script to build a JDK version-specific
+# datasketches-memory-X.jar without multi-release functionality.
+# This is intended to be used for developers compiling from source
+# who do not wish to install several versions of the JDK on their
+# machine.
+# The script does not assume a POM file and does not use Maven.
+# It does use git and also uses the script getGitProperties.sh.
+
+# Required Input Parameters:
+# \$1 = absolute path of JDK home directory
+# \$2 = Git Version Tag for this deployment
+# Example tag for SNAPSHOT : 1.0.0-SNAPSHOT
+# Example tag for Release Candidate: 1.0.0-RC1
+# Example tag for Release : 1.0.0
+# \$3 = absolute path of project.basedir
+# For example: $ <this script>.sh $JAVA_HOME 2.0.0-RC1 .
+
+if [ -z "$1" ]; then echo "Missing JDK home"; exit 1; fi
+if [ -z "$2" ]; then echo "Missing Git Tag"; exit 1; fi
+if [ -z "$3" ]; then echo "Missing project.basedir"; exit 1; fi
+
+#### Extract JDKHome, Version and ProjectBaseDir from input parameters ####
+JDKHome=$1
+GitTag=$2
+ProjectBaseDir=$3 #this must be an absolute path
+
+#### Setup absolute directory references ####
+ProjectArtifactId="memory"
+ScriptsDir=${ProjectBaseDir}/tools/scripts/
+MemoryMapFile=$ScriptsDir/LoremIpsum.txt
+
+#### Initialise path dependent variables ####
+OutputDir=target
+OutputJar=${OutputDir}/org.apache.datasketches.memory-${GitTag}.jar
+
+PackageDir=${OutputDir}/archive-tmp
+PackageSrc=${PackageDir}/src
+PackageTests=${PackageDir}/test-classes
+PackageContents=${PackageDir}/contents
+PackageMeta=${PackageContents}/META-INF
+PackageManifest=${PackageMeta}/MANIFEST.MF
+
+MemoryJava8Src=datasketches-memory-java8/src/main/java
+MemoryJava11Src=datasketches-memory-java11/src/main/java
+
+#### Move to project directory ####
+cd ${ProjectBaseDir}
+
+#### Use JAVA_HOME to set required executables ####
+if [[ -n "$JDKHome" ]] && [[ -x "${JDKHome}/bin/java" ]]; then Java_="${JDKHome}/bin/java"; else echo "No java version could be found."; exit 1; fi
+if [[ -n "$JDKHome" ]] && [[ -x "${JDKHome}/bin/javac" ]]; then Javac_="${JDKHome}/bin/javac"; else echo "No javac version could be found."; exit 1; fi
+if [[ -n "$JDKHome" ]] && [[ -x "${JDKHome}/bin/jar" ]]; then Jar_="${JDKHome}/bin/jar"; else echo "No jar version could be found."; exit 1; fi
+
+#### Parse java -version into major version number ####
+if [[ "$Java_" ]]; then
+ # This expression extracts the correct major version of the Java runtime.
+ # For older runtime versions, such as 1.8, the leading '1.' is removed.
+ # Adapted from this answer on StackOverflow:
+ # https://stackoverflow.com/questions/7334754/correct-way-to-check-java-version-from-bash-script/56243046#56243046
+ JavaVersion=$("$Java_" -version 2>&1 | head -1 | cut -d'"' -f2 | sed '/^1\./s///' | cut -d'.' -f1)
+else
+ echo "No version information could be determined from installed JDK."; exit 1;
+fi
+
+# Exit if Java version too low (< 8) or too high (> 13)
+if [[ $JavaVersion -lt 8 || $JavaVersion -gt 13 ]]; then
+ echo "Java version not supported: " $JavaVersion; exit 1;
+fi
+
+#### Cleanup and setup output directories ####
+echo
+if [ -d "$OutputDir" ]; then rm -r $OutputDir; fi
+mkdir -p $PackageSrc
+mkdir -p $PackageTests
+mkdir -p $PackageMeta
+
+#### Copy LICENSE and NOTICE ####
+cp LICENSE $PackageMeta
+cp NOTICE $PackageMeta
+
+#### Generate MANIFEST.MF ####
+cat >> ${PackageManifest}<< EOF
+Manifest-Version: 1.0
+Created-By: Apache Datasketches Memory compile-package-jar.sh
+Multi-Release: false
+EOF
+
+#### Generate git.properties file ####
+echo "$($ScriptsDir/getGitProperties.sh $ProjectBaseDir $ProjectArtifactId $GitTag)" >> $PackageManifest
+
+#### Copy source tree to target/src
+rsync -a -I $MemoryJava8Src $PackageSrc
+
+if [[ $JavaVersion -gt 10 ]]; then
+ #### Copy java 11 src trees to target/src, overwriting replacements
+ rsync -a -I $MemoryJava11Src $PackageSrc
+fi
+
+#### Compile ####
+echo "--- CLEAN & COMPILE ---"
+echo
+echo "Compiling with JDK version $JavaVersion..."
+if [[ $JavaVersion -lt 9 ]]; then
+ ${Javac_} -d $PackageContents $(find $PackageSrc -name '*.java')
+else
+ # Compile with JPMS exports
+ ${Javac_} \
+ --add-exports java.base/jdk.internal.ref=org.apache.datasketches.memory \
+ --add-exports java.base/sun.nio.ch=org.apache.datasketches.memory \
+ -d $PackageContents $(find $PackageSrc -name '*.java')
+fi
+echo
+echo "--- JARS ---"
+echo
+echo "Building JAR from ${PackageContents}..."
+${Jar_} cf $OutputJar -C $PackageContents .
+echo
+
+# Uncomment this section to display JAR contents
+# echo "--- JAR CONTENTS ---"
+# echo
+# ${Jar_} tf ${OutputJar}
+# echo
+
+echo "--- RUN JAR CHECKS ---"
+echo
+if [[ $JavaVersion -eq 8 ]]; then
+ ${Javac_} -cp $OutputJar -d $PackageTests $(find $ScriptsDir -name '*.java')
+ ${Java_} -cp $PackageTests:$OutputJar org.apache.datasketches.memory.tools.scripts.CheckMemoryJar $MemoryMapFile
+else
+ ${Javac_} \
+ --add-modules org.apache.datasketches.memory \
+ -p "$OutputJar" -d $PackageTests $(find $ScriptsDir -name '*.java')
+
+ ${Java_} \
+ --add-modules org.apache.datasketches.memory \
+ --add-exports java.base/jdk.internal.misc=org.apache.datasketches.memory \
+ --add-exports java.base/jdk.internal.ref=org.apache.datasketches.memory \
+ --add-opens java.base/java.nio=org.apache.datasketches.memory \
+ --add-opens java.base/sun.nio.ch=org.apache.datasketches.memory \
+ -p $OutputJar -cp $PackageTests org.apache.datasketches.memory.tools.scripts.CheckMemoryJar $MemoryMapFile
+fi
+echo
+echo "Successfully built ${OutputJar}"
diff --git a/bin/tools/scripts/getGitProperties.sh b/bin/tools/scripts/getGitProperties.sh
new file mode 100755
index 0000000..0d8f0dc
--- /dev/null
+++ b/bin/tools/scripts/getGitProperties.sh
@@ -0,0 +1,66 @@
+#!/bin/bash -e
+
+# 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.
+
+MyBase=$(pwd)
+ProjectBaseDir=$1 #this must be an absolute path
+ArtId=$2
+Tag=$3
+
+####Move to project directory####
+cd ${ProjectBaseDir}
+
+CR=$'\n'
+
+#Add Implementation Vendor
+prop=$prop'Implementation-Vendor: '
+tmp='The Apache Software Foundataion'$CR
+prop=$prop$tmp
+
+#Add GroupId : ArtifactId
+prop=$prop'GroupId-ArtifactId: '
+tmp='org.apache.datasketches:'$ArtId$CR
+prop=$prop$tmp
+
+# Add Branch
+prop=$prop'git.branch: '
+tmp=''$(git rev-parse --abbrev-ref HEAD)''$CR
+prop=$prop$tmp
+
+#Add commit-id
+prop=$prop'git.commit.id.full: '
+ID=$(git rev-parse HEAD)
+tmp=''$ID''$CR
+prop=$prop$tmp
+
+#Add timestamp
+prop=$prop'git.commit.time: '
+tmp=''$(git show --no-patch --no-notes --pretty='%cI' $ID)''$CR
+prop=$prop$tmp
+
+#Add user email
+prop=$prop'git-commit-user-email: '
+tmp=''$(git show --no-patch --no-notes --pretty='%ce' $ID)''$CR
+prop=$prop$tmp
+
+#Add Tag
+prop=$prop'git.commit.tag: '
+tmp=''$Tag''$CR
+prop=$prop$tmp
+
+echo "$prop"
diff --git a/bin/tools/toolchains.xml b/bin/tools/toolchains.xml
new file mode 100644
index 0000000..9ab86f2
--- /dev/null
+++ b/bin/tools/toolchains.xml
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="UTF8"?>
+
+<!--
+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.
+-->
+
+<toolchains>
+ <toolchain>
+ <type>jdk</type>
+ <provides>
+ <version>1.8</version>
+ <vendor>openjdk</vendor>
+ </provides>
+ <configuration>
+ <jdkHome>${env.JAVA8_HOME}</jdkHome>
+ </configuration>
+ </toolchain>
+ <toolchain>
+ <type>jdk</type>
+ <provides>
+ <version>8</version>
+ <vendor>openjdk</vendor>
+ </provides>
+ <configuration>
+ <jdkHome>${env.JAVA8_HOME}</jdkHome>
+ </configuration>
+ </toolchain>
+ <toolchain>
+ <type>jdk</type>
+ <provides>
+ <version>11</version>
+ <vendor>openjdk</vendor>
+ </provides>
+ <configuration>
+ <jdkHome>${env.JAVA11_HOME}</jdkHome>
+ </configuration>
+ </toolchain>
+</toolchains>
\ No newline at end of file
diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java
new file mode 100644
index 0000000..d62f61b
--- /dev/null
+++ b/src/main/java/module-info.java
@@ -0,0 +1,26 @@
+/*
+ * 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.
+ */
+@SuppressWarnings("javadoc")
+module org.apache.datasketches.memory {
+ requires java.base;
+ requires java.logging;
+ requires transitive jdk.incubator.foreign;
+ //requires jdk.incubator.foreign;
+ exports org.apache.datasketches.memory;
+}
\ No newline at end of file
diff --git a/src/main/java/org/apache/datasketches/memory/BaseBuffer.java b/src/main/java/org/apache/datasketches/memory/BaseBuffer.java
new file mode 100644
index 0000000..9cd824e
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/BaseBuffer.java
@@ -0,0 +1,136 @@
+/*
+ * 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;
+
+/**
+ * Defines the relative positional API.
+ * This is different from and simpler than Java ByteBuffer positional API.
+ * <ul><li>All based on longs instead of ints.</li>
+ * <li>Eliminated "mark". Rarely used and confusing with its silent side effects.</li>
+ * <li>The invariants are {@code 0 <= start <= position <= end <= capacity}.</li>
+ * <li>It always starts up as (0, 0, capacity, capacity).</li>
+ * <li>You set (start, position, end) in one call with
+ * {@link #setStartPositionEnd(long, long, long)}</li>
+ * <li>Position can be set directly or indirectly when using the positional get/put methods.
+ * <li>Added incrementPosition(long), which is much easier when you know the increment.</li>
+ * <li>This approach eliminated a number of methods and checks, and has no unseen side effects,
+ * e.g., mark being invalidated.</li>
+ * <li>Clearer method naming (IMHO).</li>
+ * </ul>
+ * @author Lee Rhodes
+ */
+public interface BaseBuffer extends BaseState {
+
+ /**
+ * Increments the current position by the given increment.
+ * Asserts that the resource is valid and that the positional invariants are not violated,
+ * otherwise, if asserts are enabled throws an {@link AssertionError}.
+ * @param increment the given increment
+ * @return BaseBuffer
+ */
+ BaseBuffer incrementPosition(long increment);
+
+ /**
+ * Increments the current position by the given increment.
+ * Checks that the resource is valid and that the positional invariants are not violated,
+ * otherwise throws an {@link IllegalArgumentException}.
+ * @param increment the given increment
+ * @return BaseBuffer
+ */
+ BaseBuffer incrementAndCheckPosition(final long increment);
+
+ /**
+ * Gets the end position
+ * @return the end position
+ */
+ long getEnd();
+
+ /**
+ * Gets the current position
+ * @return the current position
+ */
+ long getPosition();
+
+ /**
+ * Gets start position
+ * @return start position
+ */
+ long getStart();
+
+ /**
+ * The number of elements remaining between the current position and the end position
+ * @return {@code (end - position)}
+ */
+ long getRemaining();
+
+ /**
+ * Returns true if there are elements remaining between the current position and the end position
+ * @return {@code (end - position) > 0}
+ */
+ boolean hasRemaining();
+
+ /**
+ * Resets the current position to the start position,
+ * This does not modify any data.
+ * @return BaseBuffer
+ */
+ BaseBuffer resetPosition();
+
+ /**
+ * Sets the current position.
+ * Asserts that the positional invariants are not violated,
+ * otherwise, if asserts are enabled throws an {@link AssertionError}.
+ * @param position the given current position.
+ * @return BaseBuffer
+ */
+ BaseBuffer setPosition(long position);
+
+ /**
+ * Sets the current position.
+ * Checks that the positional invariants are not violated,
+ * otherwise, throws an {@link IllegalArgumentException}.
+ * @param position the given current position.
+ * @return BaseBuffer
+ */
+ BaseBuffer setAndCheckPosition(long position);
+
+ /**
+ * Sets start position, current position, and end position.
+ * Asserts that the positional invariants are not violated,
+ * otherwise, if asserts are enabled throws an {@link AssertionError}.
+ * @param start the start position in the buffer
+ * @param position the current position between the start and end
+ * @param end the end position in the buffer
+ * @return BaseBuffer
+ */
+ BaseBuffer setStartPositionEnd(long start, long position, long end);
+
+ /**
+ * Sets start position, current position, and end position.
+ * Checks that the positional invariants are not violated,
+ * otherwise, throws an {@link IllegalArgumentException}.
+ * @param start the start position in the buffer
+ * @param position the current position between the start and end
+ * @param end the end position in the buffer
+ * @return BaseBuffer
+ */
+ BaseBuffer setAndCheckStartPositionEnd(long start, long position, long end);
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/BaseState.java b/src/main/java/org/apache/datasketches/memory/BaseState.java
index ac85db8..a78e76e 100644
--- a/src/main/java/org/apache/datasketches/memory/BaseState.java
+++ b/src/main/java/org/apache/datasketches/memory/BaseState.java
@@ -17,13 +17,14 @@
* under the License.
*/
-
package org.apache.datasketches.memory;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
-import org.apache.datasketches.memory.internal.BaseStateImpl;
+import jdk.incubator.foreign.MemorySegment;
+
+//import org.apache.datasketches.memory.internal.BaseStateImpl;
/**
* Keeps key configuration state for Memory and Buffer plus some common static variables
@@ -32,10 +33,17 @@ import org.apache.datasketches.memory.internal.BaseStateImpl;
* @author Lee Rhodes
*/
public interface BaseState {
+
+ /**
+ * The java line separator character as a String.
+ */
+ static final String LS = System.getProperty("line.separator");
+
/**
- * The placeholder for the default MemoryRequestServer, if set at all.
+ * For off-heap segments, this closes the controlling ResourceScope. If the segment is
+ * not off-heap, this does nothing.
*/
- static final MemoryRequestServer defaultMemReqSvr = new DefaultMemoryRequestServer();
+ void close();
//Byte Order Related
@@ -55,14 +63,6 @@ public interface BaseState {
*/
boolean isByteOrderCompatible(ByteOrder byteOrder);
- /**
- * Returns true if the given object is an instance of this class and has equal data contents.
- * @param that the given object
- * @return true if the given Object is an instance of this class and has equal data contents.
- */
- @Override
- boolean equals(Object that);
-
/**
* Returns true if the given object is an instance of this class and has equal contents to
* this object in the given range of bytes. This will also check two distinct ranges within the
@@ -78,76 +78,26 @@ public interface BaseState {
long thatOffsetBytes, long lengthBytes);
/**
- * Gets the backing ByteBuffer if it exists, otherwise returns null.
- * @return the backing ByteBuffer if it exists, otherwise returns null.
- */
- ByteBuffer getByteBuffer();
-
- /**
- * Gets the capacity of this object in bytes
- * @return the capacity of this object in bytes
- */
- long getCapacity();
-
- /**
- * Gets the cumulative offset in bytes of this object from the backing resource.
- * This offset may also include other offset components such as the native off-heap
- * memory address, DirectByteBuffer split offsets, region offsets, and unsafe arrayBaseOffsets.
+ * Wraps the underlying MemorySegment in a ByteBuffer.
+ * @see MemorySegment#asByteBuffer
*
- * @return the cumulative offset in bytes of this object from the backing resource.
- */
- long getCumulativeOffset();
-
- /**
- * Gets the cumulative offset in bytes of this object from the backing resource including the given
- * offsetBytes. This offset may also include other offset components such as the native off-heap
- * memory address, DirectByteBuffer split offsets, region offsets, and unsafe arrayBaseOffsets.
+ * @return a ByteBuffer view of this memory segment.
*
- * @param offsetBytes offset to be added to the cumulative offset.
- * @return the cumulative offset in bytes of this object from the backing resource including the
- * given offsetBytes.
*/
- long getCumulativeOffset(long offsetBytes);
+ ByteBuffer asByteBuffer();
/**
- * Returns the offset of address zero of this object relative to the address zero of the
- * backing resource but not including the size of any Java object header.
- * @return the offset of address zero of this object relative to the address zero of the
- * backing resource but not including the size of any Java object header.
+ * Returns a copy of the underlying MemorySegment.
+ * The size is limited to <i>Integer.MAX_VALUE</i>.
+ * @return a copy of the underlying MemorySegment
*/
- long getRegionOffset();
+ MemorySegment asMemorySegment();
/**
- * Returns the offset of address zero of this object relative to the address zero of the
- * backing resource plus the given offsetBytes but not including the size of any Java object
- * header.
- * @param offsetBytes the given offsetBytes
- * @return the offset of address zero of this object relative to the address zero of the
- * backing resource plus the given offsetBytes but not including the size of any Java object
- * header.
- */
- long getRegionOffset(long offsetBytes);
-
- /**
- * Returns true if this object is backed by an on-heap primitive array
- * @return true if this object is backed by an on-heap primitive array
- */
- boolean hasArray();
-
- /**
- * Returns the hashCode of this object.
- *
- * <p>The hash code of this object depends upon all of its contents.
- * Because of this, it is inadvisable to use these objects as keys in hash maps
- * or similar data structures unless it is known that their contents will not change.</p>
- *
- * <p>If it is desirable to use these objects in a hash map depending only on object identity,
- * than the {@link java.util.IdentityHashMap} can be used.</p>
- *
- * @return the hashCode of this object.
+ * Gets the capacity of this object in bytes
+ * @return the capacity of this object in bytes
*/
- @Override
- int hashCode();
+ long getCapacity();
/**
* Returns the 64-bit hash of the sequence of bytes in this object specified by
@@ -201,54 +151,23 @@ public interface BaseState {
boolean isSameResource(Object that);
/**
- * Returns true if this object is valid and has not been closed.
- * This is relevant only for direct (off-heap) memory and Mapped Files.
- * @return true if this object is valid and has not been closed.
- */
- boolean isValid();
-
- /**
- * Checks that the specified range of bytes is within bounds of this object, throws
- * {@link IllegalArgumentException} if it's not: i. e. if offsetBytes < 0, or length < 0,
- * or offsetBytes + length > {@link #getCapacity()}.
- * @param offsetBytes the given offset in bytes of this object
- * @param lengthBytes the given length in bytes of this object
- */
- void checkValidAndBounds(long offsetBytes, long lengthBytes);
-
- //Monitoring
-
- /**
- * Gets the current number of active direct memory allocations.
- * @return the current number of active direct memory allocations.
- */
- static long getCurrentDirectMemoryAllocations() {
- return BaseStateImpl.getCurrentDirectMemoryAllocations();
- }
-
- /**
- * Gets the current size of active direct memory allocated.
- * @return the current size of active direct memory allocated.
+ * Returns the configured MemoryRequestSever or null, if it has not been configured.
+ * @return the configured MemoryRequestSever or null, if it has not been configured.
*/
- static long getCurrentDirectMemoryAllocated() {
- return BaseStateImpl.getCurrentDirectMemoryAllocated();
- }
+ MemoryRequestServer getMemoryRequestServer();
/**
- * Gets the current number of active direct memory map allocations.
- * @return the current number of active direct memory map allocations.
+ * Returns true if the MemoryRequestServer has been configured.
+ * @return true if the MemoryRequestServer has been configured.
*/
- static long getCurrentDirectMemoryMapAllocations() {
- return BaseStateImpl.getCurrentDirectMemoryMapAllocations();
- }
+ boolean hasMemoryRequestServer();
/**
- * Gets the current size of active direct memory map allocated.
- * @return the current size of active direct memory map allocated.
+ * Sets the default MemoryRequestServer to be used in case of capacity overflow of off-heap
+ * (Direct or Native) allocated Memory or of on-heap allocated Memory.
+ * @param memReqSvr the given default MemoryRequestServer
*/
- static long getCurrentDirectMemoryMapAllocated() {
- return BaseStateImpl.getCurrentDirectMemoryMapAllocated();
- }
+ void setMemoryRequestServer(MemoryRequestServer memReqSvr);
//TO STRING
diff --git a/src/main/java/org/apache/datasketches/memory/Buffer.java b/src/main/java/org/apache/datasketches/memory/Buffer.java
new file mode 100644
index 0000000..1a8cdbe
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/Buffer.java
@@ -0,0 +1,387 @@
+/*
+ * 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;
+
+import java.io.File;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Objects;
+
+import org.apache.datasketches.memory.internal.BaseWritableBufferImpl;
+
+/**
+ * Defines the read-only API for relative positional access to a resource.
+ *
+ * @author Lee Rhodes
+ */
+public interface Buffer extends BaseBuffer {
+
+ //BYTE BUFFER
+ /**
+ * Provides a view of the given <i>ByteBuffer</i> for read-only operations.
+ * The returned <i>Buffer</i> will use the native <i>ByteOrder</i>,
+ * ignoring the byte order of the given <i>ByteBuffer</i>.
+ * This does not change the byte order of data already in the <i>ByteBuffer</i>.
+ * The view starts relative to the buffer's position (inclusive)
+ * and ends relative to the buffer's limit (exclusive).
+ * @param byteBuffer the given ByteBuffer, must not be null.
+ * @return a new <i>Buffer</i> for read-only operations on the given ByteBuffer.
+ */
+ static Buffer wrap(ByteBuffer byteBuffer) {
+ return wrap(byteBuffer, ByteOrder.nativeOrder());
+ }
+
+ /**
+ * Provides a view of the given <i>ByteBuffer</i> for read-only operations.
+ * The returned <i>Buffer</i> will use the given <i>ByteOrder</i>,
+ * ignoring the byte order of the given <i>ByteBuffer</i>.
+ * The view starts relative to the ByteBuffer's position (inclusive)
+ * and ends relative to the ByteBuffer's limit (exclusive).
+ * @param byteBuffer the given ByteBuffer, must not be null
+ * @param byteOrder the ByteOrder to be used. It must be non-null.
+ * @return a new <i>Buffer</i> for read-only operations on the given ByteBuffer.
+ */
+ static Buffer wrap(ByteBuffer byteBuffer, ByteOrder byteOrder) {
+ Objects.requireNonNull(byteBuffer, "byteBuffer must not be null");
+ Objects.requireNonNull(byteOrder, "byteOrder must not be null");
+ ByteBuffer byteBuf = byteBuffer.asReadOnlyBuffer();
+ return BaseWritableBufferImpl.wrapByteBuffer(byteBuf, true, byteOrder);
+ }
+
+ //DUPLICATES
+ /**
+ * Returns a read-only duplicate view of this Buffer with the same byte order,
+ * but independent values of
+ * <i>start</i>, <i>position</i> and <i>end</i>.
+ * <ul>
+ * <li>Returned object's origin = this object's origin</li>
+ * <li>Returned object's <i>start</i> = this object's <i>start</i></li>
+ * <li>Returned object's <i>position</i> = this object's <i>position</i></li>
+ * <li>Returned object's <i>end</i> = this object's <i>end</i></li>
+ * <li>Returned object's <i>capacity</i> = this object' <i>capacityBytes</i></li>
+ * <li>Returned object's <i>start</i>, <i>position</i> and <i>end</i> are mutable and
+ * independent of this object's <i>start</i>, <i>position</i> and <i>end</i></li>
+ * </ul>
+ * @return a read-only duplicate view of this Buffer with the same but independent values of
+ * <i>start</i>, <i>position</i> and <i>end</i>.
+ */
+ Buffer duplicate();
+
+ /**
+ * Returns a read-only duplicate view of this Buffer with the same but independent values of
+ * <i>start</i>, <i>position</i> and <i>end</i>, but with the specified byteOrder.
+ * <ul>
+ * <li>Returned object's origin = this object's origin</li>
+ * <li>Returned object's <i>start</i> = this object's <i>start</i></li>
+ * <li>Returned object's <i>position</i> = this object's <i>position</i></li>
+ * <li>Returned object's <i>end</i> = this object's <i>end</i></li>
+ * <li>Returned object's <i>capacity</i> = this object' <i>capacityBytes</i></li>
+ * <li>Returned object's <i>start</i>, <i>position</i> and <i>end</i> are mutable and
+ * independent of this object's <i>start</i>, <i>position</i> and <i>end</i></li>
+ * </ul>
+ * @param byteOrder the given <i>ByteOrder</i>.
+ * @return a read-only duplicate view of this Buffer with the same but independent values of
+ * <i>start</i>, <i>position</i> and <i>end</i>.
+ */
+ Buffer duplicate(ByteOrder byteOrder);
+
+ //MAP
+ /**
+ * Maps the given file into <i>Buffer</i> for read operations
+ * Calling this method is equivalent to calling
+ * {@link #map(File, long, long, ByteOrder) map(file, 0, file.length(), ByteOrder.nativeOrder())}.
+ * @param file the given file to map. It must be non-null, length ≥ 0, and readable.
+ * @return mapped Buffer.
+ * @throws Exception
+ */
+ static Buffer map(File file) throws Exception {
+ return map(file, 0, file.length(), ByteOrder.nativeOrder());
+ }
+
+ /**
+ * Maps the specified portion of the given file into <i>Buffer</i> for read operations.
+ * @param file the given file to map. It must be non-null,readable and length ≥ 0.
+ * @param fileOffsetBytes the position in the given file in bytes. It must not be negative.
+ * @param capacityBytes the size of the mapped memory. It must be ≥ 0.
+ * @param byteOrder the byte order to be used. It must be non-null.
+ * @return Buffer
+ * @throws Exception
+
+ */
+ static Buffer map(File file, long fileOffsetBytes, long capacityBytes, ByteOrder byteOrder)
+ throws Exception {
+ Objects.requireNonNull(file, "file must be non-null.");
+ Objects.requireNonNull(byteOrder, "byteOrder must be non-null.");
+ if (!file.canRead()) { throw new IllegalArgumentException("file must be readable."); }
+ return BaseWritableBufferImpl.wrapMap(file, fileOffsetBytes, capacityBytes, true, byteOrder);
+ }
+
+ //REGIONS
+ /**
+ * A region is a read-only view of this object from <i>position</i> to <i>end</i>
+ * and with the same byte order.
+ * <ul>
+ * <li>Returned object's origin = this object's <i>position</i></li>
+ * <li>Returned object's <i>start</i> = 0</li>
+ * <li>Returned object's <i>position</i> = 0</li>
+ * <li>Returned object's <i>end</i> = this object's (<i>end</i> - <i>position</i>)</li>
+ * <li>Returned object's <i>capacity</i> = this object's (<i>end</i> - <i>position</i>)</li>
+ * <li>Returned object's <i>start</i>, <i>position</i> and <i>end</i> are mutable and
+ * independent of this object's <i>start</i>, <i>position</i> and <i>end</i></li>
+ * </ul>
+ * @return a new <i>Buffer</i> representing the defined region based on the current
+ * <i>position</i> and <i>end</i>.
+ */
+ default Buffer region() {
+ return region(getPosition(), getEnd() - getPosition(), getTypeByteOrder());
+ }
+
+ /**
+ * A region is a read-only view of this object from offsetBytes and with a length of
+ * capacityBytes and with the given byte order.
+ * <ul>
+ * <li>Returned object's origin = this objects' origin + <i>offsetBytes</i></li>
+ * <li>Returned object's <i>start</i> = 0</li>
+ * <li>Returned object's <i>position</i> = 0</li>
+ * <li>Returned object's <i>end</i> = <i>capacityBytes</i></li>
+ * <li>Returned object's <i>capacity</i> = <i>capacityBytes</i></li>
+ * <li>Returned object's <i>start</i>, <i>position</i> and <i>end</i> are mutable and
+ * independent of this object's <i>start</i>, <i>position</i> and <i>end</i></li>
+ * <li>Returned object's byte order = <i>byteOrder</i></li>
+ * </ul>
+ *
+ * @param offsetBytes the starting offset with respect to the origin of this <i>WritableBuffer</i>
+ * @param capacityBytes the <i>capacity</i> of the returned region in bytes
+ * @param byteOrder the given byte order
+ * @return a new <i>Buffer</i> representing the defined writable region
+ * based on the current <i>position</i>, <i>end</i> and byteOrder.
+ */
+ Buffer region(long offsetBytes, long capacityBytes, ByteOrder byteOrder);
+
+ //AS MEMORY
+ /**
+ * Convert this Buffer to a Memory. The current <i>start</i>, <i>position</i> and <i>end</i>
+ * are ignored.
+ * @return Memory
+ */
+ default Memory asMemory() {
+ return asMemory(getTypeByteOrder());
+ }
+
+ /**
+ * Convert this Buffer to a Memory with the given byte order.
+ * The current <i>start</i>, <i>position</i> and <i>end</i> are ignored.
+ * @param byteOrder the given byte order.
+ * @return Memory
+ */
+ Memory asMemory(ByteOrder byteOrder);
+
+ //NO WRAP - ACCESS PRIMITIVE HEAP ARRAYS for readOnly
+
+ //PRIMITIVE getX() and getXArray()
+
+ /**
+ * Gets the byte value at the current position.
+ * Increments the position by <i>Byte.BYTES</i>.
+ * @return the byte at the current position
+ */
+ byte getByte();
+
+ /**
+ * Gets the byte value at the given offset.
+ * This does not change the position.
+ * @param offsetBytes offset bytes relative to this Memory start
+ * @return the byte at the given offset
+ */
+ byte getByte(long offsetBytes);
+
+ /**
+ * Gets the byte array at the current position.
+ * Increments the position by <i>Byte.BYTES * (lengthBytes - dstOffsetBytes)</i>.
+ * @param dstArray The preallocated destination array.
+ * @param dstOffsetBytes offset in array units
+ * @param lengthBytes number of array units to transfer
+ */
+ void getByteArray(byte[] dstArray, int dstOffsetBytes, int lengthBytes);
+
+ /**
+ * Gets the char value at the current position.
+ * Increments the position by <i>Character.BYTES</i>.
+ * @return the char at the current position
+ */
+ char getChar();
+
+ /**
+ * Gets the char value at the given offset.
+ * This does not change the position.
+ * @param offsetBytes offset bytes relative to this Memory start
+ * @return the char at the given offset
+ */
+ char getChar(long offsetBytes);
+
+ /**
+ * Gets the char array at the current position.
+ * Increments the position by <i>Character.BYTES * (lengthChars - dstOffsetChars)</i>.
+ * @param dstArray The preallocated destination array.
+ * @param dstOffsetChars offset in array units
+ * @param lengthChars number of array units to transfer
+ */
+ void getCharArray(char[] dstArray, int dstOffsetChars, int lengthChars);
+
+ /**
+ * Gets the double value at the current position.
+ * Increments the position by <i>Double.BYTES</i>.
+ * @return the double at the current position
+ */
+ double getDouble();
+
+ /**
+ * Gets the double value at the given offset.
+ * This does not change the position.
+ * @param offsetBytes offset bytes relative to this Memory start
+ * @return the double at the given offset
+ */
+ double getDouble(long offsetBytes);
+
+ /**
+ * Gets the double array at the current position.
+ * Increments the position by <i>Double.BYTES * (lengthDoubles - dstOffsetDoubles)</i>.
+ * @param dstArray The preallocated destination array.
+ * @param dstOffsetDoubles offset in array units
+ * @param lengthDoubles number of array units to transfer
+ */
+ void getDoubleArray(double[] dstArray, int dstOffsetDoubles, int lengthDoubles);
+
+ /**
+ * Gets the float value at the current position.
+ * Increments the position by <i>Float.BYTES</i>.
+ * @return the float at the current position
+ */
+ float getFloat();
+
+ /**
+ * Gets the float value at the given offset.
+ * This does not change the position.
+ * @param offsetBytes offset bytes relative to this Memory start
+ * @return the float at the given offset
+ */
+ float getFloat(long offsetBytes);
+
+ /**
+ * Gets the float array at the current position.
+ * Increments the position by <i>Float.BYTES * (lengthFloats - dstOffsetFloats)</i>.
+ * @param dstArray The preallocated destination array.
+ * @param dstOffsetFloats offset in array units
+ * @param lengthFloats number of array units to transfer
+ */
+ void getFloatArray(float[] dstArray, int dstOffsetFloats, int lengthFloats);
+
+ /**
+ * Gets the int value at the current position.
+ * Increments the position by <i>Integer.BYTES</i>.
+ * @return the int at the current position
+ */
+ int getInt();
+
+ /**
+ * Gets the int value at the given offset.
+ * This does not change the position.
+ * @param offsetBytes offset bytes relative to this Memory start
+ * @return the int at the given offset
+ */
+ int getInt(long offsetBytes);
+
+ /**
+ * Gets the int array at the current position.
+ * Increments the position by <i>Integer.BYTES * (lengthInts - dstOffsetInts)</i>.
+ * @param dstArray The preallocated destination array.
+ * @param dstOffsetInts offset in array units
+ * @param lengthInts number of array units to transfer
+ */
+ void getIntArray(int[] dstArray, int dstOffsetInts, int lengthInts);
+
+ /**
+ * Gets the long value at the current position.
+ * Increments the position by <i>Long.BYTES</i>.
+ * @return the long at the current position
+ */
+ long getLong();
+
+ /**
+ * Gets the long value at the given offset.
+ * This does not change the position.
+ * @param offsetBytes offset bytes relative to this Memory start
+ * @return the long at the given offset
+ */
+ long getLong(long offsetBytes);
+
+ /**
+ * Gets the long array at the current position.
+ * Increments the position by <i>Long.BYTES * (lengthLongs - dstOffsetLongs)</i>.
+ * @param dstArray The preallocated destination array.
+ * @param dstOffsetLongs offset in array units
+ * @param lengthLongs number of array units to transfer
+ */
+ void getLongArray(long[] dstArray, int dstOffsetLongs, int lengthLongs);
+
+ /**
+ * Gets the short value at the current position.
+ * Increments the position by <i>Short.BYTES</i>.
+ * @return the short at the current position
+ */
+ short getShort();
+
+ /**
+ * Gets the short value at the given offset.
+ * This does not change the position.
+ * @param offsetBytes offset bytes relative to this Memory start
+ * @return the short at the given offset
+ */
+ short getShort(long offsetBytes);
+
+ /**
+ * Gets the short array at the current position.
+ * Increments the position by <i>Short.BYTES * (lengthShorts - dstOffsetShorts)</i>.
+ * @param dstArray The preallocated destination array.
+ * @param dstOffsetShorts offset in array units
+ * @param lengthShorts number of array units to transfer
+ */
+ void getShortArray(short[] dstArray, int dstOffsetShorts, int lengthShorts);
+
+ //SPECIAL PRIMITIVE READ METHODS: compareTo
+ /**
+ * 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);
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/Memory.java b/src/main/java/org/apache/datasketches/memory/Memory.java
index f25de32..6e1cb9c 100644
--- a/src/main/java/org/apache/datasketches/memory/Memory.java
+++ b/src/main/java/org/apache/datasketches/memory/Memory.java
@@ -17,11 +17,8 @@
* under the License.
*/
-
package org.apache.datasketches.memory;
-import static org.apache.datasketches.memory.internal.Util.negativeCheck;
-
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
@@ -30,8 +27,8 @@ import java.nio.channels.WritableByteChannel;
import java.util.Objects;
import org.apache.datasketches.memory.internal.BaseWritableMemoryImpl;
-import org.apache.datasketches.memory.internal.Prim;
-import org.apache.datasketches.memory.internal.UnsafeUtil;
+
+import jdk.incubator.foreign.MemorySegment;
/**
* Defines the read-only API for offset access to a resource.
@@ -41,41 +38,44 @@ import org.apache.datasketches.memory.internal.UnsafeUtil;
public interface Memory extends BaseState {
//BYTE BUFFER
-
/**
- * Accesses the given <i>ByteBuffer</i> for read-only operations. The returned <i>Memory</i> object has
- * the same byte order, as the given <i>ByteBuffer</i>.
- * @param byteBuffer the given <i>ByteBuffer</i>. It must be non-null and with capacity ≥ 0.
+ * Provides a view of the given <i>ByteBuffer</i> for read-only operations.
+ * The returned <i>Memory</i> will use the native <i>ByteOrder</i>,
+ * ignoring the byte order of the given <i>ByteBuffer</i>.
+ * @param byteBuffer the given <i>ByteBuffer</i>. It must be non-null.
* @return a new <i>Memory</i> for read-only operations on the given <i>ByteBuffer</i>.
*/
static Memory wrap(ByteBuffer byteBuffer) {
- return wrap(byteBuffer, byteBuffer.order());
+ return wrap(byteBuffer, ByteOrder.nativeOrder());
}
/**
- * Accesses the given <i>ByteBuffer</i> for read-only operations. The returned <i>Memory</i> object has
- * the given byte order, ignoring the byte order of the given <i>ByteBuffer</i> for future reads and writes.
- * @param byteBuffer the given <i>ByteBuffer</i>. It must be non-null and with capacity ≥ 0.
+ * Provides a view of the given <i>ByteBuffer</i> for read-only operations.
+ * The returned <i>Memory</i> will use the given byte order,
+ * ignoring the byte order of the given <i>ByteBuffer</i>.
+ * This does not change the byte order of data already in the <i>ByteBuffer</i>.
+ * The view starts at zero (inclusive) and ends at the ByteBuffer's capacity (exclusive).
+ * @param byteBuffer the given <i>ByteBuffer</i>. It must be non-null.
* @param byteOrder the byte order to be used. It must be non-null.
* @return a new <i>Memory</i> for read-only operations on the given <i>ByteBuffer</i>.
*/
static Memory wrap(ByteBuffer byteBuffer, ByteOrder byteOrder) {
Objects.requireNonNull(byteBuffer, "byteBuffer must not be null");
Objects.requireNonNull(byteOrder, "byteOrder must not be null");
- negativeCheck(byteBuffer.capacity(), "byteBuffer.capacity()");
- return BaseWritableMemoryImpl.wrapByteBuffer(byteBuffer, true, byteOrder, null);
+ ByteBuffer byteBuf = byteBuffer.position(0).limit(byteBuffer.capacity()).asReadOnlyBuffer();
+ return BaseWritableMemoryImpl.wrapByteBuffer(byteBuf, true, byteOrder);
}
//MAP
/**
- * Maps the entire given file into native-ordered <i>Memory</i> for read operations
+ * Maps the given file into <i>Memory</i> for read operations
* Calling this method is equivalent to calling
* {@link #map(File, long, long, ByteOrder) map(file, 0, file.length(), ByteOrder.nativeOrder())}.
* @param file the given file to map. It must be non-null, length ≥ 0, and readable.
- * @return <i>MapHandle</i> for managing the mapped memory.
- * Please read Javadocs for {@link Handle}.
+ * @return mapped Memory.
+ * @throws Exception
*/
- static MapHandle map(File file) {
+ static Memory map(File file) throws Exception {
return map(file, 0, file.length(), ByteOrder.nativeOrder());
}
@@ -84,18 +84,16 @@ public interface Memory extends BaseState {
* @param file the given file to map. It must be non-null,readable and length ≥ 0.
* @param fileOffsetBytes the position in the given file in bytes. It must not be negative.
* @param capacityBytes the size of the mapped memory. It must be ≥ 0.
- * @param byteOrder the byte order to be used for the mapped memory. It must be non-null.
- * @return <i>MapHandle</i> for managing the mapped memory.
- * Please read Javadocs for {@link Handle}.
+ * @param byteOrder the byte order to be used. It must be non-null.
+ * @return Memory
+ * @throws Exception
*/
- static MapHandle map(File file, long fileOffsetBytes, long capacityBytes, ByteOrder byteOrder) {
+ static Memory map(File file, long fileOffsetBytes, long capacityBytes, ByteOrder byteOrder)
+ throws Exception {
Objects.requireNonNull(file, "file must be non-null.");
Objects.requireNonNull(byteOrder, "byteOrder must be non-null.");
if (!file.canRead()) { throw new IllegalArgumentException("file must be readable."); }
- negativeCheck(file.length(), "file.length()");
- negativeCheck(fileOffsetBytes, "fileOffsetBytes");
- negativeCheck(capacityBytes, "capacityBytes");
- return (MapHandle) BaseWritableMemoryImpl.wrapMap(file, fileOffsetBytes, capacityBytes, true, byteOrder);
+ return BaseWritableMemoryImpl.wrapMap(file, fileOffsetBytes, capacityBytes, true, byteOrder);
}
//REGIONS
@@ -123,7 +121,7 @@ public interface Memory extends BaseState {
* </ul>
* @param offsetBytes the starting offset with respect to the origin of this Memory. It must be ≥ 0.
* @param capacityBytes the capacity of the region in bytes. It must be ≥ 0.
- * @param byteOrder the given byte order. It must be non-null.
+ * @param byteOrder the byte order to be used. It must be non-null.
* @return a new <i>Memory</i> representing the defined region based on the given
* offsetBytes, capacityBytes and byteOrder.
*/
@@ -157,15 +155,15 @@ public interface Memory extends BaseState {
* <li>Returned object's <i>capacity</i> = this object's capacity</li>
* <li>Returned object's <i>start</i>, <i>position</i> and <i>end</i> are mutable</li>
* </ul>
- * @param byteOrder the given byte order
+ * @param byteOrder the byte order to be used. It must be non-null.
* @return a new <i>Buffer</i> with the given byteOrder.
*/
Buffer asBuffer(ByteOrder byteOrder);
- //ACCESS PRIMITIVE HEAP ARRAYS for readOnly
+ //WRAP - ACCESS PRIMITIVE HEAP ARRAYS for readOnly
/**
* Wraps the given primitive array for read operations assuming native byte order.
- * @param array the given primitive array. It must be non-null and with length ≥ 0.
+ * @param array the given primitive array. It must be non-null.
* @return a new <i>Memory</i> for read operations
*/
static Memory wrap(byte[] array) {
@@ -184,115 +182,88 @@ public interface Memory extends BaseState {
}
/**
- * Wraps the given primitive array for read operations with the given byte order.
- * @param array the given primitive array. It must be non-null and length ≥ 0.
+ * Wraps the given primitive array for read operations assuming native byte order.
+ * @param array the given primitive array. It must be non-null.
* @param offsetBytes the byte offset into the given array
- * @param lengthBytes the number of bytes to include from the given array, it must be ≥ 0.
- * @param byteOrder the byte order to be used
+ * @param lengthBytes the number of bytes to include from the given array.
+ * @param byteOrder the byte order to be used. It must be non-null.
* @return a new <i>Memory</i> for read operations
*/
static Memory wrap(byte[] array, int offsetBytes, int lengthBytes, ByteOrder byteOrder) {
Objects.requireNonNull(array, "array must be non-null");
- Objects.requireNonNull(byteOrder, "byteOrder must be non-null");
- negativeCheck(offsetBytes, "offsetBytes");
- negativeCheck(lengthBytes, "lengthBytes");
- UnsafeUtil.checkBounds(offsetBytes, lengthBytes, array.length);
- return BaseWritableMemoryImpl.wrapHeapArray(array, 0, lengthBytes, true, ByteOrder.nativeOrder(), null);
- }
+ final MemorySegment slice = MemorySegment.ofArray(array).asSlice(offsetBytes, lengthBytes).asReadOnly();
+ return BaseWritableMemoryImpl.wrapSegment(slice, ByteOrder.nativeOrder(), null);
- /**
- * Wraps the given primitive array for read operations assuming native byte order.
- * @param array the given primitive array. It must be non-null and length ≥ 0.
- * @return a new <i>Memory</i> for read operations
- */
- static Memory wrap(boolean[] array) {
- Objects.requireNonNull(array, "array must be non-null");
- final long lengthBytes = array.length << Prim.BOOLEAN.shift();
- return BaseWritableMemoryImpl.wrapHeapArray(array, 0, lengthBytes, true, ByteOrder.nativeOrder(), null);
}
/**
* Wraps the given primitive array for read operations assuming native byte order.
- * @param array the given primitive array. It must be non-null and length ≥ 0.
+ * @param array the given primitive array. It must be non-null.
* @return a new <i>Memory</i> for read operations
*/
static Memory wrap(char[] array) {
Objects.requireNonNull(array, "array must be non-null");
- final long lengthBytes = array.length << Prim.CHAR.shift();
- return BaseWritableMemoryImpl.wrapHeapArray(array, 0L, lengthBytes, true, ByteOrder.nativeOrder(), null);
+ final MemorySegment slice = MemorySegment.ofArray(array).asReadOnly();
+ return BaseWritableMemoryImpl.wrapSegment(slice, ByteOrder.nativeOrder(), null);
}
/**
* Wraps the given primitive array for read operations assuming native byte order.
- * @param array the given primitive array. It must be non-null and length ≥ 0.
+ * @param array the given primitive array. It must be non-null.
* @return a new <i>Memory</i> for read operations
*/
static Memory wrap(short[] array) {
Objects.requireNonNull(array, "arr must be non-null");
- final long lengthBytes = array.length << Prim.SHORT.shift();
- return BaseWritableMemoryImpl.wrapHeapArray(array, 0L, lengthBytes, true, ByteOrder.nativeOrder(), null);
+ final MemorySegment slice = MemorySegment.ofArray(array).asReadOnly();
+ return BaseWritableMemoryImpl.wrapSegment(slice, ByteOrder.nativeOrder(), null);
}
/**
* Wraps the given primitive array for read operations assuming native byte order.
- * @param array the given primitive array. It must be non-null and length ≥ 0.
+ * @param array the given primitive array. It must be non-null.
* @return a new <i>Memory</i> for read operations
*/
static Memory wrap(int[] array) {
Objects.requireNonNull(array, "arr must be non-null");
- final long lengthBytes = array.length << Prim.INT.shift();
- return BaseWritableMemoryImpl.wrapHeapArray(array, 0L, lengthBytes, true, ByteOrder.nativeOrder(), null);
+ final MemorySegment slice = MemorySegment.ofArray(array).asReadOnly();
+ return BaseWritableMemoryImpl.wrapSegment(slice, ByteOrder.nativeOrder(), null);
}
/**
* Wraps the given primitive array for read operations assuming native byte order.
- * @param array the given primitive array. It must be non-null and length ≥ 0.
+ * @param array the given primitive array. It must be non-null.
* @return a new <i>Memory</i> for read operations
*/
static Memory wrap(long[] array) {
Objects.requireNonNull(array, "arr must be non-null");
- final long lengthBytes = array.length << Prim.LONG.shift();
- return BaseWritableMemoryImpl.wrapHeapArray(array, 0L, lengthBytes, true, ByteOrder.nativeOrder(), null);
+ final MemorySegment slice = MemorySegment.ofArray(array).asReadOnly();
+ return BaseWritableMemoryImpl.wrapSegment(slice, ByteOrder.nativeOrder(), null);
}
/**
* Wraps the given primitive array for read operations assuming native byte order.
- * @param array the given primitive array. It must be non-null and length ≥ 0.
+ * @param array the given primitive array. It must be non-null.
* @return a new <i>Memory</i> for read operations
*/
static Memory wrap(float[] array) {
Objects.requireNonNull(array, "arr must be non-null");
- final long lengthBytes = array.length << Prim.FLOAT.shift();
- return BaseWritableMemoryImpl.wrapHeapArray(array, 0L, lengthBytes, true, ByteOrder.nativeOrder(), null);
+ final MemorySegment slice = MemorySegment.ofArray(array).asReadOnly();
+ return BaseWritableMemoryImpl.wrapSegment(slice, ByteOrder.nativeOrder(), null);
}
/**
* Wraps the given primitive array for read operations assuming native byte order.
- * @param array the given primitive array. It must be non-null and length ≥ 0.
+ * @param array the given primitive array. It must be non-null.
* @return a new <i>Memory</i> for read operations
*/
static Memory wrap(double[] array) {
Objects.requireNonNull(array, "arr must be non-null");
- final long lengthBytes = array.length << Prim.DOUBLE.shift();
- return BaseWritableMemoryImpl.wrapHeapArray(array, 0L, lengthBytes, true, ByteOrder.nativeOrder(), null);
+ final MemorySegment slice = MemorySegment.ofArray(array).asReadOnly();
+ return BaseWritableMemoryImpl.wrapSegment(slice, ByteOrder.nativeOrder(), null);
}
- //PRIMITIVE getX() and getXArray()
- /**
- * Gets the boolean value at the given offset
- * @param offsetBytes offset bytes relative to this Memory start
- * @return the boolean at the given offset
- */
- boolean getBoolean(long offsetBytes);
- /**
- * Gets the boolean array at the given offset
- * @param offsetBytes offset bytes relative to this Memory start
- * @param dstArray The preallocated destination array.
- * @param dstOffsetBooleans offset in array units
- * @param lengthBooleans number of array units to transfer
- */
- void getBooleanArray(long offsetBytes, boolean[] dstArray, int dstOffsetBooleans, int lengthBooleans);
+ //PRIMITIVE getX() and getXArray()
/**
* Gets the byte value at the given offset
@@ -326,43 +297,6 @@ public interface Memory extends BaseState {
*/
void getCharArray(long offsetBytes, char[] dstArray, int dstOffsetChars, int lengthChars);
- /**
- * Gets UTF-8 encoded bytes from this Memory, starting at offsetBytes to a length of
- * utf8LengthBytes, decodes them into characters and appends them to the given Appendable.
- * This is specifically designed to reduce the production of intermediate objects (garbage),
- * thus significantly reducing pressure on the JVM Garbage Collector.
- * @param offsetBytes offset bytes relative to the Memory start
- * @param utf8LengthBytes the number of encoded UTF-8 bytes to decode. It is assumed that the
- * caller has the correct number of utf8 bytes required to decode the number of characters
- * to be appended to dst. Characters outside the ASCII range can require 2, 3 or 4 bytes per
- * character to decode.
- * @param dst the destination Appendable to append the decoded characters to.
- * @return the number of characters decoded
- * @throws IOException if dst.append() throws IOException
- * @throws Utf8CodingException in case of malformed or illegal UTF-8 input
- */
- int getCharsFromUtf8(long offsetBytes, int utf8LengthBytes, Appendable dst)
- throws IOException, Utf8CodingException;
-
- /**
- * Gets UTF-8 encoded bytes from this Memory, starting at offsetBytes to a length of
- * utf8LengthBytes, decodes them into characters and appends them to the given StringBuilder.
- * This method does *not* reset the length of the destination StringBuilder before appending
- * characters to it.
- * This is specifically designed to reduce the production of intermediate objects (garbage),
- * thus significantly reducing pressure on the JVM Garbage Collector.
- * @param offsetBytes offset bytes relative to the Memory start
- * @param utf8LengthBytes the number of encoded UTF-8 bytes to decode. It is assumed that the
- * caller has the correct number of utf8 bytes required to decode the number of characters
- * to be appended to dst. Characters outside the ASCII range can require 2, 3 or 4 bytes per
- * character to decode.
- * @param dst the destination StringBuilder to append decoded characters to.
- * @return the number of characters decoded.
- * @throws Utf8CodingException in case of malformed or illegal UTF-8 input
- */
- int getCharsFromUtf8(long offsetBytes, int utf8LengthBytes, StringBuilder dst)
- throws Utf8CodingException;
-
/**
* Gets the double value at the given offset
* @param offsetBytes offset bytes relative to this Memory start
@@ -486,5 +420,3 @@ public interface Memory extends BaseState {
throws IOException;
}
-
-
diff --git a/src/main/java/org/apache/datasketches/memory/Utf8CodingException.java b/src/main/java/org/apache/datasketches/memory/Utf8CodingException.java
deleted file mode 100644
index 895a146..0000000
--- a/src/main/java/org/apache/datasketches/memory/Utf8CodingException.java
+++ /dev/null
@@ -1,129 +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;
-
-/**
- * This exception will be thrown for errors encountered during either the encoding of characters
- * to Utf8 bytes, or the decoding of Utf8 bytes to characters.
- *
- * @author Lee Rhodes
- */
-public final class Utf8CodingException extends RuntimeException {
-
- private static final long serialVersionUID = 1L;
-
- /**
- * A coding exception occured processing UTF_8
- * @param message the error message
- */
- public Utf8CodingException(final String message) {
- super(message);
- }
-
- //DECODE
- /**
- * Exception for a short UTF_8 Decode Byte Sequence
- * @param leadByte the given lead byte
- * @param address the given address
- * @param limit the given limit
- * @param required what is required
- * @return the exception
- */
- public static Utf8CodingException shortUtf8DecodeByteSequence(final byte leadByte, final long address,
- final long limit, final int required) {
- final String s = "Too few Utf8 decode bytes remaining given the leading byte. "
- + shortSeq(leadByte, address, limit, required);
- return new Utf8CodingException(s);
- }
-
- /**
- * Exception for an illegal UTF_8 Decode Byte Sequence
- * @param bytes the illegal byte sequence
- * @return the exception.
- */
- public static Utf8CodingException illegalUtf8DecodeByteSequence(final byte[] bytes) {
- final String s = "Invalid UTF-8 decode byte sequence: " + badBytes(bytes);
- return new Utf8CodingException(s);
- }
-
- //ENCODE
- /**
- * Exception for out-of-memory
- * @return the exception
- */
- public static Utf8CodingException outOfMemory() {
- final String s = "Out-of-memory with characters remaining to be encoded";
- return new Utf8CodingException(s);
- }
-
- /**
- * Exception for an unpaired surrogate
- * @param c The last char to encode is an unpaired surrogate
- * @return the exception plus the unpaired surrogate character
- */
- public static Utf8CodingException unpairedSurrogate(final char c) {
- final String s = "Last char to encode is an unpaired surrogate: 0X"
- + Integer.toHexString(c & 0XFFFF);
- return new Utf8CodingException(s);
- }
-
- /**
- * Exception for a short UTF_8 encode byte length
- * @param remaining The surrogate pair that is short
- * @return the exception plus the surrogate pair that is short
- */
- public static Utf8CodingException shortUtf8EncodeByteLength(final int remaining) {
- final String s = "Too few MemoryImpl bytes to encode a surrogate pair: " + remaining;
- return new Utf8CodingException(s);
- }
-
- /**
- * Exception for an illegal surrogate pair
- * @param c1 the first character of the pair
- * @param c2 the second character of the pair
- * @return the exception plus the illegal pair
- */
- public static Utf8CodingException illegalSurrogatePair(final char c1, final char c2) {
- final String s = "Illegal Surrogate Pair: Char 1: " + Integer.toHexString(c1 & 0XFFFF)
- + ", Char 2: " + Integer.toHexString(c2 & 0XFFFF);
- return new Utf8CodingException(s);
- }
-
- private static String shortSeq(final byte leadByte, final long address, final long limit,
- final int required) {
- final String s = "Lead byte: " + Integer.toHexString(leadByte & 0xFF)
- + ", offset: 0X" + Long.toHexString(address)
- + ", limit: 0X" + Long.toHexString(limit)
- + ", required: " + required;
- return s;
- }
-
- private static String badBytes(final byte[] bytes) {
- final StringBuilder sb = new StringBuilder();
- final int len = bytes.length;
- int i = 0;
- for (; i < (len - 1); i++) {
- sb.append("0X" + Integer.toHexString(bytes[i] & 0XFF)).append(", ");
- }
- sb.append("0X" + Integer.toHexString(bytes[i] & 0XFF));
- return sb.toString();
- }
-
-}
diff --git a/src/main/java/org/apache/datasketches/memory/WritableBuffer.java b/src/main/java/org/apache/datasketches/memory/WritableBuffer.java
new file mode 100644
index 0000000..b0695cf
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/WritableBuffer.java
@@ -0,0 +1,367 @@
+/*
+ * 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;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Objects;
+
+import org.apache.datasketches.memory.internal.BaseWritableBufferImpl;
+
+/**
+ * Defines the writable API for relative positional access to a resource
+ *
+ * @author Lee Rhodes
+ */
+public interface WritableBuffer extends Buffer {
+
+ //BYTE BUFFER
+ /**
+ * Provides a view of the given <i>ByteBuffer</i> for write operations.
+ * The returned <i>WritableBuffer</i> will use the native <i>ByteOrder</i>,
+ * ignoring the byte order of the given <i>ByteBuffer</i>.
+ * This does not change the byte order of data already in the <i>ByteBuffer</i>.
+ * The view starts relative to the ByteBuffer's position (inclusive)
+ * and ends relative to the ByteBuffer's limit (exclusive).
+ * @param byteBuffer the given ByteBuffer. It must be non-null and writable.
+ * @return a new <i>WritableBuffer</i> for write operations on the given <i>ByteBuffer</i>.
+ */
+ static WritableBuffer writableWrap(ByteBuffer byteBuffer) {
+ return writableWrap(byteBuffer, ByteOrder.nativeOrder());
+ }
+
+ /**
+ * Provides a view of the given <i>ByteBuffer</i> for write operations.
+ * The returned <i>WritableBuffer</i> will use the given <i>ByteOrder</i>,
+ * ignoring the byte order of the given <i>ByteBuffer</i>.
+ * However, this does not change the byte order of data already in the <i>ByteBuffer</i>.
+ * The view starts relative to the ByteBuffer's position (inclusive)
+ * and ends relative to the ByteBuffer's limit (exclusive).
+ * @param byteBuffer the given ByteBuffer. It must be non-null and writable.
+ * @param byteOrder the byte order to be used. It must be non-null.
+ * @return a new <i>WritableBuffer</i> for write operations on the given <i>ByteBuffer</i>.
+ */
+ static WritableBuffer writableWrap(ByteBuffer byteBuffer, ByteOrder byteOrder) {
+ Objects.requireNonNull(byteBuffer, "ByteBuffer must not be null");
+ Objects.requireNonNull(byteOrder, "ByteOrder must not be null");
+ if (byteBuffer.isReadOnly()) { throw new ReadOnlyException("ByteBuffer must be writable."); }
+ return BaseWritableBufferImpl.wrapByteBuffer(byteBuffer, false, byteOrder);
+ }
+
+ // NO MAP
+ // NO ALLOCATE DIRECT
+
+ //DUPLICATES
+ /**
+ * Returns a duplicate writable view of this Buffer with the same but independent values of
+ * <i>start</i>, <i>position</i> and <i>end</i>.
+ * <ul>
+ * <li>Returned object's origin = this object's origin</li>
+ * <li>Returned object's <i>start</i> = this object's <i>start</i></li>
+ * <li>Returned object's <i>position</i> = this object's <i>position</i></li>
+ * <li>Returned object's <i>end</i> = this object's <i>end</i></li>
+ * <li>Returned object's <i>capacity</i> = this object' <i>capacityBytes</i></li>
+ * <li>Returned object's <i>start</i>, <i>position</i> and <i>end</i> are mutable and
+ * independent of this object's <i>start</i>, <i>position</i> and <i>end</i></li>
+ * </ul>
+ * @return a duplicate writable view of this Buffer with the same but independent values of
+ * <i>start</i>, <i>position</i> and <i>end</i>.
+ */
+ WritableBuffer writableDuplicate();
+
+ /**
+ * Returns a duplicate writable view of this Buffer with the same but independent values of
+ * <i>start</i>, <i>position</i> and <i>end</i>, but with the specified byteOrder.
+ * <ul>
+ * <li>Returned object's origin = this object's origin</li>
+ * <li>Returned object's <i>start</i> = this object's <i>start</i></li>
+ * <li>Returned object's <i>position</i> = this object's <i>position</i></li>
+ * <li>Returned object's <i>end</i> = this object's <i>end</i></li>
+ * <li>Returned object's <i>capacity</i> = this object' <i>capacityBytes</i></li>
+ * <li>Returned object's <i>start</i>, <i>position</i> and <i>end</i> are mutable and
+ * independent of this object's <i>start</i>, <i>position</i> and <i>end</i></li>
+ * </ul>
+ * @param byteOrder the byte order to be used. It must be non-null.
+ * @return a duplicate writable view of this Buffer with the same but independent values of
+ * <i>start</i>, <i>position</i> and <i>end</i>.
+ */
+ WritableBuffer writableDuplicate(ByteOrder byteOrder);
+
+ //REGIONS
+ /**
+ * A writable region is a writable view of this object.
+ * <ul>
+ * <li>Returned object's origin = this object's <i>position</i></li>
+ * <li>Returned object's <i>start</i> = 0</li>
+ * <li>Returned object's <i>position</i> = 0</li>
+ * <li>Returned object's <i>end</i> = this object's (<i>end</i> - <i>position</i>)</li>
+ * <li>Returned object's <i>capacity</i> = this object's (<i>end</i> - <i>position</i>)</li>
+ * <li>Returned object's <i>start</i>, <i>position</i> and <i>end</i> are mutable and
+ * independent of this object's <i>start</i>, <i>position</i> and <i>end</i></li>
+ * </ul>
+ * @return a new <i>WritableBuffer</i> representing the defined writable region.
+ */
+ default WritableBuffer writableRegion() {
+ return writableRegion(getPosition(), getEnd() - getPosition(), ByteOrder.nativeOrder());
+ }
+
+ /**
+ * A writable region is a writable view of this object.
+ * <ul>
+ * <li>Returned object's origin = this objects' origin + <i>offsetBytes</i></li>
+ * <li>Returned object's <i>start</i> = 0</li>
+ * <li>Returned object's <i>position</i> = 0</li>
+ * <li>Returned object's <i>end</i> = <i>capacityBytes</i></li>
+ * <li>Returned object's <i>capacity</i> = <i>capacityBytes</i></li>
+ * <li>Returned object's <i>start</i>, <i>position</i> and <i>end</i> are mutable and
+ * independent of this object's <i>start</i>, <i>position</i> and <i>end</i></li>
+ * <li>Returned object's byte order = <i>byteOrder</i></li>
+ * </ul>
+ *
+ * <p><b>Note: </b><i>asWritableMemory()</i> and <i>asMemory()</i>
+ * will return the originating <i>Memory</i> byte order.</p>
+ * @param offsetBytes the starting offset with respect to the origin of this <i>WritableBuffer</i>
+ * @param capacityBytes the <i>capacity</i> of the returned region in bytes
+ * @param byteOrder the byte order to be used. It must be non-null.
+ * @return a new <i>WritableBuffer</i> representing the defined writable region
+ * with the given offsetBytes, capacityBytes and byte order.
+ */
+ WritableBuffer writableRegion(long offsetBytes, long capacityBytes, ByteOrder byteOrder);
+
+ //AS WRITABLE MEMORY
+ /**
+ * Convert this WritableBuffer to a WritableMemory.
+ * If this object's capacity is zero, the returned object is effectively immutable and
+ * the backing storage and byte order are unspecified.
+ * @return WritableMemory
+ */
+ default WritableMemory asWritableMemory() {
+ return asWritableMemory(ByteOrder.nativeOrder());
+ }
+
+ /**
+ * Convert this WritableBuffer to a WritableMemory with the given byte order.
+ * If this object's capacity is zero, the returned object is effectively immutable and
+ * the backing storage and byte order are unspecified.
+ * @param byteOrder the byte order to be used. It must be non-null.
+ * @return WritableMemory
+ */
+ WritableMemory asWritableMemory(ByteOrder byteOrder);
+
+ //NO ALLOCATE HEAP VIA AUTOMATIC BYTE ARRAY
+
+ //NO ACCESS PRIMITIVE HEAP ARRAYS for WRITE
+
+ //PRIMITIVE putX() and putXArray()
+
+ /**
+ * Puts the byte value at the current position.
+ * Increments the position by <i>Byte.BYTES</i>.
+ * @param value the value to put
+ */
+ void putByte(byte value);
+
+ /**
+ * Puts the byte value at the given offset.
+ * This does not change the position.
+ * @param offsetBytes offset bytes relative to this <i>WritableMemory</i> start
+ * @param value the value to put
+ */
+ void putByte(long offsetBytes, byte value);
+
+ /**
+ * Puts the byte array at the current position.
+ * Increments the position by <i>Byte.BYTES * (lengthBytes - srcOffsetBytes)</i>.
+ * @param srcArray The source array.
+ * @param srcOffsetBytes offset in array units
+ * @param lengthBytes number of array units to transfer
+ */
+ void putByteArray(byte[] srcArray, int srcOffsetBytes, int lengthBytes);
+
+ /**
+ * Puts the char value at the current position.
+ * Increments the position by <i>Character.BYTES</i>.
+ * @param value the value to put
+ */
+ void putChar(char value);
+
+ /**
+ * Puts the char value at the given offset.
+ * This does not change the position.
+ * @param offsetBytes offset bytes relative to this <i>WritableMemory</i> start
+ * @param value the value to put
+ */
+ void putChar(long offsetBytes, char value);
+
+ /**
+ * Puts the char array at the current position.
+ * Increments the position by <i>Character.BYTES * (lengthChars - srcOffsetChars)</i>.
+ * @param srcArray The source array.
+ * @param srcOffsetChars offset in array units
+ * @param lengthChars number of array units to transfer
+ */
+ void putCharArray(char[] srcArray, int srcOffsetChars, int lengthChars);
+
+ /**
+ * Puts the double value at the current position.
+ * Increments the position by <i>Double.BYTES</i>.
+ * @param value the value to put
+ */
+ void putDouble(double value);
+
+ /**
+ * Puts the double value at the given offset.
+ * This does not change the position.
+ * @param offsetBytes offset bytes relative to this <i>WritableMemory</i> start
+ * @param value the value to put
+ */
+ void putDouble(long offsetBytes, double value);
+
+ /**
+ * Puts the double array at the current position.
+ * Increments the position by <i>Double.BYTES * (lengthDoubles - srcOffsetDoubles)</i>.
+ * @param srcArray The source array.
+ * @param srcOffsetDoubles offset in array units
+ * @param lengthDoubles number of array units to transfer
+ */
+ void putDoubleArray(double[] srcArray, int srcOffsetDoubles, int lengthDoubles);
+
+ /**
+ * Puts the float value at the current position.
+ * Increments the position by <i>Float.BYTES</i>.
+ * @param value the value to put
+ */
+ void putFloat(float value);
+
+ /**
+ * Puts the float value at the given offset.
+ * This does not change the position.
+ * @param offsetBytes offset bytes relative to this <i>WritableMemory</i> start
+ * @param value the value to put
+ */
+ void putFloat(long offsetBytes, float value);
+
+ /**
+ * Puts the float array at the current position.
+ * Increments the position by <i>Float.BYTES * (lengthFloats - srcOffsetFloats)</i>.
+ * @param srcArray The source array.
+ * @param srcOffsetFloats offset in array units
+ * @param lengthFloats number of array units to transfer
+ */
+ void putFloatArray(float[] srcArray, int srcOffsetFloats, int lengthFloats);
+
+ /**
+ * Puts the int value at the current position.
+ * Increments the position by <i>Integer.BYTES</i>.
+ * @param value the value to put
+ */
+ void putInt(int value);
+
+ /**
+ * Puts the int value at the given offset.
+ * This does not change the position.
+ * @param offsetBytes offset bytes relative to this <i>WritableMemory</i> start
+ * @param value the value to put
+ */
+ void putInt(long offsetBytes, int value);
+
+ /**
+ * Puts the int array at the current position.
+ * Increments the position by <i>Integer.BYTES * (lengthInts - srcOffsetInts)</i>.
+ * @param srcArray The source array.
+ * @param srcOffsetInts offset in array units
+ * @param lengthInts number of array units to transfer
+ */
+ void putIntArray(int[] srcArray, int srcOffsetInts, int lengthInts);
+
+ /**
+ * Puts the long value at the current position.
+ * Increments the position by <i>Long.BYTES</i>.
+ * @param value the value to put
+ */
+ void putLong(long value);
+
+ /**
+ * Puts the long value at the given offset.
+ * This does not change the position.
+ * @param offsetBytes offset bytes relative to this <i>WritableMemory</i> start
+ * @param value the value to put
+ */
+ void putLong(long offsetBytes, long value);
+
+ /**
+ * Puts the long array at the current position.
+ * Increments the position by <i>Long.BYTES * (lengthLongs - srcOffsetLongs)</i>.
+ * @param srcArray The source array.
+ * @param srcOffsetLongs offset in array units
+ * @param lengthLongs number of array units to transfer
+ */
+ void putLongArray(long[] srcArray, int srcOffsetLongs, int lengthLongs);
+
+ /**
+ * Puts the short value at the current position.
+ * Increments the position by <i>Short.BYTES</i>.
+ * @param value the value to put
+ */
+ void putShort(short value);
+
+ /**
+ * Puts the short value at the given offset.
+ * This does not change the position.
+ * @param offsetBytes offset bytes relative to this <i>WritableMemory</i> start
+ * @param value the value to put
+ */
+ void putShort(long offsetBytes, short value);
+
+ /**
+ * Puts the short array at the current position.
+ * Increments the position by <i>Short.BYTES * (lengthShorts - srcOffsetShorts)</i>.
+ * @param srcArray The source array.
+ * @param srcOffsetShorts offset in array units
+ * @param lengthShorts number of array units to transfer
+ */
+ void putShortArray(short[] srcArray, int srcOffsetShorts, int lengthShorts);
+
+ // NO ATOMIC METHODS
+
+ //OTHER WRITE METHODS
+
+ /**
+ * Clears all bytes of this Buffer from position to end to zero. The position will be set to end.
+ */
+ void clear();
+
+ //NO clearBits(...)
+
+ /**
+ * Fills this Buffer from position to end with the given byte value.
+ * The position will be set to <i>end</i>.
+ * @param value the given byte value
+ */
+ void fill(byte value);
+
+ //NO fill(offsetBytes, lengthBytes, value)
+
+ //NO setBits(...)
+
+ //OTHER WRITABLE API METHODS
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/WritableMemory.java b/src/main/java/org/apache/datasketches/memory/WritableMemory.java
index 14023e1..4e6d4a5 100644
--- a/src/main/java/org/apache/datasketches/memory/WritableMemory.java
+++ b/src/main/java/org/apache/datasketches/memory/WritableMemory.java
@@ -19,17 +19,15 @@
package org.apache.datasketches.memory;
-import static org.apache.datasketches.memory.internal.Util.negativeCheck;
-import static org.apache.datasketches.memory.internal.Util.zeroCheck;
-
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Objects;
import org.apache.datasketches.memory.internal.BaseWritableMemoryImpl;
-import org.apache.datasketches.memory.internal.Prim;
-import org.apache.datasketches.memory.internal.UnsafeUtil;
+
+import jdk.incubator.foreign.MemorySegment;
+import jdk.incubator.foreign.ResourceScope;
/**
* Defines the writable API for offset access to a resource.
@@ -40,43 +38,32 @@ public interface WritableMemory extends Memory {
//BYTE BUFFER
/**
- * Accesses the given <i>ByteBuffer</i> for write operations. The returned <i>WritableMemory</i> object has
- * the same byte order, as the given <i>ByteBuffer</i>.
- * @param byteBuffer the given <i>ByteBuffer</i>. It must be non-null, with capacity ≥ 0, and writable.
+ * Provides a view of the given <i>ByteBuffer</i> for write operations.
+ * The returned <i>WritableMemory</i> will use the native <i>ByteOrder</i>,
+ * ignoring the byte order of the given <i>ByteBuffer</i>.
+ * @param byteBuffer the given <i>ByteBuffer</i>. It must be non-null and writable.
* @return a new <i>WritableMemory</i> for write operations on the given <i>ByteBuffer</i>.
*/
static WritableMemory writableWrap(ByteBuffer byteBuffer) {
- return writableWrap(byteBuffer, byteBuffer.order(), defaultMemReqSvr);
+ return writableWrap(byteBuffer, ByteOrder.nativeOrder());
}
/**
- * Accesses the given <i>ByteBuffer</i> for write operations. The returned <i>WritableMemory</i> object has
- * the given byte order, ignoring the byte order of the given <i>ByteBuffer</i> for future writes and following reads.
- * However, this does not change the byte order of data already in the <i>ByteBuffer</i>.
- * @param byteBuffer the given <i>ByteBuffer</i>. It must be non-null, with capacity ≥ 0, and writable.
+ * Provides a view of the given <i>ByteBuffer</i> for write operations.
+ * The returned <i>WritableMemory</i> will use the given byte order,
+ * ignoring the byte order of the given <i>ByteBuffer</i>.
+ * This does not change the byte order of data already in the <i>ByteBuffer</i>.
+ * The view starts at zero (inclusive) and ends at the ByteBuffer's capacity (exclusive).
+ * @param byteBuffer the given <i>ByteBuffer</i>. It must be non-null and writable.
* @param byteOrder the byte order to be used. It must be non-null.
* @return a new <i>WritableMemory</i> for write operations on the given <i>ByteBuffer</i>.
*/
static WritableMemory writableWrap(ByteBuffer byteBuffer, ByteOrder byteOrder) {
- return writableWrap(byteBuffer, byteOrder, defaultMemReqSvr);
- }
-
- /**
- * Accesses the given <i>ByteBuffer</i> for write operations. The returned <i>WritableMemory</i> object has
- * the given byte order, ignoring the byte order of the given <i>ByteBuffer</i> for future reads and writes.
- * However, this does not change the byte order of data already in the <i>ByteBuffer</i>.
- * @param byteBuffer the given <i>ByteBuffer</i>. It must be non-null, with capacity ≥ 0, and writable.
- * @param byteOrder the byte order to be used. It must be non-null.
- * @param memReqSvr A user-specified <i>MemoryRequestServer</i>, which may be null.
- * This is a callback mechanism for a user client to request a larger <i>WritableMemory</i>.
- * @return a new <i>WritableMemory</i> for write operations on the given <i>ByteBuffer</i>.
- */
- static WritableMemory writableWrap(ByteBuffer byteBuffer, ByteOrder byteOrder, MemoryRequestServer memReqSvr) {
- Objects.requireNonNull(byteBuffer, "byteBuffer must be non-null");
- Objects.requireNonNull(byteOrder, "byteOrder must be non-null");
- negativeCheck(byteBuffer.capacity(), "byteBuffer.capacity()");
- if (byteBuffer.isReadOnly()) { throw new ReadOnlyException("byteBuffer must be writable."); }
- return BaseWritableMemoryImpl.wrapByteBuffer(byteBuffer, false, byteOrder, memReqSvr);
+ Objects.requireNonNull(byteBuffer, "ByteBuffer must not be null");
+ Objects.requireNonNull(byteOrder, "ByteOrder must not be null");
+ if (byteBuffer.isReadOnly()) { throw new ReadOnlyException("ByteBuffer must be writable."); }
+ ByteBuffer byteBuf = byteBuffer.position(0).limit(byteBuffer.capacity());
+ return BaseWritableMemoryImpl.wrapByteBuffer(byteBuf, false, byteOrder);
}
//MAP
@@ -84,11 +71,11 @@ public interface WritableMemory extends Memory {
* Maps the entire given file into native-ordered WritableMemory for write operations
* Calling this method is equivalent to calling
* {@link #writableMap(File, long, long, ByteOrder) writableMap(file, 0, file.length(), ByteOrder.nativeOrder())}.
- * @param file the given file to map. It must be non-null, with length ≥ 0, and writable.
- * @return WritableMapHandle for managing the mapped Memory.
- * Please read Javadocs for {@link Handle}.
+ * @param file the given file to map. It must be non-null and writable.
+ * @return mapped WritableMemory
+ * @throws Exception
*/
- static WritableMapHandle writableMap(File file) {
+ static WritableMemory writableMap(File file) throws Exception {
return writableMap(file, 0, file.length(), ByteOrder.nativeOrder());
}
@@ -97,20 +84,18 @@ public interface WritableMemory extends Memory {
*
* <p><b>Note:</b> Always qualify this method with the class name, e.g.,
* <i>WritableMemory.map(...)</i>.
- * @param file the given file to map. It must be non-null, writable and length ≥ 0.
+ * @param file the given file to map. It must be non-null and writable.
* @param fileOffsetBytes the position in the given file in bytes. It must not be negative.
* @param capacityBytes the size of the mapped Memory. It must be ≥ 0.
- * @param byteOrder the byte order to be used for the given file. It must be non-null.
- * @return WritableMapHandle for managing the mapped Memory.
- * Please read Javadocs for {@link Handle}.
- */
- static WritableMapHandle writableMap(File file, long fileOffsetBytes, long capacityBytes, ByteOrder byteOrder) {
- Objects.requireNonNull(file, "file must be non-null.");
- Objects.requireNonNull(byteOrder, "byteOrder must be non-null.");
+ * @param byteOrder the byte order to be used. It must be non-null.
+ * @return mapped WritableMemory.
+ * @throws Exception
+ */
+ static WritableMemory writableMap(File file, long fileOffsetBytes, long capacityBytes, ByteOrder byteOrder)
+ throws Exception {
+ Objects.requireNonNull(file, "File must be non-null.");
+ Objects.requireNonNull(byteOrder, "ByteOrder must be non-null.");
if (!file.canWrite()) { throw new ReadOnlyException("file must be writable."); }
- negativeCheck(file.length(), "file.length()");
- negativeCheck(fileOffsetBytes, "fileOffsetBytes");
- negativeCheck(capacityBytes, "capacityBytes");
return BaseWritableMemoryImpl.wrapMap(file, fileOffsetBytes, capacityBytes, false, byteOrder);
}
@@ -118,39 +103,45 @@ public interface WritableMemory extends Memory {
/**
* Allocates and provides access to capacityBytes directly in native (off-heap) memory.
* Native byte order is assumed.
- * The allocated memory will be 8-byte aligned, but may not be page aligned.
+ * The allocated memory will be 8-byte aligned.
*
- * <p><b>NOTE:</b> Native/Direct memory acquired may have garbage in it.
- * It is the responsibility of the using application to clear this memory, if required,
- * and to call <i>close()</i> when done.</p>
+ * <p><b>NOTICE:</b> It is the responsibility of the using application to call <i>close()</i> when done.</p>
*
- * @param capacityBytes the size of the desired memory in bytes. It must be > 0.
- * @return WritableHandle for this off-heap resource.
- * Please read Javadocs for {@link Handle}.
+ * @param capacityBytes the size of the desired memory in bytes.
+ * @param memReqSvr A user-specified MemoryRequestServer, which may be null.
+ * @return WritableMemory for this off-heap, native resource.
*/
- static WritableHandle allocateDirect(long capacityBytes) {
- return allocateDirect(capacityBytes, ByteOrder.nativeOrder(), defaultMemReqSvr);
+ @SuppressWarnings("resource")
+ static WritableMemory allocateDirect(long capacityBytes, MemoryRequestServer memReqSvr) {
+ return allocateDirect(capacityBytes, 8, ResourceScope.newConfinedScope(), ByteOrder.nativeOrder(), memReqSvr);
}
/**
* Allocates and provides access to capacityBytes directly in native (off-heap) memory.
- * The allocated memory will be 8-byte aligned, but may not be page aligned.
+ * The allocated memory will be aligned to the given <i>alignmentBytes</i>.
*
- * <p><b>NOTE:</b> Native/Direct memory acquired may have garbage in it.
- * It is the responsibility of the using application to clear this memory, if required,
- * and to call <i>close()</i> when done.</p>
+ * <p><b>NOTICE:</b> It is the responsibility of the using application to
+ * call <i>close()</i> when done.</p>
*
- * @param capacityBytes the size of the desired memory in bytes. It must be > 0.
- * @param byteOrder the given byte order. It must be non-null.
+ * @param capacityBytes the size of the desired memory in bytes.
+ * @param alignmentBytes requested segment alignment. Typically 1, 2, 4 or 8.
+ * @param scope ResourceScope for the backing MemorySegment.
+ * Typically <i>ResourceScope.newConfinedScope()</i>.
+ * @param byteOrder the byte order to be used. It must be non-null.
* @param memReqSvr A user-specified MemoryRequestServer, which may be null.
* This is a callback mechanism for a user client of direct memory to request more memory.
- * @return WritableHandle for this off-heap resource.
- * Please read Javadocs for {@link Handle}.
- */
- static WritableHandle allocateDirect(long capacityBytes, ByteOrder byteOrder, MemoryRequestServer memReqSvr) {
- Objects.requireNonNull(byteOrder, "byteOrder must be non-null");
- zeroCheck(capacityBytes, "capacityBytes");
- return BaseWritableMemoryImpl.wrapDirect(capacityBytes, byteOrder, memReqSvr);
+ * @return WritableMemory
+ */
+ @SuppressWarnings("resource")
+ static WritableMemory allocateDirect(
+ long capacityBytes,
+ long alignmentBytes,
+ ResourceScope scope,
+ ByteOrder byteOrder,
+ MemoryRequestServer memReqSvr) {
+ Objects.requireNonNull(scope, "ResourceScope must be non-null");
+ Objects.requireNonNull(byteOrder, "ByteOrder must be non-null");
+ return BaseWritableMemoryImpl.wrapDirect(capacityBytes, alignmentBytes, scope, byteOrder, memReqSvr);
}
//REGIONS
@@ -163,8 +154,8 @@ public interface WritableMemory extends Memory {
* <li>Returned object's capacity = <i>capacityBytes</i></li>
* </ul>
*
- * @param offsetBytes the starting offset with respect to this object. It must be ≥ 0.
- * @param capacityBytes the capacity of the returned object in bytes. It must be > 0.
+ * @param offsetBytes the starting offset with respect to this object.
+ * @param capacityBytes the capacity of the returned object in bytes
* @return a new <i>WritableMemory</i> representing the defined writable region.
*/
default WritableMemory writableRegion(long offsetBytes, long capacityBytes) {
@@ -181,9 +172,9 @@ public interface WritableMemory extends Memory {
* <li>Returned object's byte order = <i>byteOrder</i></li>
* </ul>
*
- * @param offsetBytes the starting offset with respect to this object. It must be ≥ 0.
- * @param capacityBytes the capacity of the returned object in bytes. It must be > 0.
- * @param byteOrder the given byte order. It must be non-null.
+ * @param offsetBytes the starting offset with respect to this object
+ * @param capacityBytes the capacity of the returned object in bytes
+ * @param byteOrder the byte order to be used. It must be non-null.
* @return a new <i>WritableMemory</i> representing the defined writable region.
*/
WritableMemory writableRegion(long offsetBytes, long capacityBytes, ByteOrder byteOrder);
@@ -216,7 +207,7 @@ public interface WritableMemory extends Memory {
* <li>Returned object's <i>capacity</i> = this object's capacity</li>
* <li>Returned object's <i>start</i>, <i>position</i> and <i>end</i> are mutable</li>
* </ul>
- * @param byteOrder the given byte order
+ * @param byteOrder the byte order to be used. It must be non-null.
* @return a new <i>WritableBuffer</i> with a view of this WritableMemory
*/
WritableBuffer asWritableBuffer(ByteOrder byteOrder);
@@ -225,34 +216,34 @@ public interface WritableMemory extends Memory {
//ALLOCATE HEAP VIA AUTOMATIC BYTE ARRAY
/**
* Creates on-heap WritableMemory with the given capacity and the native byte order.
- * @param capacityBytes the given capacity in bytes. It must be ≥ 0.
+ * This will utilize the MemoryRequestServer if it has been configured.
+ * @param capacityBytes the given capacity in bytes.
* @return a new WritableMemory for write operations on a new byte array.
*/
static WritableMemory allocate(int capacityBytes) {
- return allocate(capacityBytes, ByteOrder.nativeOrder(), defaultMemReqSvr);
+ return allocate(capacityBytes, ByteOrder.nativeOrder(), null);
}
/**
* Creates on-heap WritableMemory with the given capacity and the given byte order.
- * @param capacityBytes the given capacity in bytes. It must be ≥ 0.
- * @param byteOrder the given byte order to allocate new Memory object with. It must be non-null.
+ * @param capacityBytes the given capacity in bytes.
+ * @param byteOrder the byte order to be used. It must be non-null.
* @return a new WritableMemory for write operations on a new byte array.
*/
static WritableMemory allocate(int capacityBytes, ByteOrder byteOrder) {
- return allocate(capacityBytes, byteOrder, defaultMemReqSvr);
+ return allocate(capacityBytes, byteOrder, null);
}
/**
* Creates on-heap WritableMemory with the given capacity and the given byte order.
- * @param capacityBytes the given capacity in bytes. It must be ≥ 0.
- * @param byteOrder the given byte order to allocate new Memory object with. It must be non-null.
+ * @param capacityBytes the given capacity in bytes.
+ * @param byteOrder the byte order to be used. It must be non-null.
* @param memReqSvr A user-specified <i>MemoryRequestServer</i>, which may be null.
* This is a callback mechanism for a user client to request a larger <i>WritableMemory</i>.
* @return a new WritableMemory for write operations on a new byte array.
*/
static WritableMemory allocate(int capacityBytes, ByteOrder byteOrder, MemoryRequestServer memReqSvr) {
final byte[] arr = new byte[capacityBytes];
- negativeCheck(capacityBytes, "capacityBytes");
return writableWrap(arr, 0, capacityBytes, byteOrder, memReqSvr);
}
@@ -264,11 +255,11 @@ public interface WritableMemory extends Memory {
*
* <p><b>Note:</b> Always qualify this method with the class name, e.g.,
* <i>WritableMemory.wrap(...)</i>.
- * @param array the given primitive array. It must be non-null, with length ≥ 0.
+ * @param array the given primitive array. It must be non-null.
* @return a new WritableMemory for write operations on the given primitive array.
*/
static WritableMemory writableWrap(byte[] array) {
- return writableWrap(array, 0, array.length, ByteOrder.nativeOrder(), defaultMemReqSvr);
+ return writableWrap(array, 0, array.length, ByteOrder.nativeOrder(), null);
}
/**
@@ -276,12 +267,12 @@ public interface WritableMemory extends Memory {
*
* <p><b>Note:</b> Always qualify this method with the class name, e.g.,
* <i>WritableMemory.wrap(...)</i>.
- * @param array the given primitive array. It must be non-null, with length ≥ 0
+ * @param array the given primitive array. It must be non-null
* @param byteOrder the byte order to be used. It must be non-null.
* @return a new WritableMemory for write operations on the given primitive array.
*/
static WritableMemory writableWrap(byte[] array, ByteOrder byteOrder) {
- return writableWrap(array, 0, array.length, byteOrder, defaultMemReqSvr);
+ return writableWrap(array, 0, array.length, byteOrder, null);
}
/**
@@ -290,25 +281,22 @@ public interface WritableMemory extends Memory {
* <p><b>Note:</b> Always qualify this method with the class name, e.g.,
* <i>WritableMemory.wrap(...)</i>.
* @param array the given primitive array. It must be non-null.
- * @param offsetBytes the byte offset into the given array. It must be ≥ 0.
- * @param lengthBytes the number of bytes to include from the given array. It must be ≥ 0.
+ * @param offsetBytes the byte offset into the given array.
+ * @param lengthBytes the number of bytes to include from the given array.
* @param byteOrder the byte order to be used. It must be non-null.
* @return a new WritableMemory for write operations on the given primitive array.
*/
static WritableMemory writableWrap(byte[] array, int offsetBytes, int lengthBytes, ByteOrder byteOrder) {
- return writableWrap(array, offsetBytes, lengthBytes, byteOrder, defaultMemReqSvr);
+ return writableWrap(array, offsetBytes, lengthBytes, byteOrder, null);
}
/**
* Wraps the given primitive array for write operations with the given byte order. If the given
* lengthBytes is zero, backing storage, byte order and read-only status of the returned
* WritableMemory object are unspecified.
- *
- * <p><b>Note:</b> Always qualify this method with the class name, e.g.,
- * <i>WritableMemory.wrap(...)</i>.
* @param array the given primitive array. It must be non-null.
* @param offsetBytes the byte offset into the given array. It must be ≥ 0.
- * @param lengthBytes the number of bytes to include from the given array. It must be ≥ 0.
+ * @param lengthBytes the number of bytes to include from the given array.
* @param byteOrder the byte order to be used. It must be non-null.
* @param memReqSvr A user-specified <i>MemoryRequestServer</i>, which may be null.
* This is a callback mechanism for a user client to request a larger <i>WritableMemory</i>.
@@ -318,106 +306,79 @@ public interface WritableMemory extends Memory {
MemoryRequestServer memReqSvr) {
Objects.requireNonNull(array, "array must be non-null");
Objects.requireNonNull(byteOrder, "byteOrder must be non-null");
- negativeCheck(offsetBytes, "offsetBytes");
- negativeCheck(lengthBytes, "lengthBytes");
- UnsafeUtil.checkBounds(offsetBytes, lengthBytes, array.length);
- return BaseWritableMemoryImpl.wrapHeapArray(array, offsetBytes, lengthBytes, false, byteOrder, memReqSvr);
- }
+ final MemorySegment slice = MemorySegment.ofArray(array).asSlice(offsetBytes, lengthBytes);
+ return BaseWritableMemoryImpl.wrapSegment(slice, ByteOrder.nativeOrder(), memReqSvr);
- /**
- * Wraps the given primitive array for write operations assuming native byte order.
- * @param array the given primitive array. It must be non-null with length ≥ 0.
- * @return a new WritableMemory for write operations on the given primitive array.
- */
- static WritableMemory writableWrap(boolean[] array) {
- Objects.requireNonNull(array, "array must be non-null");
- final long lengthBytes = array.length << Prim.BOOLEAN.shift();
- return BaseWritableMemoryImpl.wrapHeapArray(array, 0, lengthBytes, false, ByteOrder.nativeOrder(), null);
}
/**
* Wraps the given primitive array for write operations assuming native byte order.
- * @param array the given primitive array. It must be non-null with length ≥ 0.
+ * @param array the given primitive array. It must be non-null.
* @return a new WritableMemory for write operations on the given primitive array.
*/
static WritableMemory writableWrap(char[] array) {
Objects.requireNonNull(array, "array must be non-null");
- final long lengthBytes = array.length << Prim.CHAR.shift();
- return BaseWritableMemoryImpl.wrapHeapArray(array, 0L, lengthBytes, false, ByteOrder.nativeOrder(), null);
+ final MemorySegment seg = MemorySegment.ofArray(array);
+ return BaseWritableMemoryImpl.wrapSegment(seg, ByteOrder.nativeOrder(), null);
}
/**
* Wraps the given primitive array for write operations assuming native byte order.
- * @param array the given primitive array. It must be non-null with length ≥ 0.
+ * @param array the given primitive array. It must be non-null.
* @return a new WritableMemory for write operations on the given primitive array.
*/
static WritableMemory writableWrap(short[] array) {
- Objects.requireNonNull(array, "arr must be non-null");
- final long lengthBytes = array.length << Prim.SHORT.shift();
- return BaseWritableMemoryImpl.wrapHeapArray(array, 0L, lengthBytes, false, ByteOrder.nativeOrder(), null);
+ Objects.requireNonNull(array, "array must be non-null");
+ final MemorySegment seg = MemorySegment.ofArray(array);
+ return BaseWritableMemoryImpl.wrapSegment(seg, ByteOrder.nativeOrder(), null);
}
/**
* Wraps the given primitive array for write operations assuming native byte order.
- * @param array the given primitive array. It must be non-null with length ≥ 0.
+ * @param array the given primitive array. It must be non-null.
* @return a new WritableMemory for write operations on the given primitive array.
*/
static WritableMemory writableWrap(int[] array) {
- Objects.requireNonNull(array, "arr must be non-null");
- final long lengthBytes = array.length << Prim.INT.shift();
- return BaseWritableMemoryImpl.wrapHeapArray(array, 0L, lengthBytes, false, ByteOrder.nativeOrder(), null);
+ Objects.requireNonNull(array, "array must be non-null");
+ final MemorySegment seg = MemorySegment.ofArray(array);
+ return BaseWritableMemoryImpl.wrapSegment(seg, ByteOrder.nativeOrder(), null);
}
/**
* Wraps the given primitive array for write operations assuming native byte order.
- * @param array the given primitive array. It must be non-null with length ≥ 0.
+ * @param array the given primitive array. It must be non-null.
* @return a new WritableMemory for write operations on the given primitive array.
*/
static WritableMemory writableWrap(long[] array) {
- Objects.requireNonNull(array, "arr must be non-null");
- final long lengthBytes = array.length << Prim.LONG.shift();
- return BaseWritableMemoryImpl.wrapHeapArray(array, 0L, lengthBytes, false, ByteOrder.nativeOrder(), null);
+ Objects.requireNonNull(array, "array must be non-null");
+ final MemorySegment seg = MemorySegment.ofArray(array);
+ return BaseWritableMemoryImpl.wrapSegment(seg, ByteOrder.nativeOrder(), null);
}
/**
* Wraps the given primitive array for write operations assuming native byte order.
- * @param array the given primitive array. It must be non-null with length ≥ 0.
+ * @param array the given primitive array. It must be non-null.
* @return a new WritableMemory for write operations on the given primitive array.
*/
static WritableMemory writableWrap(float[] array) {
- Objects.requireNonNull(array, "arr must be non-null");
- final long lengthBytes = array.length << Prim.FLOAT.shift();
- return BaseWritableMemoryImpl.wrapHeapArray(array, 0L, lengthBytes, false, ByteOrder.nativeOrder(), null);
+ Objects.requireNonNull(array, "array must be non-null");
+ final MemorySegment seg = MemorySegment.ofArray(array);
+ return BaseWritableMemoryImpl.wrapSegment(seg, ByteOrder.nativeOrder(), null);
}
/**
* Wraps the given primitive array for write operations assuming native byte order.
- * @param array the given primitive array. It must be non-null with length ≥ 0.
+ * @param array the given primitive array. It must be non-null .
* @return a new WritableMemory for write operations on the given primitive array.
*/
static WritableMemory writableWrap(double[] array) {
- Objects.requireNonNull(array, "arr must be non-null");
- final long lengthBytes = array.length << Prim.DOUBLE.shift();
- return BaseWritableMemoryImpl.wrapHeapArray(array, 0L, lengthBytes, false, ByteOrder.nativeOrder(), null);
+ Objects.requireNonNull(array, "array must be non-null");
+ final MemorySegment seg = MemorySegment.ofArray(array);
+ return BaseWritableMemoryImpl.wrapSegment(seg, ByteOrder.nativeOrder(), null);
}
//END OF CONSTRUCTOR-TYPE METHODS
//PRIMITIVE putX() and putXArray()
- /**
- * Puts the boolean value at the given offset
- * @param offsetBytes offset bytes relative to this <i>WritableMemory</i> start
- * @param value the value to put
- */
- void putBoolean(long offsetBytes, boolean value);
-
- /**
- * Puts the boolean array at the given offset
- * @param offsetBytes offset bytes relative to this <i>WritableMemory</i> start
- * @param srcArray The source array.
- * @param srcOffsetBooleans offset in array units
- * @param lengthBooleans number of array units to transfer
- */
- void putBooleanArray(long offsetBytes, boolean[] srcArray, int srcOffsetBooleans, int lengthBooleans);
/**
* Puts the byte value at the given offset
@@ -451,20 +412,6 @@ public interface WritableMemory extends Memory {
*/
void putCharArray(long offsetBytes, char[] srcArray, int srcOffsetChars, int lengthChars);
- /**
- * Encodes characters from the given CharSequence into UTF-8 bytes and puts them into this
- * <i>WritableMemory</i> begining at the given offsetBytes.
- * This is specifically designed to reduce the production of intermediate objects (garbage),
- * thus significantly reducing pressure on the JVM Garbage Collector.
- * @param offsetBytes offset bytes relative to this <i>WritableMemory</i> start
- * @param src The source CharSequence to be encoded and put into this WritableMemory. It is
- * the responsibility of the caller to provide sufficient capacity in this
- * <i>WritableMemory</i> for the encoded Utf8 bytes. Characters outside the ASCII range can
- * require 2, 3 or 4 bytes per character to encode.
- * @return the number of bytes encoded
- */
- long putCharsToUtf8(long offsetBytes, CharSequence src);
-
/**
* Puts the double value at the given offset
* @param offsetBytes offset bytes relative to this <i>WritableMemory</i> start
@@ -545,40 +492,7 @@ public interface WritableMemory extends Memory {
*/
void putShortArray(long offsetBytes, short[] srcArray, int srcOffsetShorts, int lengthShorts);
- //Atomic Methods
- /**
- * Atomically adds the given value to the long located at offsetBytes.
- * @param offsetBytes offset bytes relative to this Memory start
- * @param delta the amount to add
- * @return the the previous value
- */
- long getAndAddLong(long offsetBytes, long delta);
-
- /**
- * Atomically sets the current value at the memory location to the given updated value
- * if and only if the current value {@code ==} the expected value.
- * @param offsetBytes offset bytes relative to this Memory start
- * @param expect the expected value
- * @param update the new value
- * @return {@code true} if successful. False return indicates that
- * the current value at the memory location was not equal to the expected value.
- */
- boolean compareAndSwapLong(long offsetBytes, long expect, long update);
-
- /**
- * Atomically exchanges the given value with the current value located at offsetBytes.
- * @param offsetBytes offset bytes relative to this Memory start
- * @param newValue new value
- * @return the previous value
- */
- long getAndSetLong(long offsetBytes, long newValue);
-
//OTHER WRITE METHODS
- /**
- * Returns the primitive backing array, otherwise null.
- * @return the primitive backing array, otherwise null.
- */
- Object getArray();
/**
* Clears all bytes of this Memory to zero
@@ -623,20 +537,18 @@ public interface WritableMemory extends Memory {
//OTHER WRITABLE API METHODS
/**
- * WritableMemory enables this for ByteBuffer, Heap and Direct Memory backed resources.
- * Map backed resources will always return null.
- * Gets the MemoryRequestServer object, if set, for the above resources to request additional memory.
+ * WritableMemory enables the MemoryRequestServer Direct (Native) Memory backed resources.
+ * Other backed resources will always return null.
+ * Gets the MemoryRequestServer implementation, if set, to request additional memory.
* The user must customize the actions of the MemoryRequestServer by
- * implementing the MemoryRequestServer interface and set using one of these methods:
- * <ul><li>{@link WritableMemory#allocateDirect(long, ByteOrder, MemoryRequestServer)}</li>
- * <li>{@link WritableMemory#allocate(int, ByteOrder, MemoryRequestServer)}</li>
- * <li>{@link WritableMemory#writableWrap(ByteBuffer, ByteOrder, MemoryRequestServer)}</li>
- * </ul>
+ * implementing the MemoryRequestServer interface and set using this method:
+ * {@link WritableMemory#allocateDirect(long, long, ResourceScope, ByteOrder, MemoryRequestServer)}.
* Simple implementation examples include the DefaultMemoryRequestServer in the main tree, as well as
* the ExampleMemoryRequestServerTest and the use with ByteBuffer documented in the DruidIssue11544Test
* in the test tree.
* @return the MemoryRequestServer object or null.
*/
+ @Override
MemoryRequestServer getMemoryRequestServer();
}
diff --git a/src/main/java/org/apache/datasketches/memory/XxHash.java b/src/main/java/org/apache/datasketches/memory/XxHash.java
index d9ec5db..b657b82 100644
--- a/src/main/java/org/apache/datasketches/memory/XxHash.java
+++ b/src/main/java/org/apache/datasketches/memory/XxHash.java
@@ -20,7 +20,6 @@
package org.apache.datasketches.memory;
import static org.apache.datasketches.memory.internal.XxHash64.hash;
-import static org.apache.datasketches.memory.internal.XxHash64.hashBooleans;
import static org.apache.datasketches.memory.internal.XxHash64.hashBytes;
import static org.apache.datasketches.memory.internal.XxHash64.hashChars;
import static org.apache.datasketches.memory.internal.XxHash64.hashDoubles;
@@ -51,20 +50,6 @@ public final class XxHash {
private XxHash() { /* singleton */ }
- /**
- * Hash the given arr starting at the given offset and continuing for the given length using the
- * given seed.
- * @param arr the given array
- * @param offsetBooleans starting at this offset
- * @param lengthBooleans continuing for this length
- * @param seed the given seed
- * @return the hash
- */
- public static long hashBooleanArr(final boolean[] arr, final long offsetBooleans,
- final long lengthBooleans, final long seed) {
- return hashBooleans(arr, offsetBooleans, lengthBooleans, seed);
- }
-
/**
* Hash the given arr starting at the given offset and continuing for the given length using the
* given seed.
@@ -74,8 +59,8 @@ public final class XxHash {
* @param seed the given seed
* @return the hash
*/
- public static long hashByteArr(final byte[] arr, final long offsetBytes,
- final long lengthBytes, final long seed) {
+ public static long hashByteArr(final byte[] arr, final int offsetBytes,
+ final int lengthBytes, final long seed) {
return hashBytes(arr, offsetBytes, lengthBytes, seed);
}
@@ -88,8 +73,8 @@ public final class XxHash {
* @param seed the given seed
* @return the hash
*/
- public static long hashShortArr(final short[] arr, final long offsetShorts,
- final long lengthShorts, final long seed) {
+ public static long hashShortArr(final short[] arr, final int offsetShorts,
+ final int lengthShorts, final long seed) {
return hashShorts(arr, offsetShorts, lengthShorts, seed);
}
@@ -102,8 +87,8 @@ public final class XxHash {
* @param seed the given seed
* @return the hash
*/
- public static long hashCharArr(final char[] arr, final long offsetChars,
- final long lengthChars, final long seed) {
+ public static long hashCharArr(final char[] arr, final int offsetChars,
+ final int lengthChars, final long seed) {
return hashChars(arr, offsetChars, lengthChars, seed);
}
@@ -116,8 +101,8 @@ public final class XxHash {
* @param seed the given seed
* @return the hash
*/
- public static long hashIntArr(final int[] arr, final long offsetInts,
- final long lengthInts, final long seed) {
+ public static long hashIntArr(final int[] arr, final int offsetInts,
+ final int lengthInts, final long seed) {
return hashInts(arr, offsetInts, lengthInts, seed);
}
@@ -130,8 +115,8 @@ public final class XxHash {
* @param seed the given seed
* @return the hash
*/
- public static long hashLongArr(final long[] arr, final long offsetLongs,
- final long lengthLongs, final long seed) {
+ public static long hashLongArr(final long[] arr, final int offsetLongs,
+ final int lengthLongs, final long seed) {
return hashLongs(arr, offsetLongs, lengthLongs, seed);
}
@@ -142,7 +127,7 @@ public final class XxHash {
* @param seed A long valued seed.
* @return the hash.
*/
- public static long hashLong(final long in, final long seed) {
+ public static long hashLong(final int in, final int seed) {
return hash(in, seed);
}
@@ -155,8 +140,8 @@ public final class XxHash {
* @param seed the given seed
* @return the hash
*/
- public static long hashFloatArr(final float[] arr, final long offsetFloats,
- final long lengthFloats, final long seed) {
+ public static long hashFloatArr(final float[] arr, final int offsetFloats,
+ final int lengthFloats, final long seed) {
return hashFloats(arr, offsetFloats, lengthFloats, seed);
}
@@ -169,8 +154,8 @@ public final class XxHash {
* @param seed the given seed
* @return the hash
*/
- public static long hashDoubleArr(final double[] arr, final long offsetDoubles,
- final long lengthDoubles, final long seed) {
+ public static long hashDoubleArr(final double[] arr, final int offsetDoubles,
+ final int lengthDoubles, final long seed) {
return hashDoubles(arr, offsetDoubles, lengthDoubles, seed);
}
@@ -183,8 +168,8 @@ public final class XxHash {
* @param seed the given seed
* @return the hash
*/
- public static long hashString(final String str, final long offsetChars,
- final long lengthChars, final long seed) {
+ public static long hashString(final String str, final int offsetChars,
+ final int lengthChars, final long seed) {
return org.apache.datasketches.memory.internal.XxHash64.hashString(str, offsetChars, lengthChars, seed);
}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/BBNonNativeWritableBufferImpl.java b/src/main/java/org/apache/datasketches/memory/internal/BBNonNativeWritableBufferImpl.java
new file mode 100644
index 0000000..bf65f09
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/BBNonNativeWritableBufferImpl.java
@@ -0,0 +1,52 @@
+/*
+ * 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.ByteOrder;
+
+import org.apache.datasketches.memory.WritableBuffer;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Implementation of {@link WritableBuffer} for ByteBuffer, non-native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class BBNonNativeWritableBufferImpl extends NonNativeWritableBufferImpl {
+
+ BBNonNativeWritableBufferImpl(
+ final MemorySegment seg,
+ final int typeId) {
+ super(seg, typeId);
+ }
+
+ //DUPLICATE
+
+ @Override
+ BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly) | DUPLICATE;
+ return Util.isNativeByteOrder(byteOrder)
+ ? new BBWritableBufferImpl(seg, type)
+ : new BBNonNativeWritableBufferImpl(seg, type);
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/BBNonNativeWritableMemoryImpl.java b/src/main/java/org/apache/datasketches/memory/internal/BBNonNativeWritableMemoryImpl.java
new file mode 100644
index 0000000..a796553
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/BBNonNativeWritableMemoryImpl.java
@@ -0,0 +1,40 @@
+/*
+ * 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 org.apache.datasketches.memory.WritableMemory;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Implementation of {@link WritableMemory} for ByteBuffer, non-native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class BBNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImpl {
+
+ BBNonNativeWritableMemoryImpl(
+ final MemorySegment seg,
+ final int typeId) {
+ super(seg, typeId);
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/BBWritableBufferImpl.java b/src/main/java/org/apache/datasketches/memory/internal/BBWritableBufferImpl.java
new file mode 100644
index 0000000..d9bbebd
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/BBWritableBufferImpl.java
@@ -0,0 +1,52 @@
+/*
+ * 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.ByteOrder;
+
+import org.apache.datasketches.memory.WritableBuffer;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Implementation of {@link WritableBuffer} for ByteBuffer, native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class BBWritableBufferImpl extends NativeWritableBufferImpl {
+
+ BBWritableBufferImpl(
+ final MemorySegment seg,
+ final int typeId) {
+ super(seg, typeId);
+ }
+
+ //DUPLICATE
+
+ @Override
+ BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly) | DUPLICATE;
+ return Util.isNativeByteOrder(byteOrder)
+ ? new BBWritableBufferImpl(seg, type)
+ : new BBNonNativeWritableBufferImpl(seg, type);
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/BBWritableMemoryImpl.java b/src/main/java/org/apache/datasketches/memory/internal/BBWritableMemoryImpl.java
new file mode 100644
index 0000000..95e9a49
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/BBWritableMemoryImpl.java
@@ -0,0 +1,40 @@
+/*
+ * 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 org.apache.datasketches.memory.WritableMemory;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Implementation of {@link WritableMemory} for ByteBuffer, native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class BBWritableMemoryImpl extends NativeWritableMemoryImpl {
+
+ BBWritableMemoryImpl(
+ final MemorySegment seg,
+ final int typeId) {
+ super(seg, typeId);
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/BaseBufferImpl.java b/src/main/java/org/apache/datasketches/memory/internal/BaseBufferImpl.java
new file mode 100644
index 0000000..15ed6b5
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/BaseBufferImpl.java
@@ -0,0 +1,217 @@
+/*
+ * 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 org.apache.datasketches.memory.BaseBuffer;
+import org.apache.datasketches.memory.ReadOnlyException;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * A new positional API. This is different from and simpler than Java BufferImpl positional approach.
+ * <ul><li>All based on longs instead of ints.</li>
+ * <li>Eliminated "mark". Rarely used and confusing with its silent side effects.</li>
+ * <li>The invariants are {@code 0 <= start <= position <= end <= capacity}.</li>
+ * <li>It always starts up as (0, 0, capacity, capacity).</li>
+ * <li>You set (start, position, end) in one call with
+ * {@link #setStartPositionEnd(long, long, long)}</li>
+ * <li>Position can be set directly or indirectly when using the positional get/put methods.
+ * <li>Added incrementPosition(long), which is much easier when you know the increment.</li>
+ * <li>This approach eliminated a number of methods and checks, and has no unseen side effects,
+ * e.g., mark being invalidated.</li>
+ * <li>Clearer method naming (IMHO).</li>
+ * </ul>
+ *
+ * @author Lee Rhodes
+ */
+public abstract class BaseBufferImpl extends BaseStateImpl implements BaseBuffer {
+ private long capacity;
+ private long start = 0;
+ private long pos = 0;
+ private long end;
+
+ //Pass-through ctor
+ BaseBufferImpl(
+ final MemorySegment seg,
+ final int typeId) {
+ super(seg, typeId);
+ capacity = end = seg.byteSize();
+ }
+
+ @Override
+ public final BaseBufferImpl incrementPosition(final long increment) {
+ incrementAndAssertPositionForRead(pos, increment);
+ return this;
+ }
+
+ @Override
+ public final BaseBufferImpl incrementAndCheckPosition(final long increment) {
+ incrementAndCheckPositionForRead(pos, increment);
+ return this;
+ }
+
+ @Override
+ public final long getEnd() {
+ return end;
+ }
+
+ @Override
+ public final long getPosition() {
+ return pos;
+ }
+
+ @Override
+ public final long getStart() {
+ return start;
+ }
+
+ @Override
+ public final long getRemaining() {
+ return end - pos;
+ }
+
+ @Override
+ public final boolean hasRemaining() {
+ return (end - pos) > 0;
+ }
+
+ @Override
+ public final BaseBufferImpl resetPosition() {
+ pos = start;
+ return this;
+ }
+
+ @Override
+ public final BaseBufferImpl setPosition(final long position) {
+ assertInvariants(start, position, end, capacity);
+ pos = position;
+ return this;
+ }
+
+ @Override
+ public final BaseBufferImpl setAndCheckPosition(final long position) {
+ checkInvariants(start, position, end, capacity);
+ pos = position;
+ return this;
+ }
+
+ @Override
+ public final BaseBufferImpl setStartPositionEnd(final long start, final long position,
+ final long end) {
+ assertInvariants(start, position, end, capacity);
+ this.start = start;
+ this.end = end;
+ pos = position;
+ return this;
+ }
+
+ @Override
+ public final BaseBufferImpl setAndCheckStartPositionEnd(final long start, final long position,
+ final long end) {
+ checkInvariants(start, position, end, capacity);
+ this.start = start;
+ this.end = end;
+ pos = position;
+ return this;
+ }
+
+ //RESTRICTED
+ //Position checks are only used for Buffers
+ //asserts are used for primitives, not used at runtime
+ final void incrementAndAssertPositionForRead(final long position, final long increment) {
+ final long newPos = position + increment;
+ assertInvariants(start, newPos, end, capacity);
+ pos = newPos;
+ }
+
+ final void incrementAndAssertPositionForWrite(final long position, final long increment) {
+ assert !isReadOnly() : "BufferImpl is read-only.";
+ final long newPos = position + increment;
+ assertInvariants(start, newPos, end, capacity);
+ pos = newPos;
+ }
+
+ //checks are used for arrays and apply at runtime
+ final void incrementAndCheckPositionForRead(final long position, final long increment) {
+ final long newPos = position + increment;
+ checkInvariants(start, newPos, end, capacity);
+ pos = newPos;
+ }
+
+ final void incrementAndCheckPositionForWrite(final long position, final long increment) {
+ checkValidForWrite();
+ final long newPos = position + increment;
+ checkInvariants(start, newPos, end, capacity);
+ pos = newPos;
+ }
+
+ final void checkValidForWrite() {
+ if (isReadOnly()) {
+ throw new ReadOnlyException("BufferImpl is read-only.");
+ }
+ }
+
+ /**
+ * The invariants equation is: {@code 0 <= start <= position <= end <= capacity}.
+ * If this equation is violated and assertions are enabled,
+ * an <i>AssertionError</i> will be thrown.
+ * @param start the lowest start position
+ * @param pos the current position
+ * @param end the highest position
+ * @param cap the capacity of the backing buffer.
+ */
+ static final void assertInvariants(final long start, final long pos, final long end,
+ final long cap) {
+ assert (start | pos | end | cap | (pos - start) | (end - pos) | (cap - end) ) >= 0L
+ : "Violation of Invariants: "
+ + "start: " + start
+ + " <= pos: " + pos
+ + " <= end: " + end
+ + " <= cap: " + cap
+ + "; (pos - start): " + (pos - start)
+ + ", (end - pos): " + (end - pos)
+ + ", (cap - end): " + (cap - end);
+ }
+
+ /**
+ * The invariants equation is: {@code 0 <= start <= position <= end <= capacity}.
+ * If this equation is violated an <i>IllegalArgumentException</i> will be thrown.
+ * @param start the lowest start position
+ * @param pos the current position
+ * @param end the highest position
+ * @param cap the capacity of the backing buffer.
+ */
+ static final void checkInvariants(final long start, final long pos, final long end,
+ final long cap) {
+ if ((start | pos | end | cap | (pos - start) | (end - pos) | (cap - end) ) < 0L) {
+ throw new IllegalArgumentException(
+ "Violation of Invariants: "
+ + "start: " + start
+ + " <= pos: " + pos
+ + " <= end: " + end
+ + " <= cap: " + cap
+ + "; (pos - start): " + (pos - start)
+ + ", (end - pos): " + (end - pos)
+ + ", (cap - end): " + (cap - end)
+ );
+ }
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/BaseStateImpl.java b/src/main/java/org/apache/datasketches/memory/internal/BaseStateImpl.java
new file mode 100644
index 0000000..97dd075
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/BaseStateImpl.java
@@ -0,0 +1,407 @@
+/*
+ * 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 static jdk.incubator.foreign.MemoryAccess.getByteAtOffset;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+import org.apache.datasketches.memory.BaseState;
+import org.apache.datasketches.memory.MemoryRequestServer;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Keeps key configuration state for MemoryImpl and BufferImpl plus some common static variables
+ * and check methods.
+ *
+ * @author Lee Rhodes
+ */
+@SuppressWarnings("restriction")
+public abstract class BaseStateImpl implements BaseState {
+ public static final String JDK; //must be at least "1.8"
+ public static final int JDK_MAJOR; //8, 11, 12, etc
+
+ public static final int BOOLEAN_SHIFT = 0;
+ public static final int BYTE_SHIFT = 0;
+ public static final long SHORT_SHIFT = 1;
+ public static final long CHAR_SHIFT = 1;
+ public static final long INT_SHIFT = 2;
+ public static final long LONG_SHIFT = 3;
+ public static final long FLOAT_SHIFT = 2;
+ public static final long DOUBLE_SHIFT = 3;
+
+ public static final ByteOrder NATIVE_BYTE_ORDER = ByteOrder.nativeOrder();
+ public static final ByteOrder NON_NATIVE_BYTE_ORDER;
+
+ //class type IDs.
+ // 0000 0XXX
+ public static final int READONLY = 1;
+ public static final int REGION = 1 << 1;
+ public static final int DUPLICATE = 1 << 2; //for Buffer only
+
+ // 000X X000
+ public static final int HEAP = 0;
+ public static final int DIRECT = 1 << 3;
+ public static final int MAP = 1 << 4; //Map is always Direct also
+
+ // 00X0 0000
+ public static final int NATIVE = 0;
+ public static final int NONNATIVE = 1 << 5;
+
+ // 0X00 0000
+ public static final int MEMORY = 0;
+ public static final int BUFFER = 1 << 6;
+
+ // X000 0000
+ public static final int BYTEBUF = 1 << 7;
+
+ final MemorySegment seg;
+ final int typeId;
+ MemoryRequestServer memReqSvr;
+
+ static {
+ final String jdkVer = System.getProperty("java.version");
+ final int[] p = parseJavaVersion(jdkVer);
+ JDK = p[0] + "." + p[1];
+ JDK_MAJOR = (p[0] == 1) ? p[1] : p[0];
+
+ NON_NATIVE_BYTE_ORDER = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN
+ ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
+ }
+
+ public BaseStateImpl(final MemorySegment seg, final int typeId) {
+ this.seg = seg;
+ this.typeId = typeId;
+ }
+
+ /**
+ * Assert the requested offset and length against the allocated size.
+ * The invariants equation is: {@code 0 <= reqOff <= reqLen <= reqOff + reqLen <= allocSize}.
+ * If this equation is violated and assertions are enabled, an {@link AssertionError} will
+ * be thrown.
+ * @param reqOff the requested offset
+ * @param reqLen the requested length
+ * @param allocSize the allocated size.
+ */
+ public static void assertBounds(final long reqOff, final long reqLen, final long allocSize) {
+ assert ((reqOff | reqLen | (reqOff + reqLen) | (allocSize - (reqOff + reqLen))) >= 0) :
+ "reqOffset: " + reqOff + ", reqLength: " + reqLen
+ + ", (reqOff + reqLen): " + (reqOff + reqLen) + ", allocSize: " + allocSize;
+ }
+
+ /**
+ * Check the requested offset and length against the allocated size.
+ * The invariants equation is: {@code 0 <= reqOff <= reqLen <= reqOff + reqLen <= allocSize}.
+ * If this equation is violated an {@link IllegalArgumentException} will be thrown.
+ * @param reqOff the requested offset
+ * @param reqLen the requested length
+ * @param allocSize the allocated size.
+ */
+ public static void checkBounds(final long reqOff, final long reqLen, final long allocSize) {
+ if ((reqOff | reqLen | (reqOff + reqLen) | (allocSize - (reqOff + reqLen))) < 0) {
+ throw new IllegalArgumentException(
+ "reqOffset: " + reqOff + ", reqLength: " + reqLen
+ + ", (reqOff + reqLen): " + (reqOff + reqLen) + ", allocSize: " + allocSize);
+ }
+ }
+
+ @SuppressWarnings("resource")
+ @Override
+ public void close() {
+ if (seg != null && seg.scope().isAlive() && !seg.scope().isImplicit()) {
+ if (seg.isNative() || seg.isMapped()) {
+ seg.scope().close();
+ }
+ }
+ }
+
+ //Byte Order Related
+
+ @Override
+ public final ByteOrder getTypeByteOrder() {
+ return isNonNativeType() ? NON_NATIVE_BYTE_ORDER : ByteOrder.nativeOrder();
+ }
+
+ /**
+ * Returns true if the given byteOrder is the same as the native byte order.
+ * @param byteOrder the given byte order
+ * @return true if the given byteOrder is the same as the native byte order.
+ */
+ public static boolean isNativeByteOrder(final ByteOrder byteOrder) {
+ if (byteOrder == null) {
+ throw new IllegalArgumentException("ByteOrder parameter cannot be null.");
+ }
+ return ByteOrder.nativeOrder() == byteOrder;
+ }
+
+ @Override
+ public final boolean isByteOrderCompatible(final ByteOrder byteOrder) {
+ final ByteOrder typeBO = getTypeByteOrder();
+ return typeBO == ByteOrder.nativeOrder() && typeBO == byteOrder;
+ }
+
+ @Override
+ public final boolean equalTo(final long thisOffsetBytes, final Object that,
+ final long thatOffsetBytes, final long lengthBytes) {
+ return that instanceof BaseStateImpl
+ ? CompareAndCopy.equals(seg, thisOffsetBytes, ((BaseStateImpl) that).seg, thatOffsetBytes, lengthBytes)
+ : false;
+ }
+
+ @Override
+ public ByteBuffer asByteBuffer() {
+ return seg.asByteBuffer();
+ }
+
+ @Override
+ public MemorySegment asMemorySegment() {
+ MemorySegment arrSeg = MemorySegment.ofArray(new byte[(int)seg.byteSize()]);
+ arrSeg.copyFrom(seg);
+ return arrSeg;
+ }
+
+ @Override
+ public final long getCapacity() {
+ return seg.byteSize();
+ }
+
+ @Override
+ public MemoryRequestServer getMemoryRequestServer() {
+ return memReqSvr;
+ }
+
+ @Override
+ public boolean hasMemoryRequestServer() {
+ return memReqSvr != null;
+ }
+
+ @Override
+ public void setMemoryRequestServer(final MemoryRequestServer memReqSvr) {
+ this.memReqSvr = memReqSvr;
+ }
+
+ int getTypeId() {
+ return typeId;
+ }
+
+ @Override
+ public final long xxHash64(final long offsetBytes, final long lengthBytes, final long seed) {
+ return XxHash64.hash(seg, offsetBytes, lengthBytes, seed);
+ }
+
+ @Override
+ public final long xxHash64(final long in, final long seed) {
+ return XxHash64.hash(in, seed);
+ }
+
+ @Override
+ public final boolean hasByteBuffer() {
+ return isByteBufferType();
+ }
+
+ @Override
+ public final boolean isDirect() {
+ return seg.isNative();
+ }
+
+ @Override
+ public final boolean isSameResource(final Object that) {
+ return (this == that);
+ }
+
+ //TYPE ID Management
+
+ @Override
+ public final boolean isReadOnly() {
+ return (getTypeId() & READONLY) > 0;
+ }
+
+ final static int setReadOnlyType(final int type, final boolean readOnly) {
+ return (type & ~READONLY) | (readOnly ? READONLY : 0);
+ }
+
+ final boolean isRegionType() {
+ return (getTypeId() & REGION) > 0;
+ }
+
+ final boolean isDuplicateType() {
+ return (getTypeId() & DUPLICATE) > 0;
+ }
+
+ final boolean isMemoryType() {
+ return (getTypeId() & BUFFER) == 0;
+ }
+
+ final boolean isBufferType() {
+ return (getTypeId() & BUFFER) > 0;
+ }
+
+ final boolean isNativeType() {
+ return (getTypeId() & NONNATIVE) == 0;
+ }
+
+ final boolean isNonNativeType() {
+ return (getTypeId() & NONNATIVE) > 0;
+ }
+
+ final boolean isHeapType() {
+ return (getTypeId() >>> 3 & 3) == 0;
+ }
+
+ final boolean isDirectType() {
+ return (getTypeId() & DIRECT) > 0;
+ }
+
+ final boolean isMapType() {
+ return (getTypeId() & MAP) > 0;
+ }
+
+ final boolean isByteBufferType() {
+ return (getTypeId() & BYTEBUF) > 0;
+ }
+
+ //TO STRING
+ /**
+ * Decodes the resource type. This is primarily for debugging.
+ * @param typeId the given typeId
+ * @return a human readable string.
+ */
+ public static final String typeDecode(final int typeId) {
+ final StringBuilder sb = new StringBuilder();
+ final int group1 = typeId & 0x7;
+ switch (group1) {
+ case 1 : sb.append("ReadOnly, "); break;
+ case 2 : sb.append("Region, "); break;
+ case 3 : sb.append("ReadOnly Region, "); break;
+ case 4 : sb.append("Duplicate, "); break;
+ case 5 : sb.append("ReadOnly Duplicate, "); break;
+ case 6 : sb.append("Region Duplicate, "); break;
+ case 7 : sb.append("ReadOnly Region Duplicate, "); break;
+ default: break;
+ }
+ final int group2 = (typeId >>> 3) & 0x3;
+ switch (group2) {
+ case 0 : sb.append("Heap, "); break;
+ case 1 : sb.append("Direct, "); break;
+ case 2 : sb.append("Map, "); break;
+ default: break;
+ }
+ if ((typeId & BYTEBUF) > 0) { sb.append("ByteBuffer, "); }
+ final int group3 = (typeId >>> 5) & 0x1;
+ switch (group3) {
+ case 0 : sb.append("Native, "); break;
+ case 1 : sb.append("NonNative, "); break;
+ default: break;
+ }
+ final int group4 = (typeId >>> 6) & 0x1;
+ switch (group4) {
+ case 0 : sb.append("Memory"); break;
+ case 1 : sb.append("Buffer"); break;
+ default: break;
+ }
+ return sb.toString();
+ }
+
+ @Override
+ public final String toHexString(final String header, final long offsetBytes,
+ final int lengthBytes) {
+ final String klass = this.getClass().getSimpleName();
+ final String s1 = String.format("(..., %d, %d)", offsetBytes, lengthBytes);
+ final long hcode = hashCode() & 0XFFFFFFFFL;
+ final String call = ".toHexString" + s1 + ", hashCode: " + hcode;
+ final StringBuilder sb = new StringBuilder();
+ sb.append("### ").append(klass).append(" SUMMARY ###").append(LS);
+ sb.append("Header Comment : ").append(header).append(LS);
+ sb.append("Call Parameters : ").append(call);
+ return toHex(this, sb.toString(), offsetBytes, lengthBytes);
+ }
+
+ /**
+ * Returns a formatted hex string of an area of this object.
+ * Used primarily for testing.
+ * @param state the BaseStateImpl
+ * @param preamble a descriptive header
+ * @param offsetBytes offset bytes relative to the MemoryImpl start
+ * @param lengthBytes number of bytes to convert to a hex string
+ * @return a formatted hex string in a human readable array
+ */
+ static final String toHex(final BaseStateImpl state, final String preamble, final long offsetBytes,
+ final int lengthBytes) {
+ final MemorySegment seg = state.seg;
+ final long capacity = seg.byteSize();
+ checkBounds(offsetBytes, lengthBytes, capacity);
+ final StringBuilder sb = new StringBuilder();
+ final String segHCStr = "" + (seg.hashCode() & 0XFFFFFFFFL);
+ final MemoryRequestServer memReqSvr = state.getMemoryRequestServer();
+ final String memReqStr = memReqSvr != null
+ ? memReqSvr.getClass().getSimpleName() + ", " + (memReqSvr.hashCode() & 0XFFFFFFFFL)
+ : "null";
+ sb.append(preamble).append(LS);
+ sb.append("MemorySegment hashCode : ").append(segHCStr).append(LS);
+ sb.append("Capacity : ").append(capacity).append(LS);
+ sb.append("MemReqSvr, hashCode : ").append(memReqStr).append(LS);
+ sb.append("Read Only : ").append(state.isReadOnly()).append(LS);
+ sb.append("Type Byte Order : ").append(state.getTypeByteOrder().toString()).append(LS);
+ sb.append("Native Byte Order : ").append(ByteOrder.nativeOrder().toString()).append(LS);
+ sb.append("JDK Runtime Version : ").append(JDK).append(LS);
+ //Data detail
+ sb.append("Data, littleEndian : 0 1 2 3 4 5 6 7");
+
+ for (long i = 0; i < lengthBytes; i++) {
+ final int b = getByteAtOffset(seg, offsetBytes + i) & 0XFF;
+ if (i % 8 == 0) { //row header
+ sb.append(String.format("%n%20s: ", offsetBytes + i));
+ }
+ sb.append(String.format("%02x ", b));
+ }
+ sb.append(LS);
+
+ return sb.toString();
+ }
+
+ /**
+ * Returns first two number groups of the java version string.
+ * @param jdkVer the java version string from System.getProperty("java.version").
+ * @return first two number groups of the java version string.
+ */
+ public static int[] parseJavaVersion(final String jdkVer) {
+ final int p0, p1;
+ try {
+ String[] parts = jdkVer.trim().split("[^0-9\\.]");//grab only number groups and "."
+ parts = parts[0].split("\\."); //split out the number groups
+ p0 = Integer.parseInt(parts[0]); //the first number group
+ p1 = (parts.length > 1) ? Integer.parseInt(parts[1]) : 0; //2nd number group, or 0
+ } catch (final NumberFormatException | ArrayIndexOutOfBoundsException e) {
+ throw new IllegalArgumentException("Improper Java -version string: " + jdkVer + "\n" + e);
+ }
+ checkJavaVersion(jdkVer, p0, p1);
+ return new int[] {p0, p1};
+ }
+
+ public static void checkJavaVersion(final String jdkVer, final int p0, final int p1) {
+ if ( (p0 < 1) || ((p0 == 1) && (p1 < 8)) || (p0 > 13) ) {
+ throw new IllegalArgumentException(
+ "Unsupported JDK Major Version, must be 17; " + jdkVer);
+ }
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/BaseWritableBufferImpl.java b/src/main/java/org/apache/datasketches/memory/internal/BaseWritableBufferImpl.java
new file mode 100644
index 0000000..532f296
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/BaseWritableBufferImpl.java
@@ -0,0 +1,450 @@
+/*
+ * 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 static java.nio.channels.FileChannel.MapMode.READ_ONLY;
+import static java.nio.channels.FileChannel.MapMode.READ_WRITE;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.channels.FileChannel;
+import java.util.Objects;
+
+import org.apache.datasketches.memory.Buffer;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.ReadOnlyException;
+import org.apache.datasketches.memory.WritableBuffer;
+import org.apache.datasketches.memory.WritableMemory;
+
+import jdk.incubator.foreign.MemoryAccess;
+import jdk.incubator.foreign.MemorySegment;
+import jdk.incubator.foreign.ResourceScope;
+
+/*
+ * Developer notes: The heavier methods, such as put/get arrays, duplicate, region, clear, fill,
+ * compareTo, etc., use hard checks (check*() and incrementAndCheck*() methods), which execute at
+ * runtime and throw exceptions if violated. The cost of the runtime checks are minor compared to
+ * the rest of the work these methods are doing.
+ *
+ * <p>The light weight methods, such as put/get primitives, use asserts (assert*() and
+ * incrementAndAssert*() methods), which only execute when asserts are enabled and JIT will remove
+ * them entirely from production runtime code. The offset versions of the light weight methods will
+ * simplify to a single unsafe call, which is further simplified by JIT to an intrinsic that is
+ * often a single CPU instruction.
+ */
+
+/**
+ * Common base of native-ordered and non-native-ordered {@link WritableBuffer} implementations.
+ * Contains methods which are agnostic to the byte order.
+ */
+@SuppressWarnings("restriction")
+public abstract class BaseWritableBufferImpl extends BaseBufferImpl implements WritableBuffer {
+
+ //Pass-through ctor
+ BaseWritableBufferImpl(
+ final MemorySegment seg,
+ final int typeId) {
+ super(seg, typeId);
+ }
+
+ //HEAP
+
+ public static WritableBuffer wrapSegment(
+ final MemorySegment seg,
+ final ByteOrder byteOrder,
+ final MemoryRequestServer memReqSvr) {
+ int type = BUFFER
+ | (seg.isReadOnly() ? READONLY : 0);
+ if (byteOrder == ByteOrder.nativeOrder()) {
+ type |= NATIVE;
+ return new HeapWritableBufferImpl(seg, type, memReqSvr);
+ }
+ type |= NONNATIVE;
+ return new HeapNonNativeWritableBufferImpl(seg, type, memReqSvr);
+ }
+
+
+ //BYTE BUFFER
+
+ public static WritableBuffer wrapByteBuffer(
+ final ByteBuffer byteBuffer,
+ final boolean localReadOnly,
+ final ByteOrder byteOrder) {
+ ByteBuffer byteBuf = localReadOnly ? byteBuffer.asReadOnlyBuffer() : byteBuffer;
+ MemorySegment seg = MemorySegment.ofByteBuffer(byteBuf); //from Position to limit
+ int type = BUFFER | BYTEBUF
+ | (localReadOnly ? READONLY : 0)
+ | (seg.isMapped() ? MAP : 0)
+ | (seg.isNative() ? DIRECT : 0);
+ if (byteOrder == ByteOrder.nativeOrder()) {
+ type |= NATIVE;
+ return new BBWritableBufferImpl(seg, type);
+ }
+ type |= NONNATIVE;
+ return new BBNonNativeWritableBufferImpl(seg, type);
+ }
+
+ //MAP
+ /**
+ * The static constructor that chooses the correct Map leaf node based on the byte order.
+ * @param file the file being wrapped. It must be non-null with length > 0.
+ * @param fileOffsetBytes the file offset bytes
+ * @param capacityBytes the requested capacity of the memory mapped region. It must be > 0
+ * @param localReadOnly the requested read-only state
+ * @param byteOrder the requested byte-order
+ * @return this class constructed via the leaf node.
+ * @throws Exception
+ */
+ @SuppressWarnings("resource")
+ public static WritableBuffer wrapMap(final File file, final long fileOffsetBytes,
+ final long capacityBytes, final boolean localReadOnly, final ByteOrder byteOrder)
+ throws Exception {
+ Objects.requireNonNull(file, "File must be non-null");
+ Objects.requireNonNull(byteOrder, "ByteOrder must be non-null");
+ FileChannel.MapMode mapMode = (localReadOnly) ? READ_ONLY : READ_WRITE;
+ MemorySegment seg;
+ try {
+ seg = MemorySegment.mapFile(file.toPath(), fileOffsetBytes, capacityBytes, mapMode,
+ ResourceScope.newConfinedScope()); }
+ catch (final IllegalArgumentException | IllegalStateException | UnsupportedOperationException
+ | IOException | SecurityException e) { throw e; }
+ final boolean nativeBOType = Util.isNativeByteOrder(byteOrder);
+ final int type = BUFFER | MAP | DIRECT
+ | (localReadOnly ? READONLY : 0)
+ | (nativeBOType ? NATIVE : NONNATIVE);
+ return Util.isNativeByteOrder(byteOrder)
+ ? new MapWritableBufferImpl(seg, type)
+ : new MapNonNativeWritableBufferImpl(seg, type);
+ }
+
+ //NO DIRECTS
+ //REGIONS
+
+ @Override
+ public Buffer region(final long offsetBytes, final long capacityBytes, final ByteOrder byteOrder) {
+ return regionImpl(offsetBytes, capacityBytes, true, byteOrder);
+ }
+
+ @Override
+ public WritableBuffer writableRegion(final long offsetBytes, final long capacityBytes, final ByteOrder byteOrder) {
+ if (this.isReadOnly()) {
+ throw new ReadOnlyException("Cannot create a writable region from a read-only Memory.");
+ }
+ return regionImpl(offsetBytes, capacityBytes, false, byteOrder);
+ }
+
+ private WritableBuffer regionImpl(
+ final long offsetBytes,
+ final long capacityBytes,
+ final boolean localReadOnly,
+ final ByteOrder byteOrder) {
+ Objects.requireNonNull(byteOrder, "byteOrder must be non-null.");
+ final boolean readOnly = isReadOnly() || localReadOnly || seg.isReadOnly();
+ final MemorySegment slice = seg.asSlice(offsetBytes, capacityBytes);
+ final boolean duplicateType = isDuplicateType();
+ final boolean mapType = seg.isMapped();
+ final boolean directType = seg.isNative();
+ final boolean nativeBOType = Util.isNativeByteOrder(byteOrder);
+ final boolean byteBufferType = isByteBufferType();
+ final int type = BUFFER | REGION
+ | (readOnly ? READONLY : 0)
+ | (duplicateType ? DUPLICATE : 0)
+ | (mapType ? MAP : 0)
+ | (directType ? DIRECT : 0)
+ | (nativeBOType ? NATIVE : NONNATIVE)
+ | (byteBufferType ? BYTEBUF : 0);
+ if (byteBufferType) {
+ if (nativeBOType) { return new BBWritableBufferImpl(slice, type); }
+ return new BBNonNativeWritableBufferImpl(slice, type);
+ }
+ if (mapType) {
+ if (nativeBOType) { return new MapWritableBufferImpl(slice, type); }
+ return new MapNonNativeWritableBufferImpl(slice, type);
+ }
+ if (directType) {
+ if (nativeBOType) { return new DirectWritableBufferImpl(slice, type, memReqSvr); }
+ return new DirectNonNativeWritableBufferImpl(slice, type, memReqSvr);
+ }
+ //else heap type
+ if (nativeBOType) { return new HeapWritableBufferImpl(slice, type, memReqSvr); }
+ return new HeapNonNativeWritableBufferImpl(slice, type, memReqSvr);
+ }
+
+ //DUPLICATES
+ @Override
+ public Buffer duplicate() {
+ return writableDuplicateImpl(true, getTypeByteOrder());
+ }
+
+ @Override
+ public Buffer duplicate(final ByteOrder byteOrder) {
+ return writableDuplicateImpl(true, byteOrder);
+ }
+
+ @Override
+ public WritableBuffer writableDuplicate() {
+ if (isReadOnly()) {
+ throw new ReadOnlyException("Cannot create a writable duplicate from a read-only Buffer.");
+ }
+ return writableDuplicateImpl(false, getTypeByteOrder());
+ }
+
+ @Override
+ public WritableBuffer writableDuplicate(final ByteOrder byteOrder) {
+ if (isReadOnly()) {
+ throw new ReadOnlyException("Cannot create a writable duplicate from a read-only Buffer.");
+ }
+ return writableDuplicateImpl(false, byteOrder);
+ }
+
+ private WritableBuffer writableDuplicateImpl(final boolean localReadOnly, final ByteOrder byteOrder) {
+ final boolean readOnly = isReadOnly() || localReadOnly;
+ final WritableBuffer wbuf = toDuplicate(readOnly, byteOrder);
+ wbuf.setStartPositionEnd(getStart(), getPosition(), getEnd());
+ return wbuf;
+ }
+
+ abstract BaseWritableBufferImpl toDuplicate(boolean readOnly, ByteOrder byteOrder);
+
+ //AS MEMORY
+ @Override
+ public Memory asMemory(final ByteOrder byteOrder) {
+ return asWritableMemory(true, byteOrder);
+ }
+
+ @Override
+ public WritableMemory asWritableMemory(final ByteOrder byteOrder) {
+ if (isReadOnly()) {
+ throw new ReadOnlyException(
+ "Cannot create a writable Memory from a read-only Buffer.");
+ }
+ return asWritableMemory(false, byteOrder);
+ }
+
+ private WritableMemory asWritableMemory(final boolean localReadOnly, final ByteOrder byteOrder) {
+ Objects.requireNonNull(byteOrder, "byteOrder must be non-null");
+ final boolean readOnly = isReadOnly() || localReadOnly;
+ final boolean duplicateType = isDuplicateType();
+ final boolean mapType = seg.isMapped();
+ final boolean directType = seg.isNative();
+ final boolean nativeBOType = Util.isNativeByteOrder(byteOrder);
+ final boolean byteBufferType = isByteBufferType();
+ final int type = MEMORY
+ | (readOnly ? READONLY : 0)
+ | (duplicateType ? DUPLICATE : 0)
+ | (mapType ? MAP : 0)
+ | (directType ? DIRECT : 0)
+ | (nativeBOType ? NATIVE : NONNATIVE)
+ | (byteBufferType ? BYTEBUF : 0);
+ WritableMemory wmem;
+ if (byteBufferType) {
+ if (nativeBOType) {
+ wmem = new BBWritableMemoryImpl(seg, type);
+ } else {
+ wmem = new BBNonNativeWritableMemoryImpl(seg, type);
+ }
+ }
+ if (mapType) {
+ if (nativeBOType) {
+ wmem = new MapWritableMemoryImpl(seg, type);
+ } else {
+ wmem = new MapNonNativeWritableMemoryImpl(seg, type);
+ }
+ }
+ if (directType) {
+ if (nativeBOType) {
+ wmem = new DirectWritableMemoryImpl(seg, type, memReqSvr);
+ } else {
+ wmem = new DirectNonNativeWritableMemoryImpl(seg, type, memReqSvr);
+ }
+ }
+ //else heap type
+ if (nativeBOType) {
+ wmem = new HeapWritableMemoryImpl(seg, type, memReqSvr);
+ } else {
+ wmem = new HeapNonNativeWritableMemoryImpl(seg, type, memReqSvr);
+ }
+ return wmem;
+ }
+
+ //PRIMITIVE getX() and getXArray()
+
+ @Override
+ public final byte getByte() {
+ final long pos = getPosition();
+ final byte aByte = MemoryAccess.getByteAtOffset(seg, pos);
+ setPosition(pos + Byte.BYTES);
+ return aByte;
+ }
+
+ @Override
+ public final byte getByte(final long offsetBytes) {
+ return MemoryAccess.getByteAtOffset(seg, offsetBytes);
+ }
+
+ @Override
+ public final void getByteArray(final byte[] dstArray, final int dstOffsetBytes,
+ final int lengthBytes) {
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetBytes, lengthBytes);
+ final long pos = getPosition();
+ MemorySegment srcSlice = seg.asSlice(pos, lengthBytes);
+ dstSlice.copyFrom(srcSlice);
+ setPosition(pos + lengthBytes);
+ }
+
+ //PRIMITIVE getX() Native Endian (used by both endians)
+
+ final char getNativeOrderedChar() {
+ final long pos = getPosition();
+ final char aChar = MemoryAccess.getCharAtOffset(seg, pos);
+ setPosition(pos + Character.BYTES);
+ return aChar;
+ }
+
+ final char getNativeOrderedChar(final long offsetBytes) {
+ return MemoryAccess.getCharAtOffset(seg, offsetBytes);
+ }
+
+ final int getNativeOrderedInt() {
+ final long pos = getPosition();
+ final int anInt = MemoryAccess.getIntAtOffset(seg, pos);
+ setPosition(pos + Integer.BYTES);
+ return anInt;
+ }
+
+ final int getNativeOrderedInt(final long offsetBytes) {
+ return MemoryAccess.getIntAtOffset(seg, offsetBytes);
+ }
+
+ final long getNativeOrderedLong() {
+ final long pos = getPosition();
+ final long aLong = MemoryAccess.getLongAtOffset(seg, pos);
+ setPosition(pos + Long.BYTES);
+ return aLong;
+ }
+
+ final long getNativeOrderedLong(final long offsetBytes) {
+ return MemoryAccess.getLongAtOffset(seg, offsetBytes);
+ }
+
+ final short getNativeOrderedShort() {
+ final long pos = getPosition();
+ final short aShort = MemoryAccess.getShortAtOffset(seg, pos);
+ setPosition(pos + Short.BYTES);
+ return aShort;
+ }
+
+ final short getNativeOrderedShort(final long offsetBytes) {
+ return MemoryAccess.getShortAtOffset(seg, offsetBytes);
+ }
+
+ //OTHER PRIMITIVE READ METHODS: copyTo, compareTo
+ @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,
+ ((BaseStateImpl)that).seg, thatOffsetBytes, thatLengthBytes);
+ }
+
+ /*
+ * Develper 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.
+ */
+
+ //PRIMITIVE putX() and putXArray() implementations
+
+ @Override
+ public final void putByte(final byte value) {
+ final long pos = getPosition();
+ MemoryAccess.setByteAtOffset(seg, pos, value);
+ setPosition(pos + Byte.BYTES);
+ }
+
+ @Override
+ public final void putByte(final long offsetBytes, final byte value) {
+ MemoryAccess.setByteAtOffset(seg, offsetBytes, value);
+ }
+
+ @Override
+ public final void putByteArray(final byte[] srcArray, final int srcOffsetBytes,
+ final int lengthBytes) {
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetBytes, lengthBytes);
+ final long pos = getPosition();
+ MemorySegment dstSlice = seg.asSlice(pos, lengthBytes);
+ dstSlice.copyFrom(srcSlice);
+ setPosition(pos + lengthBytes);
+ }
+
+ //PRIMITIVE putX() Native Endian (used by both endians)
+ final void putNativeOrderedChar(final char value) {
+ final long pos = getPosition();
+ MemoryAccess.setCharAtOffset(seg, pos, value);
+ setPosition(pos + Character.BYTES);
+ }
+
+ final void putNativeOrderedChar(final long offsetBytes, final char value) {
+ MemoryAccess.setCharAtOffset(seg, offsetBytes, value);
+ }
+
+ final void putNativeOrderedInt(final int value) {
+ final long pos = getPosition();
+ MemoryAccess.setIntAtOffset(seg, pos, value);
+ setPosition(pos + Integer.BYTES);
+ }
+
+ final void putNativeOrderedInt(final long offsetBytes, final int value) {
+ MemoryAccess.setIntAtOffset(seg, offsetBytes, value);
+ }
+
+ final void putNativeOrderedLong(final long value) {
+ final long pos = getPosition();
+ MemoryAccess.setLongAtOffset(seg, pos, value);
+ setPosition(pos + Long.BYTES);
+ }
+
+ final void putNativeOrderedLong(final long offsetBytes, final long value) {
+ MemoryAccess.setLongAtOffset(seg, offsetBytes, value);
+ }
+
+ final void putNativeOrderedShort(final short value) {
+ final long pos = getPosition();
+ MemoryAccess.setShortAtOffset(seg, pos, value);
+ setPosition(pos + Short.BYTES);
+ }
+
+ final void putNativeOrderedShort(final long offsetBytes, final short value) {
+ MemoryAccess.setShortAtOffset(seg, offsetBytes, value);
+ }
+
+ //OTHER
+
+ @Override
+ public final void clear() {
+ seg.fill((byte)0);
+ }
+
+ @Override
+ public final void fill(final byte value) {
+ seg.fill(value);
+ }
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/BaseWritableMemoryImpl.java b/src/main/java/org/apache/datasketches/memory/internal/BaseWritableMemoryImpl.java
new file mode 100644
index 0000000..7953d7c
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/BaseWritableMemoryImpl.java
@@ -0,0 +1,375 @@
+/*
+ * 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 static java.nio.channels.FileChannel.MapMode.READ_ONLY;
+import static java.nio.channels.FileChannel.MapMode.READ_WRITE;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.channels.FileChannel;
+import java.nio.channels.WritableByteChannel;
+import java.util.Objects;
+
+import org.apache.datasketches.memory.Buffer;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.ReadOnlyException;
+import org.apache.datasketches.memory.WritableBuffer;
+import org.apache.datasketches.memory.WritableMemory;
+
+import jdk.incubator.foreign.MemoryAccess;
+import jdk.incubator.foreign.MemorySegment;
+import jdk.incubator.foreign.ResourceScope;
+
+/*
+ * Developer notes: The heavier methods, such as put/get arrays, duplicate, region, clear, fill,
+ * compareTo, etc., use hard checks (checkValid*() and checkBounds()), which execute at runtime and
+ * throw exceptions if violated. The cost of the runtime checks are minor compared to the rest of
+ * the work these methods are doing.
+ *
+ * <p>The light weight methods, such as put/get primitives, use asserts (assertValid*()), which only
+ * execute when asserts are enabled and JIT will remove them entirely from production runtime code.
+ * The light weight methods will simplify to a single unsafe call, which is further simplified by
+ * JIT to an intrinsic that is often a single CPU instruction.
+ */
+
+/**
+ * Common base of native-ordered and non-native-ordered {@link WritableMemory} implementations.
+ * Contains methods which are agnostic to the byte order.
+ */
+public abstract class BaseWritableMemoryImpl extends BaseStateImpl implements WritableMemory {
+
+ //Pass-through constructor
+ BaseWritableMemoryImpl(
+ final MemorySegment seg,
+ final int typeId) {
+ super(seg, typeId);
+ }
+
+ //HEAP
+
+ public static WritableMemory wrapSegment(
+ final MemorySegment seg,
+ final ByteOrder byteOrder,
+ final MemoryRequestServer memReqSvr) {
+ int type = MEMORY
+ | (seg.isReadOnly() ? READONLY : 0);
+ if (byteOrder == ByteOrder.nativeOrder()) {
+ type |= NATIVE;
+ return new HeapWritableMemoryImpl(seg, type, memReqSvr);
+ }
+ type |= NONNATIVE;
+ return new HeapNonNativeWritableMemoryImpl(seg, type, memReqSvr);
+ }
+
+ //BYTE BUFFER
+
+ public static WritableMemory wrapByteBuffer(
+ final ByteBuffer byteBuffer,
+ final boolean localReadOnly,
+ final ByteOrder byteOrder) {
+ byteBuffer.position(0).limit(byteBuffer.capacity());
+ MemorySegment seg = MemorySegment.ofByteBuffer(byteBuffer); //from 0 to capacity
+ int type = MEMORY | BYTEBUF
+ | (localReadOnly ? READONLY : 0)
+ | (seg.isMapped() ? MAP : 0)
+ | (seg.isNative() ? DIRECT : 0);
+ if (byteOrder == ByteOrder.nativeOrder()) {
+ type |= NATIVE;
+ return new BBWritableMemoryImpl(seg, type);
+ }
+ type |= NONNATIVE;
+ return new BBNonNativeWritableMemoryImpl(seg, type);
+ }
+
+ //MAP
+
+ /**
+ * The static constructor that chooses the correct Map leaf node based on the byte order.
+ * @param file the file being wrapped. It must be non-null with length > 0.
+ * @param fileOffsetBytes the file offset bytes
+ * @param capacityBytes the requested capacity of the memory mapped region. It must be > 0
+ * @param localReadOnly the requested read-only state
+ * @param byteOrder the requested byte-order
+ * @return this class constructed via the leaf node.
+ * @throws Exception
+ */
+ @SuppressWarnings("resource")
+ public static WritableMemory wrapMap(final File file, final long fileOffsetBytes,
+ final long capacityBytes, final boolean localReadOnly, final ByteOrder byteOrder)
+ throws Exception {
+ FileChannel.MapMode mapMode = (localReadOnly) ? READ_ONLY : READ_WRITE;
+ MemorySegment seg;
+ try {
+ seg = MemorySegment.mapFile(file.toPath(), fileOffsetBytes, capacityBytes, mapMode,
+ ResourceScope.newConfinedScope()); }
+ catch (final IllegalArgumentException | IllegalStateException | UnsupportedOperationException
+ | IOException | SecurityException e) { throw e; }
+ final boolean nativeBOType = Util.isNativeByteOrder(byteOrder);
+ final int type = MEMORY | MAP | DIRECT
+ | (localReadOnly ? READONLY : 0)
+ | (nativeBOType ? NATIVE : NONNATIVE);
+ return nativeBOType
+ ? new MapWritableMemoryImpl(seg, type)
+ : new MapNonNativeWritableMemoryImpl(seg, type);
+ }
+
+ //DIRECT
+
+ /**
+ * The static constructor that chooses the correct Direct leaf node based on the byte order.
+ * @param capacityBytes the requested capacity for the Direct (off-heap) memory. It must be ≥ 0.
+ * @param alignmentBytes requested segment alignment. Typically 1, 2, 4 or 8.
+ * @param scope ResourceScope for the backing MemorySegment.
+ * Typically <i>ResourceScope.newConfinedScope()</i>.
+ * @param byteOrder the byte order to be used. It must be non-null.
+ * @param memReqSvr A user-specified MemoryRequestServer, which may be null.
+ * This is a callback mechanism for a user client of direct memory to request more memory.
+ * @return WritableMemory
+ */
+ @SuppressWarnings("resource")
+ public static WritableMemory wrapDirect(
+ final long capacityBytes,
+ final long alignmentBytes,
+ final ResourceScope scope,
+ final ByteOrder byteOrder,
+ final MemoryRequestServer memReqSvr) {
+ final MemorySegment seg = MemorySegment.allocateNative(
+ capacityBytes,
+ alignmentBytes,
+ scope);
+ final boolean nativeBOType = Util.isNativeByteOrder(byteOrder);
+ final int type = MEMORY | DIRECT
+ | (nativeBOType ? NATIVE : NONNATIVE);
+ return nativeBOType
+ ? new DirectWritableMemoryImpl(seg, type, memReqSvr)
+ : new DirectNonNativeWritableMemoryImpl(seg, type, memReqSvr);
+ }
+
+ //REGIONS
+
+ @Override
+ public Memory region(final long offsetBytes, final long capacityBytes, final ByteOrder byteOrder) {
+ return regionImpl(offsetBytes, capacityBytes, true, byteOrder);
+ }
+
+ @Override
+ public WritableMemory writableRegion(final long offsetBytes, final long capacityBytes, final ByteOrder byteOrder) {
+ if (this.isReadOnly()) {
+ throw new ReadOnlyException("Cannot create a writable region from a read-only Memory.");
+ }
+ return regionImpl(offsetBytes, capacityBytes, false, byteOrder);
+ }
+
+ private WritableMemory regionImpl(
+ final long offsetBytes,
+ final long capacityBytes,
+ final boolean localReadOnly,
+ final ByteOrder byteOrder) {
+ Objects.requireNonNull(byteOrder, "byteOrder must be non-null.");
+ final boolean readOnly = isReadOnly() || localReadOnly || seg.isReadOnly();
+ final MemorySegment slice = seg.asSlice(offsetBytes, capacityBytes);
+ final boolean duplicateType = isDuplicateType();
+ final boolean mapType = seg.isMapped();
+ final boolean directType = seg.isNative();
+ final boolean nativeBOType = Util.isNativeByteOrder(byteOrder);
+ final boolean byteBufferType = isByteBufferType();
+ final int type = MEMORY | REGION
+ | (readOnly ? READONLY : 0)
+ | (duplicateType ? DUPLICATE : 0)
+ | (mapType ? MAP : 0)
+ | (directType ? DIRECT : 0)
+ | (nativeBOType ? NATIVE : NONNATIVE)
+ | (byteBufferType ? BYTEBUF : 0);
+ if (byteBufferType) {
+ if (nativeBOType) { return new BBWritableMemoryImpl(slice, type); }
+ return new BBNonNativeWritableMemoryImpl(slice, type);
+ }
+ if (mapType) {
+ if (nativeBOType) { return new MapWritableMemoryImpl(slice, type); }
+ return new MapNonNativeWritableMemoryImpl(slice, type);
+ }
+ if (directType) {
+ if (nativeBOType) { return new DirectWritableMemoryImpl(slice, type, memReqSvr); }
+ return new DirectNonNativeWritableMemoryImpl(slice, type, memReqSvr);
+ }
+ //else heap type
+ if (nativeBOType) { return new HeapWritableMemoryImpl(slice, type, memReqSvr); }
+ return new HeapNonNativeWritableMemoryImpl(slice, type, memReqSvr);
+ }
+
+ //AS BUFFER
+
+ @Override
+ public Buffer asBuffer(final ByteOrder byteOrder) {
+ return asWritableBuffer(true, byteOrder);
+ }
+
+ @Override
+ public WritableBuffer asWritableBuffer(final ByteOrder byteOrder) {
+ if (isReadOnly()) {
+ throw new ReadOnlyException(
+ "Cannot create a writable buffer from a read-only Memory.");
+ }
+ return asWritableBuffer(false, byteOrder);
+ }
+
+ private WritableBuffer asWritableBuffer(final boolean localReadOnly, final ByteOrder byteOrder) {
+ Objects.requireNonNull(byteOrder, "byteOrder must be non-null");
+ final boolean readOnly = isReadOnly() || localReadOnly;
+ final boolean duplicateType = isDuplicateType();
+ final boolean mapType = seg.isMapped();
+ final boolean directType = seg.isNative();
+ final boolean nativeBOType = Util.isNativeByteOrder(byteOrder);
+ final boolean byteBufferType = isByteBufferType();
+ final int type = BUFFER
+ | (readOnly ? READONLY : 0)
+ | (duplicateType ? DUPLICATE : 0)
+ | (mapType ? MAP : 0)
+ | (directType ? DIRECT : 0)
+ | (nativeBOType ? NATIVE : NONNATIVE)
+ | (byteBufferType ? BYTEBUF : 0);
+ WritableBuffer wbuf;
+ if (byteBufferType) {
+ if (nativeBOType) {
+ wbuf = new BBWritableBufferImpl(seg, type);
+ } else {
+ wbuf = new BBNonNativeWritableBufferImpl(seg, type);
+ }
+ }
+ if (mapType) {
+ if (nativeBOType) {
+ wbuf = new MapWritableBufferImpl(seg, type);
+ } else {
+ wbuf = new MapNonNativeWritableBufferImpl(seg, type);
+ }
+ }
+ if (directType) {
+ if (nativeBOType) {
+ wbuf = new DirectWritableBufferImpl(seg, type, memReqSvr);
+ } else {
+ wbuf = new DirectNonNativeWritableBufferImpl(seg, type, memReqSvr);
+ }
+ }
+ //else heap type
+ if (nativeBOType) {
+ wbuf = new HeapWritableBufferImpl(seg, type, memReqSvr);
+ } else {
+ wbuf = new HeapNonNativeWritableBufferImpl(seg, type, memReqSvr);
+ }
+ wbuf.setStartPositionEnd(0, 0, getCapacity());
+ return wbuf;
+ }
+
+ //PRIMITIVE getX() and getXArray()
+
+ @Override
+ public final byte getByte(final long offsetBytes) {
+ return MemoryAccess.getByteAtOffset(seg, offsetBytes);
+ }
+
+ @Override
+ public final void getByteArray(final long offsetBytes, final byte[] dstArray,
+ final int dstOffsetBytes, final int lengthBytes) {
+ checkBounds(dstOffsetBytes, lengthBytes, dstArray.length);
+ MemorySegment srcSlice = seg.asSlice(offsetBytes, lengthBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetBytes, lengthBytes);
+ dstSlice.copyFrom(srcSlice);
+ }
+
+
+ //OTHER PRIMITIVE READ METHODS: compareTo, copyTo, equals
+ @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,
+ ((BaseStateImpl)that).seg, thatOffsetBytes, thatLengthBytes);
+ }
+
+ @Override
+ public final void copyTo(final long srcOffsetBytes,
+ final WritableMemory destination, final long dstOffsetBytes, final long lengthBytes) {
+ CompareAndCopy.copy(seg, srcOffsetBytes,
+ ((BaseStateImpl)destination).seg, dstOffsetBytes, lengthBytes);
+ }
+
+ @Override
+ public final void writeTo(final long offsetBytes, final long lengthBytes,
+ final WritableByteChannel out) throws IOException {
+ checkBounds(offsetBytes, lengthBytes, seg.byteSize());
+ ByteBuffer bb = seg.asSlice(offsetBytes, lengthBytes).asByteBuffer();
+ out.write(bb);
+ }
+
+// //PRIMITIVE putX() and putXArray() implementations
+
+ @Override
+ public final void putByte(final long offsetBytes, final byte value) {
+ MemoryAccess.setByteAtOffset(seg, offsetBytes, value);
+ }
+
+ @Override
+ public final void putByteArray(final long offsetBytes, final byte[] srcArray,
+ final int srcOffsetBytes, final int lengthBytes) {
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetBytes, lengthBytes);
+ MemorySegment dstSlice = seg.asSlice(offsetBytes, lengthBytes);
+ dstSlice.copyFrom(srcSlice);
+ }
+
+// //OTHER WRITE METHODS
+
+ @Override
+ public final void clear() {
+ seg.fill((byte)0);
+ }
+
+ @Override
+ public final void clear(final long offsetBytes, final long lengthBytes) {
+ MemorySegment slice = seg.asSlice(offsetBytes, lengthBytes);
+ slice.fill((byte)0);
+ }
+
+ @Override
+ public final void clearBits(final long offsetBytes, final byte bitMask) {
+ final byte b = MemoryAccess.getByteAtOffset(seg, offsetBytes);
+ MemoryAccess.setByteAtOffset(seg, offsetBytes, (byte)(b & ~bitMask));
+ }
+
+ @Override
+ public final void fill(final byte value) {
+ seg.fill(value);
+ }
+
+ @Override
+ public final void fill(long offsetBytes, long lengthBytes, final byte value) {
+ MemorySegment slice = seg.asSlice(offsetBytes, lengthBytes);
+ slice.fill(value);
+ }
+
+ @Override
+ public final void setBits(final long offsetBytes, final byte bitMask) {
+ final byte b = MemoryAccess.getByteAtOffset(seg, offsetBytes);
+ MemoryAccess.setByteAtOffset(seg, offsetBytes, (byte)(b | bitMask));
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/CompareAndCopy.java b/src/main/java/org/apache/datasketches/memory/internal/CompareAndCopy.java
new file mode 100644
index 0000000..74a1dd1
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/CompareAndCopy.java
@@ -0,0 +1,70 @@
+/*
+ * 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 static jdk.incubator.foreign.MemoryAccess.getByteAtOffset;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * @author Lee Rhodes
+ */
+@SuppressWarnings("restriction")
+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);
+ long mm = slice1.mismatch(slice2);
+ if (mm == -1) { return 0; }
+ if ((lengthBytes1 > mm) && (lengthBytes2 > mm)) {
+ return Integer.compare(
+ getByteAtOffset(slice1, mm) & 0XFF, getByteAtOffset(slice2, 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, 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,
+ 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/DirectNonNativeWritableBufferImpl.java b/src/main/java/org/apache/datasketches/memory/internal/DirectNonNativeWritableBufferImpl.java
new file mode 100644
index 0000000..a409b66
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/DirectNonNativeWritableBufferImpl.java
@@ -0,0 +1,54 @@
+/*
+ * 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.ByteOrder;
+
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableBuffer;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Implementation of {@link WritableBuffer} for direct memory, non-native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class DirectNonNativeWritableBufferImpl extends NonNativeWritableBufferImpl {
+
+ DirectNonNativeWritableBufferImpl(
+ final MemorySegment seg,
+ final int typeId,
+ final MemoryRequestServer memReqSvr) {
+ super(seg, typeId);
+ if (memReqSvr != null) { setMemoryRequestServer(memReqSvr); }
+
+ }
+
+ @Override
+ BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly) | DUPLICATE;
+ return Util.isNativeByteOrder(byteOrder)
+ ? new DirectWritableBufferImpl(seg, type, memReqSvr)
+ : new DirectNonNativeWritableBufferImpl(seg, type, memReqSvr);
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/DirectNonNativeWritableMemoryImpl.java b/src/main/java/org/apache/datasketches/memory/internal/DirectNonNativeWritableMemoryImpl.java
new file mode 100644
index 0000000..470bc8f
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/DirectNonNativeWritableMemoryImpl.java
@@ -0,0 +1,43 @@
+/*
+ * 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 org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Implementation of {@link WritableMemory} for direct memory, non-native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class DirectNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImpl {
+
+ DirectNonNativeWritableMemoryImpl(
+ final MemorySegment seg,
+ final int typeId,
+ final MemoryRequestServer memReqSvr) {
+ super(seg, typeId);
+ if (memReqSvr != null) { setMemoryRequestServer(memReqSvr); }
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/DirectWritableBufferImpl.java b/src/main/java/org/apache/datasketches/memory/internal/DirectWritableBufferImpl.java
new file mode 100644
index 0000000..26a4d56
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/DirectWritableBufferImpl.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.ByteOrder;
+
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableBuffer;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Implementation of {@link WritableBuffer} for direct memory, native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class DirectWritableBufferImpl extends NativeWritableBufferImpl {
+
+ DirectWritableBufferImpl(
+ final MemorySegment seg,
+ final int typeId,
+ final MemoryRequestServer memReqSvr) {
+ super(seg, typeId);
+ if (memReqSvr != null) { setMemoryRequestServer(memReqSvr); }
+ }
+
+ @Override
+ BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly) | DUPLICATE;
+ return Util.isNativeByteOrder(byteOrder)
+ ? new DirectWritableBufferImpl(seg, type, memReqSvr)
+ : new DirectNonNativeWritableBufferImpl(seg, type, memReqSvr);
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/DirectWritableMemoryImpl.java b/src/main/java/org/apache/datasketches/memory/internal/DirectWritableMemoryImpl.java
new file mode 100644
index 0000000..0795c51
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/DirectWritableMemoryImpl.java
@@ -0,0 +1,43 @@
+/*
+ * 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 org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Implementation of {@link WritableMemory} for direct memory, native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class DirectWritableMemoryImpl extends NativeWritableMemoryImpl {
+
+ DirectWritableMemoryImpl(
+ final MemorySegment seg,
+ final int typeId,
+ final MemoryRequestServer memReqSvr) {
+ super(seg, typeId);
+ if (memReqSvr != null) { setMemoryRequestServer(memReqSvr); }
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/HeapNonNativeWritableBufferImpl.java b/src/main/java/org/apache/datasketches/memory/internal/HeapNonNativeWritableBufferImpl.java
new file mode 100644
index 0000000..fe46aae
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/HeapNonNativeWritableBufferImpl.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.ByteOrder;
+
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableBuffer;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Implementation of {@link WritableBuffer} for heap-based, non-native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class HeapNonNativeWritableBufferImpl extends NonNativeWritableBufferImpl {
+
+ HeapNonNativeWritableBufferImpl(
+ final MemorySegment seg,
+ final int typeId,
+ final MemoryRequestServer memReqSvr) {
+ super(seg, typeId);
+ if (memReqSvr != null) { setMemoryRequestServer(memReqSvr); }
+ }
+
+ @Override
+ BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly) | DUPLICATE;
+ return Util.isNativeByteOrder(byteOrder)
+ ? new HeapWritableBufferImpl(seg, type, memReqSvr)
+ : new HeapNonNativeWritableBufferImpl(seg, type, memReqSvr);
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/HeapNonNativeWritableMemoryImpl.java b/src/main/java/org/apache/datasketches/memory/internal/HeapNonNativeWritableMemoryImpl.java
new file mode 100644
index 0000000..4dc0766
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/HeapNonNativeWritableMemoryImpl.java
@@ -0,0 +1,43 @@
+/*
+ * 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 org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Implementation of {@link WritableMemory} for heap-based, non-native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class HeapNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImpl {
+
+ HeapNonNativeWritableMemoryImpl(
+ final MemorySegment seg,
+ final int typeId,
+ final MemoryRequestServer memReqSvr) {
+ super(seg, typeId);
+ if (memReqSvr != null) { setMemoryRequestServer(memReqSvr); }
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/HeapWritableBufferImpl.java b/src/main/java/org/apache/datasketches/memory/internal/HeapWritableBufferImpl.java
new file mode 100644
index 0000000..972dc37
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/HeapWritableBufferImpl.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.ByteOrder;
+
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableBuffer;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Implementation of {@link WritableBuffer} for heap-based, native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class HeapWritableBufferImpl extends NativeWritableBufferImpl {
+
+ HeapWritableBufferImpl(
+ final MemorySegment seg,
+ final int typeId,
+ final MemoryRequestServer memReqSvr) {
+ super(seg, typeId);
+ if (memReqSvr != null) { setMemoryRequestServer(memReqSvr); }
+ }
+
+ @Override
+ BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly) | DUPLICATE;
+ return Util.isNativeByteOrder(byteOrder)
+ ? new HeapWritableBufferImpl(seg, type, memReqSvr)
+ : new HeapNonNativeWritableBufferImpl(seg, type, memReqSvr);
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/HeapWritableMemoryImpl.java b/src/main/java/org/apache/datasketches/memory/internal/HeapWritableMemoryImpl.java
new file mode 100644
index 0000000..1d75612
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/HeapWritableMemoryImpl.java
@@ -0,0 +1,43 @@
+/*
+ * 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 org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Implementation of {@link WritableMemory} for heap-based, native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class HeapWritableMemoryImpl extends NativeWritableMemoryImpl {
+
+ HeapWritableMemoryImpl(
+ final MemorySegment seg,
+ final int typeId,
+ final MemoryRequestServer memReqSvr) {
+ super(seg, typeId);
+ if (memReqSvr != null) { setMemoryRequestServer(memReqSvr); }
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/Ints.java b/src/main/java/org/apache/datasketches/memory/internal/Ints.java
new file mode 100644
index 0000000..72a447d
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/Ints.java
@@ -0,0 +1,39 @@
+/*
+ * 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;
+
+/** Equivalent of Guava's Ints. */
+public final class Ints {
+
+ private Ints() {}
+
+ /**
+ * Checks if a cast of a long to an int is within the range of an int
+ * @param v the given long
+ * @return returns the cast int, or throws an exception that the long was out-of-range of an int.
+ */
+ public static int checkedCast(final long v) {
+ final int result = (int) v;
+ if (result != v) {
+ throw new IllegalArgumentException("Out of range: " + v);
+ }
+ return result;
+ }
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableBufferImpl.java b/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableBufferImpl.java
new file mode 100644
index 0000000..9c3a1a9
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableBufferImpl.java
@@ -0,0 +1,50 @@
+/*
+ * 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.ByteOrder;
+
+import org.apache.datasketches.memory.WritableBuffer;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Implementation of {@link WritableBuffer} for map memory, non-native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class MapNonNativeWritableBufferImpl extends NonNativeWritableBufferImpl {
+
+ MapNonNativeWritableBufferImpl(
+ final MemorySegment seg,
+ final int typeId) {
+ super(seg, typeId);
+ }
+
+ @Override
+ BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly) | DUPLICATE;
+ return Util.isNativeByteOrder(byteOrder)
+ ? new MapWritableBufferImpl(seg, type)
+ : new MapNonNativeWritableBufferImpl(seg, type);
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableMemoryImpl.java b/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableMemoryImpl.java
new file mode 100644
index 0000000..cab44fe
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/MapNonNativeWritableMemoryImpl.java
@@ -0,0 +1,40 @@
+/*
+ * 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 org.apache.datasketches.memory.WritableMemory;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Implementation of {@link WritableMemory} for map memory, non-native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class MapNonNativeWritableMemoryImpl extends NonNativeWritableMemoryImpl {
+
+ MapNonNativeWritableMemoryImpl(
+ final MemorySegment seg,
+ final int typeId) {
+ super(seg, typeId);
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/MapWritableBufferImpl.java b/src/main/java/org/apache/datasketches/memory/internal/MapWritableBufferImpl.java
new file mode 100644
index 0000000..b0fcf29
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/MapWritableBufferImpl.java
@@ -0,0 +1,50 @@
+/*
+ * 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.ByteOrder;
+
+import org.apache.datasketches.memory.WritableBuffer;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Implementation of {@link WritableBuffer} for map memory, native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class MapWritableBufferImpl extends NativeWritableBufferImpl {
+
+ MapWritableBufferImpl(
+ final MemorySegment seg,
+ final int typeId) {
+ super(seg, typeId);
+ }
+
+ @Override
+ BaseWritableBufferImpl toDuplicate(final boolean readOnly, final ByteOrder byteOrder) {
+ final int type = setReadOnlyType(typeId, readOnly) | DUPLICATE;
+ return Util.isNativeByteOrder(byteOrder)
+ ? new MapWritableBufferImpl(seg, type)
+ : new MapNonNativeWritableBufferImpl(seg, type);
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/MapWritableMemoryImpl.java b/src/main/java/org/apache/datasketches/memory/internal/MapWritableMemoryImpl.java
new file mode 100644
index 0000000..fdd5baf
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/MapWritableMemoryImpl.java
@@ -0,0 +1,40 @@
+/*
+ * 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 org.apache.datasketches.memory.WritableMemory;
+
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * Implementation of {@link WritableMemory} for map memory, native byte order.
+ *
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+final class MapWritableMemoryImpl extends NativeWritableMemoryImpl {
+
+ MapWritableMemoryImpl(
+ final MemorySegment seg,
+ final int typeId) {
+ super(seg, typeId);
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/MurmurHash3v2.java b/src/main/java/org/apache/datasketches/memory/internal/MurmurHash3v3.java
similarity index 75%
rename from src/main/java/org/apache/datasketches/memory/MurmurHash3v2.java
rename to src/main/java/org/apache/datasketches/memory/internal/MurmurHash3v3.java
index b958213..906046c 100644
--- a/src/main/java/org/apache/datasketches/memory/MurmurHash3v2.java
+++ b/src/main/java/org/apache/datasketches/memory/internal/MurmurHash3v3.java
@@ -17,10 +17,14 @@
* under the License.
*/
-package org.apache.datasketches.memory;
+package org.apache.datasketches.memory.internal;
import static java.nio.charset.StandardCharsets.UTF_8;
-import static org.apache.datasketches.memory.internal.UnsafeUtil.unsafe;
+
+import java.util.Objects;
+
+import jdk.incubator.foreign.MemoryAccess;
+import jdk.incubator.foreign.MemorySegment;
/**
* <p>The MurmurHash3 is a fast, non-cryptographic, 128-bit hash function that has
@@ -38,13 +42,16 @@ import static org.apache.datasketches.memory.internal.UnsafeUtil.unsafe;
* <p>This implementation produces exactly the same hash result as the
* MurmurHash3 function in datasketches-java given compatible inputs.</p>
*
+ * <p>This version 3 of the implementation leverages the jdk.incubator.foreign package of JDK-17 in place of
+ * the Unsafe class.
+ *
* @author Lee Rhodes
*/
-public final class MurmurHash3v2 {
+public final class MurmurHash3v3 {
private static final long C1 = 0x87c37b91114253d5L;
private static final long C2 = 0x4cf5ad432745937fL;
- //Provided for backward compatibility
+
/**
* Returns a 128-bit hash of the input.
@@ -58,7 +65,7 @@ public final class MurmurHash3v2 {
if ((in == null) || (in.length == 0)) {
throw new IllegalArgumentException("Input in is empty or null.");
}
- return hash(Memory.wrap(in), 0L, in.length << 3, seed, new long[2]);
+ return hash(MemorySegment.ofArray(in), 0L, in.length << 3, seed, new long[2]);
}
/**
@@ -73,7 +80,7 @@ public final class MurmurHash3v2 {
if ((in == null) || (in.length == 0)) {
throw new IllegalArgumentException("Input in is empty or null.");
}
- return hash(Memory.wrap(in), 0L, in.length << 2, seed, new long[2]);
+ return hash(MemorySegment.ofArray(in), 0L, in.length << 2, seed, new long[2]);
}
/**
@@ -88,7 +95,7 @@ public final class MurmurHash3v2 {
if ((in == null) || (in.length == 0)) {
throw new IllegalArgumentException("Input in is empty or null.");
}
- return hash(Memory.wrap(in), 0L, in.length << 1, seed, new long[2]);
+ return hash(MemorySegment.ofArray(in), 0L, in.length << 1, seed, new long[2]);
}
/**
@@ -103,7 +110,7 @@ public final class MurmurHash3v2 {
if ((in == null) || (in.length == 0)) {
throw new IllegalArgumentException("Input in is empty or null.");
}
- return hash(Memory.wrap(in), 0L, in.length, seed, new long[2]);
+ return hash(MemorySegment.ofArray(in), 0L, in.length, seed, new long[2]);
}
//Single primitive inputs
@@ -151,7 +158,7 @@ public final class MurmurHash3v2 {
throw new IllegalArgumentException("Input in is empty or null.");
}
final byte[] byteArr = in.getBytes(UTF_8);
- return hash(Memory.wrap(byteArr), 0L, byteArr.length, seed, hashOut);
+ return hash(MemorySegment.ofArray(byteArr), 0L, byteArr.length, seed, hashOut);
}
//The main API call
@@ -159,7 +166,7 @@ public final class MurmurHash3v2 {
/**
* Returns a 128-bit hash of the input as a long array of size 2.
*
- * @param mem The input on-heap Memory. Must be non-null and non-empty,
+ * @param seg The input on-heap Memory. Must be non-null and non-empty,
* otherwise throws IllegalArgumentException.
* @param offsetBytes the starting point within Memory.
* @param lengthBytes the total number of bytes to be hashed.
@@ -168,16 +175,13 @@ public final class MurmurHash3v2 {
* @return the hash.
*/
@SuppressWarnings("restriction")
- public static long[] hash(final Memory mem, final long offsetBytes, final long lengthBytes,
+ public static long[] hash(final MemorySegment seg, final long offsetBytes, final long lengthBytes,
final long seed, final long[] hashOut) {
- if ((mem == null) || (mem.getCapacity() == 0L)) {
- throw new IllegalArgumentException("Input mem is empty or null.");
- }
- final Object uObj = ((WritableMemory) mem).getArray();
- if (uObj == null) {
- throw new IllegalArgumentException("The backing resource of input mem is not on-heap.");
- }
- long cumOff = mem.getCumulativeOffset() + offsetBytes;
+ Objects.requireNonNull(seg, "Input MemorySegment is null");
+ if (seg.byteSize() == 0L) { throw new IllegalArgumentException("Input MemorySegment is empty."); }
+
+
+ long cumOff = offsetBytes;
long h1 = seed;
long h2 = seed;
@@ -185,8 +189,8 @@ public final class MurmurHash3v2 {
// Process the 128-bit blocks (the body) into the hash
while (rem >= 16L) {
- final long k1 = unsafe.getLong(uObj, cumOff); //0, 16, 32, ...
- final long k2 = unsafe.getLong(uObj, cumOff + 8); //8, 24, 40, ...
+ final long k1 = MemoryAccess.getLongAtOffset(seg, cumOff); //0, 16, 32, ...
+ final long k2 = MemoryAccess.getLongAtOffset(seg, cumOff + 8); //8, 24, 40, ...
cumOff += 16L;
rem -= 16L;
@@ -207,75 +211,75 @@ public final class MurmurHash3v2 {
long k2 = 0;
switch ((int) rem) {
case 15: {
- k2 ^= (unsafe.getByte(uObj, cumOff + 14) & 0xFFL) << 48;
+ k2 ^= (MemoryAccess.getByteAtOffset(seg, cumOff + 14) & 0xFFL) << 48;
}
//$FALL-THROUGH$
case 14: {
- k2 ^= (unsafe.getShort(uObj, cumOff + 12) & 0xFFFFL) << 32;
- k2 ^= (unsafe.getInt(uObj, cumOff + 8) & 0xFFFFFFFFL);
- k1 = unsafe.getLong(uObj, cumOff);
+ k2 ^= (MemoryAccess.getShortAtOffset(seg, cumOff + 12) & 0xFFFFL) << 32;
+ k2 ^= (MemoryAccess.getIntAtOffset(seg, cumOff + 8) & 0xFFFFFFFFL);
+ k1 = MemoryAccess.getLongAtOffset(seg, cumOff);
break;
}
case 13: {
- k2 ^= (unsafe.getByte(uObj, cumOff + 12) & 0xFFL) << 32;
+ k2 ^= (MemoryAccess.getByteAtOffset(seg, cumOff + 12) & 0xFFL) << 32;
}
//$FALL-THROUGH$
case 12: {
- k2 ^= (unsafe.getInt(uObj, cumOff + 8) & 0xFFFFFFFFL);
- k1 = unsafe.getLong(uObj, cumOff);
+ k2 ^= (MemoryAccess.getIntAtOffset(seg, cumOff + 8) & 0xFFFFFFFFL);
+ k1 = MemoryAccess.getLongAtOffset(seg, cumOff);
break;
}
case 11: {
- k2 ^= (unsafe.getByte(uObj, cumOff + 10) & 0xFFL) << 16;
+ k2 ^= (MemoryAccess.getByteAtOffset(seg, cumOff + 10) & 0xFFL) << 16;
}
//$FALL-THROUGH$
case 10: {
- k2 ^= (unsafe.getShort(uObj, cumOff + 8) & 0xFFFFL);
- k1 = unsafe.getLong(uObj, cumOff);
+ k2 ^= (MemoryAccess.getShortAtOffset(seg, cumOff + 8) & 0xFFFFL);
+ k1 = MemoryAccess.getLongAtOffset(seg, cumOff);
break;
}
case 9: {
- k2 ^= (unsafe.getByte(uObj, cumOff + 8) & 0xFFL);
+ k2 ^= (MemoryAccess.getByteAtOffset(seg, cumOff + 8) & 0xFFL);
}
//$FALL-THROUGH$
case 8: {
- k1 = unsafe.getLong(uObj, cumOff);
+ k1 = MemoryAccess.getLongAtOffset(seg, cumOff);
break;
}
case 7: {
- k1 ^= (unsafe.getByte(uObj, cumOff + 6) & 0xFFL) << 48;
+ k1 ^= (MemoryAccess.getByteAtOffset(seg, cumOff + 6) & 0xFFL) << 48;
}
//$FALL-THROUGH$
case 6: {
- k1 ^= (unsafe.getShort(uObj, cumOff + 4) & 0xFFFFL) << 32;
- k1 ^= (unsafe.getInt(uObj, cumOff) & 0xFFFFFFFFL);
+ k1 ^= (MemoryAccess.getShortAtOffset(seg, cumOff + 4) & 0xFFFFL) << 32;
+ k1 ^= (MemoryAccess.getIntAtOffset(seg, cumOff) & 0xFFFFFFFFL);
break;
}
case 5: {
- k1 ^= (unsafe.getByte(uObj, cumOff + 4) & 0xFFL) << 32;
+ k1 ^= (MemoryAccess.getByteAtOffset(seg, cumOff + 4) & 0xFFL) << 32;
}
//$FALL-THROUGH$
case 4: {
- k1 ^= (unsafe.getInt(uObj, cumOff) & 0xFFFFFFFFL);
+ k1 ^= (MemoryAccess.getIntAtOffset(seg, cumOff) & 0xFFFFFFFFL);
break;
}
case 3: {
- k1 ^= (unsafe.getByte(uObj, cumOff + 2) & 0xFFL) << 16;
+ k1 ^= (MemoryAccess.getByteAtOffset(seg, cumOff + 2) & 0xFFL) << 16;
}
//$FALL-THROUGH$
case 2: {
- k1 ^= (unsafe.getShort(uObj, cumOff) & 0xFFFFL);
+ k1 ^= (MemoryAccess.getShortAtOffset(seg, cumOff) & 0xFFFFL);
break;
}
case 1: {
- k1 ^= (unsafe.getByte(uObj, cumOff) & 0xFFL);
+ k1 ^= (MemoryAccess.getByteAtOffset(seg, cumOff) & 0xFFL);
break;
}
//default: break; //can't happen
diff --git a/src/main/java/org/apache/datasketches/memory/internal/NativeWritableBufferImpl.java b/src/main/java/org/apache/datasketches/memory/internal/NativeWritableBufferImpl.java
new file mode 100644
index 0000000..c2a34fe
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/NativeWritableBufferImpl.java
@@ -0,0 +1,324 @@
+/*
+ * 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 org.apache.datasketches.memory.WritableBuffer;
+
+import jdk.incubator.foreign.MemoryAccess;
+import jdk.incubator.foreign.MemorySegment;
+
+/*
+ * Developer notes: The heavier methods, such as put/get arrays, duplicate, region, clear, fill,
+ * compareTo, etc., use hard checks (check*() and incrementAndCheck*() methods), which execute at
+ * runtime and throw exceptions if violated. The cost of the runtime checks are minor compared to
+ * the rest of the work these methods are doing.
+ *
+ * <p>The light weight methods, such as put/get primitives, use asserts (assert*() and
+ * incrementAndAssert*() methods), which only execute when asserts are enabled and JIT will remove
+ * them entirely from production runtime code. The offset versions of the light weight methods will
+ * simplify to a single unsafe call, which is further simplified by JIT to an intrinsic that is
+ * often a single CPU instruction.
+ */
+
+/**
+ * Implementation of {@link WritableBuffer} for native endian byte order.
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+@SuppressWarnings("restriction")
+abstract class NativeWritableBufferImpl extends BaseWritableBufferImpl {
+
+ //Pass-through ctor
+ NativeWritableBufferImpl(
+ final MemorySegment seg,
+ final int typeId) {
+ super(seg, typeId);
+ }
+
+ //PRIMITIVE getX() and getXArray()
+ @Override
+ public char getChar() {
+ final long pos = getPosition();
+ setPosition(pos + Character.BYTES);
+ return MemoryAccess.getCharAtOffset(seg, pos);
+ }
+
+ @Override
+ public char getChar(final long offsetBytes) {
+ return MemoryAccess.getCharAtOffset(seg, offsetBytes);
+ }
+
+ @Override
+ public void getCharArray(final char[] dstArray, final int dstOffsetChars, final int lengthChars) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthChars) << CHAR_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(pos, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetChars << CHAR_SHIFT, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public double getDouble() {
+ final long pos = getPosition();
+ setPosition(pos + Double.BYTES);
+ return MemoryAccess.getDoubleAtOffset(seg, pos);
+ }
+
+ @Override
+ public double getDouble(final long offsetBytes) {
+ return MemoryAccess.getDoubleAtOffset(seg, offsetBytes);
+ }
+
+ @Override
+ public void getDoubleArray(final double[] dstArray, final int dstOffsetDoubles, final int lengthDoubles) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthDoubles) << DOUBLE_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(pos, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetDoubles << DOUBLE_SHIFT, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public float getFloat() {
+ final long pos = getPosition();
+ setPosition(pos + Float.BYTES);
+ return MemoryAccess.getFloatAtOffset(seg, pos);
+ }
+
+ @Override
+ public float getFloat(final long offsetBytes) {
+ return MemoryAccess.getFloatAtOffset(seg, offsetBytes);
+ }
+
+ @Override
+ public void getFloatArray(final float[] dstArray, final int dstOffsetFloats,
+ final int lengthFloats) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthFloats) << FLOAT_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(pos, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetFloats << FLOAT_SHIFT, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public int getInt() {
+ final long pos = getPosition();
+ setPosition(pos + Integer.BYTES);
+ return MemoryAccess.getIntAtOffset(seg, pos);
+ }
+
+ @Override
+ public int getInt(final long offsetBytes) {
+ return MemoryAccess.getIntAtOffset(seg, offsetBytes);
+ }
+
+ @Override
+ public void getIntArray(final int[] dstArray, final int dstOffsetInts, final int lengthInts) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthInts) << INT_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(pos, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetInts << INT_SHIFT, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public long getLong() {
+ final long pos = getPosition();
+ setPosition(pos + Long.BYTES);
+ return MemoryAccess.getLongAtOffset(seg, pos);
+ }
+
+ @Override
+ public long getLong(final long offsetBytes) {
+ return MemoryAccess.getLongAtOffset(seg, offsetBytes);
+ }
+
+ @Override
+ public void getLongArray(final long[] dstArray, final int dstOffsetLongs, final int lengthLongs) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthLongs) << LONG_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(pos, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetLongs << LONG_SHIFT, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public short getShort() {
+ final long pos = getPosition();
+ setPosition(pos + Short.BYTES);
+ return MemoryAccess.getShortAtOffset(seg, pos);
+ }
+
+ @Override
+ public short getShort(final long offsetBytes) {
+ return MemoryAccess.getShortAtOffset(seg, offsetBytes);
+ }
+
+ @Override
+ public void getShortArray(final short[] dstArray, final int dstOffsetShorts,
+ final int lengthShorts) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthShorts) << SHORT_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(pos, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetShorts << SHORT_SHIFT, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ setPosition(pos + copyBytes);
+ }
+
+ //PRIMITIVE putX() and putXArray()
+ @Override
+ public void putChar(final char value) {
+ final long pos = getPosition();
+ setPosition(pos + Character.BYTES);
+ MemoryAccess.setCharAtOffset(seg, pos, value);
+ }
+
+ @Override
+ public void putChar(final long offsetBytes, final char value) {
+ MemoryAccess.setCharAtOffset(seg, offsetBytes, value);
+ }
+
+ @Override
+ public void putCharArray(final char[] srcArray, final int srcOffsetChars, final int lengthChars) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthChars) << CHAR_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetChars << CHAR_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(pos, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public void putDouble(final double value) {
+ final long pos = getPosition();
+ setPosition(pos + Double.BYTES);
+ MemoryAccess.setDoubleAtOffset(seg, pos, value);
+ }
+
+ @Override
+ public void putDouble(final long offsetBytes, final double value) {
+ MemoryAccess.setDoubleAtOffset(seg, offsetBytes, value);
+ }
+
+ @Override
+ public void putDoubleArray(final double[] srcArray, final int srcOffsetDoubles, final int lengthDoubles) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthDoubles) << DOUBLE_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetDoubles << DOUBLE_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(pos, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public void putFloat(final float value) {
+ final long pos = getPosition();
+ setPosition(pos + Float.BYTES);
+ MemoryAccess.setFloatAtOffset(seg, pos, value);
+ }
+
+ @Override
+ public void putFloat(final long offsetBytes, final float value) {
+ MemoryAccess.setFloatAtOffset(seg, offsetBytes, value);
+ }
+
+ @Override
+ public void putFloatArray(final float[] srcArray, final int srcOffsetFloats,
+ final int lengthFloats) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthFloats) << FLOAT_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetFloats << FLOAT_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(pos, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public void putInt(final int value) {
+ final long pos = getPosition();
+ setPosition(pos + Integer.BYTES);
+ MemoryAccess.setIntAtOffset(seg, pos, value);
+ }
+
+ @Override
+ public void putInt(final long offsetBytes, final int value) {
+ MemoryAccess.setIntAtOffset(seg, offsetBytes, value);
+ }
+
+ @Override
+ public void putIntArray(final int[] srcArray, final int srcOffsetInts, final int lengthInts) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthInts) << INT_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetInts << INT_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(pos, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public void putLong(final long value) {
+ final long pos = getPosition();
+ setPosition(pos + Long.BYTES);
+ MemoryAccess.setLongAtOffset(seg, pos, value);
+ }
+
+ @Override
+ public void putLong(final long offsetBytes, final long value) {
+ MemoryAccess.setLongAtOffset(seg, offsetBytes, value);
+ }
+
+ @Override
+ public void putLongArray(final long[] srcArray, final int srcOffsetLongs, final int lengthLongs) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthLongs) << LONG_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetLongs << LONG_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(pos, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public void putShort(final short value) {
+ final long pos = getPosition();
+ setPosition(pos + Short.BYTES);
+ MemoryAccess.setShortAtOffset(seg, pos, value);
+ }
+
+ @Override
+ public void putShort(final long offsetBytes, final short value) {
+ MemoryAccess.setShortAtOffset(seg, offsetBytes, value);
+ }
+
+ @Override
+ public void putShortArray(final short[] srcArray, final int srcOffsetShorts,
+ final int lengthShorts) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthShorts) << SHORT_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetShorts << SHORT_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(pos, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ setPosition(pos + copyBytes);
+ }
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImpl.java b/src/main/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImpl.java
new file mode 100644
index 0000000..f969a05
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImpl.java
@@ -0,0 +1,224 @@
+/*
+ * 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 org.apache.datasketches.memory.WritableMemory;
+
+import jdk.incubator.foreign.MemoryAccess;
+import jdk.incubator.foreign.MemorySegment;
+
+/*
+ * Developer notes: The heavier methods, such as put/get arrays, duplicate, region, clear, fill,
+ * compareTo, etc., use hard checks (checkValid*() and checkBounds()), which execute at runtime and
+ * throw exceptions if violated. The cost of the runtime checks are minor compared to the rest of
+ * the work these methods are doing.
+ *
+ * <p>The light weight methods, such as put/get primitives, use asserts (assertValid*()), which only
+ * execute when asserts are enabled and JIT will remove them entirely from production runtime code.
+ * The light weight methods will simplify to a single unsafe call, which is further simplified by
+ * JIT to an intrinsic that is often a single CPU instruction.
+ */
+
+/**
+ * Implementation of {@link WritableMemory} for native endian byte order.
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+@SuppressWarnings("restriction")
+abstract class NativeWritableMemoryImpl extends BaseWritableMemoryImpl {
+
+ //Pass-through ctor
+ NativeWritableMemoryImpl(
+ final MemorySegment seg,
+ final int typeId) {
+ super(seg, typeId);
+ }
+
+ ///PRIMITIVE getX() and getXArray()
+ @Override
+ public char getChar(final long offsetBytes) {
+ return MemoryAccess.getCharAtOffset(seg, offsetBytes);
+ }
+
+ @Override
+ public void getCharArray(final long offsetBytes, final char[] dstArray, final int dstOffsetChars,
+ final int lengthChars) {
+ final long copyBytes = ((long) lengthChars) << CHAR_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(offsetBytes, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetChars << CHAR_SHIFT, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ }
+
+ @Override
+ public double getDouble(final long offsetBytes) {
+ return MemoryAccess.getDoubleAtOffset(seg, offsetBytes);
+ }
+
+ @Override
+ public void getDoubleArray(final long offsetBytes, final double[] dstArray,
+ final int dstOffsetDoubles, final int lengthDoubles) {
+ final long copyBytes = ((long) lengthDoubles) << DOUBLE_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(offsetBytes, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetDoubles << DOUBLE_SHIFT, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ }
+
+ @Override
+ public float getFloat(final long offsetBytes) {
+ return MemoryAccess.getFloatAtOffset(seg, offsetBytes);
+ }
+
+ @Override
+ public void getFloatArray(final long offsetBytes, final float[] dstArray,
+ final int dstOffsetFloats, final int lengthFloats) {
+ final long copyBytes = ((long) lengthFloats) << FLOAT_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(offsetBytes, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetFloats << FLOAT_SHIFT, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ }
+
+ @Override
+ public int getInt(final long offsetBytes) {
+ return MemoryAccess.getIntAtOffset(seg, offsetBytes);
+ }
+
+ @Override
+ public void getIntArray(final long offsetBytes, final int[] dstArray, final int dstOffsetInts,
+ final int lengthInts) {
+ final long copyBytes = ((long) lengthInts) << INT_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(offsetBytes, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetInts << INT_SHIFT, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ }
+
+ @Override
+ public long getLong(final long offsetBytes) {
+ return MemoryAccess.getLongAtOffset(seg, offsetBytes);
+ }
+
+ @Override
+ public void getLongArray(final long offsetBytes, final long[] dstArray,
+ final int dstOffsetLongs, final int lengthLongs) {
+ final long copyBytes = ((long) lengthLongs) << LONG_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(offsetBytes, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetLongs << LONG_SHIFT, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ }
+
+ @Override
+ public short getShort(final long offsetBytes) {
+ return MemoryAccess.getShortAtOffset(seg, offsetBytes);
+ }
+
+ @Override
+ public void getShortArray(final long offsetBytes, final short[] dstArray,
+ final int dstOffsetShorts, final int lengthShorts) {
+ final long copyBytes = ((long) lengthShorts) << SHORT_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(offsetBytes, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetShorts << SHORT_SHIFT, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ }
+
+ //PRIMITIVE putX() and putXArray() implementations
+ @Override
+ public void putChar(final long offsetBytes, final char value) {
+ MemoryAccess.setCharAtOffset(seg, offsetBytes, value);
+ }
+
+ @Override
+ public void putCharArray(final long offsetBytes, final char[] srcArray,
+ final int srcOffsetChars, final int lengthChars) {
+ final long copyBytes = ((long) lengthChars) << CHAR_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetChars << CHAR_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(offsetBytes, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ }
+
+ @Override
+ public void putDouble(final long offsetBytes, final double value) {
+ MemoryAccess.setDoubleAtOffset(seg, offsetBytes, value);
+ }
+
+ @Override
+ public void putDoubleArray(final long offsetBytes, final double[] srcArray,
+ final int srcOffsetDoubles, final int lengthDoubles) {
+ final long copyBytes = ((long) lengthDoubles) << DOUBLE_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetDoubles << DOUBLE_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(offsetBytes, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ }
+
+ @Override
+ public void putFloat(final long offsetBytes, final float value) {
+ MemoryAccess.setFloatAtOffset(seg, offsetBytes, value);
+ }
+
+ @Override
+ public void putFloatArray(final long offsetBytes, final float[] srcArray,
+ final int srcOffsetFloats, final int lengthFloats) {
+ final long copyBytes = ((long) lengthFloats) << FLOAT_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetFloats << FLOAT_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(offsetBytes, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ }
+
+ @Override
+ public void putInt(final long offsetBytes, final int value) {
+ MemoryAccess.setIntAtOffset(seg, offsetBytes, value);
+ }
+
+ @Override
+ public void putIntArray(final long offsetBytes, final int[] srcArray, final int srcOffsetInts,
+ final int lengthInts) {
+ final long copyBytes = ((long) lengthInts) << INT_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetInts << INT_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(offsetBytes, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ }
+
+ @Override
+ public void putLong(final long offsetBytes, final long value) {
+ MemoryAccess.setLongAtOffset(seg, offsetBytes, value);
+ }
+
+ @Override
+ public void putLongArray(final long offsetBytes, final long[] srcArray, final int srcOffsetLongs,
+ final int lengthLongs) {
+ final long copyBytes = ((long) lengthLongs) << LONG_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetLongs << LONG_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(offsetBytes, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ }
+
+ @Override
+ public void putShort(final long offsetBytes, final short value) {
+ MemoryAccess.setShortAtOffset(seg, offsetBytes, value);
+ }
+
+ @Override
+ public void putShortArray(final long offsetBytes, final short[] srcArray,
+ final int srcOffsetShorts, final int lengthShorts) {
+ final long copyBytes = ((long) lengthShorts) << SHORT_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetShorts << SHORT_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(offsetBytes, copyBytes);
+ dstSlice.copyFrom(srcSlice);
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImpl.java b/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImpl.java
new file mode 100644
index 0000000..9330f86
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImpl.java
@@ -0,0 +1,363 @@
+/*
+ * 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 org.apache.datasketches.memory.WritableBuffer;
+
+import jdk.incubator.foreign.MemoryAccess;
+import jdk.incubator.foreign.MemorySegment;
+
+/*
+ * Developer notes: The heavier methods, such as put/get arrays, duplicate, region, clear, fill,
+ * compareTo, etc., use hard checks (check*() and incrementAndCheck*() methods), which execute at
+ * runtime and throw exceptions if violated. The cost of the runtime checks are minor compared to
+ * the rest of the work these methods are doing.
+ *
+ * <p>The light weight methods, such as put/get primitives, use asserts (assert*() and
+ * incrementAndAssert*() methods), which only execute when asserts are enabled and JIT will remove
+ * them entirely from production runtime code. The offset versions of the light weight methods will
+ * simplify to a single unsafe call, which is further simplified by JIT to an intrinsic that is
+ * often a single CPU instruction.
+ */
+
+/**
+ * Implementation of {@link WritableBuffer} for non-native endian byte order.
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+@SuppressWarnings("restriction")
+abstract class NonNativeWritableBufferImpl extends BaseWritableBufferImpl {
+
+ //Pass-through ctor
+ NonNativeWritableBufferImpl(
+ final MemorySegment seg,
+ final int typeId) {
+ super(seg, typeId);
+ }
+
+ //PRIMITIVE getX() and getXArray()
+ @Override
+ public char getChar() {
+ final long pos = getPosition();
+ setPosition(pos + Character.BYTES);
+ return MemoryAccess.getCharAtOffset(seg, pos, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public char getChar(final long offsetBytes) {
+ return MemoryAccess.getCharAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public void getCharArray(final char[] dstArray, final int dstOffsetChars, final int lengthChars) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthChars) << CHAR_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(pos, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetChars << CHAR_SHIFT, copyBytes);
+ for (int index = 0; index < lengthChars; index++) {
+ final char aChar = MemoryAccess.getCharAtIndex(srcSlice, index, NON_NATIVE_BYTE_ORDER);
+ MemoryAccess.setCharAtIndex(dstSlice, index, NATIVE_BYTE_ORDER, aChar);
+ }
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public double getDouble() {
+ final long pos = getPosition();
+ setPosition(pos + Double.BYTES);
+ return MemoryAccess.getDoubleAtOffset(seg, pos, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public double getDouble(final long offsetBytes) {
+ return MemoryAccess.getDoubleAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public void getDoubleArray(final double[] dstArray, final int dstOffsetDoubles,
+ final int lengthDoubles) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthDoubles) << DOUBLE_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(pos, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetDoubles << DOUBLE_SHIFT, copyBytes);
+ for (int index = 0; index < lengthDoubles; index++) {
+ final double dbl = MemoryAccess.getDoubleAtIndex(srcSlice, index, NON_NATIVE_BYTE_ORDER);
+ MemoryAccess.setDoubleAtIndex(dstSlice, index, NATIVE_BYTE_ORDER, dbl);
+ }
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public float getFloat() {
+ final long pos = getPosition();
+ setPosition(pos + Float.BYTES);
+ return MemoryAccess.getFloatAtOffset(seg, pos, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public float getFloat(final long offsetBytes) {
+ return MemoryAccess.getFloatAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public void getFloatArray(final float[] dstArray, final int dstOffsetFloats,
+ final int lengthFloats) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthFloats) << FLOAT_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(pos, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetFloats << FLOAT_SHIFT, copyBytes);
+ for (int index = 0; index < lengthFloats; index++) {
+ final float flt = MemoryAccess.getFloatAtIndex(srcSlice, index, NON_NATIVE_BYTE_ORDER);
+ MemoryAccess.setFloatAtIndex(dstSlice, index, NATIVE_BYTE_ORDER, flt);
+ }
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public int getInt() {
+ final long pos = getPosition();
+ setPosition(pos + Integer.BYTES);
+ return MemoryAccess.getIntAtOffset(seg, pos, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public int getInt(final long offsetBytes) {
+ return MemoryAccess.getIntAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public void getIntArray(final int[] dstArray, final int dstOffsetInts, final int lengthInts) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthInts) << INT_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(pos, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetInts << INT_SHIFT, copyBytes);
+ for (int index = 0; index < lengthInts; index++) {
+ final int anInt = MemoryAccess.getIntAtIndex(srcSlice, index, NON_NATIVE_BYTE_ORDER);
+ MemoryAccess.setIntAtIndex(dstSlice, index, NATIVE_BYTE_ORDER, anInt);
+ }
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public long getLong() {
+ final long pos = getPosition();
+ setPosition(pos + Long.BYTES);
+ return MemoryAccess.getLongAtOffset(seg, pos, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public long getLong(final long offsetBytes) {
+ return MemoryAccess.getLongAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public void getLongArray(final long[] dstArray, final int dstOffsetLongs, final int lengthLongs) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthLongs) << LONG_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(pos, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetLongs << LONG_SHIFT, copyBytes);
+ for (int index = 0; index < lengthLongs; index++) {
+ final long aLong = MemoryAccess.getLongAtIndex(srcSlice, index, NON_NATIVE_BYTE_ORDER);
+ MemoryAccess.setLongAtIndex(dstSlice, index, NATIVE_BYTE_ORDER, aLong);
+ }
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public short getShort() {
+ final long pos = getPosition();
+ setPosition(pos + Short.BYTES);
+ return MemoryAccess.getShortAtOffset(seg, pos, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public short getShort(final long offsetBytes) {
+ return MemoryAccess.getShortAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public void getShortArray(final short[] dstArray, final int dstOffsetShorts,
+ final int lengthShorts) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthShorts) << SHORT_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(pos, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetShorts << SHORT_SHIFT, copyBytes);
+ for (int index = 0; index < lengthShorts; index++) {
+ final short aShort = MemoryAccess.getShortAtIndex(srcSlice, index, NON_NATIVE_BYTE_ORDER);
+ MemoryAccess.setShortAtIndex(dstSlice, index, NATIVE_BYTE_ORDER, aShort);
+ }
+ setPosition(pos + copyBytes);
+ }
+
+ //PRIMITIVE putX() and putXArray()
+ @Override
+ public void putChar(final char value) {
+ final long pos = getPosition();
+ MemoryAccess.setCharAtOffset(seg, pos, NON_NATIVE_BYTE_ORDER, value);
+ setPosition(pos + Character.BYTES);
+ }
+
+ @Override
+ public void putChar(final long offsetBytes, final char value) {
+ MemoryAccess.setCharAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER, value);
+ }
+
+ @Override
+ public void putCharArray(final char[] srcArray, final int srcOffsetChars, final int lengthChars) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthChars) << CHAR_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetChars << CHAR_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(pos, copyBytes);
+ for (int index = 0; index < lengthChars; index++) {
+ final char aChar = MemoryAccess.getCharAtIndex(srcSlice, index, NATIVE_BYTE_ORDER);
+ MemoryAccess.setCharAtIndex(dstSlice, index, NON_NATIVE_BYTE_ORDER, aChar);
+ }
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public void putDouble(final double value) {
+ final long pos = getPosition();
+ MemoryAccess.setDoubleAtOffset(seg, pos, NON_NATIVE_BYTE_ORDER, value);
+ setPosition(pos + Double.BYTES);
+ }
+
+ @Override
+ public void putDouble(final long offsetBytes, final double value) {
+ MemoryAccess.setDoubleAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER, value);
+ }
+
+ @Override
+ public void putDoubleArray(final double[] srcArray, final int srcOffsetDoubles,
+ final int lengthDoubles) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthDoubles) << DOUBLE_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetDoubles << DOUBLE_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(pos, copyBytes);
+ for (int index = 0; index < lengthDoubles; index++) {
+ final double dbl = MemoryAccess.getDoubleAtIndex(srcSlice, index, NATIVE_BYTE_ORDER);
+ MemoryAccess.setDoubleAtIndex(dstSlice, index, NON_NATIVE_BYTE_ORDER, dbl);
+ }
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public void putFloat(final float value) {
+ final long pos = getPosition();
+ MemoryAccess.setFloatAtOffset(seg, pos, NON_NATIVE_BYTE_ORDER, value);
+ setPosition(pos + Float.BYTES);
+ }
+
+ @Override
+ public void putFloat(final long offsetBytes, final float value) {
+ MemoryAccess.setFloatAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER, value);
+ }
+
+ @Override
+ public void putFloatArray(final float[] srcArray, final int srcOffsetFloats,
+ final int lengthFloats) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthFloats) << FLOAT_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetFloats << FLOAT_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(pos, copyBytes);
+ for (int index = 0; index < lengthFloats; index++) {
+ final float flt = MemoryAccess.getFloatAtIndex(srcSlice, index, NATIVE_BYTE_ORDER);
+ MemoryAccess.setFloatAtIndex(dstSlice, index, NON_NATIVE_BYTE_ORDER, flt);
+ }
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public void putInt(final int value) {
+ final long pos = getPosition();
+ MemoryAccess.setIntAtOffset(seg, pos, NON_NATIVE_BYTE_ORDER, value);
+ setPosition(pos + Integer.BYTES);
+ }
+
+ @Override
+ public void putInt(final long offsetBytes, final int value) {
+ MemoryAccess.setIntAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER, value);
+ }
+
+ @Override
+ public void putIntArray(final int[] srcArray, final int srcOffsetInts, final int lengthInts) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthInts) << INT_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetInts << INT_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(pos, copyBytes);
+ for (int index = 0; index < lengthInts; index++) {
+ final int anInt = MemoryAccess.getIntAtIndex(srcSlice, index, NATIVE_BYTE_ORDER);
+ MemoryAccess.setIntAtIndex(dstSlice, index, NON_NATIVE_BYTE_ORDER, anInt);
+ }
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public void putLong(final long value) {
+ final long pos = getPosition();
+ MemoryAccess.setLongAtOffset(seg, pos, NON_NATIVE_BYTE_ORDER, value);
+ setPosition(pos + Long.BYTES);
+ }
+
+ @Override
+ public void putLong(final long offsetBytes, final long value) {
+ MemoryAccess.setLongAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER, value);
+ }
+
+ @Override
+ public void putLongArray(final long[] srcArray, final int srcOffsetLongs, final int lengthLongs) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthLongs) << LONG_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetLongs << LONG_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(pos, copyBytes);
+ for (int index = 0; index < lengthLongs; index++) {
+ final long aLong = MemoryAccess.getLongAtIndex(srcSlice, index, NATIVE_BYTE_ORDER);
+ MemoryAccess.setLongAtIndex(dstSlice, index, NON_NATIVE_BYTE_ORDER, aLong);
+ }
+ setPosition(pos + copyBytes);
+ }
+
+ @Override
+ public void putShort(final short value) {
+ final long pos = getPosition();
+ MemoryAccess.setShortAtOffset(seg, pos, NON_NATIVE_BYTE_ORDER, value);
+ setPosition(pos + Short.BYTES);
+ }
+
+ @Override
+ public void putShort(final long offsetBytes, final short value) {
+ MemoryAccess.setShortAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER, value);
+ }
+
+ @Override
+ public void putShortArray(final short[] srcArray, final int srcOffsetShorts,
+ final int lengthShorts) {
+ final long pos = getPosition();
+ final long copyBytes = ((long) lengthShorts) << SHORT_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetShorts << SHORT_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(pos, copyBytes);
+ for (int index = 0; index < lengthShorts; index++) {
+ final short aShort = MemoryAccess.getShortAtIndex(srcSlice, index, NATIVE_BYTE_ORDER);
+ MemoryAccess.setShortAtIndex(dstSlice, index, NON_NATIVE_BYTE_ORDER, aShort);
+ }
+ setPosition(pos + copyBytes);
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImpl.java b/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImpl.java
new file mode 100644
index 0000000..eb3ce8b
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImpl.java
@@ -0,0 +1,260 @@
+/*
+ * 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 org.apache.datasketches.memory.WritableMemory;
+
+import jdk.incubator.foreign.MemoryAccess;
+import jdk.incubator.foreign.MemorySegment;
+
+/*
+ * Developer notes: The heavier methods, such as put/get arrays, duplicate, region, clear, fill,
+ * compareTo, etc., use hard checks (checkValid*() and checkBounds()), which execute at runtime and
+ * throw exceptions if violated. The cost of the runtime checks are minor compared to the rest of
+ * the work these methods are doing.
+ *
+ * <p>The light weight methods, such as put/get primitives, use asserts (assertValid*()), which only
+ * execute when asserts are enabled and JIT will remove them entirely from production runtime code.
+ * The light weight methods will simplify to a single unsafe call, which is further simplified by
+ * JIT to an intrinsic that is often a single CPU instruction.
+ */
+
+/**
+ * Implementation of {@link WritableMemory} for non-native endian byte order.
+ * @author Roman Leventov
+ * @author Lee Rhodes
+ */
+@SuppressWarnings("restriction")
+abstract class NonNativeWritableMemoryImpl extends BaseWritableMemoryImpl {
+
+ //Pass-through ctor
+ NonNativeWritableMemoryImpl(
+ final MemorySegment seg,
+ final int typeId) {
+ super(seg, typeId);
+ }
+
+ ///PRIMITIVE getX() and getXArray()
+ @Override
+ public char getChar(final long offsetBytes) {
+ return MemoryAccess.getCharAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public void getCharArray(final long offsetBytes, final char[] dstArray,
+ final int dstOffsetChars, final int lengthChars) {
+ final long copyBytes = ((long) lengthChars) << CHAR_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(offsetBytes, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetChars << CHAR_SHIFT, copyBytes);
+ for (int index = 0; index < lengthChars; index++) {
+ final char aChar = MemoryAccess.getCharAtIndex(srcSlice, index, NON_NATIVE_BYTE_ORDER);
+ MemoryAccess.setCharAtIndex(dstSlice, index, NATIVE_BYTE_ORDER, aChar);
+ }
+ }
+
+ @Override
+ public double getDouble(final long offsetBytes) {
+ return MemoryAccess.getDoubleAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public void getDoubleArray(final long offsetBytes, final double[] dstArray,
+ final int dstOffsetDoubles, final int lengthDoubles) {
+ final long copyBytes = ((long) lengthDoubles) << DOUBLE_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(offsetBytes, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetDoubles << DOUBLE_SHIFT, copyBytes);
+ for (int index = 0; index < lengthDoubles; index++) {
+ final double dbl = MemoryAccess.getDoubleAtIndex(srcSlice, index, NON_NATIVE_BYTE_ORDER);
+ MemoryAccess.setDoubleAtIndex(dstSlice, index, NATIVE_BYTE_ORDER, dbl);
+ }
+ }
+
+ @Override
+ public float getFloat(final long offsetBytes) {
+ return MemoryAccess.getFloatAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public void getFloatArray(final long offsetBytes, final float[] dstArray,
+ final int dstOffsetFloats, final int lengthFloats) {
+ final long copyBytes = ((long) lengthFloats) << FLOAT_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(offsetBytes, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetFloats << FLOAT_SHIFT, copyBytes);
+ for (int index = 0; index < lengthFloats; index++) {
+ final float flt = MemoryAccess.getFloatAtIndex(srcSlice, index, NON_NATIVE_BYTE_ORDER);
+ MemoryAccess.setFloatAtIndex(dstSlice, index, NATIVE_BYTE_ORDER, flt);
+ }
+ }
+
+ @Override
+ public int getInt(final long offsetBytes) {
+ return MemoryAccess.getIntAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public void getIntArray(final long offsetBytes, final int[] dstArray, final int dstOffsetInts,
+ final int lengthInts) {
+ final long copyBytes = ((long) lengthInts) << INT_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(offsetBytes, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetInts << INT_SHIFT, copyBytes);
+ for (int index = 0; index < lengthInts; index++) {
+ final int anInt = MemoryAccess.getIntAtIndex(srcSlice, index, NON_NATIVE_BYTE_ORDER);
+ MemoryAccess.setIntAtIndex(dstSlice, index, NATIVE_BYTE_ORDER, anInt);
+ }
+ }
+
+ @Override
+ public long getLong(final long offsetBytes) {
+ return MemoryAccess.getLongAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public void getLongArray(final long offsetBytes, final long[] dstArray,
+ final int dstOffsetLongs, final int lengthLongs) {
+ final long copyBytes = ((long) lengthLongs) << LONG_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(offsetBytes, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetLongs << LONG_SHIFT, copyBytes);
+ for (int index = 0; index < lengthLongs; index++) {
+ final long aLong = MemoryAccess.getLongAtIndex(srcSlice, index, NON_NATIVE_BYTE_ORDER);
+ MemoryAccess.setLongAtIndex(dstSlice, index, NATIVE_BYTE_ORDER, aLong);
+ }
+ }
+
+ @Override
+ public short getShort(final long offsetBytes) {
+ return MemoryAccess.getShortAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER);
+ }
+
+ @Override
+ public void getShortArray(final long offsetBytes, final short[] dstArray,
+ final int dstOffsetShorts, final int lengthShorts) {
+ final long copyBytes = ((long) lengthShorts) << SHORT_SHIFT;
+ MemorySegment srcSlice = seg.asSlice(offsetBytes, copyBytes);
+ MemorySegment dstSlice = MemorySegment.ofArray(dstArray).asSlice(dstOffsetShorts << SHORT_SHIFT, copyBytes);
+ for (int index = 0; index < lengthShorts; index++) {
+ final short aShort = MemoryAccess.getShortAtIndex(srcSlice, index, NON_NATIVE_BYTE_ORDER);
+ MemoryAccess.setShortAtIndex(dstSlice, index, NATIVE_BYTE_ORDER, aShort);
+ }
+ }
+
+ //PRIMITIVE putX() and putXArray() implementations
+ @Override
+ public void putChar(final long offsetBytes, final char value) {
+ MemoryAccess.setCharAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER, value);
+ }
+
+ @Override
+ public void putCharArray(final long offsetBytes, final char[] srcArray, final int srcOffsetChars,
+ final int lengthChars) {
+ final long copyBytes = ((long) lengthChars) << CHAR_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetChars << CHAR_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(offsetBytes, copyBytes);
+ for (int index = 0; index < lengthChars; index++) {
+ final char aChar = MemoryAccess.getCharAtIndex(srcSlice, index, NATIVE_BYTE_ORDER);
+ MemoryAccess.setCharAtIndex(dstSlice, index, NON_NATIVE_BYTE_ORDER, aChar);
+ }
+ }
+
+ @Override
+ public void putDouble(final long offsetBytes, final double value) {
+ MemoryAccess.setDoubleAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER, value);
+ }
+
+ @Override
+ public void putDoubleArray(final long offsetBytes, final double[] srcArray,
+ final int srcOffsetDoubles, final int lengthDoubles) {
+ final long copyBytes = ((long) lengthDoubles) << DOUBLE_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetDoubles << DOUBLE_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(offsetBytes, copyBytes);
+ for (int index = 0; index < lengthDoubles; index++) {
+ final double dbl = MemoryAccess.getDoubleAtIndex(srcSlice, index, NATIVE_BYTE_ORDER);
+ MemoryAccess.setDoubleAtIndex(dstSlice, index, NON_NATIVE_BYTE_ORDER, dbl);
+ }
+ }
+
+ @Override
+ public void putFloat(final long offsetBytes, final float value) {
+ MemoryAccess.setFloatAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER, value);
+ }
+
+ @Override
+ public void putFloatArray(final long offsetBytes, final float[] srcArray,
+ final int srcOffsetFloats, final int lengthFloats) {
+ final long copyBytes = ((long) lengthFloats) << FLOAT_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetFloats << FLOAT_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(offsetBytes, copyBytes);
+ for (int index = 0; index < lengthFloats; index++) {
+ final float flt = MemoryAccess.getFloatAtIndex(srcSlice, index, NATIVE_BYTE_ORDER);
+ MemoryAccess.setFloatAtIndex(dstSlice, index, NON_NATIVE_BYTE_ORDER, flt);
+ }
+ }
+
+ @Override
+ public void putInt(final long offsetBytes, final int value) {
+ MemoryAccess.setIntAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER, value);
+ }
+
+ @Override
+ public void putIntArray(final long offsetBytes, final int[] srcArray, final int srcOffsetInts,
+ final int lengthInts) {
+ final long copyBytes = ((long) lengthInts) << INT_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetInts << INT_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(offsetBytes, copyBytes);
+ for (int index = 0; index < lengthInts; index++) {
+ final int anInt = MemoryAccess.getIntAtIndex(srcSlice, index, NATIVE_BYTE_ORDER);
+ MemoryAccess.setIntAtIndex(dstSlice, index, NON_NATIVE_BYTE_ORDER, anInt);
+ }
+ }
+
+ @Override
+ public void putLong(final long offsetBytes, final long value) {
+ MemoryAccess.setLongAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER, value);
+ }
+
+ @Override
+ public void putLongArray(final long offsetBytes, final long[] srcArray, final int srcOffsetLongs,
+ final int lengthLongs) {
+ final long copyBytes = ((long) lengthLongs) << LONG_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetLongs << LONG_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(offsetBytes, copyBytes);
+ for (int index = 0; index < lengthLongs; index++) {
+ final long aLong = MemoryAccess.getLongAtIndex(srcSlice, index, NATIVE_BYTE_ORDER);
+ MemoryAccess.setLongAtIndex(dstSlice, index, NON_NATIVE_BYTE_ORDER, aLong);
+ }
+ }
+
+ @Override
+ public void putShort(final long offsetBytes, final short value) {
+ MemoryAccess.setShortAtOffset(seg, offsetBytes, NON_NATIVE_BYTE_ORDER, value);
+ }
+
+ @Override
+ public void putShortArray(final long offsetBytes, final short[] srcArray,
+ final int srcOffsetShorts, final int lengthShorts) {
+ final long copyBytes = ((long) lengthShorts) << SHORT_SHIFT;
+ MemorySegment srcSlice = MemorySegment.ofArray(srcArray).asSlice(srcOffsetShorts << SHORT_SHIFT, copyBytes);
+ MemorySegment dstSlice = seg.asSlice(offsetBytes, copyBytes);
+ for (int index = 0; index < lengthShorts; index++) {
+ final short aShort = MemoryAccess.getShortAtIndex(srcSlice, index, NATIVE_BYTE_ORDER);
+ MemoryAccess.setShortAtIndex(dstSlice, index, NON_NATIVE_BYTE_ORDER, aShort);
+ }
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/Util.java b/src/main/java/org/apache/datasketches/memory/internal/Util.java
new file mode 100644
index 0000000..1c1bed2
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/Util.java
@@ -0,0 +1,349 @@
+/*
+ * 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.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.ByteOrder;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Objects;
+import java.util.Random;
+
+import org.apache.datasketches.memory.Memory;
+
+/**
+ * @author Lee Rhodes
+ */
+@SuppressWarnings("javadoc")
+public final class Util {
+ public static final String LS = System.getProperty("line.separator");
+
+ /**
+ * Don't use sun.misc.Unsafe#copyMemory to copy blocks of memory larger than this
+ * threshold, because internally it doesn't have safepoint polls, that may cause long
+ * "Time To Safe Point" pauses in the application. This has been fixed in JDK 9 (see
+ * https://bugs.openjdk.java.net/browse/JDK-8149596 and
+ * https://bugs.openjdk.java.net/browse/JDK-8141491), but not in JDK 8, so the Memory library
+ * should keep having this boilerplate as long as it supports Java 8.
+ *
+ * <p>A reference to this can be found in java.nio.Bits.</p>
+ */
+ public static final int UNSAFE_COPY_THRESHOLD_BYTES = 1024 * 1024;
+
+ private Util() { }
+
+ //Byte Order Related
+
+ /**
+ * Returns true if the given byteOrder is the same as the native byte order.
+ * @param byteOrder the given byte order
+ * @return true if the given byteOrder is the same as the native byte order.
+ */
+ public static boolean isNativeByteOrder(final ByteOrder byteOrder) {
+ if (byteOrder == null) {
+ throw new IllegalArgumentException("ByteOrder parameter cannot be null.");
+ }
+ return ByteOrder.nativeOrder() == byteOrder;
+ }
+
+
+ /**
+ * Searches a range of the specified array of longs for the specified value using the binary
+ * search algorithm. The range must be sorted method) prior to making this call.
+ * If it is not sorted, the results are undefined. If the range contains
+ * multiple elements with the specified value, there is no guarantee which one will be found.
+ * @param mem the Memory to be searched
+ * @param fromLongIndex the index of the first element (inclusive) to be searched
+ * @param toLongIndex the index of the last element (exclusive) to be searched
+ * @param key the value to be searched for
+ * @return index of the search key, if it is contained in the array within the specified range;
+ * otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which
+ * the key would be inserted into the array: the index of the first element in the range greater
+ * than the key, or toIndex if all elements in the range are less than the specified key.
+ * Note that this guarantees that the return value will be ≥ 0 if and only if the key is found.
+ */
+ public static long binarySearchLongs(final Memory mem, final long fromLongIndex,
+ final long toLongIndex, final long key) {
+ //UnsafeUtil.checkBounds(fromLongIndex << 3, (toLongIndex - fromLongIndex) << 3, mem.getCapacity());
+ long low = fromLongIndex;
+ long high = toLongIndex - 1L;
+
+ while (low <= high) {
+ final long mid = (low + high) >>> 1;
+ final long midVal = mem.getLong(mid << 3);
+
+ if (midVal < key) { low = mid + 1; }
+ else if (midVal > key) { high = mid - 1; }
+ else { return mid; } // key found
+ }
+ return -(low + 1); // key not found.
+ }
+
+ /**
+ * Prepend the given string with zeros. If the given string is equal or greater than the given
+ * field length, it will be returned without modification.
+ * @param s the given string
+ * @param fieldLength desired total field length including the given string
+ * @return the given string prepended with zeros.
+ */
+ public static final String zeroPad(final String s, final int fieldLength) {
+ return characterPad(s, fieldLength, '0', false);
+ }
+
+ /**
+ * Prepend or postpend the given string with the given character to fill the given field length.
+ * If the given string is equal or greater than the given field length, it will be returned
+ * without modification.
+ * @param s the given string
+ * @param fieldLength the desired field length
+ * @param padChar the desired pad character
+ * @param postpend if true append the pacCharacters to the end of the string.
+ * @return prepended or postpended given string with the given character to fill the given field
+ * length.
+ */
+ public static final String characterPad(final String s, final int fieldLength,
+ final char padChar, final boolean postpend) {
+ final char[] chArr = s.toCharArray();
+ final int sLen = chArr.length;
+ if (sLen < fieldLength) {
+ final char[] out = new char[fieldLength];
+ final int blanks = fieldLength - sLen;
+
+ if (postpend) {
+ for (int i = 0; i < sLen; i++) {
+ out[i] = chArr[i];
+ }
+ for (int i = sLen; i < fieldLength; i++) {
+ out[i] = padChar;
+ }
+ } else { //prepend
+ for (int i = 0; i < blanks; i++) {
+ out[i] = padChar;
+ }
+ for (int i = blanks; i < fieldLength; i++) {
+ out[i] = chArr[i - blanks];
+ }
+ }
+
+ return String.valueOf(out);
+ }
+ return s;
+ }
+
+ /**
+ * Return true if all the masked bits of value are zero
+ * @param value the value to be tested
+ * @param bitMask defines the bits of interest
+ * @return true if all the masked bits of value are zero
+ */
+ public static final boolean isAllBitsClear(final long value, final long bitMask) {
+ return (~value & bitMask) == bitMask;
+ }
+
+ /**
+ * Return true if all the masked bits of value are one
+ * @param value the value to be tested
+ * @param bitMask defines the bits of interest
+ * @return true if all the masked bits of value are one
+ */
+ public static final boolean isAllBitsSet(final long value, final long bitMask) {
+ return (value & bitMask) == bitMask;
+ }
+
+ /**
+ * Return true if any the masked bits of value are zero
+ * @param value the value to be tested
+ * @param bitMask defines the bits of interest
+ * @return true if any the masked bits of value are zero
+ */
+ public static final boolean isAnyBitsClear(final long value, final long bitMask) {
+ return (~value & bitMask) != 0;
+ }
+
+ /**
+ * Return true if any the masked bits of value are one
+ * @param value the value to be tested
+ * @param bitMask defines the bits of interest
+ * @return true if any the masked bits of value are one
+ */
+ public static final boolean isAnyBitsSet(final long value, final long bitMask) {
+ return (value & bitMask) != 0;
+ }
+
+ /**
+ * Creates random valid Character Code Points (as integers). By definition, valid CodePoints
+ * are integers in the range 0 to Character.MAX_CODE_POINT, and exclude the surrogate values.
+ * This is used in unit testing and characterization testing of the UTF8 class. Because the
+ * characterization tools are in a separate package, this must remain public.
+ *
+ * @author Lee Rhodes
+ */
+ public static class RandomCodePoints {
+ private Random rand; //
+ private static final int ALL_CP = Character.MAX_CODE_POINT + 1;
+ private static final int MIN_SUR = Character.MIN_SURROGATE;
+ private static final int MAX_SUR = Character.MAX_SURROGATE;
+
+ /**
+ * @param deterministic if true, configure java.util.Random with a fixed seed.
+ */
+ public RandomCodePoints(final boolean deterministic) {
+ rand = deterministic ? new Random(0) : new Random();
+ }
+
+ /**
+ * Fills the given array with random valid Code Points from 0, inclusive, to
+ * <i>Character.MAX_CODE_POINT</i>, inclusive.
+ * The surrogate range, which is from <i>Character.MIN_SURROGATE</i>, inclusive, to
+ * <i>Character.MAX_SURROGATE</i>, inclusive, is always <i>excluded</i>.
+ * @param cpArr the array to fill
+ */
+ public final void fillCodePointArray(final int[] cpArr) {
+ fillCodePointArray(cpArr, 0, ALL_CP);
+ }
+
+ /**
+ * Fills the given array with random valid Code Points from <i>startCP</i>, inclusive, to
+ * <i>endCP</i>, exclusive.
+ * The surrogate range, which is from <i>Character.MIN_SURROGATE</i>, inclusive, to
+ * <i>Character.MAX_SURROGATE</i>, inclusive, is always <u>excluded</u>.
+ * @param cpArr the array to fill
+ * @param startCP the starting Code Point, included.
+ * @param endCP the ending Code Point, excluded. This value cannot exceed 0x110000.
+ */
+ public final void fillCodePointArray(final int[] cpArr, final int startCP, final int endCP) {
+ final int arrLen = cpArr.length;
+ final int numCP = Math.min(endCP, 0X110000) - Math.min(0, startCP);
+ int idx = 0;
+ while (idx < arrLen) {
+ final int cp = startCP + rand.nextInt(numCP);
+ if ((cp >= MIN_SUR) && (cp <= MAX_SUR)) {
+ continue;
+ }
+ cpArr[idx++] = cp;
+ }
+ }
+
+ /**
+ * Return a single valid random Code Point from 0, inclusive, to
+ * <i>Character.MAX_CODE_POINT</i>, inclusive.
+ * The surrogate range, which is from <i>Character.MIN_SURROGATE</i>, inclusive, to
+ * <i>Character.MAX_SURROGATE</i>, inclusive, is always <u>excluded</u>.
+ * @return a single valid random CodePoint.
+ */
+ public final int getCodePoint() {
+ return getCodePoint(0, ALL_CP);
+ }
+
+ /**
+ * Return a single valid random Code Point from <i>startCP</i>, inclusive, to
+ * <i>endCP</i>, exclusive.
+ * The surrogate range, which is from <i>Character.MIN_SURROGATE</i>, inclusive, to
+ * <i>Character.MAX_SURROGATE</i>, inclusive, is always <u>excluded</u>.
+ * @param startCP the starting Code Point, included.
+ * @param endCP the ending Code Point, excluded. This value cannot exceed 0x110000.
+ * @return a single valid random CodePoint.
+ */
+ public final int getCodePoint(final int startCP, final int endCP) {
+ final int numCP = Math.min(endCP, 0X110000) - Math.min(0, startCP);
+ while (true) {
+ final int cp = startCP + rand.nextInt(numCP);
+ if ((cp < MIN_SUR) || (cp > MAX_SUR)) {
+ return cp;
+ }
+ }
+ }
+ } //End class RandomCodePoints
+
+ public static final void zeroCheck(final long value, final String arg) {
+ if (value <= 0) {
+ throw new IllegalArgumentException("The argument '" + arg + "' may not be negative or zero.");
+ }
+ }
+
+ public static final void negativeCheck(final long value, final String arg) {
+ if (value < 0) {
+ throw new IllegalArgumentException("The argument '" + arg + "' may not be negative.");
+ }
+ }
+
+ public static final void nullCheck(final Object obj, final String arg) {
+ if (obj == null) {
+ throw new IllegalArgumentException("The argument '" + arg + "' may not be null.");
+ }
+ }
+
+ //Resources NOTE: these 3 methods are duplicated in Java/ datasketches/Util
+
+ /**
+ * Gets the absolute path of the given resource file's shortName.
+ *
+ * <p>Note that the ClassLoader.getResource(shortName) returns a URL,
+ * which can have special characters, e.g., "%20" for spaces. This method
+ * obtains the URL, converts it to a URI, then does a uri.getPath(), which
+ * decodes any special characters in the URI path. This is required to make
+ * obtaining resources operating-system independent.</p>
+ *
+ * @param shortFileName the last name in the pathname's name sequence.
+ * @return the absolute path of the given resource file's shortName.
+ */
+ public static String getResourcePath(final String shortFileName) {
+ Objects.requireNonNull(shortFileName, "input parameter " + shortFileName + " cannot be null.");
+ try {
+ final URL url = Util.class.getClassLoader().getResource(shortFileName);
+ Objects.requireNonNull(url, "resource " + shortFileName + " could not be acquired.");
+ final URI uri = url.toURI();
+ //decodes any special characters
+ final String path = uri.isAbsolute() ? Paths.get(uri).toAbsolutePath().toString() : uri.getPath();
+ return path;
+ } catch (final URISyntaxException e) {
+ throw new IllegalArgumentException("Cannot find resource: " + shortFileName + LS + e);
+ }
+ }
+
+ /**
+ * Gets the file defined by the given resource file's shortFileName.
+ * @param shortFileName the last name in the pathname's name sequence.
+ * @return the file defined by the given resource file's shortFileName.
+ */
+ public static File getResourceFile(final String shortFileName) {
+ return new File(getResourcePath(shortFileName));
+ }
+
+ /**
+ * Returns a byte array of the contents of the file defined by the given resource file's
+ * shortFileName.
+ * @param shortFileName the last name in the pathname's name sequence.
+ * @return a byte array of the contents of the file defined by the given resource file's
+ * shortFileName.
+ */
+ public static byte[] getResourceBytes(final String shortFileName) {
+ try {
+ return Files.readAllBytes(Paths.get(getResourcePath(shortFileName)));
+ } catch (final IOException e) {
+ throw new IllegalArgumentException("Cannot read resource: " + shortFileName + LS + e);
+ }
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/XxHash64.java b/src/main/java/org/apache/datasketches/memory/internal/XxHash64.java
new file mode 100644
index 0000000..76ce3e0
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/XxHash64.java
@@ -0,0 +1,308 @@
+/*
+ * 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 static org.apache.datasketches.memory.internal.BaseStateImpl.CHAR_SHIFT;
+import static org.apache.datasketches.memory.internal.BaseStateImpl.DOUBLE_SHIFT;
+import static org.apache.datasketches.memory.internal.BaseStateImpl.FLOAT_SHIFT;
+import static org.apache.datasketches.memory.internal.BaseStateImpl.INT_SHIFT;
+import static org.apache.datasketches.memory.internal.BaseStateImpl.LONG_SHIFT;
+import static org.apache.datasketches.memory.internal.BaseStateImpl.SHORT_SHIFT;
+import static jdk.incubator.foreign.MemoryAccess.*;
+import jdk.incubator.foreign.MemorySegment;
+
+/**
+ * The XxHash is a fast, non-cryptographic, 64-bit hash function that has
+ * excellent avalanche and 2-way bit independence properties.
+ * This java version adapted from the C++ version and the OpenHFT/Zero-Allocation-Hashing implementation
+ * referenced below as inspiration.
+ *
+ * <p>The C++ source repository:
+ * <a href="https://github.com/Cyan4973/xxHash">
+ * https://github.com/Cyan4973/xxHash</a>. It has a BSD 2-Clause License:
+ * <a href="http://www.opensource.org/licenses/bsd-license.php">
+ * http://www.opensource.org/licenses/bsd-license.php</a>. See LICENSE.
+ *
+ * <p>Portions of this code were adapted from
+ * <a href="https://github.com/OpenHFT/Zero-Allocation-Hashing/blob/master/src/main/java/net/openhft/hashing/XxHash.java">
+ * OpenHFT/Zero-Allocation-Hashing</a>, which has an Apache 2 license as does this site. See LICENSE.
+ *
+ * @author Lee Rhodes
+ */
+public class XxHash64 {
+ // Unsigned, 64-bit primes
+ private static final long P1 = -7046029288634856825L;
+ private static final long P2 = -4417276706812531889L;
+ private static final long P3 = 1609587929392839161L;
+ private static final long P4 = -8796714831421723037L;
+ private static final long P5 = 2870177450012600261L;
+
+ /**
+ * Returns the 64-bit hash of the sequence of bytes in the given MemorySegment
+ *
+ * @param seg A reference to the relevant MemorySegment.
+ * @param offsetBytes offset in bytes in the given segment.
+ * @param lengthBytes the length in bytes to be hashed
+ * @param seed a given seed
+ * @return the 64-bit hash of the sequence of bytes.
+ */
+ static long hash(final MemorySegment seg, long offsetBytes, final long lengthBytes, final long seed) {
+ long hash;
+ long remaining = lengthBytes;
+
+ if (remaining >= 32) {
+ long v1 = seed + P1 + P2;
+ long v2 = seed + P2;
+ long v3 = seed;
+ long v4 = seed - P1;
+
+ do {
+ v1 += getLongAtOffset(seg, offsetBytes) * P2;
+ v1 = Long.rotateLeft(v1, 31);
+ v1 *= P1;
+
+ v2 += getLongAtOffset(seg, offsetBytes + 8L) * P2;
+ v2 = Long.rotateLeft(v2, 31);
+ v2 *= P1;
+
+ v3 += getLongAtOffset(seg, offsetBytes + 16L) * P2;
+ v3 = Long.rotateLeft(v3, 31);
+ v3 *= P1;
+
+ v4 += getLongAtOffset(seg, offsetBytes + 24L) * P2;
+ v4 = Long.rotateLeft(v4, 31);
+ v4 *= P1;
+
+ offsetBytes += 32;
+ remaining -= 32;
+ } while (remaining >= 32);
+
+ hash = Long.rotateLeft(v1, 1)
+ + Long.rotateLeft(v2, 7)
+ + Long.rotateLeft(v3, 12)
+ + Long.rotateLeft(v4, 18);
+
+ v1 *= P2;
+ v1 = Long.rotateLeft(v1, 31);
+ v1 *= P1;
+ hash ^= v1;
+ hash = (hash * P1) + P4;
+
+ v2 *= P2;
+ v2 = Long.rotateLeft(v2, 31);
+ v2 *= P1;
+ hash ^= v2;
+ hash = (hash * P1) + P4;
+
+ v3 *= P2;
+ v3 = Long.rotateLeft(v3, 31);
+ v3 *= P1;
+ hash ^= v3;
+ hash = (hash * P1) + P4;
+
+ v4 *= P2;
+ v4 = Long.rotateLeft(v4, 31);
+ v4 *= P1;
+ hash ^= v4;
+ hash = (hash * P1) + P4;
+ } //end remaining >= 32
+ else {
+ hash = seed + P5;
+ }
+
+ hash += lengthBytes;
+
+ while (remaining >= 8) {
+ long k1 = getLongAtOffset(seg, offsetBytes);
+ k1 *= P2;
+ k1 = Long.rotateLeft(k1, 31);
+ k1 *= P1;
+ hash ^= k1;
+ hash = (Long.rotateLeft(hash, 27) * P1) + P4;
+ offsetBytes += 8;
+ remaining -= 8;
+ }
+
+ if (remaining >= 4) { //treat as unsigned ints
+ hash ^= (getIntAtOffset(seg, offsetBytes) & 0XFFFF_FFFFL) * P1;
+ hash = (Long.rotateLeft(hash, 23) * P2) + P3;
+ offsetBytes += 4;
+ remaining -= 4;
+ }
+
+ while (remaining != 0) { //treat as unsigned bytes
+ hash ^= (getByteAtOffset(seg, offsetBytes) & 0XFFL) * P5;
+ hash = Long.rotateLeft(hash, 11) * P1;
+ --remaining;
+ ++offsetBytes;
+ }
+
+ return finalize(hash);
+ }
+
+ /**
+ * Returns a 64-bit hash from a single long. This method has been optimized for speed when only
+ * a single hash of a long is required.
+ * @param in A long.
+ * @param seed A long valued seed.
+ * @return the hash.
+ */
+ public static long hash(final long in, final long seed) {
+ long hash = seed + P5;
+ hash += 8;
+ long k1 = in;
+ k1 *= P2;
+ k1 = Long.rotateLeft(k1, 31);
+ k1 *= P1;
+ hash ^= k1;
+ hash = (Long.rotateLeft(hash, 27) * P1) + P4;
+ return finalize(hash);
+ }
+
+ private static long finalize(long hash) {
+ hash ^= hash >>> 33;
+ hash *= P2;
+ hash ^= hash >>> 29;
+ hash *= P3;
+ hash ^= hash >>> 32;
+ return hash;
+ }
+
+ /**
+ * Hash the given arr starting at the given offset and continuing for the given length using the
+ * given seed.
+ * @param arr the given array
+ * @param offsetBytes starting at this offset
+ * @param lengthBytes continuing for this length
+ * @param seed the given seed
+ * @return the hash
+ */
+ public static long hashBytes(final byte[] arr, final int offsetBytes,
+ final int lengthBytes, final long seed) {
+ MemorySegment seg = MemorySegment.ofArray(arr);
+ return hash(seg, offsetBytes, lengthBytes, seed);
+ }
+
+ /**
+ * Hash the given arr starting at the given offset and continuing for the given length using the
+ * given seed.
+ * @param arr the given array
+ * @param offsetShorts starting at this offset
+ * @param lengthShorts continuing for this length
+ * @param seed the given seed
+ * @return the hash
+ */
+ public static long hashShorts(final short[] arr, final int offsetShorts,
+ final int lengthShorts, final long seed) {
+ MemorySegment seg = MemorySegment.ofArray(arr);
+ return hash(seg, (offsetShorts << SHORT_SHIFT), lengthShorts << SHORT_SHIFT, seed);
+ }
+
+ /**
+ * Hash the given arr starting at the given offset and continuing for the given length using the
+ * given seed.
+ * @param arr the given array
+ * @param offsetChars starting at this offset
+ * @param lengthChars continuing for this length
+ * @param seed the given seed
+ * @return the hash
+ */
+ public static long hashChars(final char[] arr, final int offsetChars,
+ final int lengthChars, final long seed) {
+ MemorySegment seg = MemorySegment.ofArray(arr);
+ return hash(seg, offsetChars << CHAR_SHIFT, lengthChars << CHAR_SHIFT, seed);
+ }
+
+ /**
+ * Hash the given arr starting at the given offset and continuing for the given length using the
+ * given seed.
+ * @param arr the given array
+ * @param offsetInts starting at this offset
+ * @param lengthInts continuing for this length
+ * @param seed the given seed
+ * @return the hash
+ */
+ public static long hashInts(final int[] arr, final int offsetInts,
+ final int lengthInts, final long seed) {
+ MemorySegment seg = MemorySegment.ofArray(arr);
+ return hash(seg, offsetInts << INT_SHIFT, lengthInts << INT_SHIFT, seed);
+ }
+
+ /**
+ * Hash the given arr starting at the given offset and continuing for the given length using the
+ * given seed.
+ * @param arr the given array
+ * @param offsetLongs starting at this offset
+ * @param lengthLongs continuing for this length
+ * @param seed the given seed
+ * @return the hash
+ */
+ public static long hashLongs(final long[] arr, final int offsetLongs,
+ final int lengthLongs, final long seed) {
+ MemorySegment seg = MemorySegment.ofArray(arr);
+ return hash(seg, offsetLongs << LONG_SHIFT, lengthLongs << LONG_SHIFT, seed);
+ }
+
+ /**
+ * Hash the given arr starting at the given offset and continuing for the given length using the
+ * given seed.
+ * @param arr the given array
+ * @param offsetFloats starting at this offset
+ * @param lengthFloats continuing for this length
+ * @param seed the given seed
+ * @return the hash
+ */
+ public static long hashFloats(final float[] arr, final int offsetFloats,
+ final int lengthFloats, final long seed) {
+ MemorySegment seg = MemorySegment.ofArray(arr);
+ return hash(seg, offsetFloats << FLOAT_SHIFT, lengthFloats << FLOAT_SHIFT, seed);
+ }
+
+ /**
+ * Hash the given arr starting at the given offset and continuing for the given length using the
+ * given seed.
+ * @param arr the given array
+ * @param offsetDoubles starting at this offset
+ * @param lengthDoubles continuing for this length
+ * @param seed the given seed
+ * @return the hash
+ */
+ public static long hashDoubles(final double[] arr, final int offsetDoubles,
+ final int lengthDoubles, final long seed) {
+ MemorySegment seg = MemorySegment.ofArray(arr);
+ return hash(seg, offsetDoubles << DOUBLE_SHIFT, lengthDoubles << DOUBLE_SHIFT, seed);
+ }
+
+ /**
+ * Hash the given arr starting at the given offset and continuing for the given length using the
+ * given seed.
+ * @param str the given string
+ * @param offsetChars starting at this offset
+ * @param lengthChars continuing for this length
+ * @param seed the given seed
+ * @return the hash
+ */
+ public static long hashString(final String str, final int offsetChars,
+ final int lengthChars, final long seed) {
+ MemorySegment seg = MemorySegment.ofArray(str.toCharArray());
+ return hash(seg, offsetChars << CHAR_SHIFT, lengthChars << CHAR_SHIFT, seed);
+ }
+
+}
diff --git a/src/main/java/org/apache/datasketches/memory/internal/package-info.java b/src/main/java/org/apache/datasketches/memory/internal/package-info.java
new file mode 100644
index 0000000..2a3bb7a
--- /dev/null
+++ b/src/main/java/org/apache/datasketches/memory/internal/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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;
diff --git a/src/main/java/org/apache/datasketches/memory/package-info.java b/src/main/java/org/apache/datasketches/memory/package-info.java
index 4dd76ca..87b0a0f 100644
--- a/src/main/java/org/apache/datasketches/memory/package-info.java
+++ b/src/main/java/org/apache/datasketches/memory/package-info.java
@@ -53,8 +53,6 @@
*
* <li>Improved performance over the prior Memory implementation.</li>
*
- * <li>Cleaner internal architecture, which will make it easier to extend in the future.</li>
- *
* <li>No external dependencies, which makes it simple to install in virtually any Java environment.
* </li>
* </ul>
@@ -82,72 +80,7 @@
* <p>The resources don't know or care about the access APIs, and the access
* APIs don't really know or care what resource they are accessing.</p>
*
- * <p>An access API is joined with
- * a resource either with a static factory method or in combination with a
- * {@link org.apache.datasketches.memory.Handle}, which is used exclusively for resources that are
- * external to the JVM, such as allocation of direct memory and memory-mapped files.</p>
- *
- * <p>The role of a Handle is to hold onto the reference of a resource that is outside the control
- * of the JVM. The resource is obtained from the handle with {@code get()}.</p>
- *
- * <p>When a handle is extended for an AutoCloseable resource and then joined with an access API
- * it becomes an <i>implementation handle</i>. There are 3 implementation handles:</p>
- *
- * <ul><li>{@link org.apache.datasketches.memory.MapHandle}
- * for read-only access to a memory-mapped file</li>
- * <li>{@link org.apache.datasketches.memory.WritableMapHandle}
- * for writable access to a memory-mapped file</li>
- * <li>{@link org.apache.datasketches.memory.WritableHandle}
- * for writable access to off-heap memory.</li>
- * </ul>
- *
- * <p>As long as the implementation handle is valid the JVM will not attempt to close the resource.</p>
- *
- * <p>An implementation handle implements {@link java.lang.AutoCloseable},
- * which also enables compile-time checks for non-closed resources. If a Handle is acquired
- * in a try-with-resources (TWR) block, it's associated resource will be automatically closed by
- * the JVM at the end of the block.
- * The resource can also be explicitly closed by the user by calling {@code Handle.close()}.</p>
- * <blockquote><pre>
- * //Using try-with-resources block:
- * try (WritableyMapHandle handle = WritableMemory.map(File file)) {
- * WritableMemory wMem = handle.get();
- * doWork(wMem) // read and write to memory mapped file.
- * }
- *
- * //Using explicit close():
- * WritableMapHandle handle = WritableMemory.map(File file);
- * WritableMemory wMem = handle.get();
- * doWork(wMem) // read and write to memory mapped file.
- * handle.close();
- * </pre></blockquote>
- *
- * <p>Where it is desirable to pass ownership of the resource (and the {@code close()}
- * responsibility) one can not use the TWR block. Instead:</p>
- * <blockquote><pre>
- * WritableMapHandle handler = WritableMemory.map(File file);
- * doWorkAndClose(handle); //passes the handle to object that closes the resource.
- * </pre></blockquote>
- *
- * <p>Whatever part of your process is responsible for allocating a resource external
- * to the JVM must be responsible for closing it or making sure it gets closed.
- * Since only the implementation Handles implement AutoCloseable, you must not let go of the
- * handle reference until you are done with its associated resource.</p>
- *
- * <p>As mentioned above, there are two ways to do this:</p>
- * <ul><li>Use a try-with-resources block. At the end of the block, the JVM will automatically
- * close the resource.</li>
- *
- * <li>If you need to pass an external resource, pass the implementation resource handle, not the
- * access API. This means you are also passing the responsibility to close the resource.
- * If you have different parts of your code holding references to the same handle,
- * whichever one closes it first will make all the other resources invalid, so be careful.
- * As long as there is at least one reference to the handle that is still valid and the resource
- * has not been closed, the resource will remain valid. If you drop all references to all handles,
- * the JVM will eventually close the resource, making it invalid, but it is possible that you might
- * run out of memory first. Depending on this is a bad idea and a could be a serious,
- * hard-to-find bug.</li>
- * </ul>
+ * <p>An access API is joined with a resource with a static factory method.
*
*<p>Moving back and forth between <i>Memory</i> and <i>Buffer</i>:</p>
*<blockquote><pre>
@@ -165,9 +98,6 @@
* Memory reg = wMem.region(offset, length);
* </pre></blockquote>
*
- * <p>With asserts enabled in the JVM, all methods are checked for bounds and
- * use-after-close violations.</p>
- *
* @author Lee Rhodes
*/
package org.apache.datasketches.memory;
diff --git a/target/.gitignore b/target/.gitignore
new file mode 100644
index 0000000..840e7d3
--- /dev/null
+++ b/target/.gitignore
@@ -0,0 +1 @@
+/classes/
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org