Search in sources :

Example 41 with BinaryObject

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

the class BinaryObjectBuilderDefaultMappersSelfTest method testObjectField.

/**
 * @throws Exception If failed.
 */
public void testObjectField() throws Exception {
    BinaryObjectBuilder builder = builder("Class");
    builder.setField("objectField", new Value(1));
    BinaryObject po = builder.build();
    assertEquals(expectedHashCode("Class"), po.type().typeId());
    assertEquals(BinaryArrayIdentityResolver.instance().hashCode(po), po.hashCode());
    assertEquals(1, po.<BinaryObject>field("objectField").<Value>deserialize().i);
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder)

Example 42 with BinaryObject

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

the class IgniteWalReaderTest method testFillWalWithDifferentTypes.

/**
 * @throws Exception if failed.
 */
public void testFillWalWithDifferentTypes() throws Exception {
    int cntEntries;
    final Map<Object, Object> ctrlMap = new HashMap<>();
    final Map<Object, Object> ctrlMapForBinaryObjects = new HashMap<>();
    final Collection<String> ctrlStringsToSearch = new HashSet<>();
    final Collection<String> ctrlStringsForBinaryObjSearch = new HashSet<>();
    final Ignite ignite0 = startGrid("node0");
    ignite0.active(true);
    final IgniteCache<Object, Object> addlCache = ignite0.getOrCreateCache(CACHE_ADDL_NAME);
    addlCache.put("1", "2");
    addlCache.put(1, 2);
    addlCache.put(1L, 2L);
    addlCache.put(TestEnum.A, "Enum_As_Key");
    addlCache.put("Enum_As_Value", TestEnum.B);
    addlCache.put(TestEnum.C, TestEnum.C);
    addlCache.put("Serializable", new TestSerializable(42));
    addlCache.put(new TestSerializable(42), "Serializable_As_Key");
    addlCache.put("Externalizable", new TestExternalizable(42));
    addlCache.put(new TestExternalizable(42), "Externalizable_As_Key");
    addlCache.put(292, new IndexedObject(292));
    final String search1 = "SomeUnexpectedStringValueAsKeyToSearch";
    ctrlStringsToSearch.add(search1);
    ctrlStringsForBinaryObjSearch.add(search1);
    addlCache.put(search1, "SearchKey");
    String search2 = "SomeTestStringContainerToBePrintedLongLine";
    final TestStringContainerToBePrinted val = new TestStringContainerToBePrinted(search2);
    // will validate original toString() was called
    ctrlStringsToSearch.add(val.toString());
    ctrlStringsForBinaryObjSearch.add(search2);
    addlCache.put("SearchValue", val);
    String search3 = "SomeTestStringContainerToBePrintedLongLine2";
    final TestStringContainerToBePrinted key = new TestStringContainerToBePrinted(search3);
    // will validate original toString() was called
    ctrlStringsToSearch.add(key.toString());
    // validate only string itself
    ctrlStringsForBinaryObjSearch.add(search3);
    addlCache.put(key, "SearchKey");
    cntEntries = addlCache.size();
    for (Cache.Entry<Object, Object> next : addlCache) ctrlMap.put(next.getKey(), next.getValue());
    for (Cache.Entry<Object, Object> next : addlCache) ctrlMapForBinaryObjects.put(next.getKey(), next.getValue());
    final String subfolderName = genDbSubfolderName(ignite0, 0);
    stopGrid("node0");
    final String workDir = U.defaultWorkDirectory();
    final File binaryMeta = U.resolveWorkDirectory(workDir, "binary_meta", false);
    final File binaryMetaWithNodeSubfolder = new File(binaryMeta, subfolderName);
    final File marshallerMapping = U.resolveWorkDirectory(workDir, "marshaller", false);
    final IgniteWalIteratorFactory factory = createWalIteratorFactory(workDir, subfolderName);
    final IgniteBiInClosure<Object, Object> objConsumer = new IgniteBiInClosure<Object, Object>() {

        @Override
        public void apply(Object key, Object val) {
            log.info("K: [" + key + ", " + (key != null ? key.getClass().getName() : "?") + "]" + " V: [" + val + ", " + (val != null ? val.getClass().getName() : "?") + "]");
            boolean rmv = remove(ctrlMap, key, val);
            if (!rmv) {
                String msg = "Unable to remove pair from control map " + "K: [" + key + "] V: [" + val + "]";
                log.error(msg);
            }
            assertFalse(val instanceof BinaryObject);
        }
    };
    final IgniteInClosure<DataRecord> toStrChecker = new IgniteInClosure<DataRecord>() {

        @Override
        public void apply(DataRecord record) {
            String strRepresentation = record.toString();
            for (Iterator<String> iter = ctrlStringsToSearch.iterator(); iter.hasNext(); ) {
                final String next = iter.next();
                if (strRepresentation.contains(next)) {
                    iter.remove();
                    break;
                }
            }
        }
    };
    scanIterateAndCount(factory, workDir, subfolderName, cntEntries, 0, objConsumer, toStrChecker);
    assertTrue(" Control Map is not empty after reading entries: " + ctrlMap, ctrlMap.isEmpty());
    assertTrue(" Control Map for strings in entries is not empty after" + " reading records: " + ctrlStringsToSearch, ctrlStringsToSearch.isEmpty());
    // Validate same WAL log with flag binary objects only
    final IgniteWalIteratorFactory keepBinFactory = new IgniteWalIteratorFactory(log, PAGE_SIZE, binaryMetaWithNodeSubfolder, marshallerMapping, true);
    final IgniteBiInClosure<Object, Object> binObjConsumer = new IgniteBiInClosure<Object, Object>() {

        @Override
        public void apply(Object key, Object val) {
            log.info("K(KeepBinary): [" + key + ", " + (key != null ? key.getClass().getName() : "?") + "]" + " V(KeepBinary): [" + val + ", " + (val != null ? val.getClass().getName() : "?") + "]");
            boolean rmv = remove(ctrlMapForBinaryObjects, key, val);
            if (!rmv) {
                if (key instanceof BinaryObject) {
                    BinaryObject keyBinObj = (BinaryObject) key;
                    String binaryObjTypeName = keyBinObj.type().typeName();
                    if (Objects.equals(TestStringContainerToBePrinted.class.getName(), binaryObjTypeName)) {
                        String data = keyBinObj.field("data");
                        rmv = ctrlMapForBinaryObjects.remove(new TestStringContainerToBePrinted(data)) != null;
                    } else if (Objects.equals(TestSerializable.class.getName(), binaryObjTypeName)) {
                        Integer iVal = keyBinObj.field("iVal");
                        rmv = ctrlMapForBinaryObjects.remove(new TestSerializable(iVal)) != null;
                    } else if (Objects.equals(TestEnum.class.getName(), binaryObjTypeName)) {
                        TestEnum key1 = TestEnum.values()[keyBinObj.enumOrdinal()];
                        rmv = ctrlMapForBinaryObjects.remove(key1) != null;
                    }
                } else if (val instanceof BinaryObject) {
                    // don't compare BO values, just remove by key
                    rmv = ctrlMapForBinaryObjects.remove(key) != null;
                }
            }
            if (!rmv)
                log.error("Unable to remove pair from control map " + "K: [" + key + "] V: [" + val + "]");
            if (val instanceof BinaryObject) {
                BinaryObject binaryObj = (BinaryObject) val;
                String binaryObjTypeName = binaryObj.type().typeName();
                if (Objects.equals(IndexedObject.class.getName(), binaryObjTypeName)) {
                    assertEquals(binaryObj.field("iVal").toString(), binaryObj.field("jVal").toString());
                    byte[] data = binaryObj.field("data");
                    for (byte datum : data) assertTrue(datum >= 'A' && datum <= 'A' + 10);
                }
            }
        }
    };
    final IgniteInClosure<DataRecord> binObjToStrChecker = new IgniteInClosure<DataRecord>() {

        @Override
        public void apply(DataRecord record) {
            String strRepresentation = record.toString();
            for (Iterator<String> iter = ctrlStringsForBinaryObjSearch.iterator(); iter.hasNext(); ) {
                final String next = iter.next();
                if (strRepresentation.contains(next)) {
                    iter.remove();
                    break;
                }
            }
        }
    };
    scanIterateAndCount(keepBinFactory, workDir, subfolderName, cntEntries, 0, binObjConsumer, binObjToStrChecker);
    assertTrue(" Control Map is not empty after reading entries: " + ctrlMapForBinaryObjects, ctrlMapForBinaryObjects.isEmpty());
    assertTrue(" Control Map for strings in entries is not empty after" + " reading records: " + ctrlStringsForBinaryObjSearch, ctrlStringsForBinaryObjSearch.isEmpty());
}
Also used : IgniteWalIteratorFactory(org.apache.ignite.internal.processors.cache.persistence.wal.reader.IgniteWalIteratorFactory) HashMap(java.util.HashMap) IgniteBiInClosure(org.apache.ignite.lang.IgniteBiInClosure) BinaryObject(org.apache.ignite.binary.BinaryObject) Ignite(org.apache.ignite.Ignite) DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) HashSet(java.util.HashSet) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) File(java.io.File) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 43 with BinaryObject

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

