All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fasterxml.clustermate.service.store.AllowUndeletingUpdates Maven / Gradle / Ivy

There is a newer version: 0.10.5
Show newest version
package com.fasterxml.clustermate.service.store;

import com.fasterxml.storemate.shared.StorableKey;
import com.fasterxml.storemate.shared.hash.HashConstants;
import com.fasterxml.storemate.store.Storable;
import com.fasterxml.storemate.store.util.OverwriteChecker;

/**
 * Helper class that implements policy to allow "overwrites" of deleted entries,
 * as long as new content appears to be same as deleted one, by content hash.
 */
public class AllowUndeletingUpdates implements OverwriteChecker
{
    public final static OverwriteChecker instance = new AllowUndeletingUpdates();

    // Nope, can't say without seeing entries in question
    @Override
    public Boolean mayOverwrite(StorableKey key) { return null; }

    /**
     * Logic for overwrite are such that we may overwrite (soft) deleted entries, as 
     * long as hash codes are compatible
     */
    @Override
    public boolean mayOverwrite(StorableKey key, Storable oldEntry, Storable newEntry)
    {
        return oldEntry.isDeleted()
                && _hashCodesMatch(oldEntry.getContentHash(), newEntry.getContentHash())
                && _hashCodesMatch(oldEntry.getCompressedHash(), newEntry.getCompressedHash())
                ;
    }

    protected boolean _hashCodesMatch(int hash1, int hash2) {
        // if either one is missing, need to accept
        if (hash1 == HashConstants.CHECKSUM_FOR_ZERO || hash2 == HashConstants.CHECKSUM_FOR_ZERO) {
            return true;
        }
        return (hash1 == hash2);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy