Search in sources :

Example 56 with BinaryObjectBuilder

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

the class GridBinaryAffinityKeySelfTest method checkAffinity.

/**
 * @param ignite Ignite.
 * @throws Exception If failed.
 */
private void checkAffinity(Ignite ignite) throws Exception {
    Affinity<Object> aff = ignite.affinity(DEFAULT_CACHE_NAME);
    GridAffinityProcessor affProc = ((IgniteKernal) ignite).context().affinity();
    IgniteCacheObjectProcessor cacheObjProc = ((IgniteKernal) ignite).context().cacheObjects();
    CacheObjectContext cacheObjCtx = cacheObjProc.contextForCache(ignite.cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class));
    for (int i = 0; i < 1000; i++) {
        assertEquals(i, aff.affinityKey(i));
        assertEquals(i, aff.affinityKey(new TestObject(i)));
        assertEquals(i, aff.affinityKey(ignite.binary().toBinary(new TestObject(i))));
        assertEquals(i, aff.affinityKey(new AffinityKey(0, i)));
        BinaryObjectBuilder bldr = ignite.binary().builder("TestObject2");
        bldr.setField("affKey", i);
        assertEquals(i, aff.affinityKey(bldr.build()));
        CacheObject cacheObj = cacheObjProc.toCacheObject(cacheObjCtx, new TestObject(i), true);
        assertEquals(i, aff.affinityKey(cacheObj));
        assertEquals(aff.mapKeyToNode(i), aff.mapKeyToNode(new TestObject(i)));
        assertEquals(aff.mapKeyToNode(i), aff.mapKeyToNode(cacheObj));
        assertEquals(i, affProc.affinityKey(DEFAULT_CACHE_NAME, i));
        assertEquals(i, affProc.affinityKey(DEFAULT_CACHE_NAME, new TestObject(i)));
        assertEquals(i, affProc.affinityKey(DEFAULT_CACHE_NAME, cacheObj));
        assertEquals(affProc.mapKeyToNode(DEFAULT_CACHE_NAME, i), affProc.mapKeyToNode(DEFAULT_CACHE_NAME, new TestObject(i)));
        assertEquals(affProc.mapKeyToNode(DEFAULT_CACHE_NAME, i), affProc.mapKeyToNode(DEFAULT_CACHE_NAME, cacheObj));
        assertEquals(affProc.mapKeyToNode(DEFAULT_CACHE_NAME, new AffinityKey(0, i)), affProc.mapKeyToNode(DEFAULT_CACHE_NAME, i));
    }
}
Also used : GridAffinityProcessor(org.apache.ignite.internal.processors.affinity.GridAffinityProcessor) IgniteCacheObjectProcessor(org.apache.ignite.internal.processors.cacheobject.IgniteCacheObjectProcessor) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 57 with BinaryObjectBuilder

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

the class GridLocalCacheStoreManagerDeserializationTest method testBinaryUpdate.

/**
 * Checks no additional unmarshalling happens in calling
 * {@link GridCacheMapEntry#innerUpdateLocal(GridCacheVersion, GridCacheOperation, Object, Object[],
 * boolean, boolean, boolean, boolean, ExpiryPolicy, boolean, boolean, CacheEntryPredicate[],
 * boolean, UUID, String)} for binary objects.
 *
 * @throws Exception
 */
public void testBinaryUpdate() throws Exception {
    // Goal is to check correct saving to store (no exception must be thrown)
    final Ignite grid = startGrid("binaryGrid");
    final IgniteCache<BinaryObject, BinaryObject> cache = grid.createCache(CACHE_NAME).withKeepBinary();
    final BinaryObjectBuilder builder = grid.binary().builder("custom_type");
    final BinaryObject entity = builder.setField("id", 0).build();
    cache.put(entity, entity);
    assert entity.equals(cache.get(entity));
    assert store.map.containsKey(entity);
    cache.remove(entity);
    assert cache.get(entity) == null;
    assert !store.map.containsKey(entity);
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) Ignite(org.apache.ignite.Ignite) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder)

Example 58 with BinaryObjectBuilder

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

the class BinaryObjectBuilderDefaultMappersSelfTest method testCharField.

/**
 * @throws Exception If failed.
 */
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());
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder)

