Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public interface XyzInput {
public Buffer getNextBuffer() throws IOException;
}
// non-reference counter
public abstract class Buffer {
private Unsafe unsafe;
private long pointer;
private Object base;
private int size;
protected Buffer(ByteBuffer bb) {
this.pointer = // from bb ...
this.base = // from bb ...
}
public int getInt(int offset) {
return unsafe.getInt(base, pointer);
}
// getLong(int offset)
// getShort(int offset)
// ...
public abstract void release();
public int size() {
return size;
}
}
// reference counter
public abstract class Buffer {
private Unsafe unsafe;
private long pointer;
private Object base;
private int offset;
private int size;
private AtomicInteger referenceCounter;
static {
// something depending on architecture to get unsafe
//@Suppress...
}
protected Buffer(ByteBuffer bb) {
if(bb.hasArray()) {
// Nonn-direct buffer
this.pointer = // from bb ...
this.offset = bb.arrayOffset();
this.base = bb.array();
} else {
// DirectBuffer
// See airlift Slice code
this.pointer = bb.xyz;
this.offset = 0;
this.base = null;
}
}
public int getInt(int offset) {
return unsafe.getInt(base, offset + pointer);
}
// getLong(int offset)
// getShort(int offset)
// ...
public int size() {
return size;
}
public void acquire() {
referenceCounter.increment(1);
}
public void release() {
if(referenceCounter.decrement(1) == 0) {
releaseBuffer();
}
}
protected abstract void releaseBuffer();
}
// might be public, but doesn't have to be public
class UnpackerBuffer {
private byte[] extra;
private int extraOffset;
private Buffer currentBuffer;
private int position;
private XyzInput input;
public int readInt() {
if(currentBuffer.size() < position + 4) {
currentBuffer.copyTo(extra, position, currentBuffer.size() - position);
currentBuffer.release();
currentBuffer = input.getNextBuffer();
// copy data to extra...
// TODO
}
int v = currentBuffer.getInt(position);
position += 4;
return v;
}
}
// Implement Unpacker
// Performance bnechmark
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment