3

I am using MapDB for key-value database for the better performace.After the map.clear() my files are not deleted in disk.I need to reclaim the diskspace after the remove() or clear() method.Below are the my code sippet.

    public static void mapDBPerformanceonFileDBStack() throws Exception{
    String fileName ="testdatabase.db";
    DB db = DBMaker
            .fileDB(fileName)
            .closeOnJvmShutdown()
            .make();
    Map myMap = db.hashMap("testmap").keySerializer(Serializer.INTEGER).valueSerializer(Serializer.STRING).createOrOpen();
    System.out.println("Intial mapsize :" +myMap.size());
    for(int i=0;i<100000;i++){
        myMap.put(i,"key added" );
    }
    db.commit();
    System.out.println("mapsize after the addition :"+myMap.size());
    myMap.clear();
    System.out.println("mapsize after clear :"+myMap.size());
    db.commit();
    db.close();
}

My testdatabase file size not reclaimed after the map.clear() - Same size after the clear().

Balakumar S
  • 31
  • 1
  • 6
  • 2
    I know this is really late, I'm just commenting for people googling this issue. You can try db.compact() which aims to reclaim any storage, however MapDB seems to be struggling to do proper cleanup (even after db.close()), so I just create a new DB everytime I need to temporarily hold big data and then just delete it manually by deleting all files that start with the name provided to the DBMaker. This surely is not a good practice though. Also note that you may need to call db.commit after making changes. – Lahzey Jan 27 '20 at 12:38

1 Answers1

0
        ...
        Store[] stores = (HtreeMap)myMap.getStores();
        for (Store store : stores) {
            store.compact();
        }
        ...
        db.commit();
        db.close();
  • 2
    Welcome to StackOverflow! Please, provide an explanation in order to improve your answer and make it more understandable for the community – xKobalt Aug 21 '20 at 07:38