Versions Compared

Key

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

...

IDIEP-97
AuthorAnton Vinogradov 
Sponsor
Created

 

Status

Status
colourGreen
titleactive

...

  • No-Op transformer will produce [-3, 0, 3, 42, 0, 0, 0] or [-3, 0, 9, 11, 0, 0, 0, 84, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103], where 0 is a protocol version.
  • Pseudo-Crypto transformer, which adds 1 to every original byte, will produce [-3, 0, 4, 43, 1, 1, 1] or [-3, 0, 10, 12, 1, 1, 1, 85, 102, 116, 117, 33, 116, 117, 115, 106, 111, 104]
  • Magic-Compressor will produce [-3, 0, 7] or [-3, 0, 17], where 7 and 17 are the result of a magic compression.

...

Code Block
languagejava
titleInterface
public interface CacheObjectTransformerManager extends GridCacheSharedManager {
    /**
     * Transforms the data.
     *
     * @param original Original data.
     * @return Transformed data (started with {@link GridBinaryMarshaller#TRANSFORMED} when restorable)
     * or {@code null} when transformation is not possible/suitable.
     */
    public @Nullable ByteBuffer transform(ByteBuffer original);

    /**
     * Restores the data.
     *
     * @param transformed Transformed data.
     * @return Restored data.
     */
    public ByteBuffer restore(ByteBuffer transformed);
}

...

Code Block
languagejava
titleCompression
class CompressionTransformer extends CacheObjectTransformerAdapter {
	protected ByteBuffer transform(ByteBuffer original) throws IgniteCheckedException {                     
		int locOverheadoverhead = 45; // OriginalTransformed length.
flag        int totalOverhead = CacheObjectTransformerUtils.OVERHEAD + locOverhead;+ length.

        int origSize = original.remaining();
        int lim = origSize - totalOverheadoverhead;              

		if (lim <= 0)             
        	return null; // Compression is not profitable.

        ByteBuffer compressed = byteBuffer(locOverheadoverhead + (int)Zstd.compressBound(origSize));    

		compressed.put(TRANSFORMED);
		compressed.putInt(origSize);    

		int size = Zstd.compress(compressed, original, 1);

 		if (size >= lim)
        	return null; // Compression is not profitable.          

		compressed.flip();          

        return compressed;
    }

    protected ByteBuffer restore(ByteBuffer transformed) {
        ByteBuffer restored = byteBuffer(transformed.getInt());

        Zstd.decompress(restored, transformed);

        restored.flip();
              
        return restored;
    }
}

...

Code Block
languagejava
titleEncryption
class EncryptionTransformer extends CacheObjectTransformerAdapter {
    private static final int SHIFT = 42; // Secret!

    protected ByteBuffer transform(ByteBuffer original) throws IgniteCheckedException {
        ByteBuffer transformed = byteBuffer(original.remaining() + 1); // Same capacity is required.

		transformed.put(TRANSFORMED);

        while (original.hasRemaining())
            transformed.put((byte)(original.get() + SHIFT));

        transformed.flip();

        return transformed;
    }

    protected ByteBuffer restore(ByteBuffer transformed, int length) {
        ByteBuffer restored = byteBuffer(transformed.remaining()); // Same size.
		
		while (transformed.hasRemaining())
            restored.put((byte)(transformed.get() - SHIFT));

        restored.flip();

        return restored;
    }
}

...