Checksum fix

All started with an issue, occuring only if nothing was commited in a newly created database.

One of our app-embedded MapDB was failing with no apparent reason. By changing the name of its valueSerializer, it was working. In fact, if the name of this class was too long, issue was there. With shorter name, no issue.

After 2 hours of tests and debugging, i finally found out that saving serializerPojo could sometimes change the data used by indexHeaderChecksum().

So indexHeaderChecksum() result should be written after saving serializerPojo to be safe in those rare but possible cases :)

While reading code, i also fixed a typo related to compress flag mask handling.

FYI, here's the failing code on current master (same bug on 0.9.9). It must be exactly this code, with same package names, to reproduce ...

    package com.validit.vitam.engine.persistence.mapdb;

    import java.io.File;

    import org.mapdb.DB;
    import org.mapdb.DBMaker;
    import org.mapdb.HTreeMap;

    public class EmptyDbCorruptionTest {
        public static final String DB_NAME = "corrupt.db";

        public static void main(String[] args) throws InterruptedException {
            File file = new File(DB_NAME);
            File pFile = new File(DB_NAME + ".p");

            file.delete();
            pFile.delete();
            Thread.sleep(500);
            if (!file.exists()) {
                openDb(false);
            }
            openDb(true); // Fails to open, because checksum is invalid
        }

        private static DB openDb(boolean second) {
            DBMaker dbMaker = DBMaker.newFileDB(new File(DB_NAME)).transactionDisable().cacheDisable();
            if (second) {
                dbMaker.readOnly();
            }
            dbMaker.mmapFileEnable();
            dbMaker.closeOnJvmShutdown();
            DB db = dbMaker.make();
            
            try {
                HTreeMap<Object, Object> recordsCount = db.createHashMap("failing").valueSerializer(new SerializerABCDEFGH()).makeOrGet();
                return db;
            } finally {
                if (second) {
                    db.close();
                }
            }

        }
    }

With serializer class

    package com.validit.vitam.engine.persistence.mapdb;

    import java.io.DataInput;
    import java.io.DataOutput;
    import java.io.IOException;
    import java.io.Serializable;

    import org.mapdb.Serializer;

    public class SerializerABCDEFGH implements Serializer<Integer>, Serializable {
        private static final long serialVersionUID = 5044530660251666504L;

        @Override
        public void serialize(DataOutput out, Integer obj) throws IOException {
            out.writeInt(obj);
        }
        
        @Override
        public Integer deserialize(DataInput in, int available) throws IOException {
            return in.readInt();
        }
        
        @Override
        public int fixedSize() {
            return -1;
        }
    }