Java Code Examples for org.apache.ignite.binary.BinaryObjectBuilder#build()

The following examples show how to use org.apache.ignite.binary.BinaryObjectBuilder#build() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinaryObjectBuilderAdditionalSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
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));
}
 
Example 2
Source File: CacheJdbcPojoStore.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Construct binary object from query result.
 *
 * @param typeName Type name.
 * @param fields Fields descriptors.
 * @param loadColIdxs Select query columns index.
 * @param rs ResultSet.
 * @return Constructed binary object.
 * @throws CacheLoaderException If failed to construct binary object.
 */
protected Object buildBinaryObject(String typeName, JdbcTypeField[] fields, Map<String, Integer> loadColIdxs, ResultSet rs) throws CacheLoaderException {
    try {
        BinaryObjectBuilder builder = ignite.binary().builder(typeName);

        for (JdbcTypeField field : fields) {
            Integer colIdx = columnIndex(loadColIdxs, field.getDatabaseFieldName());

            Object colVal = transformer.getColumnValue(rs, colIdx, field.getJavaFieldType());

            builder.setField(field.getJavaFieldName(), colVal, (Class<Object>)field.getJavaFieldType());
        }

        return builder.build();
    }
    catch (SQLException e) {
        throw new CacheException("Failed to read binary object: " + typeName, e);
    }
}
 
Example 3
Source File: IgniteCacheAbstractInsertSqlQuerySelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 *
 */
Object createPerson(int id, String name) {
    if (!isBinaryMarshaller()) {
        Person p = new Person(id);
        p.name = name;

        return p;
    }
    else {
        BinaryObjectBuilder o = grid(0).binary().builder("Person");
        o.setField("id", id);
        o.setField("name", name);

        return o.build();
    }
}
 
Example 4
Source File: BinaryObjectBuilderDefaultMappersSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testCharField() throws Exception {
    BinaryObjectBuilder builder = builder("Class");

    builder.setField("charField", (char)1);

    BinaryObject po = builder.build();

    assertEquals(expectedHashCode("Class"), po.type().typeId());
    assertEquals(BinaryArrayIdentityResolver.instance().hashCode(po), po.hashCode());

    assertEquals((char)1, po.<Character>field("charField").charValue());
}
 
Example 5
Source File: BinaryObjectBuilderDefaultMappersSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testBooleanField() throws Exception {
    BinaryObjectBuilder builder = builder("Class");

    builder.setField("booleanField", true);

    BinaryObject po = builder.build();

    assertEquals(expectedHashCode("Class"), po.type().typeId());
    assertEquals(BinaryArrayIdentityResolver.instance().hashCode(po), po.hashCode());

    assertTrue(po.<Boolean>field("booleanField"));
}
 
Example 6
Source File: BinaryObjectBuilderDefaultMappersSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testByteField() throws Exception {
    BinaryObjectBuilder builder = builder("Class");

    builder.setField("byteField", (byte)1);

    BinaryObject po = builder.build();

    assertEquals(expectedHashCode("Class"), po.type().typeId());
    assertEquals(BinaryArrayIdentityResolver.instance().hashCode(po), po.hashCode());

    assertEquals((byte) 1, po.<Byte>field("byteField").byteValue());
}
 
Example 7
Source File: IgniteBinaryIdentityBenchmark.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param key Key field value.
 * @return Binary object without hash code explicitly set at build time.
 */
BinaryObject createFieldsIdentityBinaryKey(int key) {
    BinaryObjectBuilder bldr = ignite().binary().builder("BinaryKeyWithFieldsIdentity");

    setBuilderFields(bldr, key);

    return bldr.build();
}
 
