Search in sources :

Example 91 with BinaryObject

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

the class CacheKeepBinaryIterationTest method doTestLocalEntries.

/**
     * @param ccfg Cache configuration.
     */
private void doTestLocalEntries(final CacheConfiguration<Object, Object> ccfg, boolean keepBinary, boolean primitives) throws Exception {
    IgniteCache<Object, Object> cache = grid(0).createCache(ccfg);
    assertEquals(0, cache.size());
    try {
        for (int i = 0; i < KEYS; i++) if (primitives)
            cache.put(i, i);
        else
            cache.put(new QueryTestKey(i), new QueryTestValue(i));
        for (int i = 0; i < getServerNodeCount(); i++) {
            IgniteCache<Object, Object> cache0 = grid(i).cache(ccfg.getName());
            if (keepBinary)
                cache0 = cache0.withKeepBinary();
            for (CachePeekMode mode : CachePeekMode.values()) {
                int size = 0;
                for (Cache.Entry<Object, Object> e : cache0.localEntries(mode)) {
                    Object key = e.getKey();
                    Object val = e.getValue();
                    if (!primitives) {
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, keepBinary == key instanceof BinaryObject);
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, keepBinary == val instanceof BinaryObject);
                    } else {
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, key instanceof Integer);
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, val instanceof Integer);
                    }
                    ++size;
                }
                if (mode == CachePeekMode.ALL || mode == CachePeekMode.PRIMARY || mode == CachePeekMode.BACKUP || (mode == CachePeekMode.NEAR && i == 0 && ccfg.getNearConfiguration() != null))
                    assertTrue("Zero result at mode: " + mode, size > 0);
            }
        }
    } finally {
        if (ccfg.getEvictionPolicy() != null) {
            // TODO: IGNITE-3462. Fixes evictionPolicy issues at cache destroy.
            stopAllGrids();
            startGridsMultiThreaded(getServerNodeCount());
        } else
            grid(0).destroyCache(ccfg.getName());
    }
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) CachePeekMode(org.apache.ignite.cache.CachePeekMode) BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 92 with BinaryObject

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

the class UpdatePlanBuilder method createSupplier.

/**
     * Detect appropriate method of instantiating key or value (take from param, create binary builder,
     * invoke default ctor, or allocate).
     *
     * @param cctx Cache context.
     * @param desc Table descriptor.
     * @param colIdx Column index if key or value is present in columns list, {@code -1} if it's not.
     * @param hasProps Whether column list affects individual properties of key or value.
     * @param key Whether supplier should be created for key or for value.
     * @return Closure returning key or value.
     * @throws IgniteCheckedException If failed.
     */
