Search in sources :

Example 51 with SqlQuery

use of org.apache.ignite.cache.query.SqlQuery in project ignite by apache.

the class IgniteCacheDistributedPartitionQuerySelfTest method testLocalQuery.

/** Tests local query over partitions. */
public void testLocalQuery() {
    Affinity<Object> affinity = grid(0).affinity("cl");
    int[] parts = affinity.primaryPartitions(grid(0).localNode());
    Arrays.sort(parts);
    IgniteCache<ClientKey, Client> cl = grid(0).cache("cl");
    SqlQuery<ClientKey, Client> qry1 = new SqlQuery<>(Client.class, "1=1");
    qry1.setLocal(true);
    qry1.setPartitions(parts[0]);
    List<Cache.Entry<ClientKey, Client>> clients = cl.query(qry1).getAll();
    for (Cache.Entry<ClientKey, Client> client : clients) assertEquals("Incorrect partition", parts[0], affinity.partition(client.getKey()));
    SqlFieldsQuery qry2 = new SqlFieldsQuery("select cl._KEY, cl._VAL from \"cl\".Client cl");
    qry2.setLocal(true);
    qry2.setPartitions(parts[0]);
    List<List<?>> rows = cl.query(qry2).getAll();
    for (List<?> row : rows) assertEquals("Incorrect partition", parts[0], affinity.partition(row.get(0)));
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) List(java.util.List) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 52 with SqlQuery

use of org.apache.ignite.cache.query.SqlQuery in project ignite by apache.

the class IgniteCrossCachesJoinsQueryTest method checkOrganizationPersonsJoin.

/**
     * @param cache Cache.
     */
private void checkOrganizationPersonsJoin(IgniteCache cache) {
    if (skipQuery(cache, PERSON_CACHE_NAME, ORG_CACHE_NAME))
        return;
    qry = "checkOrganizationPersonsJoin";
    SqlFieldsQuery qry = new SqlFieldsQuery("select o.name, p.name " + "from \"" + ORG_CACHE_NAME + "\".Organization o, \"" + PERSON_CACHE_NAME + "\".Person p " + "where p.orgId = o._key and o._key=?");
    qry.setDistributedJoins(distributedJoins());
    SqlQuery qry2 = null;
    if (PERSON_CACHE_NAME.equals(cache.getName())) {
        qry2 = new SqlQuery(Person.class, "from \"" + ORG_CACHE_NAME + "\".Organization, \"" + PERSON_CACHE_NAME + "\".Person " + "where Person.orgId = Organization._key and Organization._key=?");
        qry2.setDistributedJoins(distributedJoins());
    }
    long total = 0;
    for (int i = 0; i < data.personsPerOrg.size(); i++) {
        qry.setArgs(i);
        if (qry2 != null)
            qry2.setArgs(i);
        List<List<Object>> res = cache.query(qry).getAll();
        assertEquals((int) data.personsPerOrg.get(i), res.size());
        if (qry2 != null) {
            List<List<Object>> res2 = cache.query(qry2).getAll();
            assertEquals((int) data.personsPerOrg.get(i), res2.size());
        }
        total += res.size();
    }
    SqlFieldsQuery qry3 = new SqlFieldsQuery("select count(*) " + "from \"" + ORG_CACHE_NAME + "\".Organization o, \"" + PERSON_CACHE_NAME + "\".Person p where p.orgId = o._key");
    qry3.setDistributedJoins(distributedJoins());
    List<List<Object>> res = cache.query(qry3).getAll();
    assertEquals(1, res.size());
    assertEquals(total, res.get(0).get(0));
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) ArrayList(java.util.ArrayList) List(java.util.List) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 53 with SqlQuery

use of org.apache.ignite.cache.query.SqlQuery in project ignite by apache.

the class DynamicIndexAbstractConcurrentSelfTest method testConcurrentPutRemove.

/**
     * PUT/REMOVE data from cache and build index concurrently.
     *
     * @throws Exception If failed,
     */
