com.fasterxml.clustermate.service.store.AllowUndeletingUpdates Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of clustermate-service Show documentation
Show all versions of clustermate-service Show documentation
Building blocks for ClusterMate-based services and servers.
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);
}
}