@SuppressWarnings({ "ConstantConditions", "unchecked" })
private static KeyValueSupplier createSupplier(final GridCacheContext<?, ?> cctx, GridQueryTypeDescriptor desc, final int colIdx, boolean hasProps, final boolean key, boolean forUpdate) throws IgniteCheckedException {
    final String typeName = key ? desc.keyTypeName() : desc.valueTypeName();
    //Try to find class for the key locally.
    final Class<?> cls = key ? U.firstNotNull(U.classForName(desc.keyTypeName(), null), desc.keyClass()) : desc.valueClass();
    boolean isSqlType = QueryUtils.isSqlType(cls);
    // If we don't need to construct anything from scratch, just return value from given list.
    if (isSqlType || !hasProps) {
        if (colIdx != -1)
            return new PlainValueSupplier(colIdx);
        else if (isSqlType)
            // Non constructable keys and values (SQL types) must be present in the query explicitly.
            throw new IgniteCheckedException((key ? "Key" : "Value") + " is missing from query");
    }
    if (cctx.binaryMarshaller()) {
        if (colIdx != -1) {
            // If we have key or value explicitly present in query, create new builder upon them...
            return new KeyValueSupplier() {

                /** {@inheritDoc} */
                @Override
                public Object apply(List<?> arg) throws IgniteCheckedException {
                    Object obj = arg.get(colIdx);
                    if (obj == null)
                        return null;
                    BinaryObject bin = cctx.grid().binary().toBinary(obj);
                    return cctx.grid().binary().builder(bin);
                }
            };
        } else {
            // ...and if we don't, just create a new builder.
            return new KeyValueSupplier() {

                /** {@inheritDoc} */
                @Override
                public Object apply(List<?> arg) throws IgniteCheckedException {
                    return cctx.grid().binary().builder(typeName);
                }
            };
        }
    } else {
        if (colIdx != -1) {
            if (forUpdate && colIdx == 1) {
                // so we have to clone it. And on UPDATE we don't expect any key supplier.
                assert !key;
                return new KeyValueSupplier() {

                    /** {@inheritDoc} */
                    @Override
                    public Object apply(List<?> arg) throws IgniteCheckedException {
                        byte[] oldPropBytes = cctx.marshaller().marshal(arg.get(1));
                        // colVal is another object now, we can mutate it
                        return cctx.marshaller().unmarshal(oldPropBytes, U.resolveClassLoader(cctx.gridConfig()));
                    }
                };
            } else
                // We either are not updating, or the new value is given explicitly, no cloning needed.
                return new PlainValueSupplier(colIdx);
        }
        Constructor<?> ctor;
        try {
            ctor = cls.getDeclaredConstructor();
            ctor.setAccessible(true);
        } catch (NoSuchMethodException | SecurityException ignored) {
            ctor = null;
        }
        if (ctor != null) {
            final Constructor<?> ctor0 = ctor;
            // Use default ctor, if it's present...
            return new KeyValueSupplier() {

                /** {@inheritDoc} */
                @Override
                public Object apply(List<?> arg) throws IgniteCheckedException {
                    try {
                        return ctor0.newInstance();
                    } catch (Exception e) {
                        if (S.INCLUDE_SENSITIVE)
                            throw new IgniteCheckedException("Failed to instantiate " + (key ? "key" : "value") + " [type=" + typeName + ']', e);
                        else
                            throw new IgniteCheckedException("Failed to instantiate " + (key ? "key" : "value") + '.', e);
                    }
                }
            };
        } else {
            // ...or allocate new instance with unsafe, if it's not
            return new KeyValueSupplier() {

                /** {@inheritDoc} */
                @Override
                public Object apply(List<?> arg) throws IgniteCheckedException {
                    try {
                        return GridUnsafe.allocateInstance(cls);
                    } catch (InstantiationException e) {
                        if (S.INCLUDE_SENSITIVE)
                            throw new IgniteCheckedException("Failed to instantiate " + (key ? "key" : "value") + " [type=" + typeName + ']', e);
                        else
                            throw new IgniteCheckedException("Failed to instantiate " + (key ? "key" : "value") + '.', e);
                    }
                }
            };
        }
    }
}
Also used : CacheException(javax.cache.CacheException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BinaryObject(org.apache.ignite.binary.BinaryObject) List(java.util.List) BinaryObject(org.apache.ignite.binary.BinaryObject)

Example 93 with BinaryObject

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

the class GridIndexingSpiAbstractSelfTest method testSpi.

/**
     * @throws Exception If failed.
     */
public void testSpi() throws Exception {
    IgniteH2Indexing spi = getIndexing();
    IgniteCache<Integer, BinaryObject> cacheA = ignite0.createCache(cacheACfg());
    IgniteCache<Integer, BinaryObject> cacheB = ignite0.createCache(cacheBCfg());
    assertFalse(spi.queryLocalSql(spi.schema(typeAA.cacheName()), "select * from A.A", null, Collections.emptySet(), typeAA.name(), null, null).hasNext());
    assertFalse(spi.queryLocalSql(spi.schema(typeAB.cacheName()), "select * from A.B", null, Collections.emptySet(), typeAB.name(), null, null).hasNext());
    assertFalse(spi.queryLocalSql(spi.schema(typeBA.cacheName()), "select * from B.A", null, Collections.emptySet(), typeBA.name(), null, null).hasNext());
    assertFalse(spi.queryLocalSql(spi.schema(typeBA.cacheName()), "select * from B.A, A.B, A.A", null, Collections.emptySet(), typeBA.name(), null, null).hasNext());
    try {
        spi.queryLocalSql(spi.schema(typeBA.cacheName()), "select aa.*, ab.*, ba.* from A.A aa, A.B ab, B.A ba", null, Collections.emptySet(), typeBA.name(), null, null).hasNext();
        fail("Enumerations of aliases in select block must be prohibited");
    } catch (IgniteCheckedException ignored) {
    // all fine
    }
    assertFalse(spi.queryLocalSql(spi.schema(typeAB.cacheName()), "select ab.* from A.B ab", null, Collections.emptySet(), typeAB.name(), null, null).hasNext());
    assertFalse(spi.queryLocalSql(spi.schema(typeBA.cacheName()), "select   ba.*   from B.A  as ba", null, Collections.emptySet(), typeBA.name(), null, null).hasNext());
    cacheA.put(1, aa("A", 1, "Vasya", 10).build());
    cacheA.put(1, ab(1, "Vasya", 20, "Some text about Vasya goes here.").build());
    cacheB.put(1, ba(2, "Petya", 25, true).build());
    cacheB.put(1, ba(2, "Kolya", 25, true).build());
    cacheA.put(2, aa("A", 2, "Valera", 19).build());
    cacheA.put(3, aa("A", 3, "Borya", 18).build());
    cacheA.put(4, ab(4, "Vitalya", 20, "Very Good guy").build());
    // Query data.
    Iterator<IgniteBiTuple<Integer, BinaryObjectImpl>> res = spi.queryLocalSql(spi.schema(typeAA.cacheName()), "from a order by age", null, Collections.emptySet(), typeAA.name(), null, null);
    assertTrue(res.hasNext());
    assertEquals(aa("A", 3, "Borya", 18).build(), value(res.next()));
    assertTrue(res.hasNext());
    assertEquals(aa("A", 2, "Valera", 19).build(), value(res.next()));
    assertFalse(res.hasNext());
    res = spi.queryLocalSql(spi.schema(typeAA.cacheName()), "select aa.* from a aa order by aa.age", null, Collections.emptySet(), typeAA.name(), null, null);
    assertTrue(res.hasNext());
    assertEquals(aa("A", 3, "Borya", 18).build(), value(res.next()));
    assertTrue(res.hasNext());
    assertEquals(aa("A", 2, "Valera", 19).build(), value(res.next()));
    assertFalse(res.hasNext());
    res = spi.queryLocalSql(spi.schema(typeAB.cacheName()), "from b order by name", null, Collections.emptySet(), typeAB.name(), null, null);
    assertTrue(res.hasNext());
    assertEquals(ab(1, "Vasya", 20, "Some text about Vasya goes here.").build(), value(res.next()));
    assertTrue(res.hasNext());
    assertEquals(ab(4, "Vitalya", 20, "Very Good guy").build(), value(res.next()));
    assertFalse(res.hasNext());
    res = spi.queryLocalSql(spi.schema(typeAB.cacheName()), "select bb.* from b as bb order by bb.name", null, Collections.emptySet(), typeAB.name(), null, null);
    assertTrue(res.hasNext());
    assertEquals(ab(1, "Vasya", 20, "Some text about Vasya goes here.").build(), value(res.next()));
    assertTrue(res.hasNext());
    assertEquals(ab(4, "Vitalya", 20, "Very Good guy").build(), value(res.next()));
    assertFalse(res.hasNext());
    res = spi.queryLocalSql(spi.schema(typeBA.cacheName()), "from a", null, Collections.emptySet(), typeBA.name(), null, null);
    assertTrue(res.hasNext());
    assertEquals(ba(2, "Kolya", 25, true).build(), value(res.next()));
    assertFalse(res.hasNext());
    // Text queries
    Iterator<IgniteBiTuple<Integer, BinaryObjectImpl>> txtRes = spi.queryLocalText(spi.schema(typeAB.cacheName()), "good", typeAB.name(), null);
    assertTrue(txtRes.hasNext());
    assertEquals(ab(4, "Vitalya", 20, "Very Good guy").build(), value(txtRes.next()));
    assertFalse(txtRes.hasNext());
    // Fields query
    GridQueryFieldsResult fieldsRes = spi.queryLocalSqlFields(spi.schema("A"), "select a.a.name n1, a.a.age a1, b.a.name n2, " + "b.a.age a2 from a.a, b.a where a.a.id = b.a.id ", Collections.emptySet(), null, false, 0, null);
    String[] aliases = { "N1", "A1", "N2", "A2" };
    Object[] vals = { "Valera", 19, "Kolya", 25 };
    IgniteSpiCloseableIterator<List<?>> it = fieldsRes.iterator();
    assertTrue(it.hasNext());
    List<?> fields = it.next();
    assertEquals(4, fields.size());
    int i = 0;
    for (Object f : fields) {
        assertEquals(aliases[i], fieldsRes.metaData().get(i).fieldName());
        assertEquals(vals[i++], f);
    }
    assertFalse(it.hasNext());
    // Remove
    cacheA.remove(2);
    cacheB.remove(1);
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridQueryFieldsResult(org.apache.ignite.internal.processors.query.GridQueryFieldsResult) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) ArrayList(java.util.ArrayList) List(java.util.List)

Example 94 with BinaryObject

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

the class IgniteSqlMergeQueryBenchmark method test.

/** {@inheritDoc} */
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    if (rnd.nextBoolean()) {
        double salary = rnd.nextDouble() * args.range() * 1000;
        double maxSalary = salary + 1000;
        Collection<Cache.Entry<Integer, Object>> entries = executeQuery(salary, maxSalary);
        for (Cache.Entry<Integer, Object> entry : entries) {
            Object o = entry.getValue();
            double s = o instanceof Person ? ((Person) o).getSalary() : ((BinaryObject) o).<Double>field("salary");
            if (s < salary || s > maxSalary)
                throw new Exception("Invalid person retrieved [min=" + salary + ", max=" + maxSalary + ", person=" + o + ']');
        }
        qryCnt.getAndIncrement();
    } else {
        int i = rnd.nextInt(args.range());
        cache.query(new SqlFieldsQuery("merge into Person(_key, id, firstName, lastName, salary) " + "values (?, ?, ?, ?, ?)").setArgs(i, i, "firstName" + i, "lastName" + i, (double) i * 1000));
        putCnt.getAndIncrement();
    }
    return true;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BinaryObject(org.apache.ignite.binary.BinaryObject) Person(org.apache.ignite.yardstick.cache.model.Person) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 95 with BinaryObject

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

the class IgniteBinaryIdentityBenchmark method setUp.

/** {@inheritDoc} */
@Override
public void setUp(BenchmarkConfiguration cfg) throws Exception {
    super.setUp(cfg);
    println(cfg, "Populating data...");
    long start = System.nanoTime();
    try (IgniteDataStreamer<BinaryObject, Object> dataLdr = ignite().dataStreamer(cache.getName())) {
        for (int i = 0; i < args.range() && !Thread.currentThread().isInterrupted(); ) {
            dataLdr.addData(createKey(i), new SampleValue(i));
            if (++i % 100000 == 0)
                println(cfg, "Items populated: " + i);
        }
    }
    println(cfg, "Finished populating data in " + ((System.nanoTime() - start) / 1_000_000) + " ms.");
}
Also used : SampleValue(org.apache.ignite.yardstick.cache.model.SampleValue) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObject(org.apache.ignite.binary.BinaryObject)

Aggregations

BinaryObject (org.apache.ignite.binary.BinaryObject)173 BinaryObjectBuilder (org.apache.ignite.binary.BinaryObjectBuilder)54 BinaryTypeConfiguration (org.apache.ignite.binary.BinaryTypeConfiguration)24 IgniteCache (org.apache.ignite.IgniteCache)21 Ignite (org.apache.ignite.Ignite)19 HashMap (java.util.HashMap)14 Map (java.util.Map)14 LinkedHashMap (java.util.LinkedHashMap)13 ArrayList (java.util.ArrayList)12 Cache (javax.cache.Cache)12 BinaryObjectBuilderImpl (org.apache.ignite.internal.binary.builder.BinaryObjectBuilderImpl)10 GridBinaryTestClasses (org.apache.ignite.internal.binary.mutabletest.GridBinaryTestClasses)9 List (java.util.List)8 UUID (java.util.UUID)8 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)8 BigInteger (java.math.BigInteger)7 Date (java.util.Date)6 BinaryType (org.apache.ignite.binary.BinaryType)6 IgniteEx (org.apache.ignite.internal.IgniteEx)6 Transaction (org.apache.ignite.transactions.Transaction)6