Skip to content

Commit a49c06c

Browse files
Add support for XXHash algos (#966)
1 parent 754473b commit a49c06c

File tree

6 files changed

+534
-1
lines changed

6 files changed

+534
-1
lines changed

NOTICE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
11
AWS Crt Java
22
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
SPDX-License-Identifier: Apache-2.0.
4+
5+
** XXHash - https://xxhash.com/
6+
Copyright (c) 2012-2021 Yann Collet
7+
All rights reserved.
8+
9+
BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)
10+
11+
Redistribution and use in source and binary forms, with or without modification,
12+
are permitted provided that the following conditions are met:
13+
14+
* Redistributions of source code must retain the above copyright notice, this
15+
list of conditions and the following disclaimer.
16+
17+
* Redistributions in binary form must reproduce the above copyright notice, this
18+
list of conditions and the following disclaimer in the documentation and/or
19+
other materials provided with the distribution.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
25+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,6 @@ To debug native code with VSCode or CLion or any other IDE:
298298
3. Set the parameters to be the ones used by the ```mvn``` script, as per above
299299
4. Set the working directory to the `aws-crt-java` directory
300300
5. On windows, you will need to manually load the PDB via the Modules window in Visual Studio, as it is not embedded in the JAR. It will be in the ```target/cmake-build/lib/windows/<arch>``` folder.
301+
302+
## Attribution
303+
This library exposes native XXHash implementation (https://github.com/Cyan4973/xxHash) through JNI interface.
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/**
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0.
4+
*/
5+
package software.amazon.awssdk.crt.checksums;
6+
import software.amazon.awssdk.crt.CrtResource;
7+
8+
public class XXHash extends CrtResource {
9+
10+
private XXHash(long nativeHandle) {
11+
acquireNativeHandle(nativeHandle);
12+
}
13+
14+
/**
15+
* Determines whether a resource releases its dependencies at the same time the native handle is released or if it waits.
16+
* Resources that wait are responsible for calling releaseReferences() manually.
17+
*/
18+
@Override
19+
protected boolean canReleaseReferencesImmediately() { return true; }
20+
21+
/**
22+
* Releases the instance's reference to the underlying native key pair
23+
*/
24+
@Override
25+
protected void releaseNativeHandle() {
26+
if (!isNull()) {
27+
xxHashRelease(getNativeHandle());
28+
}
29+
}
30+
31+
/**
32+
* Create new streaming XXHash64.
33+
* @param seed seed to use for the hash
34+
* @return new XXHash instance
35+
*/
36+
static public XXHash newXXHash64(long seed) {
37+
long nativeHandle = xxHash64Create(seed);
38+
return new XXHash(nativeHandle);
39+
}
40+
41+
42+
/**
43+
* Create new streaming XXHash64.
44+
* @return new XXHash instance
45+
*/
46+
static public XXHash newXXHash64() {
47+
long nativeHandle = xxHash64Create(0);
48+
return new XXHash(nativeHandle);
49+
}
50+
51+
/**
52+
* Create new streaming XXHash3_64.
53+
* @param seed seed to use for the hash
54+
* @return new XXHash instance
55+
*/
56+
static public XXHash newXXHash3_64(long seed) {
57+
long nativeHandle = xxHash364Create(seed);
58+
return new XXHash(nativeHandle);
59+
}
60+
61+
/**
62+
* Create new streaming XXHash3_64.
63+
* @return new XXHash instance
64+
*/
65+
static public XXHash newXXHash3_64() {
66+
long nativeHandle = xxHash364Create(0);
67+
if (nativeHandle != 0) {
68+
return new XXHash(nativeHandle);
69+
}
70+
71+
return null;
72+
}
73+
74+
/**
75+
* Create new streaming XXHash3_128.
76+
* @param seed seed to use for the hash
77+
* @return new XXHash instance
78+
*/
79+
static public XXHash newXXHash3_128(long seed) {
80+
long nativeHandle = xxHash3128Create(seed);
81+
if (nativeHandle != 0) {
82+
return new XXHash(nativeHandle);
83+
}
84+
85+
return null;
86+
}
87+
88+
/**
89+
* Create new streaming XXHash3_128.
90+
* @return new XXHash instance
91+
*/
92+
static public XXHash newXXHash3_128() {
93+
long nativeHandle = xxHash3128Create(0);
94+
if (nativeHandle != 0) {
95+
return new XXHash(nativeHandle);
96+
}
97+
98+
return null;
99+
}
100+
101+
/**
102+
* Update xxhash state from input
103+
* @param input input to update with
104+
*/
105+
public void update(byte[] input) {
106+
xxHashUpdate(getNativeHandle(), input);
107+
}
108+
109+
/**
110+
* Return digest for the current state of hash.
111+
* @return hash as bytes in big endian
112+
*/
113+
public byte[] digest() {
114+
return xxHashFinalize(getNativeHandle());
115+
}
116+
117+
/**
118+
* Oneshot compute XXHash64.
119+
* @param input input input to hash
120+
* @param seed seed
121+
* @return xxhash64 hash
122+
*/
123+
static public byte[] computeXXHash64(byte[] input, long seed) {
124+
return xxHash64Compute(input, seed);
125+
}
126+
127+
/**
128+
* Oneshot compute XXHash64.
129+
* @param input input input to hash
130+
* @return xxhash64 hash
131+
*/
132+
static public byte[] computeXXHash64(byte[] input) {
133+
return xxHash64Compute(input, 0);
134+
}
135+
136+
137+
/**
138+
* Oneshot compute XXHash3_64.
139+
* @param input input input to hash
140+
* @param seed seed
141+
* @return xxhash64 hash
142+
*/
143+
static public byte[] computeXXHash3_64(byte[] input, long seed) {
144+
return xxHash364Compute(input, seed);
145+
}
146+
147+
/**
148+
* Oneshot compute XXHash3_64.
149+
* @param input input input to hash
150+
* @return xxhash64 hash
151+
*/
152+
static public byte[] computeXXHash3_64(byte[] input) {
153+
return xxHash364Compute(input, 0);
154+
}
155+
156+
/**
157+
* Oneshot compute XXHash3_128.
158+
* @param input input input to hash
159+
* @param seed seed
160+
* @return xxhash64 hash
161+
*/
162+
static public byte[] computeXXHash3_128(byte[] input, long seed) {
163+
return xxHash3128Compute(input, seed);
164+
}
165+
166+
/**
167+
* Oneshot compute XXHash3_128.
168+
* @param input input input to hash
169+
* @return xxhash64 hash
170+
*/
171+
static public byte[] computeXXHash3_128(byte[] input) {
172+
return xxHash3128Compute(input, 0);
173+
}
174+
175+
/*******************************************************************************
176+
* native methods
177+
******************************************************************************/
178+
private static native byte[] xxHash64Compute(byte[] input, long seed);
179+
private static native byte[] xxHash364Compute(byte[] input, long seed);
180+
private static native byte[] xxHash3128Compute(byte[] input, long seed);
181+
182+
private static native long xxHash64Create(long seed);
183+
private static native long xxHash364Create(long seed);
184+
private static native long xxHash3128Create(long seed);
185+
private static native void xxHashRelease(long xxhash);
186+
187+
private static native void xxHashUpdate(long xxhash, byte[] input);
188+
private static native byte[] xxHashFinalize(long xxhash);
189+
}

0 commit comments

Comments
 (0)