Skip to content

Commit

Permalink
convert AerospikeCacheKeyProcessor into an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr committed Jun 19, 2024
1 parent 4bc70aa commit d65c93f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
package org.springframework.data.aerospike.cache;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.ByteBufferOutput;
import lombok.Getter;
import org.apache.commons.codec.digest.MurmurHash3;

import java.util.Arrays;

public abstract class AerospikeCacheKeyProcessor {

@Getter
private static final Kryo kryoInstance = new Kryo();
public interface AerospikeCacheKeyProcessor {

/**
* Serialize the given key and calculate hash based on the serialization result.
*
* @param key Object to be serialized and hashed
* @return AerospikeCacheKey instantiated with either a String or a long number
*/
public AerospikeCacheKey serializeAndHash(Object key) {
return calculateHash(serialize(key));
}
AerospikeCacheKey serializeAndHash(Object key);

/**
* Serialize the given key.
Expand All @@ -30,12 +18,7 @@ public AerospikeCacheKey serializeAndHash(Object key) {
* @param key Object to be serialized
* @return byte[]
*/
public byte[] serialize(Object key) {
ByteBufferOutput output = new ByteBufferOutput(1024); // Initial buffer size
kryoInstance.writeClassAndObject(output, key);
output.flush();
return output.toBytes();
}
byte[] serialize(Object key);

/**
* Calculate hash based on the given byte array.
Expand All @@ -45,7 +28,5 @@ public byte[] serialize(Object key) {
* @param data Byte array to be hashed
* @return AerospikeCacheKey instantiated with either a String or a long number
*/
public AerospikeCacheKey calculateHash(byte[] data) {
return AerospikeCacheKey.of(Arrays.toString(MurmurHash3.hash128(data)));
}
AerospikeCacheKey calculateHash(byte[] data);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package org.springframework.data.aerospike.cache;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.ByteBufferOutput;
import lombok.Getter;
import org.apache.commons.codec.digest.MurmurHash3;
import org.objenesis.strategy.StdInstantiatorStrategy;

public class AerospikeCacheKeyProcessorImpl extends AerospikeCacheKeyProcessor {
import java.util.Arrays;

public class AerospikeCacheKeyProcessorImpl implements AerospikeCacheKeyProcessor {

@Getter
private static final Kryo kryoInstance = new Kryo();

public AerospikeCacheKeyProcessorImpl() {
configureKryo();
Expand All @@ -20,4 +29,19 @@ public void configureKryo() {
getKryoInstance().setRegistrationRequired(false);
getKryoInstance().setInstantiatorStrategy(new StdInstantiatorStrategy());
}

public AerospikeCacheKey serializeAndHash(Object key) {
return calculateHash(serialize(key));
}

public byte[] serialize(Object key) {
ByteBufferOutput output = new ByteBufferOutput(1024); // Initial buffer size
kryoInstance.writeClassAndObject(output, key);
output.flush();
return output.toBytes();
}

public AerospikeCacheKey calculateHash(byte[] data) {
return AerospikeCacheKey.of(Arrays.toString(MurmurHash3.hash128(data)));
}
}

0 comments on commit d65c93f

Please sign in to comment.