public interface IgnitePortables
circular or null references.PortableObject format).
To work with the portable format directly, user should create a special cache projection using IgniteCache.withKeepPortable() method and then retrieve individual fields as needed:
IgniteCache<PortableObject, PortableObject> prj = cache.withKeepPortable();
// Convert instance of MyKey to portable format.
// We could also use PortableBuilder to create the key in portable format directly.
PortableObject key = grid.portables().toPortable(new MyKey());
PortableObject val = prj.get(key);
String field = val.field("myFieldName");
Alternatively, if we have class definitions in the classpath, we may choose to work with deserialized
typed objects at all times. In this case we do incur the deserialization cost. However, if
PortableMarshaller.isKeepDeserialized() is true then Ignite will only deserialize on the first access
and will cache the deserialized object, so it does not have to be deserialized again:
IgniteCache<MyKey.class, MyValue.class> cache = grid.cache(null); MyValue val = cache.get(new MyKey()); // Normal java getter. String fieldVal = val.getMyFieldName();If we used, for example, one of the automatically handled portable types for a key, like integer, and still wanted to work with binary portable format for values, then we would declare cache projection as follows:
IgniteCache<Integer.class, PortableObject> prj = cache.withKeepPortable();
PortableObject format. Following
classes are never converted (e.g., toPortable(Object) method will return original
object, and instances of these classes will be stored in cache without changes):
String and array of StringsUUID and array of UUIDsDate and array of DatesTimestamp and array of TimestampsArrayList in Java will become
List in C#, LinkedList in Java is LinkedList in C#, HashMap
in Java is Dictionary in C#, and TreeMap in Java becomes SortedDictionary
in C#, etc.
PortableBuilder which allows to build portable objects dynamically:
PortableBuilder builder = Ignition.ignite().portables().builder();
builder.typeId("MyObject");
builder.stringField("fieldA", "A");
build.intField("fieldB", "B");
PortableObject portableObj = builder.build();
For the cases when class definition is present
in the class path, it is also possible to populate a standard POJO and then
convert it to portable format, like so:
MyObject obj = new MyObject();
obj.setFieldA("A");
obj.setFieldB(123);
PortableObject portableObj = Ignition.ignite().portables().toPortable(obj);
NOTE: you don't need to convert typed objects to portable format before storing
them in cache, Ignite will do that automatically.
metadata(Class)
methods. Having metadata also allows for proper formatting of PortableObject#toString() method,
even when portable objects are kept in binary format only, which may be necessary for audit reasons.
PortableMarshaller.setClassNames(Collection).
The only requirement Ignite imposes is that your object has an empty
constructor. Note, that since server side does not have to know the class definition,
you only need to list portable objects in configuration on the client side. However, if you
list them on the server side as well, then you get the ability to deserialize portable objects
into concrete types on the server as well as on the client.
Here is an example of portable configuration (note that star (*) notation is supported):
...
<!-- Explicit portable objects configuration. -->
<property name="marshaller">
<bean class="org.apache.ignite.internal.portable.api.PortableMarshaller">
<property name="classNames">
<list>
<value>my.package.for.portable.objects.*</value>
<value>org.apache.ignite.examples.client.portable.Employee</value>
</list>
</property>
</bean>
</property>
...
or from code:
IgniteConfiguration cfg = new IgniteConfiguration();
PortableMarshaller marsh = new PortableMarshaller();
marsh.setClassNames(Arrays.asList(
Employee.class.getName(),
Address.class.getName())
);
cfg.setMarshaller(marsh);
You can also specify class name for a portable object via PortableTypeConfiguration.
Do it in case if you need to override other configuration properties on per-type level, like
ID-mapper, or serializer.
Employee object with
Organization, and want to colocate employees with organization they work for,
so you can process them together, you need to specify an alternate affinity key.
With portable objects you would have to do it as following:
<property name="marshaller">
<bean class="org.gridgain.grid.marshaller.portable.PortableMarshaller">
...
<property name="typeConfigurations">
<list>
<bean class="org.apache.ignite.internal.portable.api.PortableTypeConfiguration">
<property name="className" value="org.apache.ignite.examples.client.portable.EmployeeKey"/>
<property name="affinityKeyFieldName" value="organizationId"/>
</bean>
</list>
</property>
...
</bean>
</property>
PortableMarshalAware interface, like so:
public class Address implements PortableMarshalAware {
private String street;
private int zip;
// Empty constructor required for portable deserialization.
public Address() {}
@Override public void writePortable(PortableWriter writer) throws PortableException {
writer.writeString("street", street);
writer.writeInt("zip", zip);
}
@Override public void readPortable(PortableReader reader) throws PortableException {
street = reader.readString("street");
zip = reader.readInt("zip");
}
}
Alternatively, if you cannot change class definitions, you can provide custom serialization
logic in PortableSerializer either globally in PortableMarshaller or
for a specific type via PortableTypeConfiguration instance.
Similar to java serialization you can use writeReplace() and readResolve() methods.
readResolve is defined as follows: ANY-ACCESS-MODIFIER Object readResolve().
It may be used to replace the de-serialized object by another one of your choice.
writeReplace is defined as follows: ANY-ACCESS-MODIFIER Object writeReplace(). This method
allows the developer to provide a replacement object that will be serialized instead of the original one.
PortableIdMapper implementation.
ID-mapper may be provided either globally in PortableMarshaller,
or for a specific type via PortableTypeConfiguration instance.
CacheTypeMetadata inside of specific
CacheConfiguration instance,
like so:
...
<bean class="org.apache.ignite.cache.CacheConfiguration">
...
<property name="typeMetadata">
<list>
<bean class="CacheTypeMetadata">
<property name="type" value="Employee"/>
<!-- Fields to index in ascending order. -->
<property name="ascendingFields">
<map>
<entry key="name" value="java.lang.String"/>
<!-- Nested portable objects can also be indexed. -->
<entry key="address.zip" value="java.lang.Integer"/>
</map>
</property>
</bean>
</list>
</property>
</bean>
| Modifier and Type | Method and Description |
|---|---|
PortableBuilder |
builder(int typeId)
Creates new portable builder.
|
PortableBuilder |
builder(PortableObject portableObj)
Creates portable builder initialized by existing portable object.
|
PortableBuilder |
builder(String typeName)
Creates new portable builder.
|
Collection<PortableMetadata> |
metadata()
Gets metadata for all known types.
|
PortableMetadata |
metadata(Class<?> cls)
Gets metadata for provided class.
|
PortableMetadata |
metadata(int typeId)
Gets metadata for provided type ID.
|
PortableMetadata |
metadata(String typeName)
Gets metadata for provided class name.
|
<T> T |
toPortable(Object obj)
Converts provided object to instance of
PortableObject. |
int |
typeId(String typeName)
Gets type ID for given type name.
|
int typeId(String typeName)
typeName - Type name.<T> T toPortable(@Nullable Object obj) throws PortableException
PortableObject.obj - Object to convert.PortableException - In case of error.PortableBuilder builder(int typeId)
typeId - ID of the type.PortableBuilder builder(String typeName)
typeName - Type name.PortableBuilder builder(PortableObject portableObj)
portableObj - Portable object to initialize builder.@Nullable PortableMetadata metadata(Class<?> cls) throws PortableException
cls - Class.PortableException - In case of error.@Nullable PortableMetadata metadata(String typeName) throws PortableException
typeName - Type name.PortableException - In case of error.@Nullable PortableMetadata metadata(int typeId) throws PortableException
typeId - Type ID.PortableException - In case of error.Collection<PortableMetadata> metadata() throws PortableException
PortableException - In case of error.
Follow @ApacheIgnite
Ignite Fabric : ver. 1.4.0 Release Date : September 24 2015