timoninmaxim commented on a change in pull request #9118: URL: https://github.com/apache/ignite/pull/9118#discussion_r660684442
########## File path: modules/indexing/src/test/java/org/apache/ignite/cache/query/MultifieldIndexQueryTest.java ########## @@ -0,0 +1,598 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.cache.query; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Comparator; +import java.util.List; +import java.util.Objects; +import java.util.Random; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.LongStream; +import javax.cache.Cache; +import javax.cache.CacheException; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.IgniteDataStreamer; +import org.apache.ignite.cache.query.annotations.QuerySqlField; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.testframework.GridTestUtils; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.between; +import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.eq; +import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.gt; +import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.gte; +import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lt; +import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lte; + +/** */ +@RunWith(Parameterized.class) +public class MultifieldIndexQueryTest extends GridCommonAbstractTest { + /** */ + private static final String CACHE = "TEST_CACHE"; + + /** */ + private static final String INDEX = "TEST_IDX"; + + /** */ + private static final String DESC_INDEX = "TEST_DESC_IDX"; + + /** */ + private static final int CNT = 10_000; + + /** */ + @Parameterized.Parameter(0) + public int nodesCnt; + + /** */ + private Ignite ignite; + + /** */ + private IgniteCache cache; + + /** */ + @Parameterized.Parameters(name = "nodesCnt={0}") + public static Collection<Object[]> testParams() { + return Arrays.asList( + new Object[] {1}, + new Object[] {2}); + } + + /** {@inheritDoc} */ + @Override protected void beforeTest() throws Exception { + ignite = startGrids(nodesCnt); + + cache = ignite.cache(CACHE); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() { + stopAllGrids(); + } + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); + + CacheConfiguration<?, ?> ccfg = new CacheConfiguration<>() + .setName("TEST_CACHE") + .setIndexedTypes(Long.class, Person.class) + .setQueryParallelism(1); + + cfg.setCacheConfiguration(ccfg); + + return cfg; + } + + /** */ + @Test + public void testQueryKeyPKIndex() { + insertData(); + + int pivot = new Random().nextInt(CNT); + + IndexQuery<Long, Person> qry = new IndexQuery<Long, Person>(Person.class, "_key_PK") + .setCriteria(lt("_KEY", (long) pivot)); + + checkPerson(cache.query(qry), 0, pivot); + } + + /** */ + @Test + public void testEmptyCacheQuery() { + IndexQuery<Long, Person> qry = new IndexQuery<Long, Person>(Person.class) + .setCriteria(lt("id", Integer.MAX_VALUE), lt("secId", Integer.MAX_VALUE)); + + QueryCursor<Cache.Entry<Long, Person>> cursor = cache.query(qry); + + assertTrue(cursor.getAll().isEmpty()); + + // Check same query with specify index name. + qry = new IndexQuery<Long, Person>(Person.class, INDEX) + .setCriteria(lt("id", Integer.MAX_VALUE), lt("secId", Integer.MAX_VALUE)); + + assertTrue(cache.query(qry).getAll().isEmpty()); + } + + /** */ + @Test + public void testCheckBoundaries() { + cache.put(1L, new Person(0, 1)); + cache.put(2L, new Person(1, 0)); + cache.put(3L, new Person(1, 1)); + + IndexQuery<Long, Person> qry = new IndexQuery<Long, Person>(Person.class) + .setCriteria(between("id", 0, 1), eq("secId", 1)); + + List<Cache.Entry<Long, Person>> result = cache.query(qry).getAll(); + + assertEquals(2, result.size()); + + result.sort(Comparator.comparingLong(Cache.Entry::getKey)); + + assertEquals(1L, (long) result.get(0).getKey()); + assertEquals(3L, (long) result.get(1).getKey()); + + assertEquals(new Person(0, 1), result.get(0).getValue()); + assertEquals(new Person(1, 1), result.get(1).getValue()); + } + + /** */ + @Test + public void testLtQueryMultipleField() { + insertData(); + + int pivot = new Random().nextInt(CNT); + + // Should return empty result for ID that less any inserted. + IndexQuery<Long, Person> qry = new IndexQuery<Long, Person>(Person.class) + .setCriteria(lt("id", -1), lt("secId", pivot)); + + assertTrue(cache.query(qry).getAll().isEmpty()); + + // Should return all data for ID and SECID that greater any inserted. Review comment: Greater is related to specified value in query. We set ID = 1 that greater that any inserted (id = 0 for all rows). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