Example 8
Source File: BinaryObjectTypeCompatibilityTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testCompatibilityWithObject() throws Exception {
    Ignite ignite = startGrid();

    BinaryObjectBuilder bldr = ignite.binary().builder("ObjectWrapper");
    bldr.setField("objField", new Object());
    bldr.build();

    validateMap(bldr, "objField", new HashMap<>());
    validateMap(bldr, "objField", new LinkedHashMap<>());
    validateMap(bldr, "objField", new TreeMap<>());

    validateCollection(bldr, "objField", new ArrayList<>());
    validateCollection(bldr, "objField", new LinkedList<>());
    validateCollection(bldr, "objField", new HashSet<>());
    validateCollection(bldr, "objField", new LinkedHashSet<>());
    validateCollection(bldr, "objField", new TreeSet<>());

    validate(bldr, "objField", (byte)RANDOM.nextInt());
    validate(bldr, "objField", (short)RANDOM.nextInt());
    validate(bldr, "objField", (char)RANDOM.nextInt());
    validate(bldr, "objField", RANDOM.nextInt());
    validate(bldr, "objField", RANDOM.nextLong());
    validate(bldr, "objField", RANDOM.nextFloat());
    validate(bldr, "objField", RANDOM.nextDouble());

    validate(bldr, "objField", Enum.DEFAULT);
    validate(bldr, "objField", new BigDecimal(RANDOM.nextInt()));
    validate(bldr, "objField", "Test string");
    validate(bldr, "objField", new Date());
    validate(bldr, "objField", new Timestamp(System.currentTimeMillis()));
    validate(bldr, "objField", new Time(System.currentTimeMillis()));
    validate(bldr, "objField", UUID.randomUUID());
}
 
Example 9
Source File: BinaryObjectBuilderDefaultMappersSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testFloatArrayField() throws Exception {
    BinaryObjectBuilder builder = builder("Class");

    builder.setField("floatArrayField", new float[] {1, 2, 3});

    BinaryObject po = builder.build();

    assertEquals(expectedHashCode("Class"), po.type().typeId());
    assertEquals(BinaryArrayIdentityResolver.instance().hashCode(po), po.hashCode());

    assertTrue(Arrays.equals(new float[] {1, 2, 3}, po.<float[]>field("floatArrayField")));
}
 
Example 10
Source File: BinaryObjectBuilderDefaultMappersSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testDecimalArrayField() throws Exception {
    BinaryObjectBuilder builder = builder("Class");

    builder.setField("decimalArrayField", new BigDecimal[] {BigDecimal.ONE, BigDecimal.TEN});

    BinaryObject po = builder.build();

    assertEquals(expectedHashCode("Class"), po.type().typeId());
    assertEquals(BinaryArrayIdentityResolver.instance().hashCode(po), po.hashCode());

    assertTrue(Arrays.equals(new BigDecimal[] {BigDecimal.ONE, BigDecimal.TEN}, po.<String[]>field("decimalArrayField")));
}
 
Example 11
Source File: BinarySerialiedFieldComparatorSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Build object.
 *
 * @param parts Parts.
 * @return Result.
 */
private BinaryObjectImpl build(Object... parts) {
    String typeName = "Type" + TYPE_CTR.get();

    BinaryObjectBuilder builder = grid().binary().builder(typeName);

    if (!F.isEmpty(parts)) {
        for (int i = 0; i < parts.length; )
            builder.setField((String)parts[i++], parts[i++]);
    }

    return (BinaryObjectImpl) builder.build();
}
 
Example 12
Source File: BinaryObjectBuilderDefaultMappersSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testLongArrayField() throws Exception {
    BinaryObjectBuilder builder = builder("Class");

    builder.setField("longArrayField", new long[] {1, 2, 3});

    BinaryObject po = builder.build();

    assertEquals(expectedHashCode("Class"), po.type().typeId());
    assertEquals(BinaryArrayIdentityResolver.instance().hashCode(po), po.hashCode());

    assertTrue(Arrays.equals(new long[] {1, 2, 3}, po.<long[]>field("longArrayField")));
}
 
Example 13
Source File: GridCommandHandlerMetadataTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Check the command '--meta details'.
 * Steps:
 * - Creates binary two types for a test (by BinaryObjectBuilder) with several fields and shemas;
 * - execute the command '--meta details' for the type Type0 by name
 * - check metadata print.
 * - execute the command '--meta details' for the type Type0 by type ID on different formats.
 * - check metadata print.
 */