public void testConcurrentPutRemove() throws Exception {
    // Start several nodes.
    Ignite srv1 = Ignition.start(serverConfiguration(1));
    Ignition.start(serverConfiguration(2));
    Ignition.start(serverConfiguration(3));
    Ignition.start(serverConfiguration(4));
    awaitPartitionMapExchange();
    IgniteCache<BinaryObject, BinaryObject> cache = createSqlCache(srv1).withKeepBinary();
    // Start data change operations from several threads.
    final AtomicBoolean stopped = new AtomicBoolean();
    IgniteInternalFuture updateFut = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            while (!stopped.get()) {
                Ignite node = grid(ThreadLocalRandom.current().nextInt(1, 5));
                int key = ThreadLocalRandom.current().nextInt(0, LARGE_CACHE_SIZE);
                int val = ThreadLocalRandom.current().nextInt();
                BinaryObject keyObj = key(node, key);
                if (ThreadLocalRandom.current().nextBoolean()) {
                    BinaryObject valObj = value(node, val);
                    node.cache(CACHE_NAME).put(keyObj, valObj);
                } else
                    node.cache(CACHE_NAME).remove(keyObj);
            }
            return null;
        }
    }, 4);
    // Let some to arrive.
    Thread.sleep(500L);
    // Create index.
    QueryIndex idx = index(IDX_NAME_1, field(FIELD_NAME_1));
    queryProcessor(srv1).dynamicIndexCreate(CACHE_NAME, CACHE_NAME, TBL_NAME, idx, false).get();
    // Stop updates once index is ready.
    stopped.set(true);
    updateFut.get();
    // Make sure index is there.
    assertIndex(CACHE_NAME, TBL_NAME, IDX_NAME_1, field(FIELD_NAME_1));
    assertIndexUsed(IDX_NAME_1, SQL_SIMPLE_FIELD_1, SQL_ARG_1);
    // Get expected values.
    Map<Long, Long> expKeys = new HashMap<>();
    for (int i = 0; i < LARGE_CACHE_SIZE; i++) {
        BinaryObject val = cache.get(key(srv1, i));
        if (val != null) {
            long fieldVal = val.field(FIELD_NAME_1);
            if (fieldVal >= SQL_ARG_1)
                expKeys.put((long) i, fieldVal);
        }
    }
    // Validate query result.
    for (Ignite node : Ignition.allGrids()) {
        IgniteCache<BinaryObject, BinaryObject> nodeCache = node.cache(CACHE_NAME).withKeepBinary();
        SqlQuery qry = new SqlQuery(typeName(ValueClass.class), SQL_SIMPLE_FIELD_1).setArgs(SQL_ARG_1);
        List<Cache.Entry<BinaryObject, BinaryObject>> res = nodeCache.query(qry).getAll();
        assertEquals("Cache size mismatch [exp=" + expKeys.size() + ", actual=" + res.size() + ']', expKeys.size(), res.size());
        for (Cache.Entry<BinaryObject, BinaryObject> entry : res) {
            long key = entry.getKey().field(FIELD_KEY);
            Long fieldVal = entry.getValue().field(FIELD_NAME_1);
            assertTrue("Expected key is not in result set: " + key, expKeys.containsKey(key));
            assertEquals("Unexpected value [key=" + key + ", expVal=" + expKeys.get(key) + ", actualVal=" + fieldVal + ']', expKeys.get(key), fieldVal);
        }
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BinaryObject(org.apache.ignite.binary.BinaryObject) QueryIndex(org.apache.ignite.cache.QueryIndex) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 54 with SqlQuery

use of org.apache.ignite.cache.query.SqlQuery in project ignite by apache.

the class DynamicIndexAbstractSelfTest method assertSqlSimpleData.

/**
     * Assert SQL simple data state.
     *
     * @param node Node.
     * @param sql SQL query.
     * @param expSize Expected size.
     */
protected static void assertSqlSimpleData(Ignite node, String sql, int expSize) {
    SqlQuery qry = new SqlQuery(typeName(ValueClass.class), sql).setArgs(SQL_ARG_1);
    List<Cache.Entry<BinaryObject, BinaryObject>> res = node.cache(CACHE_NAME).withKeepBinary().query(qry).getAll();
    Set<Long> ids = new HashSet<>();
    for (Cache.Entry<BinaryObject, BinaryObject> entry : res) {
        long id = entry.getKey().field(FIELD_KEY);
        long field1 = entry.getValue().field(FIELD_NAME_1_ESCAPED);
        long field2 = entry.getValue().field(FIELD_NAME_2_ESCAPED);
        assertTrue(field1 >= SQL_ARG_1);
        assertEquals(id, field1);
        assertEquals(id, field2);
        assertTrue(ids.add(id));
    }
    assertEquals("Size mismatch [node=" + node.name() + ", exp=" + expSize + ", actual=" + res.size() + ", ids=" + ids + ']', expSize, res.size());
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) BinaryObject(org.apache.ignite.binary.BinaryObject) HashSet(java.util.HashSet) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 55 with SqlQuery

use of org.apache.ignite.cache.query.SqlQuery in project ignite by apache.

the class IgniteTxMultiNodeAbstractTest method onRemoveItemQueried.

/**
     *
     * @param putCntr Put counter to cache.
     * @param ignite Grid.
     * @param retry Retry count.
     * @throws IgniteCheckedException If failed.
     */
@SuppressWarnings("unchecked")
private void onRemoveItemQueried(boolean putCntr, Ignite ignite, int retry) throws IgniteCheckedException {
    IgniteCache<String, Integer> cache = ignite.cache(DEFAULT_CACHE_NAME);
    UUID locId = ignite.cluster().localNode().id();
    UUID cntrPrimaryId = primaryId(ignite, RMVD_CNTR_KEY);
    boolean isCntrPrimary = cntrPrimaryId.equals(locId);
    try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        if (DEBUG)
            ignite.log().info("Before item lock [retry=" + retry + ", xid=" + tx.xid() + ", node=" + ignite.name() + ", isCntrPrimary=" + isCntrPrimary + ", nearId=" + locId + ", nearEntry=" + nearEntry(locId, RMVD_CNTR_KEY) + (isCntrPrimary ? ", dhtEntry=" + dhtEntry(locId, RMVD_CNTR_KEY) : "") + ']');
        Integer cntr = cache.get(RMVD_CNTR_KEY);
        assert cntr != null : "Received null counter [retry=" + retry + ", isCntrPrimary=" + isCntrPrimary + ", nearEntry=" + nearEntry(locId, RMVD_CNTR_KEY) + (isCntrPrimary ? ", dhtEntry=" + dhtEntry(locId, RMVD_CNTR_KEY) : "") + ']';
        int newVal = cntr - 1;
        if (putCntr) {
            if (DEBUG)
                ignite.log().info("Before item put counter [retry=" + retry + ", isCntrPrimary=" + isCntrPrimary + ", cur=" + cntr + ", new=" + newVal + ", nearEntry=" + nearEntry(locId, RMVD_CNTR_KEY) + (isCntrPrimary ? ", dhtEntry=" + dhtEntry(locId, RMVD_CNTR_KEY) : "") + ']');
            cache.put(RMVD_CNTR_KEY, newVal);
        }
        while (true) {
            SqlQuery<String, Integer> qry = new SqlQuery<>(Integer.class, "_key != 'RMVD_CNTR_KEY' and _val >= 0");
            if (DEBUG)
                ignite.log().info("Before executing query [retry=" + retry + ", locId=" + locId + ", txId=" + tx.xid() + ']');
            Cache.Entry<String, Integer> entry = F.first(cache.query(qry).getAll());
            if (entry == null) {
                ignite.log().info("*** Queue is empty.");
                return;
            }
            String itemKey = entry.getKey();
            UUID itemPrimaryId = primaryId(ignite, itemKey);
            // Lock the item key.
            if (cache.get(itemKey) != null) {
                if (DEBUG)
                    ignite.log().info("Before item remove [retry=" + retry + ", key=" + itemKey + ", cur=" + cntr + ", nearEntry=" + nearEntry(locId, itemKey) + ", dhtEntry=" + dhtEntry(itemPrimaryId, itemKey) + ']');
                assert cache.remove(itemKey) : "Failed to remove key [locId=" + locId + ", primaryId=" + itemPrimaryId + ", key=" + itemKey + ']';
                if (DEBUG)
                    info("After item remove item [retry=" + retry + ", key=" + itemKey + ", cur=" + cntr + ", new=" + newVal + ", nearEntry=" + nearEntry(locId, itemKey) + ", dhtEntry=" + dhtEntry(itemPrimaryId, itemKey) + ']');
                break;
            } else
                cache.remove(itemKey);
        }
        tx.commit();
    } catch (Error e) {
        ignite.log().error("Error in test.", e);
        throw e;
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SqlQuery(org.apache.ignite.cache.query.SqlQuery) Transaction(org.apache.ignite.transactions.Transaction) UUID(java.util.UUID) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Aggregations

SqlQuery (org.apache.ignite.cache.query.SqlQuery)57 Cache (javax.cache.Cache)20 IgniteCache (org.apache.ignite.IgniteCache)20 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)18 Ignite (org.apache.ignite.Ignite)17 List (java.util.List)13 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 CAX (org.apache.ignite.internal.util.typedef.CAX)9 ArrayList (java.util.ArrayList)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 BinaryObject (org.apache.ignite.binary.BinaryObject)7 CacheException (javax.cache.CacheException)6 Collection (java.util.Collection)4 Random (java.util.Random)4 QueryCursor (org.apache.ignite.cache.query.QueryCursor)4 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 UUID (java.util.UUID)3