use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.
the class DmlStatementsProcessor method rowToKeyValue.
/**
* Convert row presented as an array of Objects into key-value pair to be inserted to cache.
* @param cctx Cache context.
* @param row Row to process.
* @param plan Update plan.
* @throws IgniteCheckedException if failed.
*/
@SuppressWarnings({ "unchecked", "ConstantConditions", "ResultOfMethodCallIgnored" })
private IgniteBiTuple<?, ?> rowToKeyValue(GridCacheContext cctx, List<?> row, UpdatePlan plan) throws IgniteCheckedException {
GridH2RowDescriptor rowDesc = plan.tbl.rowDescriptor();
GridQueryTypeDescriptor desc = rowDesc.type();
Object key = plan.keySupplier.apply(row);
if (QueryUtils.isSqlType(desc.keyClass())) {
assert plan.keyColIdx != -1;
key = convert(key, rowDesc, desc.keyClass(), plan.colTypes[plan.keyColIdx]);
}
Object val = plan.valSupplier.apply(row);
if (QueryUtils.isSqlType(desc.valueClass())) {
assert plan.valColIdx != -1;
val = convert(val, rowDesc, desc.valueClass(), plan.colTypes[plan.valColIdx]);
}
if (key == null)
throw new IgniteSQLException("Key for INSERT or MERGE must not be null", IgniteQueryErrorCode.NULL_KEY);
if (val == null)
throw new IgniteSQLException("Value for INSERT or MERGE must not be null", IgniteQueryErrorCode.NULL_VALUE);
Map<String, Object> newColVals = new HashMap<>();
for (int i = 0; i < plan.colNames.length; i++) {
if (i == plan.keyColIdx || i == plan.valColIdx)
continue;
String colName = plan.colNames[i];
GridQueryProperty prop = desc.property(colName);
assert prop != null;
Class<?> expCls = prop.type();
newColVals.put(colName, convert(row.get(i), rowDesc, expCls, plan.colTypes[i]));
}
// We update columns in the order specified by the table for a reason - table's
// column order preserves their precedence for correct update of nested properties.
Column[] cols = plan.tbl.getColumns();
// First 3 columns are _key, _val and _ver. Skip 'em.
for (int i = DEFAULT_COLUMNS_COUNT; i < cols.length; i++) {
if (plan.tbl.rowDescriptor().isKeyValueOrVersionColumn(i))
continue;
String colName = cols[i].getName();
if (!newColVals.containsKey(colName))
continue;
Object colVal = newColVals.get(colName);
desc.setValue(colName, key, val, colVal);
}
if (cctx.binaryMarshaller()) {
if (key instanceof BinaryObjectBuilder)
key = ((BinaryObjectBuilder) key).build();
if (val instanceof BinaryObjectBuilder)
val = ((BinaryObjectBuilder) val).build();
}
return new IgniteBiTuple<>(key, val);
}
use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.
the class CacheJdbcPojoStoreTest method wrap.
/**
* @param obj Object.
*/
private Object wrap(Object obj) throws IllegalAccessException {
if (binaryEnable) {
Class<?> cls = obj.getClass();
BinaryObjectBuilder builder = ig.binary().builder(cls.getName());
for (Field f : cls.getDeclaredFields()) {
if (f.getName().contains("serialVersionUID"))
continue;
f.setAccessible(true);
builder.setField(f.getName(), f.get(obj));
}
return builder.build();
}
return obj;
}
use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.
the class BinaryObjectBuilderAdditionalSelfTest method testCorrectMetadataNullField.
/**
*/
public void testCorrectMetadataNullField() {
BinaryObjectBuilder builder = binaries().builder("SomeType2");
builder.setField("dateField", null, Date.class);
builder.setField("objectField", null, GridBinaryTestClasses.Company.class);
builder.build();
builder = binaries().builder("SomeType2");
builder.setField("dateField", new Date());
builder.setField("objectField", new GridBinaryTestClasses.Company());
builder.build();
}
use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.
the class BinaryObjectBuilderAdditionalSelfTest method testSameBinaryKey.
/**
* @throws Exception If failed.
*/
public void testSameBinaryKey() throws Exception {
IgniteCache<BinaryObject, BinaryObject> replicatedCache = jcache(0).withKeepBinary();
IgniteCache<BinaryObject, BinaryObject> partitionedCache = jcache(0, "partitioned").withKeepBinary();
BinaryObjectBuilder keyBuilder = ignite(0).binary().builder("keyType").setField("F1", "V1");
BinaryObjectBuilder valBuilder = ignite(0).binary().builder("valueType").setField("F2", "V2").setField("F3", "V3");
BinaryObject key = keyBuilder.build();
BinaryObject val = valBuilder.build();
replicatedCache.put(key, val);
partitionedCache.put(key, val);
assertNotNull(replicatedCache.get(key));
assertNotNull(partitionedCache.get(key));
}
use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.
the class BinaryObjectBuilderAdditionalSelfTest method testBuilderExternalizable.
/**
* Checks that externalizable value is correctly serialized/deserialized.
*
* @throws Exception If failed.
*/
public void testBuilderExternalizable() throws Exception {
BinaryObjectBuilder builder = newWrapper("TestType");
final TestObjectExternalizable exp = new TestObjectExternalizable("test");
final TestObjectExternalizable[] expArr = new TestObjectExternalizable[] { new TestObjectExternalizable("test1"), new TestObjectExternalizable("test2") };
BinaryObject extObj = builder.setField("extVal", exp).setField("extArr", expArr).build();
assertEquals(exp, extObj.field("extVal"));
Assert.assertArrayEquals(expArr, (Object[]) extObj.field("extArr"));
builder = extObj.toBuilder();
extObj = builder.setField("intVal", 10).build();
assertEquals(exp, extObj.field("extVal"));
Assert.assertArrayEquals(expArr, (Object[]) extObj.field("extArr"));
assertEquals(Integer.valueOf(10), extObj.field("intVal"));
builder = extObj.toBuilder();
extObj = builder.setField("strVal", "some string").build();
assertEquals(exp, extObj.field("extVal"));
Assert.assertArrayEquals(expArr, (Object[]) extObj.field("extArr"));
assertEquals(Integer.valueOf(10), extObj.field("intVal"));
assertEquals("some string", extObj.field("strVal"));
}
Aggregations