forked from xerial/snappy-java
/
PureJavaSnappy.java
309 lines (280 loc) · 11.4 KB
/
PureJavaSnappy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package org.xerial.snappy.pure;
import static org.xerial.snappy.pure.UnsafeUtil.getAddress;
import static sun.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.nio.ByteBuffer;
import java.util.concurrent.ConcurrentLinkedDeque;
import org.xerial.snappy.SnappyApi;
/**
* A pure-java Snappy implementation using https://github.com/airlift/aircompressor
*/
public class PureJavaSnappy implements SnappyApi
{
/**
* Using a {@link ConcurrentLinkedDeque}, with values constantly popped and pushed from the head, leads to the fewest
* {@code short[]} instances remaining live over time.
*/
private final static ConcurrentLinkedDeque<SoftReference<short[]>> CACHED_TABLES = new ConcurrentLinkedDeque<>();
private final static int MAX_OUTPUT_LENGTH = Integer.MAX_VALUE;
@Override
public long rawCompress(long inputAddr, long inputSize, long destAddr)
throws IOException
{
final short[] table = getTable();
try
{
return SnappyRawCompressor.compress(null, inputAddr, inputSize, null, destAddr, MAX_OUTPUT_LENGTH, table);
}
finally
{
returnTable(table);
}
}
@Override
public long rawUncompress(long inputAddr, long inputSize, long destAddr)
throws IOException
{
return SnappyRawDecompressor.decompress(null, inputAddr, inputSize, null, destAddr, MAX_OUTPUT_LENGTH);
}
@Override
public int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed, int outputOffset)
throws IOException
{
Object inputBase;
long inputAddress;
long inputLimit;
if (input.isDirect()) {
inputBase = null;
long address = getAddress(input);
inputAddress = address + input.position();
inputLimit = address + input.limit();
}
else if (input.hasArray()) {
inputBase = input.array();
inputAddress = ARRAY_BYTE_BASE_OFFSET + input.arrayOffset() + input.position();
inputLimit = ARRAY_BYTE_BASE_OFFSET + input.arrayOffset() + input.limit();
}
else {
throw new IllegalArgumentException("Unsupported input ByteBuffer implementation " + input.getClass().getName());
}
Object outputBase;
long outputAddress;
long outputLimit;
if (compressed.isDirect()) {
outputBase = null;
long address = getAddress(compressed);
outputAddress = address + compressed.position();
outputLimit = address + compressed.limit();
}
else if (compressed.hasArray()) {
outputBase = compressed.array();
outputAddress = ARRAY_BYTE_BASE_OFFSET + compressed.arrayOffset() + compressed.position();
outputLimit = ARRAY_BYTE_BASE_OFFSET + compressed.arrayOffset() + compressed.limit();
}
else {
throw new IllegalArgumentException("Unsupported output ByteBuffer implementation " + compressed.getClass().getName());
}
// HACK: Assure JVM does not collect Slice wrappers while compressing, since the
// collection may trigger freeing of the underlying memory resulting in a segfault
// There is no other known way to signal to the JVM that an object should not be
// collected in a block, and technically, the JVM is allowed to eliminate these locks.
synchronized (input) {
synchronized (compressed) {
final short[] table = getTable();
try
{
int written = SnappyRawCompressor.compress(
inputBase,
inputAddress,
inputLimit,
outputBase,
outputAddress,
outputLimit,
table);
compressed.position(compressed.position() + written);
return written;
}
finally
{
returnTable(table);
}
}
}
}
@Override
public int rawCompress(Object input, int inputOffset, int inputByteLength, Object output, int outputOffset)
throws IOException
{
long inputAddress = ARRAY_BYTE_BASE_OFFSET + inputOffset;
long inputLimit = inputAddress + inputByteLength;
long outputAddress = ARRAY_BYTE_BASE_OFFSET + outputOffset;
long outputLimit = outputAddress + MAX_OUTPUT_LENGTH;
final short[] table = getTable();
try
{
return SnappyRawCompressor.compress(input, inputAddress, inputLimit, output, outputAddress, outputLimit, table);
}
finally
{
returnTable(table);
}
}
@Override
public int rawUncompress(ByteBuffer compressed, int inputOffset, int inputLength, ByteBuffer uncompressed, int outputOffset)
throws IOException
{
Object inputBase;
long inputAddress;
long inputLimit;
if (compressed.isDirect()) {
inputBase = null;
long address = getAddress(compressed);
inputAddress = address + compressed.position();
inputLimit = address + compressed.limit();
}
else if (compressed.hasArray()) {
inputBase = compressed.array();
inputAddress = ARRAY_BYTE_BASE_OFFSET + compressed.arrayOffset() + compressed.position();
inputLimit = ARRAY_BYTE_BASE_OFFSET + compressed.arrayOffset() + compressed.limit();
}
else {
throw new IllegalArgumentException("Unsupported input ByteBuffer implementation " + compressed.getClass().getName());
}
Object outputBase;
long outputAddress;
long outputLimit;
if (uncompressed.isDirect()) {
outputBase = null;
long address = getAddress(uncompressed);
outputAddress = address + uncompressed.position();
outputLimit = address + uncompressed.limit();
}
else if (uncompressed.hasArray()) {
outputBase = uncompressed.array();
outputAddress = ARRAY_BYTE_BASE_OFFSET + uncompressed.arrayOffset() + uncompressed.position();
outputLimit = ARRAY_BYTE_BASE_OFFSET + uncompressed.arrayOffset() + uncompressed.limit();
}
else {
throw new IllegalArgumentException("Unsupported output ByteBuffer implementation " + uncompressed.getClass().getName());
}
// HACK: Assure JVM does not collect Slice wrappers while decompressing, since the
// collection may trigger freeing of the underlying memory resulting in a segfault
// There is no other known way to signal to the JVM that an object should not be
// collected in a block, and technically, the JVM is allowed to eliminate these locks.
synchronized (compressed) {
synchronized (uncompressed) {
int written = SnappyRawDecompressor.decompress(inputBase, inputAddress, inputLimit, outputBase, outputAddress, outputLimit);
uncompressed.position(uncompressed.position() + written);
return written;
}
}
}
@Override
public int rawUncompress(Object input, int inputOffset, int inputLength, Object output, int outputOffset)
throws IOException
{
long inputAddress = ARRAY_BYTE_BASE_OFFSET + inputOffset;
long inputLimit = inputAddress + inputLength;
long outputAddress = ARRAY_BYTE_BASE_OFFSET + outputOffset;
long outputLimit = outputAddress + MAX_OUTPUT_LENGTH;
return SnappyRawDecompressor.decompress(input, inputAddress, inputLimit, output, outputAddress, outputLimit);
}
@Override
public int maxCompressedLength(int source_bytes)
{
return SnappyRawCompressor.maxCompressedLength(source_bytes);
}
@Override
public int uncompressedLength(ByteBuffer compressed, int offset, int len)
throws IOException
{
Object inputBase;
long inputAddress;
long inputLimit;
if(compressed.isDirect()) {
inputBase = null;
long address = getAddress(compressed);
inputAddress = address + compressed.position();
inputLimit = address + compressed.limit();
}
else if (compressed.hasArray()){
inputBase = compressed.array();
inputAddress = ARRAY_BYTE_BASE_OFFSET + offset;
inputLimit = ARRAY_BYTE_BASE_OFFSET + len;
}
else {
throw new IllegalArgumentException("Unsupported input ByteBuffer implementation: " + compressed.getClass().getName());
}
return SnappyRawDecompressor.getUncompressedLength(inputBase, inputAddress, inputLimit);
}
@Override
public int uncompressedLength(Object input, int offset, int len)
throws IOException
{
long compressedAddress = ARRAY_BYTE_BASE_OFFSET + offset;
long compressedLimit = ARRAY_BYTE_BASE_OFFSET + len;
return SnappyRawDecompressor.getUncompressedLength(input, compressedAddress, compressedLimit);
}
@Override
public long uncompressedLength(long inputAddr, long len)
throws IOException
{
return SnappyRawDecompressor.getUncompressedLength(null, inputAddr, inputAddr + len);
}
@Override
public boolean isValidCompressedBuffer(ByteBuffer compressed, int offset, int len)
throws IOException
{
throw new UnsupportedOperationException("isValidCompressedBuffer is not supported in pure-java mode");
}
@Override
public boolean isValidCompressedBuffer(Object input, int offset, int len)
throws IOException
{
throw new UnsupportedOperationException("isValidCompressedBuffer is not supported in pure-java mode");
}
@Override
public boolean isValidCompressedBuffer(long inputAddr, long offset, long len)
throws IOException
{
throw new UnsupportedOperationException("isValidCompressedBuffer is not supported in pure-java mode");
}
@Override
public void arrayCopy(Object src, int offset, int byteLength, Object dest, int dOffset)
throws IOException
{
System.arraycopy(src, offset, dest, dOffset, byteLength);
}
private static short[] getTable()
{
SoftReference<short[]> existingRef;
while((existingRef = CACHED_TABLES.poll()) != null)
{
short[] table = existingRef.get();
if (table != null)
{
//purge oldest entries have lost references
SoftReference<short[]> entry;
boolean lastEmpty = true;
while (lastEmpty && (entry = CACHED_TABLES.peekLast()) != null)
{
if (entry.get() == null)
{
CACHED_TABLES.removeLastOccurrence(entry);
}
else
{
lastEmpty = false;
}
}
return table;
}
}
return new short[SnappyRawCompressor.MAX_HASH_TABLE_SIZE];
}
private static void returnTable(short[] table)
{
CACHED_TABLES.addFirst(new SoftReference<short[]>(table));
}
}