the class IgniteWalReaderTest method iterateAndCountDataRecord.

/**
 * Iterates over data records, checks each DataRecord and its entries, finds out all transactions in WAL.
 *
 * @param walIter iterator to use.
 * @return count of data records observed for each global TX ID. Contains null for non tx updates.
 * @throws IgniteCheckedException if failure.
 */
private Map<GridCacheVersion, Integer> iterateAndCountDataRecord(final WALIterator walIter, @Nullable final IgniteBiInClosure<Object, Object> cacheObjHnd, @Nullable final IgniteInClosure<DataRecord> dataRecordHnd) throws IgniteCheckedException {
    final Map<GridCacheVersion, Integer> entriesUnderTxFound = new HashMap<>();
    try (WALIterator stIt = walIter) {
        while (stIt.hasNextX()) {
            final IgniteBiTuple<WALPointer, WALRecord> next = stIt.nextX();
            final WALRecord walRecord = next.get2();
            if (walRecord.type() == WALRecord.RecordType.DATA_RECORD && walRecord instanceof DataRecord) {
                final DataRecord dataRecord = (DataRecord) walRecord;
                if (dataRecordHnd != null)
                    dataRecordHnd.apply(dataRecord);
                final List<DataEntry> entries = dataRecord.writeEntries();
                for (DataEntry entry : entries) {
                    final GridCacheVersion globalTxId = entry.nearXidVersion();
                    Object unwrappedKeyObj;
                    Object unwrappedValObj;
                    if (entry instanceof UnwrapDataEntry) {
                        UnwrapDataEntry unwrapDataEntry = (UnwrapDataEntry) entry;
                        unwrappedKeyObj = unwrapDataEntry.unwrappedKey();
                        unwrappedValObj = unwrapDataEntry.unwrappedValue();
                    } else if (entry instanceof LazyDataEntry) {
                        unwrappedKeyObj = null;
                        unwrappedValObj = null;
                    // can't check value
                    } else {
                        final CacheObject val = entry.value();
                        unwrappedValObj = val instanceof BinaryObject ? val : val.value(null, false);
                        final CacheObject key = entry.key();
                        unwrappedKeyObj = key instanceof BinaryObject ? key : key.value(null, false);
                    }
                    if (dumpRecords)
                        log.info("//Entry operation " + entry.op() + "; cache Id" + entry.cacheId() + "; " + "under transaction: " + globalTxId + // ; entry " + entry +
                        "; Key: " + unwrappedKeyObj + "; Value: " + unwrappedValObj);
                    if (cacheObjHnd != null && (unwrappedKeyObj != null || unwrappedValObj != null))
                        cacheObjHnd.apply(unwrappedKeyObj, unwrappedValObj);
                    final Integer entriesUnderTx = entriesUnderTxFound.get(globalTxId);
                    entriesUnderTxFound.put(globalTxId, entriesUnderTx == null ? 1 : entriesUnderTx + 1);
                }
            } else if (walRecord.type() == WALRecord.RecordType.TX_RECORD && walRecord instanceof TxRecord) {
                final TxRecord txRecord = (TxRecord) walRecord;
                final GridCacheVersion globalTxId = txRecord.nearXidVersion();
                if (dumpRecords)
                    log.info("//Tx Record, state: " + txRecord.state() + "; nearTxVersion" + globalTxId);
            }
        }
    }
    return entriesUnderTxFound;
}
Also used : WALRecord(org.apache.ignite.internal.pagemem.wal.record.WALRecord) HashMap(java.util.HashMap) UnwrapDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrapDataEntry) TxRecord(org.apache.ignite.internal.pagemem.wal.record.TxRecord) DataEntry(org.apache.ignite.internal.pagemem.wal.record.DataEntry) LazyDataEntry(org.apache.ignite.internal.pagemem.wal.record.LazyDataEntry) UnwrapDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrapDataEntry) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) BinaryObject(org.apache.ignite.binary.BinaryObject) WALIterator(org.apache.ignite.internal.pagemem.wal.WALIterator) LazyDataEntry(org.apache.ignite.internal.pagemem.wal.record.LazyDataEntry) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) WALPointer(org.apache.ignite.internal.pagemem.wal.WALPointer)

