Search in sources :

Example 6 with BinaryField

use of org.apache.ignite.binary.BinaryField in project ignite by apache.

the class WorkingWithBinaryObjects method binaryFieldsExample.

public static void binaryFieldsExample() {
    try (Ignite ignite = Ignition.start()) {
        // tag::binaryField[]
        Collection<BinaryObject> persons = getPersons();
        BinaryField salary = null;
        double total = 0;
        int count = 0;
        for (BinaryObject person : persons) {
            if (salary == null) {
                salary = person.type().field("salary");
            }
            total += (float) salary.value(person);
            count++;
        }
        double avg = total / count;
        // end::binaryField[]
        System.out.println("binary fields example:" + avg);
    }
}
Also used : BinaryField(org.apache.ignite.binary.BinaryField) BinaryObject(org.apache.ignite.binary.BinaryObject) Ignite(org.apache.ignite.Ignite)

Example 7 with BinaryField

use of org.apache.ignite.binary.BinaryField in project ignite by apache.

the class GridCacheContext method prepareAffinityField.

/**
 * Prepare affinity field for builder (if possible).
 *
 * @param builder Builder.
 */
public void prepareAffinityField(BinaryObjectBuilder builder) {
    assert binaryMarshaller();
    assert builder instanceof BinaryObjectBuilderImpl;
    BinaryObjectBuilderImpl builder0 = (BinaryObjectBuilderImpl) builder;
    if (!cacheObjCtx.customAffinityMapper()) {
        CacheDefaultBinaryAffinityKeyMapper mapper = (CacheDefaultBinaryAffinityKeyMapper) cacheObjCtx.defaultAffMapper();
        BinaryField field = mapper.affinityKeyField(builder0.typeId());
        if (field != null) {
            String fieldName = field.name();
            builder0.affinityFieldName(fieldName);
        }
    }
}
Also used : BinaryField(org.apache.ignite.binary.BinaryField) BinaryObjectBuilderImpl(org.apache.ignite.internal.binary.builder.BinaryObjectBuilderImpl)

Example 8 with BinaryField

use of org.apache.ignite.binary.BinaryField in project ignite by apache.

the class CacheObjectBinaryProcessorImpl method affinityKeyField.

/**
 * Get affinity key field.
 *
 * @param typeId Binary object type ID.
 * @return Affinity key.
 */
public BinaryField affinityKeyField(int typeId) {
    // Fast path for already cached field.
    T1<BinaryField> fieldHolder = affKeyFields.get(typeId);
    if (fieldHolder != null)
        return fieldHolder.get();
    // Slow path if affinity field is not cached yet.
    String name = binaryCtx.affinityKeyFieldName(typeId);
    if (name != null) {
        BinaryField field = binaryCtx.createField(typeId, name);
        affKeyFields.putIfAbsent(typeId, new T1<>(field));
        return field;
    } else {
        affKeyFields.putIfAbsent(typeId, new T1<>(null));
        return null;
    }
}
Also used : BinaryField(org.apache.ignite.binary.BinaryField)

Example 9 with BinaryField

use of org.apache.ignite.binary.BinaryField in project ignite by apache.

the class CacheDefaultBinaryAffinityKeyMapper method affinityKey.

/**
 * {@inheritDoc}
 */
@Override
public Object affinityKey(Object key) {
    try {
        key = proc.toBinary(key, false);
    } catch (IgniteException e) {
        U.error(log, "Failed to marshal key to binary: " + key, e);
    }
    if (key instanceof BinaryObject) {
        assert key instanceof BinaryObjectEx : "All BinaryObject implementations must implement " + BinaryObjectEx.class.getName() + ": " + key.getClass().getName();
        BinaryObjectEx key0 = (BinaryObjectEx) key;
        BinaryField affField = affinityKeyField(key0.typeId());
        if (affField != null) {
            Object res = affField.value(key0);
            if (res != null)
                return res;
        }
        return key;
    } else
        return super.affinityKey(key);
}
Also used : BinaryField(org.apache.ignite.binary.BinaryField) BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteException(org.apache.ignite.IgniteException) BinaryObjectEx(org.apache.ignite.internal.binary.BinaryObjectEx) BinaryObject(org.apache.ignite.binary.BinaryObject)

Example 10 with BinaryField

use of org.apache.ignite.binary.BinaryField in project ignite by apache.

the class BinaryFieldsAbstractSelfTest method nestedContext.

/**
 * Get test context with nested test object.
 *
 * @param marsh Binary marshaller.
 * @param fieldName Field name.
 * @return Test context.
 * @throws Exception If failed.
 */
private TestContext nestedContext(BinaryMarshaller marsh, String fieldName) throws Exception {
    TestObject obj = createObject();
    TestOuterObject outObj = new TestOuterObject(obj);
    BinaryObjectExImpl portOutObj = toBinary(marsh, outObj);
    BinaryObjectExImpl portObj = portOutObj.field("fInner");
    assert portObj != null;
    BinaryField field = portObj.type().field(fieldName);
    return new TestContext(obj, portObj, field);
}
Also used : BinaryField(org.apache.ignite.binary.BinaryField)

Aggregations

BinaryField (org.apache.ignite.binary.BinaryField)14 BinaryObject (org.apache.ignite.binary.BinaryObject)3 BinaryType (org.apache.ignite.binary.BinaryType)3 BinaryObjectEx (org.apache.ignite.internal.binary.BinaryObjectEx)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 AffinityKeyMapper (org.apache.ignite.cache.affinity.AffinityKeyMapper)2 CacheDefaultBinaryAffinityKeyMapper (org.apache.ignite.internal.processors.cache.CacheDefaultBinaryAffinityKeyMapper)2 CacheObjectContext (org.apache.ignite.internal.processors.cache.CacheObjectContext)2 GridCacheDefaultAffinityKeyMapper (org.apache.ignite.internal.processors.cache.GridCacheDefaultAffinityKeyMapper)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 EntryProcessorException (javax.cache.processor.EntryProcessorException)1 Ignite (org.apache.ignite.Ignite)1 IgniteException (org.apache.ignite.IgniteException)1 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)1 GridKernalContext (org.apache.ignite.internal.GridKernalContext)1 BinaryObjectBuilderImpl (org.apache.ignite.internal.binary.builder.BinaryObjectBuilderImpl)1 T1 (org.apache.ignite.internal.util.typedef.T1)1 Nullable (org.jetbrains.annotations.Nullable)1 Test (org.junit.Test)1