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

The following examples show how to use org.mapdb.DB#close() . 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: Huge_Read.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
public static void main(String[] args){
DB db = DBMaker.newFileDB(new File("/tmp/db2"))
        .journalDisable()
                //.asyncWriteDisable()
        .make();

Map<Integer, String> map = db.getTreeMap("map");

long time = System.currentTimeMillis();
long max = (int) 1e8;
long step = max/100;
for(int i=0;i<max;i++){
    map.get(i);
    if(i%step == 0){
        System.out.println(100.0 * i/max);
    }

}

System.out.println("Closing");
db.close();

System.out.println(System.currentTimeMillis() - time);
}
 
Example 2
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 3
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 4
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 5
Source File: MapDBTestSuite.java    From eagle with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnHeapDB() {
    DB db = DBMaker.heapDB().make();
    BTreeMap<Long, String> map = db.treeMap("btree").keySerializer(Serializer.LONG).valueSerializer(Serializer.STRING).create();
    Assert.assertFalse(map.putIfAbsentBoolean(1L, "val_1"));
    Assert.assertTrue(map.putIfAbsentBoolean(1L, "val_2"));
    Assert.assertTrue(map.putIfAbsentBoolean(1L, "val_3"));
    Assert.assertFalse(map.putIfAbsentBoolean(2L, "val_4"));

    Assert.assertEquals("val_1", map.get(1L));
    Assert.assertEquals("val_4", map.get(2L));

    Assert.assertTrue(map.replace(2L, "val_4", "val_5"));
    Assert.assertEquals("val_5", map.get(2L));

    map.close();
    db.close();
}
 
Example 6
Source File: Huge_Insert.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public static void main(String[] args){
    DB db = DBMaker
            //.newFileDB(new File("/mnt/big/db/aa"))
            .newAppendFileDB(new File("/mnt/big/db/aa" + System.currentTimeMillis()))
            .make();

    Map map = db
            .getTreeMap("map");
            //.getHashMap("map");

    long time = System.currentTimeMillis();
    long max = (int) 1e8;
    AtomicLong progress = new AtomicLong(0);
    Utils.printProgress(progress);

    while(progress.incrementAndGet()<max){
        Long val = Utils.RANDOM.nextLong();
        map.put(val, "test"+val);
    }
    progress.set(-1);

    System.out.println("Closing");
    db.close();

    System.out.println(System.currentTimeMillis() - time);

}
 
Example 7
Source File: BatchOwlLoader.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
public static void load(OwlLoadConfiguration config)
    throws InterruptedException, ExecutionException {
  Injector i = Guice.createInjector(new OwlLoaderModule(config),
      new Neo4jModule(config.getGraphConfiguration()));
  BatchOwlLoader loader = i.getInstance(BatchOwlLoader.class);
  logger.info("Loading ontologies...");
  Stopwatch timer = Stopwatch.createStarted();
  // TODO catch exception and delete the incomplete graph through the graph location
  loader.loadOntology();
  DB mapDb = i.getInstance(DB.class);
  mapDb.close();
  logger.info(format("Loading took %d minutes", timer.elapsed(TimeUnit.MINUTES)));
}
 
Example 8
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 9
Source File: DiffIndexUpgrades.java    From olca-app with Mozilla Public License 2.0 5 votes vote down vote up
public static int getVersion(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");
	int version = v == null || v.intValue() <= 0 ? 1 : v.intValue();
	db.close();
	index.open();
	return version;
}
 
Example 10
Source File: MapTest.java    From bidder with Apache License 2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {

        HTreeMap objects;
        DB db = DBMaker
                .fileDB("junk.db")
                .fileMmapEnable()            // Always enable mmap
                .fileMmapEnableIfSupported() // Only enable mmap on supported platforms
                .fileMmapPreclearDisable()   // Make mmap file faster

                // Unmap (release resources) file when its closed.
                // That can cause JVM crash if file is accessed after it was unmapped
                // (there is possible race condition).
                .cleanerHackEnable()
                .transactionEnable()
                .make();

//optionally preload file content into disk cache
        db.getStore().fileLoad();

        objects = db.hashMap("objects").createOrOpen();

        double time = System.currentTimeMillis();

        Map m = new HashMap();

        for (int i=0;i<1000000;i++) {
            objects.put("ben", "faul");
        }

        time = System.currentTimeMillis() - time;

        time = time/1000;
        double d = 1000000/time;

        System.out.println("D: " + d + " writes/second");

        db.close();

	}
 
Example 11
Source File: TreeMap_Performance_Tunning.java    From scava with Eclipse Public License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    Random r = new Random();



    System.out.println(" Node size |  small vals  |  large vals  |  large vals outside node" );

    for(int nodeSize:nodeSizes){

        System.out .print("    "+nodeSize+"      |");

        for(int j=0;j<3;j++){

            boolean useSmallValues = (j==0);
            boolean valueOutsideOfNodes = (j==2);

            DB db = DBMaker
                    .newFileDB(new File("/mnt/big/adsasd"))
                    .deleteFilesAfterClose()
                    .closeOnJvmShutdown()
                    .journalDisable()
                    .cacheSize(10) //use small cache size, to simulate much larger store with relatively small cache.
                    .make();


            Map<Long,String> map = db.createTreeMap("test",nodeSize, valueOutsideOfNodes, null, null, null );

            long startTime = System.currentTimeMillis();

            for(int i=0;i<1e6;i++){
                long key = r.nextLong();
                String value =  useSmallValues?
                        //small value
                        "abc"+key:
                        //large value
                        "qwdkqwdoqpwfwe-09fewkljklcejewfcklajewjkleawckjlaweklcwelkcwecklwecjwekecklwecklaa"
                                +"kvlskldvklsdklcklsdvkdflvvvvvvvvvvvvvvvvvvvvvvvsl;kzlkvlksdlkvklsdklvkldsklk"
                                +key;
                map.put(key, value);
            }

            System.out.print("    ");
            System.out.print((System.currentTimeMillis()-startTime)/1000+" s");
            System.out.print("   |");
            db.close();
        }
        System.out.println("");
    }
}
 