@Test
public void testMetadataDetails() {
    injectTestSystemOut();

    BinaryObjectBuilder bob0 = crd.binary().builder("TypeName0");
    bob0.setField("fld0", 0);
    bob0.build();

    bob0 = crd.binary().builder("TypeName0");
    bob0.setField("fld1", "0");
    bob0.build();

    bob0 = crd.binary().builder("TypeName0");
    bob0.setField("fld0", 1);
    bob0.setField("fld2", UUID.randomUUID());
    BinaryObject bo0 = bob0.build();

    BinaryObjectBuilder bob1 = crd.binary().builder("TypeName1");
    bob1.setField("fld0", 0);
    bob1.build();

    bob1 = crd.binary().builder("TypeName1");
    bob1.setField("fld0", 0);
    bob1.setField("fld1", new Date());
    bob1.setField("fld2", 0.1);
    bob1.setField("fld3", new long[]{0, 1, 2, 3});

    BinaryObject bo1 = bob1.build();

    assertEquals(EXIT_CODE_OK, execute("--meta", "details", "--typeName", "TypeName0"));
    checkTypeDetails(log, testOut.toString(), crd.context().cacheObjects().metadata(bo0.type().typeId()));

    assertEquals(EXIT_CODE_OK, execute("--meta", "details", "--typeId",
        "0x" + Integer.toHexString(crd.context().cacheObjects().typeId("TypeName1"))));
    checkTypeDetails(log, testOut.toString(), crd.context().cacheObjects().metadata(bo1.type().typeId()));

    assertEquals(EXIT_CODE_OK, execute("--meta", "details", "--typeId",
        Integer.toString(crd.context().cacheObjects().typeId("TypeName1"))));
    checkTypeDetails(log, testOut.toString(), crd.context().cacheObjects().metadata(bo1.type().typeId()));
}
 
