Versions Compared

Key

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

...

Code Block
languagejava
titleInterface
public interface CacheObjectTransformer {
    /** 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 offsetoriginal Original data.
     * @param length Data length.
     * @return Byte array contains the transformed Transformed data started with non-filled area with {@link #OVERHEAD} size.
     * @throws IgniteCheckedException or {@code null} when transformation is not possible/suitable.
     */
    public byte[]ByteBuffer transform(byte[] bytes, int offset, int length) throws IgniteCheckedException;      ByteBuffer original);

    /**
     * Restores the data.
     *
     * @param bytes  Byte array contains the transformed transformed Transformed data.
     * @return Restored data.
	 * @param offset Data offset.     */
    public ByteBuffer restore(ByteBuffer transformed);

    /**
     * @param length Data length Returns {@code true} when direct byte buffers are required.
     *
 @return  Byte array contains* the@return restoredDirect dataflag.
     */
    public byte[]boolean restore(byte[] bytes, int offset, int lengthdirect();
}

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 performanceallows users to use direct and wrapped byte buffers.

Every customer may implement this interface in a proper way if necessary and specify it in the configuration:

Code Block
languagejava
titleCustom transformer
IgniteConfiguration getConfiguration() {
	IgniteConfiguration cfg = ...

	cfg.setCacheObjectTransformer(new XXXTransformer());

	return cfg;
}

Simplified API

But, most customers just want to transform the data, so, they may extend the adapter with the simplified API:

...

languagejava
titleCacheObjectTransformerAdapter

...

Examples

Compression example

Code Block
languagejava
titleCompression
class CompressionTransformer extends CacheObjectTransformerAdapter {
	protected ByteBuffer transform(ByteBuffer original) throws IgniteCheckedException {                     
		int locOverhead = 4; // Original length.
        int totalOverhead = CacheObjectTransformer.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;
    }
}

...