Search in sources :

Example 1 with DataInput_

use of suite.util.DataInput_ in project suite by stupidsing.

the class JournalledFileFactory method journalled.

private static // 
JournalledPageFile journalled(// 
PageFile df, // 
PageFile jpf, // 
PageFile ppf, int pageSize) {
    Serializer<Bytes> bytesSerializer = serialize.bytes(pageSize);
    Serializer<JournalEntry> journalEntrySerializer = new Serializer<>() {

        public JournalEntry read(DataInput_ dataInput) throws IOException {
            int pointer = dataInput.readInt();
            Bytes bytes = bytesSerializer.read(dataInput);
            return new JournalEntry(pointer, bytes);
        }

        public void write(DataOutput_ dataOutput, JournalEntry journalEntry) throws IOException {
            dataOutput.writeInt(journalEntry.pointer);
            bytesSerializer.write(dataOutput, journalEntry.bytes);
        }
    };
    PageFile dataFile = df;
    SerializedPageFile<JournalEntry> journalPageFile = SerializedFileFactory.serialized(jpf, journalEntrySerializer);
    SerializedPageFile<Integer> pointerPageFile = SerializedFileFactory.serialized(ppf, serialize.int_);
    int nCommittedJournalEntries0 = pointerPageFile.load(0);
    List<JournalEntry> journalEntries = new ArrayList<>();
    for (int jp = 0; jp < nCommittedJournalEntries0; jp++) journalEntries.add(journalPageFile.load(jp));
    return new JournalledPageFile() {

        private int nCommittedJournalEntries = nCommittedJournalEntries0;

        public void close() throws IOException {
            dataFile.close();
            journalPageFile.close();
            pointerPageFile.close();
        }

        public synchronized Bytes load(int pointer) {
            IntObjPair<JournalEntry> pair = findPageInJournal(pointer);
            if (pair != null)
                return pair.t1.bytes;
            else
                return dataFile.load(pointer);
        }

        public synchronized void save(int pointer, Bytes bytes) {
            IntObjPair<JournalEntry> pair = findDirtyPageInJournal(pointer);
            int jp;
            JournalEntry journalEntry;
            if (pair != null) {
                jp = pair.t0;
                journalEntry = pair.t1;
            } else {
                jp = journalEntries.size();
                journalEntries.add(journalEntry = new JournalEntry(pointer, null));
            }
            journalEntry.bytes = bytes;
            journalPageFile.save(jp, journalEntry);
        }

        /**
         * Marks a snapshot that data can be recovered to.
         */
        public synchronized void commit() {
            while (nCommittedJournalEntries < journalEntries.size()) {
                JournalEntry journalEntry = journalEntries.get(nCommittedJournalEntries++);
                dataFile.save(journalEntry.pointer, journalEntry.bytes);
            }
            if (8 < nCommittedJournalEntries)
                saveJournal();
        }

        /**
         * Makes sure the current snapshot of data is saved and recoverable
         * on failure, upon the return of method call.
         */
        public synchronized void sync() {
            journalPageFile.sync();
            saveJournal();
            pointerPageFile.sync();
        }

        private void saveJournal() {
            pointerPageFile.save(0, nCommittedJournalEntries);
            if (128 < nCommittedJournalEntries)
                applyJournal();
        }

        /**
         * Shortens the journal by applying them to page file.
         */
        public synchronized void applyJournal() {
            // make sure all changes are written to main file
            dataFile.sync();
            // clear all committed entries
            journalEntries.subList(0, nCommittedJournalEntries).clear();
            // reset committed pointer
            pointerPageFile.save(0, nCommittedJournalEntries = 0);
            pointerPageFile.sync();
            // write back entries for next commit
            for (int jp = 0; jp < journalEntries.size(); jp++) journalPageFile.save(jp, journalEntries.get(jp));
        }

        private IntObjPair<JournalEntry> findPageInJournal(int pointer) {
            return findPageInJournal(pointer, 0);
        }

        private IntObjPair<JournalEntry> findDirtyPageInJournal(int pointer) {
            return findPageInJournal(pointer, nCommittedJournalEntries);
        }

        private IntObjPair<JournalEntry> findPageInJournal(int pointer, int start) {
            IntObjPair<JournalEntry> pair = null;
            for (int jp = start; jp < journalEntries.size(); jp++) {
                JournalEntry journalEntry = journalEntries.get(jp);
                if (journalEntry.pointer == pointer)
                    pair = IntObjPair.of(jp, journalEntry);
            }
            return pair;
        }
    };
}
Also used : PageFile(suite.file.PageFile) SerializedPageFile(suite.file.SerializedPageFile) JournalledPageFile(suite.file.JournalledPageFile) DataOutput_(suite.util.DataOutput_) ArrayList(java.util.ArrayList) JournalledPageFile(suite.file.JournalledPageFile) Bytes(suite.primitive.Bytes) DataInput_(suite.util.DataInput_) Serializer(suite.util.Serialize.Serializer)

Example 2 with DataInput_

use of suite.util.DataInput_ in project suite by stupidsing.

the class SerializedStoreCache method get.

public V get(K key, Source<V> source) {
    Bytes keyBytes = serialize(keySerializer, key);
    Bytes valueBytes = storeCache.get(keyBytes, () -> serialize(valueSerializer, source.source()));
    return Rethrow.ex(() -> {
        try (ByteArrayInputStream bais = new ByteArrayInputStream(valueBytes.toArray());
            DataInput_ dis = DataInput_.of(bais)) {
            return valueSerializer.read(dis);
        }
    });
}
Also used : Bytes(suite.primitive.Bytes) ByteArrayInputStream(java.io.ByteArrayInputStream) DataInput_(suite.util.DataInput_)

Example 3 with DataInput_

use of suite.util.DataInput_ 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

Bytes (suite.primitive.Bytes)3 DataInput_ (suite.util.DataInput_)3 ArrayList (java.util.ArrayList)2 DataOutput_ (suite.util.DataOutput_)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Extent (suite.file.ExtentAllocator.Extent)1 ExtentFile (suite.file.ExtentFile)1 JournalledPageFile (suite.file.JournalledPageFile)1 PageFile (suite.file.PageFile)1 SerializedPageFile (suite.file.SerializedPageFile)1 BytesBuilder (suite.primitive.Bytes.BytesBuilder)1 Serialize (suite.util.Serialize)1 Serializer (suite.util.Serialize.Serializer)1