Versions Compared

Key

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

...

Code Block
languagejava
titleSPI
public interface CacheObjectTransformerSpi extends IgniteSpi {
    /** Additional space required to store the transformed data. */
    public int OVERHEAD = 6;

    /**
     * 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 ending with the transformed data.
     * @param offset Transformed data offset.
     * @param length Original data length.
     * @return Byte array contains the restored data.
     */
    public byte[] restore(byte[] bytes, int offset, int length); 
}

This API is known for the overhead used to store transformed data and is able to work with byte arrays with custom offsets, which is necessary to guarantee performance.

...

Code Block
languagejava
titleCacheObjectTransformerSpiAdapter
public abstract class CacheObjectTransformerSpiAdapter extends IgniteSpiAdapter implements CacheObjectTransformerSpi {
...
    /**
     * Transforms the data.
     *
     * @param original Original data.
     * @return Transformed data.
     * @throws IgniteCheckedException when transformation is not possible/suitable.
     */
    protected abstract ByteBuffer transform(ByteBuffer original) throws IgniteCheckedException;

      

	/**
     * Restores the data.
     *
     * @param transformed Transformed data.
     * @param length Original data length.
     * @return Restored data.
     */
    protected abstract ByteBuffer restore(ByteBuffer transformed, int length); 
}

Compression example

Code Block
languagejava
titleCompressionSpi
class CompressionTransformerSpi extends CacheObjectTransformerSpiAdapter {
    private static final LZ4Factory lz4Factory = LZ4Factory.fastestInstance();

    protected ByteBuffer transform(ByteBuffer original) throws IgniteCheckedException {
        int lim = original.remaining() - CacheObjectTransformerSpi.OVERHEAD;

        if (lim <= 0)
            throw new IgniteCheckedException("Compression is not profitable.");

        ByteBuffer compressed = byteBuffer(lim);

        Zstd.compress(compressed, original, 1);

        compressed.flip();             

        return compressed;
    }

    protected ByteBuffer restore(ByteBuffer transformed, int length) {
        ByteBuffer restored = byteBuffer(length);

        Zstd.decompress(restored, transformed);

        restored.flip();
              
        return restored;
    }
}

...