timoninmaxim commented on code in PR #10767:
URL: https://github.com/apache/ignite/pull/10767#discussion_r1222000811
##########
modules/core/src/main/java/org/apache/ignite/internal/processors/platform/client/cache/ClientCacheIndexQueryRequest.java:
##########
@@ -57,6 +57,8 @@ public ClientCacheIndexQueryRequest(BinaryRawReaderEx reader)
{
int part = reader.readInt();
+ int limit = reader.readInt();
Review Comment:
Ignite supports backward compatibility with older versions of thin clients.
Server should check version before parsing client requests. Please, take a look
on `ProtocolBitmaskFeature`.
##########
modules/core/src/main/java/org/apache/ignite/cache/query/IndexQuery.java:
##########
@@ -152,6 +155,30 @@ public String getIndexName() {
return idxName;
}
+ /**
+ * Gets limit to response records count.
+ *
+ * @return Limit value.
+ */
+ public int getLimit() {
+ return limit;
+ }
+
+ /**
+ * Sets limit to response records count.
+ *
+ * @param limit POsitive limit to set.
+ * @return {@code this} For chaining.
+ */
+ public IndexQuery<K, V> setLimit(int limit) {
+ if (limit <= 0)
Review Comment:
Please, use `A.ensure` for such checks
##########
modules/indexing/src/test/java/org/apache/ignite/cache/query/IndexQueryLimitTest.java:
##########
@@ -0,0 +1,274 @@
+/*
+ * 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.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+import java.util.Random;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.IntUnaryOperator;
+import javax.cache.Cache;
+import org.apache.ignite.Ignite;
+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.internal.processors.cache.query.QueryCursorEx;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+
+import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
+import static org.apache.ignite.cache.CacheMode.REPLICATED;
+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.in;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lt;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lte;
+
+/** */
+public class IndexQueryLimitTest extends GridCommonAbstractTest {
+ /** */
+ private static final String CACHE = "TEST_CACHE";
+
+ /** */
+ private static final String IDX = "PERSON_ID_IDX";
+
+ /** */
+ private static final int CNT = 10_000;
+
+ /** */
+ private Ignite crd;
+
+ /** {@inheritDoc} */
+ @Override protected void beforeTest() throws Exception {
+ crd = startGrids(4);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() {
+ stopAllGrids();
+ }
+
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String
igniteInstanceName) throws Exception {
+ IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+ CacheConfiguration<Long, Person> ccfg = new CacheConfiguration<Long,
Person>()
+ .setName(CACHE)
+ .setIndexedTypes(Long.class, Person.class)
+ .setAtomicityMode(TRANSACTIONAL)
+ .setCacheMode(REPLICATED)
+ .setQueryParallelism(1)
+ .setBackups(0);
+
+ cfg.setCacheConfiguration(ccfg);
+
+ return cfg;
+ }
+
+ /** */
+ @Test
+ public void testRangeQueriesWithoutDuplicates() throws Exception {
+ checkRangeQueries(1);
+ }
+
+ /** */
+ @Test
+ public void testRangeQueriesWithDuplicates() throws Exception {
+ checkRangeQueries(10);
+ }
+
+ /** */
+ @Test
+ public void testSetLimit() throws Exception {
+ GridTestUtils.assertThrows(log, () -> new IndexQuery<>(Person.class,
IDX).setLimit(0),
+ IllegalArgumentException.class, "Limit must be positive.");
+
+ int limit = 1 + new Random().nextInt(1000);
+
+ GridTestUtils.assertThrows(log, () -> new IndexQuery<>(Person.class,
IDX).setLimit(0 - limit),
+ IllegalArgumentException.class, "Limit must be positive.");
+
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+
+ qry.setLimit(limit);
+
+ assertEquals(limit, qry.getLimit());
+ }
+
+ /** */
+ private void checkRangeQueries(int duplicates) throws Exception {
+
+ // Query empty cache.
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+
+ assertTrue(crd.cache(CACHE).query(qry).getAll().isEmpty());
+
+ // Add data
+ insertData(duplicates);
+
+ // All
+ checkLimit(null, 0, CNT, duplicates);
+
+ // Range queries.
+ String fld = "id";
+
+ int pivot = new Random().nextInt(CNT);
+
+ // Eq.
+ checkLimit(eq(fld, pivot), pivot, pivot + 1, duplicates);
+
+ // Lt.
+ checkLimit(lt(fld, pivot), 0, pivot, duplicates);
+
+ // Lte.
+ checkLimit(lte(fld, pivot), 0, pivot + 1, duplicates);
+
+ // Gt.
+ checkLimit(gt(fld, pivot), pivot + 1, CNT, duplicates);
+
+ // Gte.
+ checkLimit(gte(fld, pivot), pivot, CNT, duplicates);
+
+ // Between.
+ int lower = new Random().nextInt(CNT / 2);
+ int upper = lower + CNT / 20;
+
+ checkLimit(between(fld, lower, upper), lower, upper + 1, duplicates);
+
+ // In.
+ checkLimit(in(fld, F.asList(pivot, pivot + 1)), pivot, pivot + 2,
duplicates);
+ }
+
+ /** */
+ private void checkLimit(IndexQueryCriterion criterion, int left, int
right, int duplicates) throws Exception {
+ int rows = right - left;
+ int limit = new Random().nextInt(rows + 1 - 1) + 1;
+
+ // limit < rows
+ checkLimit(criterion, limit, left, left + limit, duplicates);
+
+ // limit >= rows
+ if (rows > 1) {
+ limit = new Random().nextInt(CNT + 2 - rows) + rows;
+ checkLimit(criterion, limit, left, right, duplicates);
+ }
+ }
+
+ /** */
+ private void checkLimit(IndexQueryCriterion criterion, int limit, int
left, int right, int duplicates) throws Exception {
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+ if (criterion != null) {
Review Comment:
Please add empty line here
##########
modules/indexing/src/test/java/org/apache/ignite/cache/query/IndexQueryLimitTest.java:
##########
@@ -0,0 +1,274 @@
+/*
+ * 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.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+import java.util.Random;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.IntUnaryOperator;
+import javax.cache.Cache;
+import org.apache.ignite.Ignite;
+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.internal.processors.cache.query.QueryCursorEx;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+
+import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
+import static org.apache.ignite.cache.CacheMode.REPLICATED;
+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.in;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lt;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lte;
+
+/** */
+public class IndexQueryLimitTest extends GridCommonAbstractTest {
+ /** */
+ private static final String CACHE = "TEST_CACHE";
+
+ /** */
+ private static final String IDX = "PERSON_ID_IDX";
+
+ /** */
+ private static final int CNT = 10_000;
+
+ /** */
+ private Ignite crd;
+
+ /** {@inheritDoc} */
+ @Override protected void beforeTest() throws Exception {
+ crd = startGrids(4);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() {
+ stopAllGrids();
+ }
+
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String
igniteInstanceName) throws Exception {
+ IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+ CacheConfiguration<Long, Person> ccfg = new CacheConfiguration<Long,
Person>()
+ .setName(CACHE)
+ .setIndexedTypes(Long.class, Person.class)
+ .setAtomicityMode(TRANSACTIONAL)
+ .setCacheMode(REPLICATED)
+ .setQueryParallelism(1)
+ .setBackups(0);
+
+ cfg.setCacheConfiguration(ccfg);
+
+ return cfg;
+ }
+
+ /** */
+ @Test
+ public void testRangeQueriesWithoutDuplicates() throws Exception {
+ checkRangeQueries(1);
+ }
+
+ /** */
+ @Test
+ public void testRangeQueriesWithDuplicates() throws Exception {
+ checkRangeQueries(10);
+ }
+
+ /** */
+ @Test
+ public void testSetLimit() throws Exception {
+ GridTestUtils.assertThrows(log, () -> new IndexQuery<>(Person.class,
IDX).setLimit(0),
+ IllegalArgumentException.class, "Limit must be positive.");
+
+ int limit = 1 + new Random().nextInt(1000);
+
+ GridTestUtils.assertThrows(log, () -> new IndexQuery<>(Person.class,
IDX).setLimit(0 - limit),
+ IllegalArgumentException.class, "Limit must be positive.");
+
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+
+ qry.setLimit(limit);
+
+ assertEquals(limit, qry.getLimit());
+ }
+
+ /** */
+ private void checkRangeQueries(int duplicates) throws Exception {
+
+ // Query empty cache.
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+
+ assertTrue(crd.cache(CACHE).query(qry).getAll().isEmpty());
+
+ // Add data
+ insertData(duplicates);
+
+ // All
+ checkLimit(null, 0, CNT, duplicates);
+
+ // Range queries.
+ String fld = "id";
+
+ int pivot = new Random().nextInt(CNT);
+
+ // Eq.
+ checkLimit(eq(fld, pivot), pivot, pivot + 1, duplicates);
+
+ // Lt.
+ checkLimit(lt(fld, pivot), 0, pivot, duplicates);
+
+ // Lte.
+ checkLimit(lte(fld, pivot), 0, pivot + 1, duplicates);
+
+ // Gt.
+ checkLimit(gt(fld, pivot), pivot + 1, CNT, duplicates);
+
+ // Gte.
+ checkLimit(gte(fld, pivot), pivot, CNT, duplicates);
+
+ // Between.
+ int lower = new Random().nextInt(CNT / 2);
+ int upper = lower + CNT / 20;
+
+ checkLimit(between(fld, lower, upper), lower, upper + 1, duplicates);
+
+ // In.
+ checkLimit(in(fld, F.asList(pivot, pivot + 1)), pivot, pivot + 2,
duplicates);
+ }
+
+ /** */
+ private void checkLimit(IndexQueryCriterion criterion, int left, int
right, int duplicates) throws Exception {
+ int rows = right - left;
+ int limit = new Random().nextInt(rows + 1 - 1) + 1;
+
+ // limit < rows
+ checkLimit(criterion, limit, left, left + limit, duplicates);
+
+ // limit >= rows
+ if (rows > 1) {
+ limit = new Random().nextInt(CNT + 2 - rows) + rows;
+ checkLimit(criterion, limit, left, right, duplicates);
+ }
+ }
+
+ /** */
+ private void checkLimit(IndexQueryCriterion criterion, int limit, int
left, int right, int duplicates) throws Exception {
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+ if (criterion != null) {
+ qry.setCriteria(criterion);
+ }
+ qry.setLimit(limit);
+
+ QueryCursor<Cache.Entry<Long, Person>> cursor =
crd.cache(CACHE).query(qry);
+
+ int expSize = (right - left) * duplicates;
+ if (limit > 0 && limit < expSize) {
+ expSize = limit;
+ }
+
+ Set<Long> expKeys = new HashSet<>(expSize);
+ List<Integer> expOrderedValues = new LinkedList<>();
+
+ IntUnaryOperator op = (i) -> i + 1;
+
+ loop: for (int i = left; i != right; i = op.applyAsInt(i)) {
+ for (int j = 0; j < duplicates; j++) {
+ expOrderedValues.add(i);
+ expKeys.add((long)CNT * j + i);
+ if (expOrderedValues.size() >= limit)
+ break loop;
+ }
+ }
+
+ AtomicInteger actSize = new AtomicInteger();
+
+ ((QueryCursorEx<Cache.Entry<Long, Person>>)cursor).getAll(entry -> {
+
Review Comment:
Please remove empty line here
##########
modules/indexing/src/test/java/org/apache/ignite/cache/query/IndexQueryLimitTest.java:
##########
@@ -0,0 +1,274 @@
+/*
+ * 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.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+import java.util.Random;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.IntUnaryOperator;
+import javax.cache.Cache;
+import org.apache.ignite.Ignite;
+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.internal.processors.cache.query.QueryCursorEx;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+
+import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
+import static org.apache.ignite.cache.CacheMode.REPLICATED;
+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.in;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lt;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lte;
+
+/** */
+public class IndexQueryLimitTest extends GridCommonAbstractTest {
+ /** */
+ private static final String CACHE = "TEST_CACHE";
+
+ /** */
+ private static final String IDX = "PERSON_ID_IDX";
+
+ /** */
+ private static final int CNT = 10_000;
+
+ /** */
+ private Ignite crd;
+
+ /** {@inheritDoc} */
+ @Override protected void beforeTest() throws Exception {
+ crd = startGrids(4);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() {
+ stopAllGrids();
+ }
+
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String
igniteInstanceName) throws Exception {
+ IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+ CacheConfiguration<Long, Person> ccfg = new CacheConfiguration<Long,
Person>()
+ .setName(CACHE)
+ .setIndexedTypes(Long.class, Person.class)
+ .setAtomicityMode(TRANSACTIONAL)
+ .setCacheMode(REPLICATED)
+ .setQueryParallelism(1)
+ .setBackups(0);
+
+ cfg.setCacheConfiguration(ccfg);
+
+ return cfg;
+ }
+
+ /** */
+ @Test
+ public void testRangeQueriesWithoutDuplicates() throws Exception {
+ checkRangeQueries(1);
+ }
+
+ /** */
+ @Test
+ public void testRangeQueriesWithDuplicates() throws Exception {
+ checkRangeQueries(10);
+ }
+
+ /** */
+ @Test
+ public void testSetLimit() throws Exception {
+ GridTestUtils.assertThrows(log, () -> new IndexQuery<>(Person.class,
IDX).setLimit(0),
+ IllegalArgumentException.class, "Limit must be positive.");
+
+ int limit = 1 + new Random().nextInt(1000);
+
+ GridTestUtils.assertThrows(log, () -> new IndexQuery<>(Person.class,
IDX).setLimit(0 - limit),
+ IllegalArgumentException.class, "Limit must be positive.");
+
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+
+ qry.setLimit(limit);
+
+ assertEquals(limit, qry.getLimit());
+ }
+
+ /** */
+ private void checkRangeQueries(int duplicates) throws Exception {
+
+ // Query empty cache.
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+
+ assertTrue(crd.cache(CACHE).query(qry).getAll().isEmpty());
+
+ // Add data
+ insertData(duplicates);
+
+ // All
+ checkLimit(null, 0, CNT, duplicates);
+
+ // Range queries.
+ String fld = "id";
+
+ int pivot = new Random().nextInt(CNT);
+
+ // Eq.
+ checkLimit(eq(fld, pivot), pivot, pivot + 1, duplicates);
Review Comment:
I suppose to check only one criterion (any) is enough
##########
modules/indexing/src/test/java/org/apache/ignite/cache/query/IndexQueryLimitTest.java:
##########
@@ -0,0 +1,274 @@
+/*
+ * 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.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+import java.util.Random;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.IntUnaryOperator;
+import javax.cache.Cache;
+import org.apache.ignite.Ignite;
+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.internal.processors.cache.query.QueryCursorEx;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+
+import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
+import static org.apache.ignite.cache.CacheMode.REPLICATED;
+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.in;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lt;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lte;
+
+/** */
+public class IndexQueryLimitTest extends GridCommonAbstractTest {
+ /** */
+ private static final String CACHE = "TEST_CACHE";
+
+ /** */
+ private static final String IDX = "PERSON_ID_IDX";
+
+ /** */
+ private static final int CNT = 10_000;
+
+ /** */
+ private Ignite crd;
+
+ /** {@inheritDoc} */
+ @Override protected void beforeTest() throws Exception {
+ crd = startGrids(4);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() {
+ stopAllGrids();
+ }
+
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String
igniteInstanceName) throws Exception {
+ IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+ CacheConfiguration<Long, Person> ccfg = new CacheConfiguration<Long,
Person>()
+ .setName(CACHE)
+ .setIndexedTypes(Long.class, Person.class)
+ .setAtomicityMode(TRANSACTIONAL)
+ .setCacheMode(REPLICATED)
+ .setQueryParallelism(1)
+ .setBackups(0);
+
+ cfg.setCacheConfiguration(ccfg);
+
+ return cfg;
+ }
+
+ /** */
+ @Test
+ public void testRangeQueriesWithoutDuplicates() throws Exception {
+ checkRangeQueries(1);
+ }
+
+ /** */
+ @Test
+ public void testRangeQueriesWithDuplicates() throws Exception {
+ checkRangeQueries(10);
+ }
+
+ /** */
+ @Test
+ public void testSetLimit() throws Exception {
+ GridTestUtils.assertThrows(log, () -> new IndexQuery<>(Person.class,
IDX).setLimit(0),
+ IllegalArgumentException.class, "Limit must be positive.");
+
+ int limit = 1 + new Random().nextInt(1000);
+
+ GridTestUtils.assertThrows(log, () -> new IndexQuery<>(Person.class,
IDX).setLimit(0 - limit),
+ IllegalArgumentException.class, "Limit must be positive.");
+
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+
+ qry.setLimit(limit);
+
+ assertEquals(limit, qry.getLimit());
+ }
+
+ /** */
+ private void checkRangeQueries(int duplicates) throws Exception {
+
+ // Query empty cache.
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+
+ assertTrue(crd.cache(CACHE).query(qry).getAll().isEmpty());
+
+ // Add data
+ insertData(duplicates);
+
+ // All
+ checkLimit(null, 0, CNT, duplicates);
+
+ // Range queries.
+ String fld = "id";
+
+ int pivot = new Random().nextInt(CNT);
+
+ // Eq.
+ checkLimit(eq(fld, pivot), pivot, pivot + 1, duplicates);
+
+ // Lt.
+ checkLimit(lt(fld, pivot), 0, pivot, duplicates);
+
+ // Lte.
+ checkLimit(lte(fld, pivot), 0, pivot + 1, duplicates);
+
+ // Gt.
+ checkLimit(gt(fld, pivot), pivot + 1, CNT, duplicates);
+
+ // Gte.
+ checkLimit(gte(fld, pivot), pivot, CNT, duplicates);
+
+ // Between.
+ int lower = new Random().nextInt(CNT / 2);
+ int upper = lower + CNT / 20;
+
+ checkLimit(between(fld, lower, upper), lower, upper + 1, duplicates);
+
+ // In.
+ checkLimit(in(fld, F.asList(pivot, pivot + 1)), pivot, pivot + 2,
duplicates);
+ }
+
+ /** */
+ private void checkLimit(IndexQueryCriterion criterion, int left, int
right, int duplicates) throws Exception {
+ int rows = right - left;
+ int limit = new Random().nextInt(rows + 1 - 1) + 1;
+
+ // limit < rows
+ checkLimit(criterion, limit, left, left + limit, duplicates);
+
+ // limit >= rows
+ if (rows > 1) {
+ limit = new Random().nextInt(CNT + 2 - rows) + rows;
+ checkLimit(criterion, limit, left, right, duplicates);
+ }
+ }
+
+ /** */
+ private void checkLimit(IndexQueryCriterion criterion, int limit, int
left, int right, int duplicates) throws Exception {
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+ if (criterion != null) {
+ qry.setCriteria(criterion);
+ }
+ qry.setLimit(limit);
+
+ QueryCursor<Cache.Entry<Long, Person>> cursor =
crd.cache(CACHE).query(qry);
+
+ int expSize = (right - left) * duplicates;
+ if (limit > 0 && limit < expSize) {
+ expSize = limit;
+ }
+
+ Set<Long> expKeys = new HashSet<>(expSize);
+ List<Integer> expOrderedValues = new LinkedList<>();
+
+ IntUnaryOperator op = (i) -> i + 1;
+
+ loop: for (int i = left; i != right; i = op.applyAsInt(i)) {
+ for (int j = 0; j < duplicates; j++) {
+ expOrderedValues.add(i);
+ expKeys.add((long)CNT * j + i);
Review Comment:
and here
##########
modules/indexing/src/test/java/org/apache/ignite/cache/query/IndexQueryLimitTest.java:
##########
@@ -0,0 +1,274 @@
+/*
+ * 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.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+import java.util.Random;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.IntUnaryOperator;
+import javax.cache.Cache;
+import org.apache.ignite.Ignite;
+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.internal.processors.cache.query.QueryCursorEx;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+
+import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
+import static org.apache.ignite.cache.CacheMode.REPLICATED;
+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.in;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lt;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lte;
+
+/** */
+public class IndexQueryLimitTest extends GridCommonAbstractTest {
+ /** */
+ private static final String CACHE = "TEST_CACHE";
+
+ /** */
+ private static final String IDX = "PERSON_ID_IDX";
+
+ /** */
+ private static final int CNT = 10_000;
+
+ /** */
+ private Ignite crd;
+
+ /** {@inheritDoc} */
+ @Override protected void beforeTest() throws Exception {
+ crd = startGrids(4);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() {
+ stopAllGrids();
+ }
+
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String
igniteInstanceName) throws Exception {
+ IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+ CacheConfiguration<Long, Person> ccfg = new CacheConfiguration<Long,
Person>()
+ .setName(CACHE)
+ .setIndexedTypes(Long.class, Person.class)
+ .setAtomicityMode(TRANSACTIONAL)
+ .setCacheMode(REPLICATED)
+ .setQueryParallelism(1)
+ .setBackups(0);
+
+ cfg.setCacheConfiguration(ccfg);
+
+ return cfg;
+ }
+
+ /** */
+ @Test
+ public void testRangeQueriesWithoutDuplicates() throws Exception {
+ checkRangeQueries(1);
+ }
+
+ /** */
+ @Test
+ public void testRangeQueriesWithDuplicates() throws Exception {
+ checkRangeQueries(10);
+ }
+
+ /** */
+ @Test
+ public void testSetLimit() throws Exception {
+ GridTestUtils.assertThrows(log, () -> new IndexQuery<>(Person.class,
IDX).setLimit(0),
+ IllegalArgumentException.class, "Limit must be positive.");
+
+ int limit = 1 + new Random().nextInt(1000);
+
+ GridTestUtils.assertThrows(log, () -> new IndexQuery<>(Person.class,
IDX).setLimit(0 - limit),
+ IllegalArgumentException.class, "Limit must be positive.");
+
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+
+ qry.setLimit(limit);
+
+ assertEquals(limit, qry.getLimit());
+ }
+
+ /** */
+ private void checkRangeQueries(int duplicates) throws Exception {
+
+ // Query empty cache.
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+
+ assertTrue(crd.cache(CACHE).query(qry).getAll().isEmpty());
+
+ // Add data
+ insertData(duplicates);
+
+ // All
+ checkLimit(null, 0, CNT, duplicates);
+
+ // Range queries.
+ String fld = "id";
+
+ int pivot = new Random().nextInt(CNT);
+
+ // Eq.
+ checkLimit(eq(fld, pivot), pivot, pivot + 1, duplicates);
+
+ // Lt.
+ checkLimit(lt(fld, pivot), 0, pivot, duplicates);
+
+ // Lte.
+ checkLimit(lte(fld, pivot), 0, pivot + 1, duplicates);
+
+ // Gt.
+ checkLimit(gt(fld, pivot), pivot + 1, CNT, duplicates);
+
+ // Gte.
+ checkLimit(gte(fld, pivot), pivot, CNT, duplicates);
+
+ // Between.
+ int lower = new Random().nextInt(CNT / 2);
+ int upper = lower + CNT / 20;
+
+ checkLimit(between(fld, lower, upper), lower, upper + 1, duplicates);
+
+ // In.
+ checkLimit(in(fld, F.asList(pivot, pivot + 1)), pivot, pivot + 2,
duplicates);
+ }
+
+ /** */
+ private void checkLimit(IndexQueryCriterion criterion, int left, int
right, int duplicates) throws Exception {
+ int rows = right - left;
+ int limit = new Random().nextInt(rows + 1 - 1) + 1;
+
+ // limit < rows
+ checkLimit(criterion, limit, left, left + limit, duplicates);
+
+ // limit >= rows
+ if (rows > 1) {
+ limit = new Random().nextInt(CNT + 2 - rows) + rows;
+ checkLimit(criterion, limit, left, right, duplicates);
Review Comment:
There should be empty line between definition and method invoke.
##########
modules/indexing/src/test/java/org/apache/ignite/cache/query/IndexQueryLimitTest.java:
##########
@@ -0,0 +1,274 @@
+/*
+ * 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.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+import java.util.Random;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.IntUnaryOperator;
+import javax.cache.Cache;
+import org.apache.ignite.Ignite;
+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.internal.processors.cache.query.QueryCursorEx;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+
+import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
+import static org.apache.ignite.cache.CacheMode.REPLICATED;
+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.in;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lt;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lte;
+
+/** */
+public class IndexQueryLimitTest extends GridCommonAbstractTest {
+ /** */
+ private static final String CACHE = "TEST_CACHE";
+
+ /** */
+ private static final String IDX = "PERSON_ID_IDX";
+
+ /** */
+ private static final int CNT = 10_000;
+
+ /** */
+ private Ignite crd;
+
+ /** {@inheritDoc} */
+ @Override protected void beforeTest() throws Exception {
+ crd = startGrids(4);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() {
+ stopAllGrids();
+ }
+
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String
igniteInstanceName) throws Exception {
+ IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+ CacheConfiguration<Long, Person> ccfg = new CacheConfiguration<Long,
Person>()
+ .setName(CACHE)
+ .setIndexedTypes(Long.class, Person.class)
+ .setAtomicityMode(TRANSACTIONAL)
+ .setCacheMode(REPLICATED)
+ .setQueryParallelism(1)
+ .setBackups(0);
+
+ cfg.setCacheConfiguration(ccfg);
+
+ return cfg;
+ }
+
+ /** */
+ @Test
+ public void testRangeQueriesWithoutDuplicates() throws Exception {
+ checkRangeQueries(1);
+ }
+
+ /** */
+ @Test
+ public void testRangeQueriesWithDuplicates() throws Exception {
+ checkRangeQueries(10);
+ }
+
+ /** */
+ @Test
+ public void testSetLimit() throws Exception {
+ GridTestUtils.assertThrows(log, () -> new IndexQuery<>(Person.class,
IDX).setLimit(0),
+ IllegalArgumentException.class, "Limit must be positive.");
+
+ int limit = 1 + new Random().nextInt(1000);
+
+ GridTestUtils.assertThrows(log, () -> new IndexQuery<>(Person.class,
IDX).setLimit(0 - limit),
+ IllegalArgumentException.class, "Limit must be positive.");
+
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+
+ qry.setLimit(limit);
+
+ assertEquals(limit, qry.getLimit());
+ }
+
+ /** */
+ private void checkRangeQueries(int duplicates) throws Exception {
+
Review Comment:
Please remove this empty line.
##########
modules/indexing/src/test/java/org/apache/ignite/cache/query/IndexQueryLimitTest.java:
##########
@@ -0,0 +1,257 @@
+package org.apache.ignite.cache.query;
+
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+import java.util.Random;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.IntUnaryOperator;
+import javax.cache.Cache;
+import org.apache.ignite.Ignite;
+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.internal.processors.cache.query.QueryCursorEx;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+
+import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
+import static org.apache.ignite.cache.CacheMode.REPLICATED;
+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.in;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lt;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lte;
+
+/** */
+public class IndexQueryLimitTest extends GridCommonAbstractTest {
+ /** */
+ private static final String CACHE = "TEST_CACHE";
+
+ /** */
+ private static final String IDX = "PERSON_ID_IDX";
+
+ /** */
+ private static final int CNT = 10_000;
+
+ /** */
+ private Ignite crd;
+
+ /** {@inheritDoc} */
+ @Override protected void beforeTest() throws Exception {
+ crd = startGrids(4);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() {
+ stopAllGrids();
+ }
+
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String
igniteInstanceName) throws Exception {
+ IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+ CacheConfiguration<Long, Person> ccfg = new CacheConfiguration<Long,
Person>()
+ .setName(CACHE)
+ .setIndexedTypes(Long.class, Person.class)
+ .setAtomicityMode(TRANSACTIONAL)
+ .setCacheMode(REPLICATED)
+ .setQueryParallelism(1)
+ .setBackups(0);
+
+ cfg.setCacheConfiguration(ccfg);
+
+ return cfg;
+ }
+
+ /** */
+ @Test
+ public void testRangeQueriesWithoutDuplicates() throws Exception {
+ checkRangeQueries(1);
+ }
+
+ /** */
+ @Test
+ public void testRangeQueriesWithDuplicates() throws Exception {
+ checkRangeQueries(10);
+ }
+
+ /** */
+ @Test
+ public void testSetLimit() throws Exception {
+ GridTestUtils.assertThrows(log, () -> new IndexQuery<>(Person.class,
IDX).setLimit(0),
+ IllegalArgumentException.class, "Limit must be positive.");
+
+ int limit = 1 + new Random().nextInt(1000);
+
+ GridTestUtils.assertThrows(log, () -> new IndexQuery<>(Person.class,
IDX).setLimit(0 - limit),
+ IllegalArgumentException.class, "Limit must be positive.");
+
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+
+ qry.setLimit(limit);
+
+ assertEquals(limit, qry.getLimit());
+ }
+
+ /** */
+ private void checkRangeQueries(int duplicates) throws Exception {
+
+ // Query empty cache.
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+
+ assertTrue(crd.cache(CACHE).query(qry).getAll().isEmpty());
+
+ // Add data
+ insertData(duplicates);
+
+ // All
+ checkLimit(null, 0, CNT, duplicates);
+
+ // Range queries.
+ String fld = "id";
+
+ int pivot = new Random().nextInt(CNT);
+
+ // Eq.
+ checkLimit(eq(fld, pivot), pivot, pivot + 1, duplicates);
+
+ // Lt.
+ checkLimit(lt(fld, pivot), 0, pivot, duplicates);
+
+ // Lte.
+ checkLimit(lte(fld, pivot), 0, pivot + 1, duplicates);
+
+ // Gt.
+ checkLimit(gt(fld, pivot), pivot + 1, CNT, duplicates);
+
+ // Gte.
+ checkLimit(gte(fld, pivot), pivot, CNT, duplicates);
+
+ // Between.
+ int lower = new Random().nextInt(CNT / 2);
+ int upper = lower + CNT / 20;
+
+ checkLimit(between(fld, lower, upper), lower, upper + 1, duplicates);
+
+ // In.
+ checkLimit(in(fld, F.asList(pivot, pivot + 1)), pivot, pivot + 2,
duplicates);
+ }
+
+ /** */
+ private void checkLimit(IndexQueryCriterion criterion, int left, int
right, int duplicates) throws Exception {
+ int rows = right - left;
+ int limit = new Random().nextInt(rows + 1 - 1) + 1;
Review Comment:
`+1 - 1?`
##########
modules/indexing/src/test/java/org/apache/ignite/cache/query/IndexQueryLimitTest.java:
##########
@@ -0,0 +1,274 @@
+/*
+ * 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.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+import java.util.Random;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.IntUnaryOperator;
+import javax.cache.Cache;
+import org.apache.ignite.Ignite;
+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.internal.processors.cache.query.QueryCursorEx;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+
+import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
+import static org.apache.ignite.cache.CacheMode.REPLICATED;
+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.in;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lt;
+import static org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lte;
+
+/** */
+public class IndexQueryLimitTest extends GridCommonAbstractTest {
+ /** */
+ private static final String CACHE = "TEST_CACHE";
+
+ /** */
+ private static final String IDX = "PERSON_ID_IDX";
+
+ /** */
+ private static final int CNT = 10_000;
+
+ /** */
+ private Ignite crd;
+
+ /** {@inheritDoc} */
+ @Override protected void beforeTest() throws Exception {
+ crd = startGrids(4);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() {
+ stopAllGrids();
+ }
+
+ /** {@inheritDoc} */
+ @Override protected IgniteConfiguration getConfiguration(String
igniteInstanceName) throws Exception {
+ IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+ CacheConfiguration<Long, Person> ccfg = new CacheConfiguration<Long,
Person>()
+ .setName(CACHE)
+ .setIndexedTypes(Long.class, Person.class)
+ .setAtomicityMode(TRANSACTIONAL)
+ .setCacheMode(REPLICATED)
+ .setQueryParallelism(1)
+ .setBackups(0);
+
+ cfg.setCacheConfiguration(ccfg);
+
+ return cfg;
+ }
+
+ /** */
+ @Test
+ public void testRangeQueriesWithoutDuplicates() throws Exception {
+ checkRangeQueries(1);
+ }
+
+ /** */
+ @Test
+ public void testRangeQueriesWithDuplicates() throws Exception {
+ checkRangeQueries(10);
+ }
+
+ /** */
+ @Test
+ public void testSetLimit() throws Exception {
+ GridTestUtils.assertThrows(log, () -> new IndexQuery<>(Person.class,
IDX).setLimit(0),
+ IllegalArgumentException.class, "Limit must be positive.");
+
+ int limit = 1 + new Random().nextInt(1000);
+
+ GridTestUtils.assertThrows(log, () -> new IndexQuery<>(Person.class,
IDX).setLimit(0 - limit),
+ IllegalArgumentException.class, "Limit must be positive.");
+
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+
+ qry.setLimit(limit);
+
+ assertEquals(limit, qry.getLimit());
+ }
+
+ /** */
+ private void checkRangeQueries(int duplicates) throws Exception {
+
+ // Query empty cache.
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+
+ assertTrue(crd.cache(CACHE).query(qry).getAll().isEmpty());
+
+ // Add data
+ insertData(duplicates);
+
+ // All
+ checkLimit(null, 0, CNT, duplicates);
+
+ // Range queries.
+ String fld = "id";
+
+ int pivot = new Random().nextInt(CNT);
+
+ // Eq.
+ checkLimit(eq(fld, pivot), pivot, pivot + 1, duplicates);
+
+ // Lt.
+ checkLimit(lt(fld, pivot), 0, pivot, duplicates);
+
+ // Lte.
+ checkLimit(lte(fld, pivot), 0, pivot + 1, duplicates);
+
+ // Gt.
+ checkLimit(gt(fld, pivot), pivot + 1, CNT, duplicates);
+
+ // Gte.
+ checkLimit(gte(fld, pivot), pivot, CNT, duplicates);
+
+ // Between.
+ int lower = new Random().nextInt(CNT / 2);
+ int upper = lower + CNT / 20;
+
+ checkLimit(between(fld, lower, upper), lower, upper + 1, duplicates);
+
+ // In.
+ checkLimit(in(fld, F.asList(pivot, pivot + 1)), pivot, pivot + 2,
duplicates);
+ }
+
+ /** */
+ private void checkLimit(IndexQueryCriterion criterion, int left, int
right, int duplicates) throws Exception {
+ int rows = right - left;
+ int limit = new Random().nextInt(rows + 1 - 1) + 1;
+
+ // limit < rows
+ checkLimit(criterion, limit, left, left + limit, duplicates);
+
+ // limit >= rows
+ if (rows > 1) {
+ limit = new Random().nextInt(CNT + 2 - rows) + rows;
+ checkLimit(criterion, limit, left, right, duplicates);
+ }
+ }
+
+ /** */
+ private void checkLimit(IndexQueryCriterion criterion, int limit, int
left, int right, int duplicates) throws Exception {
+ IndexQuery<Long, Person> qry = new IndexQuery<>(Person.class, IDX);
+ if (criterion != null) {
+ qry.setCriteria(criterion);
+ }
+ qry.setLimit(limit);
+
+ QueryCursor<Cache.Entry<Long, Person>> cursor =
crd.cache(CACHE).query(qry);
+
+ int expSize = (right - left) * duplicates;
+ if (limit > 0 && limit < expSize) {
Review Comment:
and here
--
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]