THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
Code Block | ||||
---|---|---|---|---|
| ||||
private byte[] arrayFromValueBytes(CacheObjectValueContext ctx) { return CacheObjectTransformerUtils.restoreIfNecessary(valBytes, ctx); } private byte[] valueBytesFromArray(CacheObjectValueContext ctx) { return CacheObjectTransformerUtils.transformIfNecessary(arr, start, arr.length, ctx); } public void finishUnmarshal(CacheObjectValueContext ctx, ClassLoader ldr) throws IgniteCheckedException { if (arr == null) arr = arrayFromValueBytes(ctx); } public void prepareMarshal(CacheObjectValueContext ctx) { if (valBytes == null) valBytes = valueBytesFromArray(ctx); } |
...
Some customers may want to encrypt the data, some to compress it, while some just keep it as is.
So, we must provide a simple way to append any transformation.
The simplest way is to use Service Provider Interface (IgniteSpi):
Code Block | ||||
---|---|---|---|---|
| ||||
public interface CacheObjectTransformerSpi extendsCacheObjectTransformer IgniteSpi { /** Additional space required to store the transformed data. */ public int OVERHEAD = 2; /** * Transforms the data. * * @param bytes Byte array contains the data. * @param offset Data offset. * @param length Data length. * @return Byte array contains the transformed data started with non-filled area with {@link #OVERHEAD} size. * @throws IgniteCheckedException when transformation is not possible/suitable. */ public byte[] transform(byte[] bytes, int offset, int length) throws IgniteCheckedException; /** * Restores the data. * * @param bytes Byte array contains the transformed data. * @param offset Data offset. * @param length Data length. * @return Byte array contains the restored data. */ public byte[] restore(byte[] bytes, int offset, int length); } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
class CompressionTransformer extends CacheObjectTransformerAdapter { protected ByteBuffer transform(ByteBuffer original) throws IgniteCheckedException { int locOverhead = 4; // Original length. int totalOverhead = CacheObjectTransformerSpiCacheObjectTransformer.OVERHEAD + locOverhead; int origSize = original.remaining(); int lim = origSize - totalOverhead; if (lim <= 0) throw new IgniteCheckedException("Compression is not profitable."); ByteBuffer compressed = byteBuffer(lim); compressed.position(locOverhead); Zstd.compress(compressed, original, 1); compressed.flip(); compressed.putInt(origSize); compressed.rewind(); return compressed; } protected ByteBuffer restore(ByteBuffer transformed) { ByteBuffer restored = byteBuffer(transformed.getInt()); Zstd.decompress(restored, transformed); restored.flip(); return restored; } } |
...