Example 44 with BinaryObject

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

the class GridCacheReplicatedPreloadSelfTest method testDeployment.

/**
 * @throws Exception If test failed.
 */
public void testDeployment() throws Exception {
    // TODO GG-11141.
    if (true)
        return;
    preloadMode = SYNC;
    try {
        Ignite g1 = startGrid(1);
        Ignite g2 = startGrid(2);
        IgniteCache<Integer, Object> cache1 = g1.cache(DEFAULT_CACHE_NAME);
        IgniteCache<Integer, Object> cache2 = g2.cache(DEFAULT_CACHE_NAME);
        ClassLoader ldr = grid(1).configuration().getClassLoader();
        Object v1 = ldr.loadClass("org.apache.ignite.tests.p2p.CacheDeploymentTestValue3").newInstance();
        cache1.put(1, v1);
        info("Stored value in cache1 [v=" + v1 + ", ldr=" + v1.getClass().getClassLoader() + ']');
        Object v2 = cache2.get(1);
        info("Read value from cache2 [v=" + v2 + ", ldr=" + v2.getClass().getClassLoader() + ']');
        assert v2 != null;
        assert v2.toString().equals(v1.toString());
        assert !v2.getClass().getClassLoader().equals(getClass().getClassLoader());
        assert v2.getClass().getClassLoader().getClass().getName().contains("GridDeploymentClassLoader") || grid(2).configuration().getMarshaller() instanceof BinaryMarshaller;
        Object e1 = ldr.loadClass("org.apache.ignite.tests.p2p.CacheDeploymentTestEnumValue").getEnumConstants()[0];
        cache1.put(2, e1);
        Object e2 = cache2.get(2);
        if (g1.configuration().getMarshaller() instanceof BinaryMarshaller) {
            BinaryObject enumObj = (BinaryObject) cache2.withKeepBinary().get(2);
            assertEquals(0, enumObj.enumOrdinal());
            assertTrue(enumObj.type().isEnum());
            assertTrue(enumObj instanceof BinaryEnumObjectImpl);
        }
        assert e2 != null;
        assert e2.toString().equals(e1.toString());
        assert !e2.getClass().getClassLoader().equals(getClass().getClassLoader());
        assert e2.getClass().getClassLoader().getClass().getName().contains("GridDeploymentClassLoader") || grid(2).configuration().getMarshaller() instanceof BinaryMarshaller;
        stopGrid(1);
        Ignite g3 = startGrid(3);
        IgniteCache<Integer, Object> cache3 = g3.cache(DEFAULT_CACHE_NAME);
        Object v3 = cache3.localPeek(1, CachePeekMode.ONHEAP);
        assert v3 != null;
        info("Read value from cache3 [v=" + v3 + ", ldr=" + v3.getClass().getClassLoader() + ']');
        assert v3 != null;
        assert v3.toString().equals(v1.toString());
        assert !v3.getClass().getClassLoader().equals(getClass().getClassLoader());
        assert v3.getClass().getClassLoader().getClass().getName().contains("GridDeploymentClassLoader") || grid(3).configuration().getMarshaller() instanceof BinaryMarshaller;
    } finally {
        stopAllGrids();
    }
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) Ignite(org.apache.ignite.Ignite) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryEnumObjectImpl(org.apache.ignite.internal.binary.BinaryEnumObjectImpl)

