Search in sources :

Example 41 with SqlQuery

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

the class IgniteCacheProxy method query.

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public <R> QueryCursor<R> query(Query<R> qry) {
    A.notNull(qry, "qry");
    GridCacheGateway<K, V> gate = this.gate;
    CacheOperationContext prev = onEnter(gate, opCtx);
    try {
        ctx.checkSecurity(SecurityPermission.CACHE_READ);
        validate(qry);
        convertToBinary(qry);
        CacheOperationContext opCtxCall = ctx.operationContextPerCall();
        boolean keepBinary = opCtxCall != null && opCtxCall.isKeepBinary();
        if (qry instanceof ContinuousQuery)
            return (QueryCursor<R>) queryContinuous((ContinuousQuery<K, V>) qry, qry.isLocal(), keepBinary);
        if (qry instanceof SqlQuery)
            return (QueryCursor<R>) ctx.kernalContext().query().querySql(ctx, (SqlQuery) qry, keepBinary);
        if (qry instanceof SqlFieldsQuery)
            return (FieldsQueryCursor<R>) ctx.kernalContext().query().querySqlFields(ctx, (SqlFieldsQuery) qry, keepBinary);
        if (qry instanceof ScanQuery)
            return query((ScanQuery) qry, null, projection(qry.isLocal()));
        return (QueryCursor<R>) query(qry, projection(qry.isLocal()));
    } catch (Exception e) {
        if (e instanceof CacheException)
            throw (CacheException) e;
        throw new CacheException(e);
    } finally {
        onLeave(gate, prev);
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) CacheException(javax.cache.CacheException) ScanQuery(org.apache.ignite.cache.query.ScanQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) QueryCursor(org.apache.ignite.cache.query.QueryCursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor)

Example 42 with SqlQuery

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

the class JettyRestProcessorAbstractSelfTest method initCache.

/**
 * Init cache.
 */
protected void initCache() {
    CacheConfiguration typedCache = new CacheConfiguration<>("test_typed_access");
    ignite(0).getOrCreateCache(typedCache);
    CacheConfiguration<Integer, Organization> orgCacheCfg = new CacheConfiguration<>("organization");
    orgCacheCfg.setIndexedTypes(Integer.class, Organization.class);
    IgniteCache<Integer, Organization> orgCache = ignite(0).getOrCreateCache(orgCacheCfg);
    orgCache.clear();
    Organization o1 = new Organization(1, "o1");
    Organization o2 = new Organization(2, "o2");
    orgCache.put(1, o1);
    orgCache.put(2, o2);
    CacheConfiguration<Integer, Person> personCacheCfg = new CacheConfiguration<>("person");
    personCacheCfg.setIndexedTypes(Integer.class, Person.class);
    IgniteCache<Integer, Person> personCache = grid(0).getOrCreateCache(personCacheCfg);
    personCache.clear();
    Person p1 = new Person(1, "John", "Doe", 2000);
    Person p2 = new Person(1, "Jane", "Doe", 1000);
    Person p3 = new Person(2, "John", "Smith", 1000);
    Person p4 = new Person(2, "Jane", "Smith", 2000);
    personCache.put(p1.getId(), p1);
    personCache.put(p2.getId(), p2);
    personCache.put(p3.getId(), p3);
    personCache.put(p4.getId(), p4);
    SqlQuery<Integer, Person> qry = new SqlQuery<>(Person.class, "salary > ? and salary <= ?");
    qry.setArgs(1000, 2000);
    assertEquals(2, personCache.query(qry).getAll().size());
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 43 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)

Example 44 with SqlQuery

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

the class IgniteRepositoryQuery method prepareQuery.

/**
 * @param prmtrs Prmtrs.
 * @return prepared query for execution
 */
@NotNull
private Query prepareQuery(Object[] prmtrs) {
    Object[] parameters = prmtrs;
    String sql = qry.sql();
    Query query;
    switch(qry.options()) {
        case SORTING:
            sql = IgniteQueryGenerator.addSorting(new StringBuilder(sql), (Sort) parameters[parameters.length - 1]).toString();
            parameters = Arrays.copyOfRange(parameters, 0, parameters.length - 1);
            break;
        case PAGINATION:
            sql = IgniteQueryGenerator.addPaging(new StringBuilder(sql), (Pageable) parameters[parameters.length - 1]).toString();
            parameters = Arrays.copyOfRange(parameters, 0, parameters.length - 1);
            break;
    }
    if (qry.isFieldQuery()) {
        SqlFieldsQuery sqlFieldsQry = new SqlFieldsQuery(sql);
        sqlFieldsQry.setArgs(parameters);
        query = sqlFieldsQry;
    } else {
        SqlQuery sqlQry = new SqlQuery(type, sql);
        sqlQry.setArgs(parameters);
        query = sqlQry;
    }
    return query;
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) Query(org.apache.ignite.cache.query.Query) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) SqlQuery(org.apache.ignite.cache.query.SqlQuery) RepositoryQuery(org.springframework.data.repository.query.RepositoryQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) NotNull(org.jetbrains.annotations.NotNull)

Example 45 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)

Aggregations

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