Created
May 4, 2014 03:54
-
-
Save frsyuki/92cd3c291615f9fd8785 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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