Search in sources :

Example 1 with RecordManager

use of jdbm.RecordManager in project qi4j-sdk by Qi4j.

the class JdbmEntityStoreMixin method restore.

@Override
public Output<String, IOException> restore() {
    return new Output<String, IOException>() {

        @Override
        public <SenderThrowableType extends Throwable> void receiveFrom(Sender<? extends String, SenderThrowableType> sender) throws IOException, SenderThrowableType {
            File dbFile = new File(getDatabaseName() + ".db");
            File lgFile = new File(getDatabaseName() + ".lg");
            // Create temporary store
            File tempDatabase = Files.createTemporayFileOf(dbFile);
            final RecordManager recordManager = RecordManagerFactory.createRecordManager(tempDatabase.getAbsolutePath(), new Properties());
            ByteArrayComparator comparator = new ByteArrayComparator();
            final BTree index = BTree.createInstance(recordManager, comparator, serializer, DefaultSerializer.INSTANCE, 16);
            recordManager.setNamedObject("index", index.getRecid());
            recordManager.commit();
            try {
                sender.sendTo(new Receiver<String, IOException>() {

                    int counter = 0;

                    @Override
                    public void receive(String item) throws IOException {
                        // Commit one batch
                        if ((counter++ % 1000) == 0) {
                            recordManager.commit();
                        }
                        String id = item.substring("{\"identity\":\"".length());
                        id = id.substring(0, id.indexOf('"'));
                        // Insert
                        byte[] stateArray = item.getBytes("UTF-8");
                        long stateIndex = recordManager.insert(stateArray, serializer);
                        index.insert(id.getBytes("UTF-8"), stateIndex, false);
                    }
                });
            } catch (IOException e) {
                recordManager.close();
                tempDatabase.delete();
                throw e;
            } catch (Throwable senderThrowableType) {
                recordManager.close();
                tempDatabase.delete();
                throw (SenderThrowableType) senderThrowableType;
            }
            // Import went ok - continue
            recordManager.commit();
            // close file handles otherwise Microsoft Windows will fail to rename database files.
            recordManager.close();
            lock.writeLock().lock();
            try {
                // Replace old database with new
                JdbmEntityStoreMixin.this.recordManager.close();
                boolean deletedOldDatabase = true;
                deletedOldDatabase &= dbFile.delete();
                deletedOldDatabase &= lgFile.delete();
                if (!deletedOldDatabase) {
                    throw new IOException("Could not remove old database");
                }
                boolean renamedTempDatabase = true;
                renamedTempDatabase &= new File(tempDatabase.getAbsolutePath() + ".db").renameTo(dbFile);
                renamedTempDatabase &= new File(tempDatabase.getAbsolutePath() + ".lg").renameTo(lgFile);
                if (!renamedTempDatabase) {
                    throw new IOException("Could not replace database with temp database");
                }
                // Start up again
                initialize();
            } finally {
                lock.writeLock().unlock();
            }
        }
    };
}
Also used : BTree(jdbm.btree.BTree) IOException(java.io.IOException) Properties(java.util.Properties) Sender(org.qi4j.io.Sender) CacheRecordManager(jdbm.recman.CacheRecordManager) RecordManager(jdbm.RecordManager) Output(org.qi4j.io.Output) File(java.io.File) ByteArrayComparator(jdbm.helper.ByteArrayComparator)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 Properties (java.util.Properties)1 RecordManager (jdbm.RecordManager)1 BTree (jdbm.btree.BTree)1 ByteArrayComparator (jdbm.helper.ByteArrayComparator)1 CacheRecordManager (jdbm.recman.CacheRecordManager)1 Output (org.qi4j.io.Output)1 Sender (org.qi4j.io.Sender)1