Example 14
Source File: BinaryObjectBuilderAdditionalSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Test {@link BinaryObjectBuilder#build()} adds type mapping to the binary marshaller's cache.
 */
@Test
public void testMarshallerMappings() throws IgniteCheckedException, ClassNotFoundException {
    String typeName = "TestType";

    int typeId = BinaryContext.defaultIdMapper().typeId(typeName);

    BinaryObjectBuilder builder = newWrapper(typeName);

    builder.build();

    MarshallerContext marshCtx = grid(0).context().marshallerContext();

    String actualTypeName = marshCtx.getClassName(MarshallerPlatformIds.JAVA_ID, typeId);

    assertEquals(typeName, actualTypeName);
}
 
Example 15
Source File: AbstractJdbcPojoQuerySelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override protected void beforeTestsStarted() throws Exception {
    Ignite ignite = startGrid(0);

    BinaryObjectBuilder builder = ignite.binary().builder(TEST_OBJECT);
    BinaryObjectBuilder builder2 = ignite.binary().builder(TEST_OBJECT_2);

    builder2.setField("id", 1);
    builder2.setField("boolVal", true);

    BinaryObject testObject = builder2.build();

    builder.setField("id", 1);
    builder.setField("testObject", testObject);

    BinaryObject binObj = builder.build();

    IgniteCache<String, BinaryObject> cache = grid(0).cache(DEFAULT_CACHE_NAME);

    cache.put("0", binObj);
}
 
Example 16
Source File: BinaryMetadataRemoveTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Tests remove type metadata at all nodes (coordinator, server, client).
 */
@Test
public void testRemoveTypeOnNodes() throws Exception {
    List<IgniteEx[]> testNodeSets = new ArrayList<>();

    // Add all servers permutations to tests sets.
    for (Ignite ign0 : G.allGrids()) {
        for (Ignite ign1 : G.allGrids()) {
            for (Ignite ign2 : G.allGrids()) {
                IgniteEx ignx0 = (IgniteEx)ign0;
                IgniteEx ignx1 = (IgniteEx)ign1;
                IgniteEx ignx2 = (IgniteEx)ign2;

                if (!ignx0.context().clientNode()
                    && !ignx1.context().clientNode()
                    && !ignx2.context().clientNode())
                    testNodeSets.add(new IgniteEx[] {ignx0, ignx1, ignx2});
            }
        }
    }

    testNodeSets.add(new IgniteEx[] {grid("srv0"), grid("cli0"), grid("cli0")});
    testNodeSets.add(new IgniteEx[] {grid("cli0"), grid("cli0"), grid("cli0")});
    testNodeSets.add(new IgniteEx[] {grid("cli0"), grid("cli1"), grid("cli2")});

    for (IgniteEx[] testNodeSet : testNodeSets) {
        IgniteEx ignCreateType = testNodeSet[0];
        IgniteEx ignRemoveType = testNodeSet[1];
        IgniteEx ignRecreateType = testNodeSet[2];

        log.info("+++ Check [createOn=" + ignCreateType.name() +
            ", removeOn=" + ignRemoveType.name() + ", recreateOn=" + ignRecreateType.name());

        BinaryObjectBuilder builder0 = ignCreateType.binary().builder("Type0");

        builder0.setField("f", 1);
        builder0.build();

        delayIfClient(ignCreateType, ignRemoveType, ignRecreateType);

        removeType(ignRemoveType, "Type0");

        delayIfClient(ignCreateType, ignRemoveType, ignRecreateType);

        BinaryObjectBuilder builder1 = ignRecreateType.binary().builder("Type0");
        builder1.setField("f", "string");
        builder1.build();

        delayIfClient(ignCreateType, ignRemoveType, ignRecreateType);

        // Remove type at the end of test case.
        removeType(grid("srv0"), "Type0");

        delayIfClient(ignCreateType, ignRemoveType, ignRecreateType);
    }
}
 
Example 17
Source File: BinaryObjectBuilderAdditionalSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 *
 */
@Test
public void testCorrectMetadataNullField2() {
    BinaryObjectBuilder builder = binaries().builder("SomeType3");

    builder.setField("dateField", null, Date.class);

    builder.setField("objectField", null, GridBinaryTestClasses.Company.class);

    BinaryObject obj = builder.build();

    builder = binaries().builder(obj);

    builder.setField("dateField", new Date());

    builder.setField("objectField", new GridBinaryTestClasses.Company());

    builder.build();
}
 
Example 18
Source File: BinaryTypeRegistrationTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * Register binary type.
 *
 * @param ignite Ignate instance.
 * @param fieldName Field name of new object.
 */
private static void register(Ignite ignite, String fieldName) {
    IgniteBinary binary = ignite.binary();

    BinaryObjectBuilder builder = binary.builder("TestType");

    builder.setField(fieldName, 1);

    builder.build();
}
 
Example 19
Source File: BinaryMarshallerSelfTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @throws IgniteCheckedException If failed.
 */
@Test
public void testThreadLocalArrayReleased() throws Exception {
    // Checking the writer directly.
    assertEquals(false, INSTANCE.isAcquired());

    BinaryMarshaller marsh = binaryMarshaller();

    try (BinaryWriterExImpl writer = new BinaryWriterExImpl(binaryContext(marsh))) {
        assertEquals(true, INSTANCE.isAcquired());

        writer.writeString("Thread local test");

        writer.array();

        assertEquals(true, INSTANCE.isAcquired());
    }

    // Checking the binary marshaller.
    assertEquals(false, INSTANCE.isAcquired());

    marsh = binaryMarshaller();

    marsh.marshal(new SimpleObject());

    assertEquals(false, INSTANCE.isAcquired());

    marsh = binaryMarshaller();

    // Checking the builder.
    BinaryObjectBuilder builder = new BinaryObjectBuilderImpl(binaryContext(marsh),
        "org.gridgain.foo.bar.TestClass");

    builder.setField("a", "1");

    BinaryObject binaryObj = builder.build();

    assertEquals(false, INSTANCE.isAcquired());
}
 
Example 20
Source File: GridCacheBinaryObjectsAbstractSelfTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testReplace() throws Exception {
    IgniteCache<Integer, TestObject> c = jcache(0);

    for (int i = 0; i < ENTRY_CNT; i++)
        c.put(i, new TestObject(i));

    for (int i = 0; i < ENTRY_CNT; i++) {
        TestObject obj = c.get(i);

        assertEquals(i, obj.val);
    }

    IgniteCache<Integer, BinaryObject> kpc = keepBinaryCache();

    BinaryObjectBuilder bldr = grid(0).binary().builder(TestObject.class.getName());

    bldr.setField("val", -42);

    BinaryObject testObj = bldr.build();

    for (int i = 0; i < ENTRY_CNT; i++) {
        BinaryObject po = kpc.get(i);

        assertEquals(i, (int)po.field("val"));

        assertTrue(kpc.replace(i, po, testObj));
    }
}