Search in sources :

Example 1 with ExtentFile

use of suite.file.ExtentFile 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;
        }
    };
}
Also used : Serialize(suite.util.Serialize) Extent(suite.file.ExtentAllocator.Extent) DataOutput_(suite.util.DataOutput_) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ExtentFile(suite.file.ExtentFile) Bytes(suite.primitive.Bytes) DataInput_(suite.util.DataInput_) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Extent (suite.file.ExtentAllocator.Extent)1 ExtentFile (suite.file.ExtentFile)1 Bytes (suite.primitive.Bytes)1 BytesBuilder (suite.primitive.Bytes.BytesBuilder)1 DataInput_ (suite.util.DataInput_)1 DataOutput_ (suite.util.DataOutput_)1 Serialize (suite.util.Serialize)1