The SimpleDatabase is the simplest Database implementation. It does not have any special capabilities.
See Example 3.1, “Creating a database and inserting two records” for an example of how a SimpleDatabase can be created.
The LoggingTransactionalDatabase is a transactional Database that keeps a rollback log during read/write transactions. If the transaction is rolled back, the rollback log is replayed to restore the database contents to what it was before the transaction. If the transaction is not completed before the JVM terminates, the rollback log is replayed when a new database object is created with the same rollback file. This ensures that the database can be restored to a consistent state in the event of a program crash.
Example 4.1. Creating a logging transactional database on a heap backend
// Create a LoggingTransactionalDatabase using a // LoggingTransactionalDatabaseBuilder object. // Keep the database data in the File f and the rollback log in the File lf. // // If the rollback log file is not empty, the rollback log there is replayed to // restore the database contents. TransactionalDatabase<String, String> db = new LoggingTransactionalDatabaseBuilder<String, String>(). // The logging transactional database needs serializers for writing data to // the rollback log. setKeySerializer(StringSerializer.INSTANCE). setValueSerializer(StringSerializer.INSTANCE). create( new HeapBackendBuilder<String, String>(). setKeySerializer(StringSerializer.INSTANCE). setValueSerializer(StringSerializer.INSTANCE). // Adapt the File database file to a ReadWritableFile. create(new ReadWritableFileAdapter(f)), // Adapt the File log file to a ReadWritableFile. new ReadWritableFileAdapter(lf));
The ShadowCopyTransactionalDatabase uses a shadow copy of the database, i.e. a copy of the entire database, that it records modifications to during read/write transactions. If the transaction is committed, the original database is replaced with the shadow copy. If it is rolled back, the shadow copy is discarded. For large databases, this is really inefficient, but for small databases the performance is comparable to logging transactions.
Since the ShadowCopyTransactionalDatabase needs to be able to create a new DatabaseBackend object for each shadow copy, it is given a DatabaseBackendFactory for the backend when it is created instead of just one DatabaseBackend object.
Example 4.2. Creating a shadow copy transactional database on a heap backend
// Create a ShadowCopyTransactionalDatabase on the database EFile f // and that keeps its shadow copy files in the Directory tmpDir. // // f or tmpDir must not be in a locking FileSystem. // // The ShadowCopyTxnDatabaseFileManager is an interface that lets the // database manipulate the backend's files without knowing exactly how many they // are or how they are structured. // A LogAdapter that logs to stdout and stderr. LogAdapterHolder lah = new LogAdapterHolder( new StdOutLogAdapter()); ShadowCopyTxnDatabaseFileManager fm = // There is an implementation tailored for the HeapBackend. new HeapBackendFileManager(f, tmpDir); // The shadow copy transactional database uses a backend factory instead of a // single DatabaseBackend object since it must be able to create new // backend objects for its shadow copy files. DatabaseBackendFactory<String, String, Long> dbf= new HeapBackendFactory<String, String>( // Key and value Serializer:s. StringSerializer.INSTANCE, StringSerializer.INSTANCE, lah); TransactionalDatabase<String, String> db = new ShadowCopyTransactionalDatabase<String, String, Long>(fm, dbf, false, lah);