@Override public void run() { IgniteCache<Object, Object> cache = grid(ThreadLocalRandom.current().nextInt(4)) .cache(DEFAULT_CACHE_NAME); try (FieldsQueryCursor<List<?>> cur = cache.query( new SqlFieldsQuery("INSERT INTO MvccTestSqlIndexValue (_key, idxVal1) values (4,4),(5,5),(6,6)"))) { assertEquals(3L, cur.iterator().next().get(0)); } } }).get(TX_TIMEOUT);
@Override public void run() { IgniteEx node = grid(0); try { try (Transaction tx = node.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) { tx.timeout(TX_TIMEOUT); barrier.await(TX_TIMEOUT, TimeUnit.MILLISECONDS); IgniteCache<Object, Object> cache0 = node.cache(DEFAULT_CACHE_NAME); SqlFieldsQuery qry; synchronized (barrier) { qry = new SqlFieldsQuery("SELECT * FROM Integer"); try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) { assertEquals(1, cur.getAll().size()); } } barrier.await(TX_TIMEOUT, TimeUnit.MILLISECONDS); qry = new SqlFieldsQuery("UPDATE Integer SET _val = (_key * 10)"); try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) { assertEquals(1L, cur.iterator().next().get(0)); } tx.commit(); } } catch (Exception e) { onException(ex, e); } } }, 2, "tx-thread");
/** * Test query without caches. * * @throws Exception If failed. */ @Test public void testQueryWithoutCacheOnPublicSchema() throws Exception { GridQueryProcessor qryProc = node.context().query(); SqlFieldsQuery qry = new SqlFieldsQuery("SELECT 1").setSchema("PUBLIC"); List<List<?>> res = qryProc.querySqlFields(qry, true).getAll(); assertEquals(1, res.size()); assertEquals(1, res.get(0).size()); assertEquals(1, res.get(0).get(0)); Iterator<List<?>> iter = qryProc.querySqlFields(qry, true).iterator(); assertTrue(iter.hasNext()); List<?> row = iter.next(); assertEquals(1, row.size()); assertEquals(1, row.get(0)); assertFalse(iter.hasNext()); }
/** * Internal check routine. * * @param cls Entity class. * @throws Exception If failed. */ @SuppressWarnings("unchecked") private void check(Class cls) throws Exception { cache.put(1, createInstance(cls, 10)); cache.put(2, createInstance(cls, 20)); cache.put(3, createInstance(cls, 30)); Iterator iter = cache.query(new SqlQuery(cls, "val=20")).iterator(); assert iter.hasNext(); Cache.Entry res = (Cache.Entry)iter.next(); assertEquals(2, res.getKey()); assertEquals(Integer.valueOf(20), U.field(res.getValue(), "val")); assert !iter.hasNext(); iter = cache.query( new SqlFieldsQuery("SELECT p.val FROM " + cls.getSimpleName() + " p WHERE p.val=20")).iterator(); assert iter.hasNext(); List<Object> fieldsRes = (List<Object>)iter.next(); assertEquals(20, fieldsRes.get(0)); assert !iter.hasNext(); }
/** * Test query without caches. * * @throws Exception If failed. */ @Test public void testQueryWithoutCacheOnCacheSchema() throws Exception { node.createCache(new CacheConfiguration<PersonKey, Person>() .setName(CACHE_PERSON) .setIndexedTypes(PersonKey.class, Person.class)); GridQueryProcessor qryProc = node.context().query(); SqlFieldsQuery qry = new SqlFieldsQuery("SELECT 1").setSchema(CACHE_PERSON); List<List<?>> res = qryProc.querySqlFields(qry, true).getAll(); assertEquals(1, res.size()); assertEquals(1, res.get(0).size()); assertEquals(1, res.get(0).get(0)); Iterator<List<?>> iter = qryProc.querySqlFields(qry, true).iterator(); assertTrue(iter.hasNext()); List<?> row = iter.next(); assertEquals(1, row.size()); assertEquals(1, row.get(0)); assertFalse(iter.hasNext()); }
/** * Check auto clenup running queries on fully readed iterator. * * @throws Exception Exception in case of failure. */ @Test public void testAutoCloseQueryAfterIteratorIsExhausted(){ IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME); for (int i = 0; i < 100; i++) cache.put(i, i); FieldsQueryCursor<List<?>> query = cache.query(new SqlFieldsQuery("SELECT * FROM Integer order by _key")); query.iterator().forEachRemaining((e) -> { Assert.assertEquals("Should be one running query", 1, ignite.context().query().runningQueries(-1).size()); }); assertNoRunningQueries(); }
/** * @throws Exception On failed. */ @Test public void testDetachedConnectionOfLocalQueryOnNodeRestart() throws Exception { for (int i = 0; i < ITERS; ++i) { startGridAndPopulateCache(1); IgniteCache cache = grid(0).cache(CACHE_NAME); // Execute unfinished & finished queries. cache.query(new SqlFieldsQuery("select * from String").setLocal(true)).iterator().next(); cache.query(new SqlFieldsQuery("select * from String").setLocal(true)).getAll(); cache.query(new SqlFieldsQuery("select * from String").setLocal(true)).iterator().next(); stopAllGrids(); U.sleep(50); } stopAllGrids(); checkAllConnectionAreClosed(); }
/** * @throws Exception If failed. */ @Test public void testQueryInsertStaticCacheImplicit() throws Exception { ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT) .setIndexedTypes(Integer.class, Integer.class); startGridsMultiThreaded(4); Random rnd = ThreadLocalRandom.current(); Ignite checkNode = grid(rnd.nextInt(4)); Ignite updateNode = grid(rnd.nextInt(4)); IgniteCache cache = checkNode.cache(DEFAULT_CACHE_NAME); SqlFieldsQuery qry = new SqlFieldsQuery("INSERT INTO Integer (_key, _val) values (1,1),(2,2),(3,3)") .setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS); IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME); try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) { assertEquals(3L, cur.iterator().next().get(0)); } assertEquals(1, cache.get(1)); assertEquals(2, cache.get(2)); assertEquals(3, cache.get(3)); }
/** */ @Test public void testReplicated() { sql("CREATE TABLE repl_tbl (id LONG PRIMARY KEY, val LONG) WITH \"template=replicated\"").getAll(); try { for (int i = 0; i < 10; i++) sql("insert into repl_tbl(id,val) VALUES(" + i + "," + i + ")").getAll(); Iterator<List<?>> it0 = sql(new SqlFieldsQuery("SELECT * FROM repl_tbl where id > ?").setArgs(1)).iterator(); it0.next(); sql(new SqlFieldsQuery("SELECT * FROM repl_tbl where id > ?").setArgs(1)).getAll(); it0.next(); } finally { sql("DROP TABLE repl_tbl").getAll(); } }
/** * @throws Exception If failed. */ @Test public void testQueryReducerMergeValues() throws Exception { ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT) .setIndexedTypes(Integer.class, CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue.class); startGridsMultiThreaded(4); Random rnd = ThreadLocalRandom.current(); Ignite node = grid(rnd.nextInt(4)); IgniteCache<Object, CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue> cache = node.cache(DEFAULT_CACHE_NAME); cache.put(1, new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(1)); cache.put(3, new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(3)); SqlFieldsQuery qry = new SqlFieldsQuery("MERGE INTO MvccTestSqlIndexValue (_key, idxVal1)" + " values (1,?),(2,?),(3,?)"); qry.setArgs(1, 4, 6); try (FieldsQueryCursor<List<?>> cur = cache.query(qry)) { assertEquals(3L, cur.iterator().next().get(0)); } assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(1), cache.get(1)); assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(4), cache.get(2)); assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(6), cache.get(3)); qry = new SqlFieldsQuery("MERGE INTO MvccTestSqlIndexValue (_key, idxVal1) values (4,4)"); try (FieldsQueryCursor<List<?>> cur = cache.query(qry)) { assertEquals(1L, cur.iterator().next().get(0)); } assertEquals(new CacheMvccSqlTxQueriesAbstractTest.MvccTestSqlIndexValue(4), cache.get(4)); }
/** * @throws Exception If failed. */ @Test public void testSelectProducesTransaction() throws Exception { ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT) .setIndexedTypes(Integer.class, MvccTestSqlIndexValue.class); startGridsMultiThreaded(4); Random rnd = ThreadLocalRandom.current(); Ignite node = grid(rnd.nextInt(4)); IgniteCache<Object, Object> cache = node.cache(DEFAULT_CACHE_NAME); SqlFieldsQuery qry = new SqlFieldsQuery("INSERT INTO MvccTestSqlIndexValue (_key, idxVal1) values (1,1),(2,2),(3,3)"); try (FieldsQueryCursor<List<?>> cur = cache.query(qry)) { assertEquals(3L, cur.iterator().next().get(0)); } SqlFieldsQueryEx qryEx = new SqlFieldsQueryEx("SELECT * FROM MvccTestSqlIndexValue", true); qryEx.setAutoCommit(false); try (FieldsQueryCursor<List<?>> cur = cache.query(qryEx)) { assertEquals(3, cur.getAll().size()); } try (GridNearTxLocal tx = cache.unwrap(IgniteEx.class).context().cache().context().tm().userTx()) { assertNotNull(tx); } }
/** */ @Test public void testLocalQuery() { sql("CREATE TABLE tbl (id LONG PRIMARY KEY, val LONG)").getAll(); try { for (int i = 0; i < 10; i++) sql("insert into tbl(id,val) VALUES(" + i + "," + i + ")").getAll(); Iterator<List<?>> it0 = sql( new SqlFieldsQuery("SELECT * FROM tbl where id > ?") .setArgs(1) .setLocal(true)) .iterator(); it0.next(); sql(new SqlFieldsQuery("SELECT * FROM tbl where id > ?").setArgs(1).setLocal(true)).getAll(); it0.next(); } finally { sql("DROP TABLE tbl").getAll(); } }
/** * @throws Exception If failed. */ @Test public void testQueryInsertSubqueryImplicit() throws Exception { ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT) .setIndexedTypes(Integer.class, Integer.class, Integer.class, MvccTestSqlIndexValue.class); startGridsMultiThreaded(4); awaitPartitionMapExchange(); Random rnd = ThreadLocalRandom.current(); Ignite checkNode = grid(rnd.nextInt(4)); Ignite updateNode = grid(rnd.nextInt(4)); IgniteCache cache = checkNode.cache(DEFAULT_CACHE_NAME); cache.putAll(F.asMap( 1, new MvccTestSqlIndexValue(1), 2, new MvccTestSqlIndexValue(2), 3, new MvccTestSqlIndexValue(3))); SqlFieldsQuery qry = new SqlFieldsQuery("INSERT INTO Integer (_key, _val)" + " SELECT _key * 10, idxVal1 FROM MvccTestSqlIndexValue") .setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS); IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME); try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) { assertEquals(3L, cur.iterator().next().get(0)); } assertEquals(1, cache0.get(10)); assertEquals(2, cache0.get(20)); assertEquals(3, cache0.get(30)); }
/** * @throws Exception If failed. */ @Test public void testQueryUpdateSubqueryImplicit() throws Exception { ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT) .setIndexedTypes(Integer.class, Integer.class, Integer.class, MvccTestSqlIndexValue.class); startGridsMultiThreaded(4); awaitPartitionMapExchange(); Random rnd = ThreadLocalRandom.current(); Ignite checkNode = grid(rnd.nextInt(4)); Ignite updateNode = grid(rnd.nextInt(4)); IgniteCache<Object, Object> cache = checkNode.cache(DEFAULT_CACHE_NAME); cache.putAll(F.asMap( 1, new MvccTestSqlIndexValue(1), 2, new MvccTestSqlIndexValue(2), 3, new MvccTestSqlIndexValue(3))); SqlFieldsQuery qry = new SqlFieldsQuery("UPDATE MvccTestSqlIndexValue AS t " + "SET (idxVal1) = (SELECT idxVal1*10 FROM MvccTestSqlIndexValue WHERE t._key = _key)") .setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS); IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME); try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) { assertEquals(3L, cur.iterator().next().get(0)); } assertEquals(10, ((MvccTestSqlIndexValue)cache.get(1)).idxVal1); assertEquals(20, ((MvccTestSqlIndexValue)cache.get(2)).idxVal1); assertEquals(30, ((MvccTestSqlIndexValue)cache.get(3)).idxVal1); }
/** * @throws Exception If failed. */ @Test public void testQueryInsertUpdateSameKeys() throws Exception { ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT) .setIndexedTypes(Integer.class, Integer.class); startGridsMultiThreaded(4); Random rnd = ThreadLocalRandom.current(); Ignite checkNode = grid(rnd.nextInt(4)); final Ignite updateNode = grid(rnd.nextInt(4)); IgniteCache cache = checkNode.cache(DEFAULT_CACHE_NAME); try (Transaction tx = updateNode.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) { tx.timeout(TX_TIMEOUT); SqlFieldsQuery qry = new SqlFieldsQuery("INSERT INTO Integer (_key, _val) values (1,1),(2,2),(3,3)"); IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME); try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) { assertEquals(3L, cur.iterator().next().get(0)); } qry = new SqlFieldsQuery("UPDATE Integer SET _val = (_key * 10)"); try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) { cur.getAll(); } tx.commit(); } assertEquals(10, cache.get(1)); assertEquals(20, cache.get(2)); assertEquals(30, cache.get(3)); }
/** * @throws Exception If failed. */ @Test public void testQueryDeleteStaticCacheImplicit() throws Exception { ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT) .setIndexedTypes(Integer.class, Integer.class); startGridsMultiThreaded(4); Random rnd = ThreadLocalRandom.current(); Ignite checkNode = grid(rnd.nextInt(4)); Ignite updateNode = grid(rnd.nextInt(4)); IgniteCache cache = checkNode.cache(DEFAULT_CACHE_NAME); cache.putAll(F.asMap(1, 1, 2, 2, 3, 3)); assertEquals(1, cache.get(1)); assertEquals(2, cache.get(2)); assertEquals(3, cache.get(3)); SqlFieldsQuery qry = new SqlFieldsQuery("DELETE FROM Integer WHERE 1 = 1") .setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS); IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME); try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) { assertEquals(3L, cur.iterator().next().get(0)); } assertNull(cache.get(1)); assertNull(cache.get(2)); assertNull(cache.get(3)); }
/** * @throws Exception If failed. */ @Test public void testQueryUpdateStaticCacheImplicit() throws Exception { ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT) .setIndexedTypes(Integer.class, Integer.class); startGridsMultiThreaded(4); Random rnd = ThreadLocalRandom.current(); Ignite checkNode = grid(rnd.nextInt(4)); Ignite updateNode = grid(rnd.nextInt(4)); IgniteCache cache = checkNode.cache(DEFAULT_CACHE_NAME); cache.putAll(F.asMap(1, 1, 2, 2, 3, 3)); assertEquals(1, cache.get(1)); assertEquals(2, cache.get(2)); assertEquals(3, cache.get(3)); SqlFieldsQuery qry = new SqlFieldsQuery("UPDATE Integer SET _val = (_key * 10)") .setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS); IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME); try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) { assertEquals(3L, cur.iterator().next().get(0)); } assertEquals(10, cache.get(1)); assertEquals(20, cache.get(2)); assertEquals(30, cache.get(3)); }
assertEquals(3L, cur.iterator().next().get(0));
/** * @throws Exception If failed. */ @Test public void testQueryUpdateStaticCache() throws Exception { ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT) .setIndexedTypes(Integer.class, Integer.class); startGridsMultiThreaded(4); Random rnd = ThreadLocalRandom.current(); Ignite checkNode = grid(rnd.nextInt(4)); Ignite updateNode = grid(rnd.nextInt(4)); IgniteCache cache = checkNode.cache(DEFAULT_CACHE_NAME); cache.putAll(F.asMap(1, 1, 2, 2, 3, 3)); assertEquals(1, cache.get(1)); assertEquals(2, cache.get(2)); assertEquals(3, cache.get(3)); try (Transaction tx = updateNode.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) { tx.timeout(TX_TIMEOUT); SqlFieldsQuery qry = new SqlFieldsQuery("UPDATE Integer SET _val = (_key * 10)"); IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME); try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) { assertEquals(3L, cur.iterator().next().get(0)); } tx.commit(); } assertEquals(10, cache.get(1)); assertEquals(20, cache.get(2)); assertEquals(30, cache.get(3)); }
/** * @throws Exception If failed. */ @Test public void testQueryInsertClientImplicit() throws Exception { ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT) .setIndexedTypes(Integer.class, Integer.class); startGrid(0); client = true; startGrid(1); awaitPartitionMapExchange(); Ignite checkNode = grid(0); Ignite updateNode = grid(1); IgniteCache cache = checkNode.cache(DEFAULT_CACHE_NAME); SqlFieldsQuery qry = new SqlFieldsQuery("INSERT INTO Integer (_key, _val) values (1,1),(2,2),(3,3)") .setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS); IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME); try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) { assertEquals(3L, cur.iterator().next().get(0)); } assertEquals(1, cache.get(1)); assertEquals(2, cache.get(2)); assertEquals(3, cache.get(3)); }