javaberkeley-db-je

Berkeley JE StoredMap : replace existing value without loading previous one


I want to update a StoredMap value and I don't care about the old value. I cannot find a way to avoid the previous value from being loaded.

StoredMap<Integer, SyntaxDocument> tsCol = new StoredMap<Integer, SyntaxDocument>(tsdb, new IntegerBinding(), new PassageBinding(), true);
tsCol.put(1, doc); // insert value => ok
tsCol.put(1, doc); // <- load previous value but I don't care. I want to avoid the "heavy" PassageBinding process.
tsCol.putAll(Collections.singletonMap(1, doc)); // Even this one load the old value

Is there a way to optimize my code and update an existing value without loading it (or at least to prevent the binding to process old DatabaseEntry bytes)?

NOTE: that calling remove then put is slower.


Solution

  • Solution is to use lowlevel Database API :

    Database tsdb = environment.openDatabase(null, "tsdb", dbconfig);
    PassageBinding binding = new PassageBinding();
    DatabaseEntry idDbEntry = new DatabaseEntry();
    
    IntegerBinding.intToEntry(id, idDbEntry);
    DatabaseEntry dbEntry = new DatabaseEntry();
    pb.objectToEntry(data, dbEntry);
    
    tsdb.put(null, idDbEntry, dbEntry); // <-- replace existing value without loading it.