Versions Compared

Key

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

...

All we need is to cover all CacheObjects.

CacheObjects

Most of them has have the following structure:

Code Block
languagejava
titleXXX extends CacheObjectAdapter
 protected Object val; // Unmarshalled value.
 protected byte[] valBytes; // Marshalled value bytes.

...

Code Block
languagejava
titleBinaryObjectImpl
private Object obj; // Deserialized value.
private byte[] arr; // Serialized bytes.

(De)serialization is a simmilar similar to (un)marshalling, it's a process to gain java class instance from bytes and or vice versa, but it happen happens at different time times and code layerlayers.

(Un)marshalling happens on putting/getting an object to/from the cache, but (de)serialization happens on building/deserializing of a binary object detached from any cache.

A In a lucky circumstance, BinaryObjectImpl require no marshalling, serialization already generates byte which can be used as marshalled bytes.

But, if we're going to transform the data during the marshaling/unmarshalling phase we need to add an additional data layer to the BinaryObjectImpl:

Code Block
languagejava
titleBinaryObjectImpl
private Object obj; // Deserialized value.
private byte[] arr; // Serialized bytes.
private byte[] valBytes; // Marshalled value bytes.

Where valBytes == arr when the transformation is disabled.

It's not possible to just replace arr with valBytes because, unlike, for example, from CacheObjectImpl arr is not just a mashalled bytes, it's a object's value requredrequired, for example, to provide hashCode/schemaId/typeId/objectField.

So, BinaryObjectImpl requres requires valBytes to arr conversion:

Code Block
languagejava
titleBinaryObjectImpl (un)marshalling
private byte[] arrayFromValueBytes(CacheObjectValueContext ctx) {
    return CacheObjectTransformer.restoreIfNecessary(valBytes, ctx);
}

private byte[] valueBytesFromArray(CacheObjectValueContext ctx) {
    return CacheObjectTransformer.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);
}

SPI (Service Provider Interface)

Some customets customers may want to encrypt the data, some to compress it, while some just keep it as is.

So, we must provide the a simple way to append any transformation.

...

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 about overhead used to store transformed data and allowns allows to work with byte arrays with custom offsets, which is necessary to guarantee the performance.

Every customey customer may impelent implement this interface in a proper way if necessary:

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

	cfg.setCacheObjectTransformerSpi(new XXXTransformerSpi());

	return cfg;
}

...

But, most customers just want to thansform transform the data, so, they may extend the adapter with the simple API.

...