Skip to content
laforge49 edited this page Jul 5, 2012 · 2 revisions

IMDB is an in-memory database. Its state is a StringMapJid whose values are ActorJid's. And on an i7 with SSD it can process 1 million transactions per second.

The database state is written alternately to two areas in the backing file. On completion of the processing of a block of transactions, a copy of the state is made and a write to the backing file is initiated--providing the previous write has completed.

The state written to the backing file contains both a timestamp and a checksum. On startup, both states are read and the latest valid state, if any, is used. The state contains the name of a log file and a position within that log file under the keys $$LOG_FILE_NAME and $$LOG_POSITION respectively. This information is used to determine which log file, and where in that log file, to begin reprocessing logged transactions to bring the state up to date.

##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);
JAFuture future = new JAFuture();
Path directoryPath = FileSystems.getDefault().getPath("CheckpointTest");
OpenDbFile openDbFile = new OpenDbFile(10000);

AggregateTransaction aggregateAddTransaction =
        AddIntegerTransactionFactory.at(factoryMailbox, "counter", 2);
AggregateTransaction aggregateIncrementTransaction =
        IncrementIntegerTransactionFactory.at(factoryMailbox, "counter");
AggregateTransaction aggregateGetTransaction =
        GetIntegerTransactionFactory.at(factoryMailbox, "counter");

System.out.println("db1");
IMDB db1 = new IMDB(mailboxFactory, factory, directoryPath, 10240);
db1.clearDirectory();
openDbFile.send(future, db1);
System.out.println("online");
TransactionAggregator transactionAggregator1 = db1.getTransactionAggregator();
aggregateIncrementTransaction.sendEvent(transactionAggregator1);
int total1 = (Integer) aggregateGetTransaction.send(future, transactionAggregator1);
assertEquals(1, total1);
db1.closeDbFile();

System.out.println("db2");
IMDB db2 = new IMDB(mailboxFactory, factory, directoryPath, 10240);
openDbFile.send(future, db2);
System.out.println("online");
TransactionAggregator transactionAggregator2 = db2.getTransactionAggregator();
aggregateIncrementTransaction.sendEvent(transactionAggregator2);
aggregateIncrementTransaction.sendEvent(transactionAggregator2);
int total2 = (Integer) aggregateGetTransaction.send(future, transactionAggregator2);
assertEquals(3, total2);
db2.closeDbFile();

System.out.println("db3");
IMDB db3 = new IMDB(mailboxFactory, factory, directoryPath, 10240);
openDbFile.send(future, db3);
System.out.println("online");
TransactionAggregator transactionAggregator3 = db3.getTransactionAggregator();
aggregateIncrementTransaction.sendEvent(transactionAggregator3);
aggregateIncrementTransaction.sendEvent(transactionAggregator3);
aggregateAddTransaction.sendEvent(transactionAggregator3);
int total3 = (Integer) aggregateGetTransaction.send(future, transactionAggregator3);
assertEquals(7, total3);
db3.closeDbFile();

mailboxFactory.close();

Back: Transactions Up: Home

Clone this wiki locally