Search in sources :

Example 1 with DataOutput_

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

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

Example 3 with DataOutput_

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

the class StoreCache method getOutlet.

public Outlet<Bytes> getOutlet(Bytes key, Source<Outlet<Bytes>> source) {
    return Rethrow.ex(() -> {
        long current = System.currentTimeMillis();
        Path path;
        int i = 0;
        while (Files.exists(path = path(key, i++, ""))) if (isUpToDate(path, current)) {
            InputStream is = Files.newInputStream(path);
            DataInputStream dis = new DataInputStream(is);
            if (isMatch(key, dis))
                return read(dis).closeAtEnd(is);
            dis.close();
            is.close();
        } else {
            Files.delete(path);
            break;
        }
        Pair<Boolean, Path> pair = match(key);
        if (pair.t0) {
            InputStream vis = Files.newInputStream(pair.t1);
            DataInputStream vdis = new DataInputStream(vis);
            return read(vdis).closeAtEnd(vis);
        } else {
            Outlet<Bytes> outlet = source.source();
            OutputStream vos = FileUtil.out(pair.t1);
            DataOutput_ vdo = DataOutput_.of(vos);
            return // 
            Outlet.of(() -> Rethrow.ex(() -> {
                Bytes value = outlet.next();
                if (value != null)
                    vdo.writeBytes(value);
                return value;
            })).closeAtEnd(// 
            vos).closeAtEnd(vdo);
        }
    });
}
Also used : Path(java.nio.file.Path) Bytes(suite.primitive.Bytes) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) DataOutput_(suite.util.DataOutput_) OutputStream(java.io.OutputStream) DataInputStream(java.io.DataInputStream)

Example 4 with DataOutput_

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

the class StoreCache method writeKey.

private void writeKey(Path path, Bytes key) throws IOException {
    try (OutputStream kos = FileUtil.out(path);
        DataOutput_ kdo = DataOutput_.of(kos)) {
        kdo.writeInt(key.size());
        kdo.writeBytes(key);
    }
}
Also used : DataOutput_(suite.util.DataOutput_) OutputStream(java.io.OutputStream)

Aggregations

DataOutput_ (suite.util.DataOutput_)4 Bytes (suite.primitive.Bytes)3 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 DataInput_ (suite.util.DataInput_)2 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)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