Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
titleBinaryObjectImpl (un)marshalling
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);
}

...

Transformer

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.

API

The simplest way is to use Service Provider Interface (IgniteSpi):

Code Block
languagejava
titleSPI
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
languagejava
titleCompressionSpi
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;
    }
}

...