Skip to content

Commit 5f31e2b

Browse files
authored
Merge pull request #39 from balthz/optional-direct-allocation
Make direct buffer allocation optional
2 parents 26cb5f8 + 35fdd72 commit 5f31e2b

File tree

5 files changed

+62
-9
lines changed

5 files changed

+62
-9
lines changed

src/main/java/me/lemire/integercompression/DeltaZigzagVariableByte.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @author MURAOKA Taro http://github.com/koron
1515
*/
16-
public final class DeltaZigzagVariableByte implements IntegerCODEC {
16+
public class DeltaZigzagVariableByte implements IntegerCODEC {
1717

1818
@Override
1919
public String toString() {
@@ -27,7 +27,7 @@ public void compress(int[] inBuf, IntWrapper inPos, int inLen,
2727
return;
2828
}
2929

30-
ByteBuffer byteBuf = ByteBuffer.allocateDirect(inLen * 5 + 3);
30+
ByteBuffer byteBuf = makeBuffer(inLen * 5 + 3);
3131
DeltaZigzagEncoding.Encoder ctx = new DeltaZigzagEncoding.Encoder(0);
3232

3333
// Delta+Zigzag+VariableByte encoding.
@@ -127,4 +127,15 @@ public void uncompress(int[] inBuf, IntWrapper inPos, int inLen,
127127
outPos.set(op);
128128
inPos.set(inPosLast);
129129
}
130+
131+
/**
132+
* Creates a new buffer of the requested size.
133+
*
134+
* In case you need a different way to allocate buffers, you can override this method
135+
* with a custom behavior. The default implementation allocates a new Java direct
136+
* {@link ByteBuffer} on each invocation.
137+
*/
138+
protected ByteBuffer makeBuffer(int sizeInBytes) {
139+
return ByteBuffer.allocateDirect(sizeInBytes);
140+
}
130141
}

src/main/java/me/lemire/integercompression/FastPFOR.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
*
3939
* @author Daniel Lemire
4040
*/
41-
public final class FastPFOR implements IntegerCODEC,SkippableIntegerCODEC {
41+
public class FastPFOR implements IntegerCODEC,SkippableIntegerCODEC {
4242
final static int OVERHEAD_OF_EACH_EXCEPT = 8;
4343
/**
4444
*
@@ -68,7 +68,7 @@ public final class FastPFOR implements IntegerCODEC,SkippableIntegerCODEC {
6868
private FastPFOR(int pagesize) {
6969
pageSize = pagesize;
7070
// Initiate arrrays.
71-
byteContainer = ByteBuffer.allocateDirect(3 * pageSize
71+
byteContainer = makeBuffer(3 * pageSize
7272
/ BLOCK_SIZE + pageSize);
7373
byteContainer.order(ByteOrder.LITTLE_ENDIAN);
7474
for (int k = 1; k < dataTobePacked.length; ++k)
@@ -329,4 +329,15 @@ public void uncompress(int[] in, IntWrapper inpos, int inlength, int[] out,
329329
public String toString() {
330330
return this.getClass().getSimpleName();
331331
}
332+
333+
/**
334+
* Creates a new buffer of the requested size.
335+
*
336+
* In case you need a different way to allocate buffers, you can override this method
337+
* with a custom behavior. The default implementation allocates a new Java direct
338+
* {@link ByteBuffer} on each invocation.
339+
*/
340+
protected ByteBuffer makeBuffer(int sizeInBytes) {
341+
return ByteBuffer.allocateDirect(sizeInBytes);
342+
}
332343
}

src/main/java/me/lemire/integercompression/FastPFOR128.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
* @author Daniel Lemire
2323
*/
24-
public final class FastPFOR128 implements IntegerCODEC,SkippableIntegerCODEC {
24+
public class FastPFOR128 implements IntegerCODEC,SkippableIntegerCODEC {
2525
final static int OVERHEAD_OF_EACH_EXCEPT = 8;
2626
/**
2727
*
@@ -50,7 +50,7 @@ public final class FastPFOR128 implements IntegerCODEC,SkippableIntegerCODEC {
5050
public FastPFOR128(int pagesize) {
5151
pageSize = pagesize;
5252
// Initiate arrrays.
53-
byteContainer = ByteBuffer.allocateDirect(3 * pageSize
53+
byteContainer = makeBuffer(3 * pageSize
5454
/ BLOCK_SIZE + pageSize);
5555
byteContainer.order(ByteOrder.LITTLE_ENDIAN);
5656
for (int k = 1; k < dataTobePacked.length; ++k)
@@ -310,4 +310,15 @@ public void uncompress(int[] in, IntWrapper inpos, int inlength, int[] out,
310310
public String toString() {
311311
return this.getClass().getSimpleName();
312312
}
313+
314+
/**
315+
* Creates a new buffer of the requested size.
316+
*
317+
* In case you need a different way to allocate buffers, you can override this method
318+
* with a custom behavior. The default implementation allocates a new Java direct
319+
* {@link ByteBuffer} on each invocation.
320+
*/
321+
protected ByteBuffer makeBuffer(int sizeInBytes) {
322+
return ByteBuffer.allocateDirect(sizeInBytes);
323+
}
313324
}

src/main/java/me/lemire/integercompression/VariableByte.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void headlessCompress(int[] in, IntWrapper inpos, int inlength, int[] out
3939
IntWrapper outpos) {
4040
if (inlength == 0)
4141
return;
42-
ByteBuffer buf = ByteBuffer.allocateDirect(inlength * 8);
42+
ByteBuffer buf = makeBuffer(inlength * 8);
4343
buf.order(ByteOrder.LITTLE_ENDIAN);
4444
for (int k = inpos.get(); k < inpos.get() + inlength; ++k) {
4545
final long val = in[k] & 0xFFFFFFFFL; // To be consistent with
@@ -202,4 +202,14 @@ public void headlessUncompress(int[] in, IntWrapper inpos, int inlength, int[] o
202202
inpos.set(p + (s!=0 ? 1 : 0));
203203
}
204204

205+
/**
206+
* Creates a new buffer of the requested size.
207+
*
208+
* In case you need a different way to allocate buffers, you can override this method
209+
* with a custom behavior. The default implementation allocates a new Java direct
210+
* {@link ByteBuffer} on each invocation.
211+
*/
212+
protected ByteBuffer makeBuffer(int sizeInBytes) {
213+
return ByteBuffer.allocateDirect(sizeInBytes);
214+
}
205215
}

src/main/java/me/lemire/integercompression/differential/IntegratedVariableByte.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void compress(int[] in, IntWrapper inpos, int inlength,
3838
if (inlength == 0)
3939
return;
4040
int initoffset = 0;
41-
ByteBuffer buf = ByteBuffer.allocateDirect(inlength * 8);
41+
ByteBuffer buf = makeBuffer(inlength * 8);
4242
buf.order(ByteOrder.LITTLE_ENDIAN);
4343
for (int k = inpos.get(); k < inpos.get() + inlength; ++k) {
4444
final long val = (in[k] - initoffset) & 0xFFFFFFFFL; // To be consistent with unsigned integers in C/C++
@@ -187,7 +187,7 @@ public void headlessCompress(int[] in, IntWrapper inpos, int inlength,
187187
return;
188188
int initoffset = initvalue.get();
189189
initvalue.set(in[inpos.get()+inlength -1]);
190-
ByteBuffer buf = ByteBuffer.allocateDirect(inlength * 8);
190+
ByteBuffer buf = makeBuffer(inlength * 8);
191191
buf.order(ByteOrder.LITTLE_ENDIAN);
192192
for (int k = inpos.get(); k < inpos.get() + inlength; ++k) {
193193
final long val = (in[k] - initoffset) & 0xFFFFFFFFL; // To be consistent with unsigned integers in C/C++
@@ -253,4 +253,14 @@ public void headlessUncompress(int[] in, IntWrapper inpos, int inlength,
253253
inpos.set(p + (s!=0 ? 1 : 0));
254254
}
255255

256+
/**
257+
* Creates a new buffer of the requested size.
258+
*
259+
* In case you need a different way to allocate buffers, you can override this method
260+
* with a custom behavior. The default implementation allocates a new Java direct
261+
* {@link ByteBuffer} on each invocation.
262+
*/
263+
protected ByteBuffer makeBuffer(int sizeInBytes) {
264+
return ByteBuffer.allocateDirect(sizeInBytes);
265+
}
256266
}

0 commit comments

Comments
 (0)