use of suite.file.JournalledPageFile in project suite by stupidsing.
the class B_TreeBuilder method build.
public static <//
Key> //
Pair<B_Tree<Key, Integer>, KeyDataStore<Key>> build(//
Path path, //
boolean isNew, //
Serializer<Key> ks, //
Comparator<Key> cmp, //
int pageSize, int nPages) {
if (isNew)
Rethrow.ex(() -> Files.deleteIfExists(path));
JournalledPageFile jpf = JournalledFileFactory.journalled(path, pageSize);
B_Tree<Key, Integer> b_tree = new B_TreeBuilder<>(ks, serialize.int_).build(jpf, cmp, nPages);
if (isNew) {
b_tree.create();
jpf.commit();
}
return Pair.of(b_tree, new B_TreeStore<>(b_tree, jpf::commit));
}
use of suite.file.JournalledPageFile 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;
}
};
}
use of suite.file.JournalledPageFile in project suite by stupidsing.
the class B_TreeTest method testAccess.
@Test
public void testAccess() throws IOException {
int pageSize = 4096;
Path path = Constants.tmp("b_tree-file");
Files.deleteIfExists(path);
B_TreeBuilder<Integer, String> builder = new B_TreeBuilder<>(serialize.int_, serialize.string(16));
shuffleNumbers();
try (JournalledPageFile jpf = JournalledFileFactory.journalled(path, pageSize);
B_Tree<Integer, String> b_tree = builder.build(jpf, comparator, pageSize)) {
b_tree.create();
testStep0(b_tree);
jpf.commit();
jpf.sync();
}
shuffleNumbers();
try (JournalledPageFile jpf = JournalledFileFactory.journalled(path, pageSize);
B_Tree<Integer, String> b_tree = builder.build(jpf, comparator, pageSize)) {
testStep1(b_tree);
jpf.commit();
jpf.sync();
}
try (JournalledPageFile jpf = JournalledFileFactory.journalled(path, pageSize);
B_Tree<Integer, String> b_tree = builder.build(jpf, comparator, pageSize)) {
testStep2(b_tree);
jpf.commit();
jpf.sync();
jpf.applyJournal();
}
}
use of suite.file.JournalledPageFile in project suite by stupidsing.
the class B_TreeTest method testDump.
@Test
public void testDump() throws IOException {
int pageSize = 4096;
Path path = Constants.tmp("b_tree-dump");
Files.deleteIfExists(path);
B_TreeBuilder<Integer, String> builder = new B_TreeBuilder<>(serialize.int_, serialize.string(16));
try (JournalledPageFile jpf = JournalledFileFactory.journalled(path, pageSize);
B_Tree<Integer, String> b_tree = builder.build(jpf, comparator, pageSize)) {
b_tree.create();
for (int i = 0; i < 32; i++) b_tree.put(i, Integer.toString(i));
b_tree.dump(System.out);
System.out.println(To.list(b_tree.keys(3, 10)));
jpf.commit();
}
}
Aggregations