Search in sources :

Example 16 with BinaryObject

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();
    }
}
Also used : Employee(org.apache.ignite.examples.model.Employee) BinaryObject(org.apache.ignite.binary.BinaryObject) Ignite(org.apache.ignite.Ignite)

Example 17 with BinaryObject

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);
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteException(org.apache.ignite.IgniteException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 18 with BinaryObject

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);
    }
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteException(org.apache.ignite.IgniteException) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 19 with BinaryObject

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();
}
Also used : BinaryType(org.apache.ignite.binary.BinaryType) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) SB(org.apache.ignite.internal.util.typedef.internal.SB)

Example 20 with BinaryObject

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());
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder)

Aggregations

BinaryObject (org.apache.ignite.binary.BinaryObject)200 BinaryObjectBuilder (org.apache.ignite.binary.BinaryObjectBuilder)55 Ignite (org.apache.ignite.Ignite)28 BinaryTypeConfiguration (org.apache.ignite.binary.BinaryTypeConfiguration)26 IgniteCache (org.apache.ignite.IgniteCache)25 HashMap (java.util.HashMap)16 Cache (javax.cache.Cache)15 ArrayList (java.util.ArrayList)13 Map (java.util.Map)13 LinkedHashMap (java.util.LinkedHashMap)12 List (java.util.List)10 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)10 IgniteEx (org.apache.ignite.internal.IgniteEx)10 BinaryObjectBuilderImpl (org.apache.ignite.internal.binary.builder.BinaryObjectBuilderImpl)10 IgniteException (org.apache.ignite.IgniteException)9 GridBinaryTestClasses (org.apache.ignite.internal.binary.mutabletest.GridBinaryTestClasses)9 UUID (java.util.UUID)8 BigInteger (java.math.BigInteger)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)7 BinaryType (org.apache.ignite.binary.BinaryType)7