Example 12
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 13
Source File: CollectionsUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenSetCreatedInDB_whenMultipleElementsAdded_checkOnlyOneExists() {

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

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

    String myString = "Baeldung!";

    set.add(myString);
    set.add(myString);

    assertEquals(1, set.size());

    db.close();
}
 
Example 14
Source File: HelloBaeldungUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenInMemoryDBInstantiateCorrectly_whenDataSavedAndRetrieved_checkRetrievalCorrect() {

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

    String welcomeMessageKey = "Welcome Message";
    String welcomeMessageString = "Hello Baeldung!";

    HTreeMap myMap = db.hashMap("myMap").createOrOpen();
    myMap.put(welcomeMessageKey, welcomeMessageString);

    String welcomeMessageFromDB = (String) myMap.get(welcomeMessageKey);

    db.close();

    assertEquals(welcomeMessageString, welcomeMessageFromDB);
}
 
Example 15
Source File: HelloBaeldungUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenInFileDBInstantiateCorrectly_whenDataSavedAndRetrieved_checkRetrievalCorrect() {

    DB db = DBMaker.fileDB("file.db").make();

    String welcomeMessageKey = "Welcome Message";
    String welcomeMessageString = "Hello Baeldung!";

    HTreeMap myMap = db.hashMap("myMap").createOrOpen();
    myMap.put(welcomeMessageKey, welcomeMessageString);

    String welcomeMessageFromDB = (String) myMap.get(welcomeMessageKey);

    db.close();

    assertEquals(welcomeMessageString, welcomeMessageFromDB);
}
 
Example 16
Source File: Lazily_Loaded_Records.java    From scava with Eclipse Public License 2.0 4 votes vote down vote up
public static void main(String[] args) {

        DB db = DBMaker.newMemoryDB().make();
        //
        // TreeMap has build in support for lazily loaded values.
        // In that case each value are not stored inside node,
        // but in separate record.
        //
        // use DB.createTreeMap to create TreeMap with non-default parameters

        boolean valuesStoredOutsideNodes = true;
        Map map = db.createTreeMap("name",32, valuesStoredOutsideNodes, null, null, null);
        map.put("key","this string is loaded lazily with 'map.get(key)' ");


        //
        // Other option for lazily loaded record is to use Atomic.Var.
        // In this case you have singleton record with name.
        // As bonus you can update reference in thread-safe atomic manner.
        //
        Atomic.Var<String> record =
                Atomic.createVar(db, "lazyRecord", "aaa", db.getDefaultSerializer());

        record.set("some value");
        System.out.println(record.get());


        // Last option is to use low level Engine storage directly.
        // Each stored record gets assigned unique recid (record id),
        // which is latter used to get or update record.
        // Your code should store only recid as reference to object.
        // All MapDB collections are written this way.

        //insert new record
        long recid = db.getEngine().put("something", Serializer.STRING_SERIALIZER);

        //load record
        String lazyString = db.getEngine().get(recid, Serializer.STRING_SERIALIZER);

        //update record
        db.getEngine().update(recid, "new value", Serializer.STRING_SERIALIZER);


        //I hope this example helped!
        db.close();

    }
 
Example 17
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();
}
 
Example 18
Source File: TestDynRR.java    From Clusion with GNU General Public License v3.0 2 votes vote down vote up
public static void main(String[] args) throws Exception {
	
	
	
	
	// init
	DB db = DBMaker.fileDB("test.db").fileMmapEnable().fileMmapPreclearDisable()
			.allocateStartSize(124 * 1024 * 1024).allocateIncrement(5 * 1024 * 1024).make();
	
	ConcurrentMap<String, byte[]> dictionary = (ConcurrentMap<String, byte[]>) db.hashMap("test").createOrOpen();		
	
	byte[] sk = new byte[32];
	
	String SN = "X4238y57y78234";
	String countyID = "00000000000000000000000000001562";

	System.out.println(countyID.getBytes("UTF-8").length);

	
	
	Map<String, String> lookup = new HashMap<String, String>();		
	lookup.put(SN, countyID);
	
	
	// creation of the update token	
	Map<String, byte[]> utk  = DynRR.updateToken(sk, lookup);
	
	
	// update
	DynRR.update(dictionary, utk);
	
	// search token
	byte[][] stk = DynRR.searchToken(sk, SN);
	
	
	//query operation
	byte[] encryptedResult = DynRR.query(stk[0], dictionary);
	
	//resolve operation
	String plaintextCounty = DynRR.resolve(stk[1], encryptedResult);
	
	
	System.out.println(plaintextCounty);

	// close the database
	db.close();
	
}