DB - laforge49/JFile GitHub Wiki

The state of a database is updated by applying transactions to it. These transactions are first passed through the online transaction pipeline, which aggregates transactions into blocks and logs them prior to processing them.

When a database is restarted, it first processes whatever transactions were previously logged to bring the state of the database up to date. In the simplest case, the database is never written to disk, so all previously logged transactions must be reprocessed.

##CounterDB

CounterDB is a subclass of DB. Its state is a single int--the counter.

There are two transactions implemented for CounterDb: GetCounterTransaction and IncrementCounterTransaction. Both are subclasses of Jid and neither carry any persistent state.

public class GetCounterTransaction extends _TransactionJid {
    protected void eval(long blockTimestamp, RP rp) throws Exception {
        CounterDB db = (CounterDB) getAncestor(CounterDB.class);
        rp.processResponse(db.getCounter());
    }
}

public class IncrementCounterTransaction extends _TransactionJid {
    protected void eval(long blockTimestamp, RP rp) throws Exception {
        CounterDB db = (CounterDB) getAncestor(CounterDB.class);
        rp.processResponse(db.increment());
    }
}

##Sample Usage

MailboxFactory mailboxFactory = JAMailboxFactory.newMailboxFactory(10);
Mailbox factoryMailbox = mailboxFactory.createMailbox();
JAFactory factory = new JAFactory();
factory.initialize(factoryMailbox);
(new JidFactories()).initialize(factoryMailbox, factory);
(new JFileFactories()).initialize(factoryMailbox, factory);
factory.defineActorType("inc", IncrementCounterTransaction.class);
factory.defineActorType("get", GetCounterTransaction.class);
Path directoryPath = FileSystems.getDefault().getPath("CounterTest");
JAFuture future = new JAFuture();
AggregateTransaction inc = new AggregateTransaction("inc");
AggregateTransaction get = new AggregateTransaction("get");

CounterDB db1 = new CounterDB(mailboxFactory, factory, directoryPath);
db1.clearDirectory();
(new OpenDbFile(10000)).send(future, db1);
TransactionAggregator transactionAggregator1 = db1.getTransactionAggregator();
inc.sendEvent(transactionAggregator1);
int total1 = (Integer) (new AggregateTransaction("get")).send(future, transactionAggregator1);
assertEquals(1, total1);
db1.closeDbFile();

CounterDB db2 = new CounterDB(mailboxFactory, factory, directoryPath);
(new OpenDbFile(10000)).send(future, db2);
TransactionAggregator transactionAggregator2 = db2.getTransactionAggregator();
inc.sendEvent(transactionAggregator2);
inc.sendEvent(transactionAggregator2);
int total2 = (Integer) (new AggregateTransaction("get")).send(future, transactionAggregator2);
assertEquals(3, total2);
db2.closeDbFile();

CounterDB db3 = new CounterDB(mailboxFactory, factory, directoryPath);
(new OpenDbFile(10000)).send(future, db3);
TransactionAggregator transactionAggregator3 = db3.getTransactionAggregator();
inc.sendEvent(transactionAggregator3);
inc.sendEvent(transactionAggregator3);
inc.sendEvent(transactionAggregator3);
int total3 = (Integer) get.send(future, transactionAggregator3);
assertEquals(6, total3);
db3.closeDbFile();

mailboxFactory.close();

##JFile Classes Referenced on this Page

DB
AggregateTransaction
CounterDB
IncrementCounter
GetCounter
CounterTest

Back: Block Pipelines Up: Home Next: Transactions