Example 59 with BinaryObjectBuilder

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

the class BinaryObjectBuilderDefaultMappersSelfTest method testOffheapBinary.

/**
 * @throws Exception If failed.
 */
public void testOffheapBinary() throws Exception {
    BinaryObjectBuilder builder = builder("Class");
    builder.setField("i", 111);
    builder.setField("f", 111.111f);
    builder.setField("iArr", new int[] { 1, 2, 3 });
    builder.setField("obj", new Key(1));
    builder.setField("col", Arrays.asList(new Value(1), new Value(2)), Collection.class);
    BinaryObject po = builder.build();
    byte[] arr = ((CacheObjectBinaryProcessorImpl) (grid(0)).context().cacheObjects()).marshal(po);
    long ptr = GridUnsafe.allocateMemory(arr.length + 5);
    try {
        long ptr0 = ptr;
        GridUnsafe.putBoolean(null, ptr0++, false);
        int len = arr.length;
        if (BIG_ENDIAN)
            GridUnsafe.putIntLE(ptr0, len);
        else
            GridUnsafe.putInt(ptr0, len);
        GridUnsafe.copyHeapOffheap(arr, GridUnsafe.BYTE_ARR_OFF, ptr0 + 4, arr.length);
        BinaryObject offheapObj = (BinaryObject) ((CacheObjectBinaryProcessorImpl) (grid(0)).context().cacheObjects()).unmarshal(ptr, false);
        assertEquals(BinaryObjectOffheapImpl.class, offheapObj.getClass());
        assertEquals(expectedHashCode("Class"), offheapObj.type().typeId());
        assertEquals(BinaryArrayIdentityResolver.instance().hashCode(po), offheapObj.hashCode());
        assertEquals(111, offheapObj.<Integer>field("i").intValue());
        assertEquals(111.111f, offheapObj.<Float>field("f").floatValue(), 0);
        assertTrue(Arrays.equals(new int[] { 1, 2, 3 }, offheapObj.<int[]>field("iArr")));
        assertEquals(1, offheapObj.<BinaryObject>field("obj").<Key>deserialize().i);
        List<BinaryObject> list = offheapObj.field("col");
        assertEquals(2, list.size());
        assertEquals(1, list.get(0).<Value>deserialize().i);
        assertEquals(2, list.get(1).<Value>deserialize().i);
        assertEquals(po, offheapObj);
        assertEquals(offheapObj, po);
    } finally {
        GridUnsafe.freeMemory(ptr);
    }
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) CacheObjectBinaryProcessorImpl(org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder)

Example 60 with BinaryObjectBuilder

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

the class BinaryObjectBuilderDefaultMappersSelfTest method testMetaData.

/**
 * @throws Exception If failed.
 */
public void testMetaData() throws Exception {
    BinaryObjectBuilder builder = builder("org.test.MetaTest");
    builder.setField("intField", 1);
    builder.setField("byteArrayField", new byte[] { 1, 2, 3 });
    BinaryObject po = builder.build();
    BinaryType meta = po.type();
    assertEquals(expectedTypeName("org.test.MetaTest"), meta.typeName());
    Collection<String> fields = meta.fieldNames();
    assertEquals(2, fields.size());
    assertTrue(fields.contains("intField"));
    assertTrue(fields.contains("byteArrayField"));
    assertEquals("int", meta.fieldTypeName("intField"));
    assertEquals("byte[]", meta.fieldTypeName("byteArrayField"));
    builder = builder("org.test.MetaTest");
    builder.setField("intField", 2);
    builder.setField("uuidField", UUID.randomUUID());
    po = builder.build();
    meta = po.type();
    assertEquals(expectedTypeName("org.test.MetaTest"), meta.typeName());
    fields = meta.fieldNames();
    assertEquals(3, fields.size());
    assertTrue(fields.contains("intField"));
    assertTrue(fields.contains("byteArrayField"));
    assertTrue(fields.contains("uuidField"));
    assertEquals("int", meta.fieldTypeName("intField"));
    assertEquals("byte[]", meta.fieldTypeName("byteArrayField"));
    assertEquals("UUID", meta.fieldTypeName("uuidField"));
}
Also used : BinaryType(org.apache.ignite.binary.BinaryType) 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