use of suite.util.Serialize in project suite by stupidsing.
the class FileFactory method extentFile.
public static ExtentFile extentFile(PageFile pf) {
Serialize serialize = Serialize.me;
Serializer<Extent> extentSerializer = serialize.extent();
Serializer<Bytes> bytesSerializer = serialize.variableLengthBytes;
SerializedPageFile<Block> pageFile = SerializedFileFactory.serialized(pf, new Serializer<>() {
public Block read(DataInput_ dataInput) throws IOException {
Extent extent = extentSerializer.read(dataInput);
Bytes bytes = bytesSerializer.read(dataInput);
return new Block(extent, bytes);
}
public void write(DataOutput_ dataOutput, Block block) throws IOException {
extentSerializer.write(dataOutput, block.extent);
bytesSerializer.write(dataOutput, block.bytes);
}
});
return new ExtentFile() {
public void close() throws IOException {
pageFile.close();
}
public void sync() {
pageFile.sync();
}
public Bytes load(Extent extent) {
BytesBuilder bb = new BytesBuilder();
for (int pointer = extent.start; pointer < extent.end; pointer++) {
Block block = pageFile.load(pointer);
Util.assert_(Objects.equals(block.extent, extent));
bb.append(block.bytes);
}
return bb.toBytes();
}
public void save(Extent extent, Bytes bytes) {
for (int pointer = extent.start, p = 0; pointer < extent.end; pointer++) {
int p1 = p + blockSize;
pageFile.save(pointer, new Block(extent, bytes.range(p, p1)));
p = p1;
}
}
public List<Extent> scan(int start, int end) {
List<Extent> extents = new ArrayList<>();
int pointer = start;
while (pointer < end) {
Extent extent = pageFile.load(pointer).extent;
if (start <= extent.start && extent.end <= end)
extents.add(extent);
pointer = extent.end;
}
return extents;
}
};
}
Aggregations