MapDB file DB recovery / reuse (write/search)

522 views
Skip to first unread message

Bhavin Pandya

unread,
May 7, 2015, 10:03:41 PM5/7/15
to ma...@googlegroups.com
Hi,

With MapDB, Is it possible to reuse (write/search) the existing file db created from DBMaker.newFileDB(file) after the JVM dies to map it to new JVM's new DB and reuse that ? 

Example :

If i had created a set and MapDB like 

DB db = DBMaker.newFileDB(new File("c:/tmp/test.db")).cacheDisable().transactionDisable().mmapFileEnable()
.closeOnJvmShutdown().freeSpaceReclaimQ(10).make();
Set<String> set = db.createHashSet("set1").expireAfterWrite(100, TimeUnit.HOURS).make();

And I had put 1_000 records in set using set.add(String s). Then the JVM dies but file is not deleted from the disk. 


so next time when i start, 

DB db = DBMaker.newFileDB(new File("c:/tmp/test.db")).cacheDisable().transactionDisable().mmapFileEnable()
.closeOnJvmShutdown().freeSpaceReclaimQ(10).make();
Set<String> set = db.createHashSet("set1").expireAfterWrite(100, TimeUnit.HOURS).make();

I expect to be able to get all 1_000 records accessed from set created here.

But, when i run the test like that, i get 0 for set.size() and when i search for the record that i put in before the JVM died, i don't get that record in the set after JVM restart. 

Is there any way to recover that test.db file that was created by another process ? and anyway to access the records through new set as explained in above example ?

Thanks,
Bhavin

Igor Castang

unread,
May 7, 2015, 11:11:17 PM5/7/15
to ma...@googlegroups.com
this may be a dumb question from me but when you are done inserting in your set and before the JVM exits, do you call db.commit(); ?

Bhavin Pandya

unread,
May 10, 2015, 7:59:30 PM5/10/15
to ma...@googlegroups.com
According to Javadoc, DB.commit() is only required if you have transactions enabled. In my case, i have got transaction disabled. This should save the records directly into the store. In order to avoid corruption, it needs to though call DB.close(), which i assume the closeOnJVMShutdown hook should have done. 

Just to clarify this further, I am running this test on 1.0.7 version of the MapDB.

Thanks,
Bhavin

Igor Castang

unread,
May 11, 2015, 2:55:52 PM5/11/15
to ma...@googlegroups.com
then I don't know. Your use case is very "typical" you should be able to have the data persist across JVM start/stop.
Maybe try to create your set without the expiration time and see if this way it works ?

Bhavin Pandya

unread,
May 12, 2015, 3:06:21 AM5/12/15
to ma...@googlegroups.com
You are right Igor, I realized, what i was doing wrong, 

instead of using createHashSet the second time I have to use 

db.get("set1"); 

which will give me the existing set, it works fine now !!

Cheers,
Bhavin

Jan Kotek

unread,
May 12, 2015, 3:39:50 AM5/12/15
to ma...@googlegroups.com, Bhavin Pandya

Glad to hear it works for you. The db.createHashSet("set1").make() method should throw an exception if collection already exists. There is `makeOrGet()` method which returns existing.

 

Data can be reused if store is properly closed (ie use transaction with WAL for crash protection, or properly close store without transaction).

 

Jan

--
You received this message because you are subscribed to the Google Groups "MapDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapdb+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Reply all
Reply to author
Forward
0 new messages