/** * Fails if result has entries. * * @param qryStr to execute. * @param arg argument that will be passed to query. * @param cache cache on which query will be executed. */ private void assertEmptyEnumQry(String qryStr, Object arg, IgniteCache<Long, EnumObject> cache) { final SqlQuery<Long, EnumObject> qry = new SqlQuery<>(EnumObject.class, qryStr); qry.setArgs(arg); final List<Cache.Entry<Long, EnumObject>> res = cache.query(qry).getAll(); assert res.isEmpty(); }
/** * @param ignite Ignite. * @param orgId Organization id. * @return Count of found Person object with specified orgId */ private static int getPersonsCountBySqlLocalQuery(final IgniteEx ignite, int orgId) { List res = ignite.cache(Person.class.getSimpleName()) .query(new SqlQuery<Person.Key, Person>(Person.class, "orgId = ?").setArgs(orgId).setLocal(true)) .getAll(); return res.size(); }
/** * @param key Key. * @param cache Cache. */ private void checkNoValue(Object key, IgniteCache cache) { SqlQuery<Integer, MvccTestSqlIndexValue> qry; qry = new SqlQuery<>(MvccTestSqlIndexValue.class, "_key = ?"); qry.setArgs(key); List<IgniteCache.Entry<Integer, MvccTestSqlIndexValue>> res = cache.query(qry).getAll(); assertTrue(res.isEmpty()); }
/** {@inheritDoc} */ @Override public void doRead() { int idx = random.nextInt(entriesCount()); cache().query( new SqlQuery<>(TestingEntity.class, "val >= ? and val < ?") .setArgs(idx, idx + 500) ).getAll(); } };
/** * Fails if result size not equals to resSize or * at least one entry has different of resType type. * * @param qryStr to execute. * @param arg to be passed to query. * @param resType to compare with. * @param cache cache. * @param resSize size of the result. */ private void assertEnumQry(String qryStr, Object arg, EnumType resType, IgniteCache<Long, EnumObject> cache, int resSize) { final SqlQuery<Long, EnumObject> qry = new SqlQuery<>(EnumObject.class, qryStr); qry.setArgs(arg); final List<Cache.Entry<Long, EnumObject>> res = cache.query(qry).getAll(); assert resSize == res.size(); assertEnumType(res, resType); }
private void checkSqlQuery(IgniteCache<Long,TestData> cache, Boolean dataPageScanEnabled) { DirectPageScanIndexing.expectedDataPageScanEnabled = dataPageScanEnabled; assertTrue(cache.query(new SqlQuery<>(TestData.class, "from TestData use index() where check_scan_flag(?,false)") // Force full scan with USE INDEX() .setArgs(DirectPageScanIndexing.expectedDataPageScanEnabled) .setDataPageScanEnabled(DirectPageScanIndexing.expectedDataPageScanEnabled)) .getAll().isEmpty()); checkSqlLastFindDataPageScan(dataPageScanEnabled); }
protected void checkCacheOperations(IgniteCache<Integer, Value> cache) throws Exception { super.checkCacheOperations(cache); // Check SQL API. String sql = "fieldVal >= ? and fieldVal <= ?"; List<Cache.Entry<Integer, Value>> res = cache.query( new SqlQuery<Integer, Value>(Value.class, sql).setArgs(1, 100)).getAll(); assertNotNull(res); assertEquals(100, res.size()); } }
/** {@inheritDoc} */ @Override public List<Cache.Entry<Integer, GridCacheQueryTestValue>> call() throws Exception { IgniteCache<Integer, GridCacheQueryTestValue> c = ignite.cache(CACHE_NAME); String sqlStr = "FROM GridCacheQueryTestValue WHERE fieldname = ?"; SqlQuery<Integer, GridCacheQueryTestValue> sql = new SqlQuery<>(GridCacheQueryTestValue.class, sqlStr); sql.setArgs("C"); return c.query(sql.setSql(sqlStr)).getAll(); } }
/** */ @Test public void testNonCollocatedRetryMessage() { SqlQuery<String, JoinSqlTestHelper.Person> qry = new SqlQuery<String, JoinSqlTestHelper.Person>( JoinSqlTestHelper.Person.class, JoinSqlTestHelper.JOIN_SQL).setArgs("Organization #0"); qry.setDistributedJoins(true); try { List<Cache.Entry<String, JoinSqlTestHelper.Person>> prsns = personCache.query(qry).getAll(); fail("No CacheException emitted. Collection size=" + prsns.size()); } catch (CacheException e) { assertTrue(e.getMessage(), e.getMessage().contains("Failed to execute non-collocated query")); } }
/** */ @Test public void testDisappearedCacheWasNotFoundMessage() { SqlQuery<String, Person> qry = new SqlQuery<String, Person>(Person.class, JoinSqlTestHelper.JOIN_SQL).setArgs("Organization #0"); qry.setDistributedJoins(true); try { personCache.query(qry).getAll(); fail("No CacheException emitted."); } catch (CacheException e) { boolean exp = e.getMessage().contains("Cache not found on local node (was concurrently destroyed?)"); if (!exp) throw e; } }
/** * @param caches Cache names to invoke a query against to. * @param node Ignite node. */ private void checkPersonsQuery(String[] caches, Ignite node) { SqlQuery<Integer, Person> qry = new SqlQuery<>( Person.class, "SELECT p.* FROM Person p WHERE p.lname=? ORDER BY p.fname"); for (String cacheName : caches) { IgniteCache<Object, Object> cache = node.cache(cacheName); List<Cache.Entry<Integer, Person>> persons = cache.query(qry.setArgs(cacheName)).getAll(); for (int i = 0; i < entriesCount(); i++) assertEquals(new Person("" + i, cacheName), persons.get(i).getValue()); assertEquals(entriesCount(), persons.size()); } }
/** */ @Test public void testDisappearedCacheCauseRetryMessage() { SqlQuery<String, JoinSqlTestHelper.Person> qry = new SqlQuery<String, JoinSqlTestHelper.Person>(JoinSqlTestHelper.Person.class, JoinSqlTestHelper.JOIN_SQL).setArgs("Organization #0"); qry.setDistributedJoins(true); try { personCache.query(qry).getAll(); fail("No CacheException emitted."); } catch (CacheException e) { assertTrue(e.getMessage(), e.getMessage().contains("Failed to reserve partitions for query (cache is not found on local node) [")); } }
/** @throws Exception If failed. */ @Test public void testQuery() throws Exception { IgniteCache<Integer, ObjectValue> cache = ignite.cache(DEFAULT_CACHE_NAME); int cnt = 10; for (int i = 0; i < cnt; i++) cache.getAndPut(i, new ObjectValue("test" + i, i)); for (int i = 0; i < cnt; i++) { assertNotNull(cache.localPeek(i, ONHEAP_PEEK_MODES)); cache.localEvict(Collections.singleton(i)); // Swap. } SqlQuery<Integer, ObjectValue> qry = new SqlQuery(ObjectValue.class, "intVal >= ? order by intVal"); assertEquals(10, cache.query(qry.setArgs(0)).getAll().size()); } }
/** * Ctor. * * @param reader Reader. */ public ClientCacheSqlQueryRequest(BinaryRawReaderEx reader) { super(reader); qry = new SqlQuery(reader.readString(), reader.readString()) .setArgs(PlatformCache.readQueryArgs(reader)) .setDistributedJoins(reader.readBoolean()) .setLocal(reader.readBoolean()) .setReplicatedOnly(reader.readBoolean()) .setPageSize(reader.readInt()) .setTimeout((int) reader.readLong(), TimeUnit.MILLISECONDS); }
/** * JUnit. * * @throws Exception In case of error. */ @Test public void testIntegerType() throws Exception { IgniteCache<Integer, Integer> cache = jcache(Integer.class, Integer.class); int key = 898; int val = 2; cache.put(key, val); QueryCursor<Cache.Entry<Integer, Integer>> qry = cache.query(new SqlQuery<Integer, Integer>(Integer.class, "_key = ? and _val > 1").setArgs(key)); Cache.Entry<Integer, Integer> entry = F.first(qry.getAll()); assert entry != null; assertEquals(key, entry.getKey().intValue()); assertEquals(val, entry.getValue().intValue()); }
/** * @throws IgniteCheckedException if failed. */ @Test public void testBadHashObjectKey() throws IgniteCheckedException { IgniteCache<BadHashKeyObject, Byte> cache = jcache(BadHashKeyObject.class, Byte.class); cache.put(new BadHashKeyObject("test_key1"), (byte)1); cache.put(new BadHashKeyObject("test_key0"), (byte)10); cache.put(new BadHashKeyObject("test_key1"), (byte)7); assertEquals(10, cache.query(new SqlQuery<BadHashKeyObject, Byte>(Byte.class, "_key = ?"). setArgs(new BadHashKeyObject("test_key0"))).getAll().get(0).getValue().intValue()); }
/** * JUnit. * * @throws Exception In case of error. */ @Test public void testObjectWithString() throws Exception { IgniteCache<Integer, ObjectValue2> cache = jcache(Integer.class, ObjectValue2.class); cache.put(1, new ObjectValue2("value 1")); cache.put(2, new ObjectValue2("value 2")); cache.put(3, new ObjectValue2("value 3")); QueryCursor<Cache.Entry<Integer, ObjectValue2>> qry = cache.query(new SqlQuery<Integer, ObjectValue2>(ObjectValue2.class, "strVal like ?").setArgs("value%")); int expCnt = 3; List<Cache.Entry<Integer, ObjectValue2>> results = qry.getAll(); assertEquals(expCnt, results.size()); qry = cache.query(new SqlQuery<Integer, ObjectValue2>(ObjectValue2.class, "strVal > ?").setArgs("value 1")); results = qry.getAll(); assertEquals(expCnt - 1, results.size()); qry = cache.query(new TextQuery<Integer, ObjectValue2>(ObjectValue2.class, "value")); results = qry.getAll(); assertEquals(0, results.size()); //Should fail for FULL_TEXT index, but SORTED }
/** * Assert SQL simple data state. * * @param node Node. * @param sql SQL query. * @param expSize Expected size. */ protected static void assertSqlCompositeData(Ignite node, String sql, int expSize) { SqlQuery qry = new SqlQuery(typeName(ValueClass.class), sql).setArgs(SQL_ARG_1, SQL_ARG_2); 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_2); assertEquals(id, field1); assertEquals(id, field2); assertTrue(ids.add(id)); } assertEquals("Size mismatch [exp=" + expSize + ", actual=" + res.size() + ", ids=" + ids + ']', expSize, res.size()); }
/** * Reads sql query. * * @param reader Binary reader. * @return Query. */ private Query readSqlQuery(BinaryRawReaderEx reader) { boolean loc = reader.readBoolean(); String sql = reader.readString(); String typ = reader.readString(); final int pageSize = reader.readInt(); Object[] args = readQueryArgs(reader); boolean distrJoins = reader.readBoolean(); int timeout = reader.readInt(); boolean replicated = reader.readBoolean(); return new SqlQuery(typ, sql) .setPageSize(pageSize) .setArgs(args) .setLocal(loc) .setDistributedJoins(distrJoins) .setTimeout(timeout, TimeUnit.MILLISECONDS) .setReplicatedOnly(replicated); }
/** * @throws Exception If failed. */ @Test public void testArray() throws Exception { IgniteCache<Integer, ArrayObject> cache = jcache(Integer.class, ArrayObject.class); cache.put(1, new ArrayObject(new Long[] {1L, null, 3L})); cache.put(2, new ArrayObject(new Long[] {4L, 5L, 6L})); QueryCursor<Cache.Entry<Integer, ArrayObject>> q = cache.query(new SqlQuery<Integer, ArrayObject>(ArrayObject.class, "array_contains(arr, cast(? as long))"). setArgs(4)); Collection<Cache.Entry<Integer, ArrayObject>> res = q.getAll(); assertEquals(1, res.size()); Cache.Entry<Integer, ArrayObject> e = F.first(res); assertEquals(2, (int)e.getKey()); assertArrayEquals(new Long[]{4L, 5L, 6L}, e.getValue().arr); }