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);
}
}
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);
}
}
}
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;
}
}
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);
}
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);
}
Aggregations