use of org.apache.ignite.binary.BinaryObject in project ignite by apache.
the class ComputeClientBinaryTaskExecutionExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println();
System.out.println(">>> Binary objects task execution example started.");
if (ignite.cluster().forRemotes().nodes().isEmpty()) {
System.out.println();
System.out.println(">>> This example requires remote nodes to be started.");
System.out.println(">>> Please start at least 1 remote node.");
System.out.println(">>> Refer to example's javadoc for details on configuration.");
System.out.println();
return;
}
// Generate employees to calculate average salary for.
Collection<Employee> employees = employees();
System.out.println();
System.out.println(">>> Calculating average salary for employees:");
for (Employee employee : employees) System.out.println(">>> " + employee);
// Convert collection of employees to collection of binary objects.
// This allows to send objects across nodes without requiring to have
// Employee class on classpath of these nodes.
Collection<BinaryObject> binaries = ignite.binary().toBinary(employees);
// Execute task and get average salary.
Long avgSalary = ignite.compute(ignite.cluster().forRemotes()).execute(new ComputeClientTask(), binaries);
System.out.println();
System.out.println(">>> Average salary for all employees: " + avgSalary);
System.out.println();
}
}
use of org.apache.ignite.binary.BinaryObject in project ignite by apache.
the class GridCacheAffinityImpl method affinityKey.
/**
* {@inheritDoc}
*/
@Override
public Object affinityKey(K key) {
A.notNull(key, "key");
if (key instanceof CacheObject && !(key instanceof BinaryObject)) {
CacheObjectContext ctx = cctx.cacheObjectContext();
if (ctx == null)
throw new IgniteException(FAILED_TO_FIND_CACHE_ERR_MSG + cctx.name());
key = ((CacheObject) key).value(ctx, false);
}
CacheConfiguration ccfg = cctx.config();
if (ccfg == null)
throw new IgniteException(FAILED_TO_FIND_CACHE_ERR_MSG + cctx.name());
return ccfg.getAffinityMapper().affinityKey(key);
}
use of org.apache.ignite.binary.BinaryObject in project ignite by apache.
the class BinaryObjectExImpl method hasCircularReferences.
/**
* Check if object graph has circular references.
*
* @return {@code true} if object has circular references.
*/
public boolean hasCircularReferences() {
try {
BinaryReaderHandles ctx = new BinaryReaderHandles();
ctx.put(start(), this);
return hasCircularReferences(ctx, new IdentityHashMap<BinaryObject, Integer>());
} catch (BinaryObjectException e) {
throw new IgniteException("Failed to check binary object for circular references", e);
}
}
use of org.apache.ignite.binary.BinaryObject in project ignite by apache.
the class BinaryObjectExImpl method toString.
/**
* @param ctx Reader context.
* @param handles Handles for already traversed objects.
* @return String representation.
*/
private String toString(BinaryReaderHandles ctx, IdentityHashMap<BinaryObject, Integer> handles) {
int idHash = System.identityHashCode(this);
int hash = hashCode();
BinaryType meta;
try {
meta = rawType();
} catch (BinaryObjectException ignore) {
meta = null;
}
if (meta == null || !S.INCLUDE_SENSITIVE)
return S.toString(S.INCLUDE_SENSITIVE ? BinaryObject.class.getSimpleName() : "BinaryObject", "idHash", idHash, false, "hash", hash, false, "typeId", typeId(), true);
handles.put(this, idHash);
SB buf = new SB(meta.typeName());
if (meta.fieldNames() != null) {
buf.a(" [idHash=").a(idHash).a(", hash=").a(hash);
for (String name : meta.fieldNames()) {
Object val = field(ctx, name);
buf.a(", ").a(name).a('=');
appendValue(val, buf, ctx, handles);
}
buf.a(']');
}
return buf.toString();
}
use of org.apache.ignite.binary.BinaryObject in project ignite by apache.
the class BinaryObjectBuilderDefaultMappersSelfTest method testNullField.
/**
* @throws Exception If failed.
*/
public void testNullField() throws Exception {
BinaryObjectBuilder builder = builder("Class");
builder.setField("objField", (Object) null);
builder.setField("otherField", "value");
BinaryObject obj = builder.build();
assertNull(obj.field("objField"));
assertEquals("value", obj.field("otherField"));
assertEquals(BinaryArrayIdentityResolver.instance().hashCode(obj), obj.hashCode());
builder = builder(obj);
builder.setField("objField", "value", Object.class);
builder.setField("otherField", (Object) null);
obj = builder.build();
assertNull(obj.field("otherField"));
assertEquals("value", obj.field("objField"));
assertEquals(BinaryArrayIdentityResolver.instance().hashCode(obj), obj.hashCode());
}
Aggregations