Example 45 with BinaryObject

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

the class IgnitePdsBinaryMetadataOnClusterRestartTest method testNewBinaryMetadataIsWrittenOnOldCoordinator.

/**
 * @see <a href="https://issues.apache.org/jira/browse/IGNITE-7258">IGNITE-7258</a> refer to the following JIRA for more context about the problem verified by the test.
 */
public void testNewBinaryMetadataIsWrittenOnOldCoordinator() throws Exception {
    Ignite ignite0 = startGridInASeparateWorkDir("A");
    Ignite ignite1 = startGridInASeparateWorkDir("B");
    ignite0.active(true);
    IgniteCache<Object, Object> cache0 = ignite0.cache(CACHE_NAME);
    BinaryObject bObj0 = ignite0.binary().builder("DynamicType0").setField("intField", 10).build();
    cache0.put(0, bObj0);
    stopGrid("A");
    IgniteCache<Object, Object> cache1 = ignite1.cache(CACHE_NAME);
    BinaryObject bObj1 = ignite1.binary().builder("DynamicType1").setField("strField", "str").build();
    cache1.put(1, bObj1);
    stopAllGrids();
    ignite0 = startGridInASeparateWorkDir("A");
    startGridInASeparateWorkDir("B");
    awaitPartitionMapExchange();
    stopGrid("B");
    cache0 = ignite0.cache(CACHE_NAME).withKeepBinary();
    bObj0 = (BinaryObject) cache0.get(0);
    bObj1 = (BinaryObject) cache0.get(1);
    assertEquals("DynamicType0", binaryTypeName(bObj0));
    assertEquals("DynamicType1", binaryTypeName(bObj1));
    assertEquals(10, (int) bObj0.field(DYNAMIC_INT_FIELD_NAME));
    assertEquals("str", bObj1.field(DYNAMIC_STR_FIELD_NAME));
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) Ignite(org.apache.ignite.Ignite) BinaryObject(org.apache.ignite.binary.BinaryObject)

Aggregations

BinaryObject (org.apache.ignite.binary.BinaryObject)200 BinaryObjectBuilder (org.apache.ignite.binary.BinaryObjectBuilder)55 Ignite (org.apache.ignite.Ignite)28 BinaryTypeConfiguration (org.apache.ignite.binary.BinaryTypeConfiguration)26 IgniteCache (org.apache.ignite.IgniteCache)25 HashMap (java.util.HashMap)16 Cache (javax.cache.Cache)15 ArrayList (java.util.ArrayList)13 Map (java.util.Map)13 LinkedHashMap (java.util.LinkedHashMap)12 List (java.util.List)10 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)10 IgniteEx (org.apache.ignite.internal.IgniteEx)10 BinaryObjectBuilderImpl (org.apache.ignite.internal.binary.builder.BinaryObjectBuilderImpl)10 IgniteException (org.apache.ignite.IgniteException)9 GridBinaryTestClasses (org.apache.ignite.internal.binary.mutabletest.GridBinaryTestClasses)9 UUID (java.util.UUID)8 BigInteger (java.math.BigInteger)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)7 BinaryType (org.apache.ignite.binary.BinaryType)7