Search in sources :

Example 46 with BinaryObjectBuilder

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);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) GridBoundedConcurrentLinkedHashMap(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashMap) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridQueryTypeDescriptor(org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor) GridQueryProperty(org.apache.ignite.internal.processors.query.GridQueryProperty) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) Column(org.h2.table.Column) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder)

Example 47 with BinaryObjectBuilder

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

Example 48 with BinaryObjectBuilder

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();
}
Also used : BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) GridBinaryTestClasses(org.apache.ignite.internal.binary.mutabletest.GridBinaryTestClasses) Date(java.util.Date)

Example 49 with BinaryObjectBuilder

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

Example 50 with BinaryObjectBuilder

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

Aggregations

BinaryObjectBuilder (org.apache.ignite.binary.BinaryObjectBuilder)83 BinaryObject (org.apache.ignite.binary.BinaryObject)63 Ignite (org.apache.ignite.Ignite)11 IgniteBinary (org.apache.ignite.IgniteBinary)8 GridBinaryTestClasses (org.apache.ignite.internal.binary.mutabletest.GridBinaryTestClasses)7 Date (java.util.Date)4 HashMap (java.util.HashMap)4 BinaryType (org.apache.ignite.binary.BinaryType)4 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 GridQueryProperty (org.apache.ignite.internal.processors.query.GridQueryProperty)3 GridQueryTypeDescriptor (org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor)3 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)3 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)3 Column (org.h2.table.Column)3 Field (java.lang.reflect.Field)2 SQLException (java.sql.SQLException)2 LinkedHashMap (java.util.LinkedHashMap)2 UUID (java.util.UUID)2