Java Code Examples for org.mapdb.DB#commit()

The following examples show how to use org.mapdb.DB#commit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StatisticDataRepositoryMapDB.java    From AisAbnormal with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void close() {
    LOG.info("Attempting to commit statistic data repository.");
    if (!readOnly) {
        db.commit();
    }
    LOG.info("Statistic data repository committed.");

    if (this.dumpToDiskOnClose) {
        LOG.info("Dump in-memory data to disk.");
        DB onDisk = openDiskDatabase(dbFile, false);
        copyToDatabase(onDisk);
        /*
        LOG.info("Compacting data file.");
        onDisk.compact(); // necessary?
        LOG.info("Completed compacting data file.");
        */
        onDisk.commit();
        onDisk.close();
        LOG.info("Dump in-memory data to disk: Done.");
    }

    LOG.info("Attempting to close statistic data repository.");
    db.close();
    LOG.info("Statistic data repository closed.");
}
 
Example 2
Source File: StatisticDataRepositoryMapDB.java    From AisAbnormal with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void backupDBToDisk() {
    LOG.debug("Preparing to backup database to disk.");

    this.backupToDiskLock.lock();
    try {
        File backupDBFile = prepareBackupDBFileFor(dbFile);
        if (backupDBFile == null) {
            LOG.error("Failed to prepare DB backup file. Cannot backup database to disk.");
            return;
        }

        DB backupDB = openDiskDatabase(backupDBFile, false);
        copyToDatabase(backupDB);
        backupDB.commit();
        backupDB.close();

        LOG.info("Database successfully backed up to to disk (\"" + backupDBFile.getName() + "\").");
    } finally {
        this.backupToDiskLock.unlock();
    }
}
 
Example 3
Source File: DiffIndexUpgrades.java    From olca-app with Mozilla Public License 2.0 6 votes vote down vote up
private static void upgradeV2(DiffIndex index) {
	index.close();
	File file = new File(index.getDir(), "indexfile");
	DB db = DBMaker.fileDB(file).lockDisable().closeOnJvmShutdown().make();
	org.mapdb.Atomic.Integer v = db.atomicInteger("version");
	File tmpFile = new File(index.getDir(), "tmpindexfile");
	DB db2 = DBMaker.fileDB(tmpFile).lockDisable().closeOnJvmShutdown().make();
	Map<String, Diff> oldIndex = db.hashMap("diffIndex");
	Map<String, Set<String>> changedTopLevelElements = db.hashMap("changedTopLevelElements");
	Map<String, Diff> tmpIndex = db2.hashMap("diffIndex");
	Map<String, Set<String>> tmpChangedTopLevelElements = db2.hashMap("changedTopLevelElements");
	for (String oldKey : oldIndex.keySet()) {
		Diff value = oldIndex.get(oldKey);
		tmpIndex.put(value.getDataset().toId(), value);
	}
	tmpChangedTopLevelElements.putAll(changedTopLevelElements);
	v = db2.atomicInteger("version");
	v.set(2);
	db2.commit();
	db2.close();
	db.close();
	file.delete();
	tmpFile.renameTo(file);
	index.open();
}
 
Example 4
Source File: _HelloWorld.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public static void main(String[] args){

        //Configure and open database using builder pattern.
        //All options are available with code auto-completion.
        File dbFile = Utils.tempDbFile();
        DB db = DBMaker.newFileDB(dbFile)
                .closeOnJvmShutdown()
                .encryptionEnable("password")
                .make();

        //open an collection, TreeMap has better performance then HashMap
        ConcurrentNavigableMap<Integer,String> map = db.getTreeMap("collectionName");

        map.put(1,"one");
        map.put(2,"two");
        //map.keySet() is now [1,2] even before commit

        db.commit();  //persist changes into disk

        map.put(3,"three");
        //map.keySet() is now [1,2,3]
        db.rollback(); //revert recent changes
        //map.keySet() is now [1,2]

        db.close();

    }
 
Example 5
Source File: Custom_Value.java    From scava with Eclipse Public License 2.0 4 votes vote down vote up
public static void main(String[] args) {
	
	// Open or create db file
	DB db = DBMaker.newFileDB(new File("dbCustomValue"))
			.make();
	
	// Open or create table
	Map<String,Person> dbMap = db.getTreeMap("personAndCity");
	
	// Add data
	Person bilbo = new Person("Bilbo","The Shire");
	Person sauron = new Person("Sauron","Mordor");
	Person radagast = new Person("Radagast","Crazy Farm");
	
	dbMap.put("west",bilbo);
	dbMap.put("south",sauron);
	dbMap.put("mid",radagast);

	// Commit and close
	db.commit();
	db.close();


       //
       // Second option for using cystom values is to use your own serializer.
       // This usually leads to better performance as MapDB does not have to
       // analyze the class structure.
       //

       class CustomSerializer implements Serializer<Person>, Serializable{

           @Override
           public void serialize(DataOutput out, Person value) throws IOException {
               out.writeUTF(value.getName());
               out.writeUTF(value.getCity());
           }

           @Override
           public Person deserialize(DataInput in, int available) throws IOException {
               return new Person(in.readUTF(), in.readUTF());
           }
       }

       Serializer<Person> serializer = new CustomSerializer();

       DB db2 = DBMaker.newTempFileDB().make();

       Map<String,Person> map2 = db2.createHashMap("map",null, serializer); //key serializer is null (use default)

       map2.put("North", new Person("Yet another dwarf","Somewhere"));

       db2.close();


}
 
Example 6
Source File: StatisticDataRepositoryMapDB.java    From AisAbnormal with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void putMetaData(DB db, DatasetMetaData datasetMetadata) {
    BTreeMap<String, DatasetMetaData> allMetadata = db.createTreeMap(COLLECTION_METADATA).makeOrGet();
    allMetadata.put(KEY_METADATA, datasetMetadata);
    db.commit();
}
 
Example 7
Source File: TransactionsUnitTest.java    From tutorials with MIT License 3 votes vote down vote up
@Test
public void givenValidDBSetup_whenTransactionCommittedAndRolledBack_checkPreviousStateAchieved() {

    DB db = DBMaker.memoryDB().transactionEnable().make();

    NavigableSet<String> set = db
      .treeSet("mySet")
      .serializer(Serializer.STRING)
      .createOrOpen();

    set.add("One");
    set.add("Two");

    db.commit();

    assertEquals(2, set.size());

    set.add("Three");

    assertEquals(3, set.size());

    db.rollback();

    assertEquals(2, set.size());

    db.close();
}