Skip to content

Commit 9cbc585

Browse files
authored
IGNITE-14699 Add IndexQuery API. (apache#9118)
1 parent f7d3410 commit 9cbc585

36 files changed

+4550
-44
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.ignite.cache.query;
19+
20+
import java.util.ArrayList;
21+
import java.util.Arrays;
22+
import java.util.Collections;
23+
import java.util.HashSet;
24+
import java.util.List;
25+
import java.util.Set;
26+
import javax.cache.Cache;
27+
import org.apache.ignite.internal.util.typedef.internal.A;
28+
import org.apache.ignite.lang.IgniteExperimental;
29+
30+
/**
31+
* Index query runs over internal index structure and returns cache entries for index rows that match specified criteria.
32+
*/
33+
@IgniteExperimental
34+
public final class IndexQuery<K, V> extends Query<Cache.Entry<K, V>> {
35+
/** */
36+
private static final long serialVersionUID = 0L;
37+
38+
/** Index query criteria. */
39+
private List<IndexQueryCriterion> criteria;
40+
41+
/** Cache Value type. Describes a table within a cache that runs a query. */
42+
private final String valType;
43+
44+
/** Index name. */
45+
private final String idxName;
46+
47+
/**
48+
* Specify index with cache value class and index name.
49+
*
50+
* @param valCls Cache value class.
51+
* @param idxName Index name.
52+
*/
53+
public IndexQuery(Class<?> valCls, String idxName) {
54+
this(valCls.getName(), idxName);
55+
}
56+
57+
/**
58+
* Specify index with cache value type and index name.
59+
*
60+
* @param valType Cache value type.
61+
* @param idxName Index name.
62+
*/
63+
public IndexQuery(String valType, String idxName) {
64+
A.notEmpty(valType, "valType");
65+
A.notEmpty(idxName, "idxName");
66+
67+
this.valType = valType;
68+
this.idxName = idxName;
69+
}
70+
71+
/**
72+
* Sets conjunction (AND) criteria for index query.
73+
*
74+
* @param criteria Criteria to set.
75+
* @return {@code this} for chaining.
76+
*/
77+
public IndexQuery<K, V> setCriteria(IndexQueryCriterion... criteria) {
78+
validateAndSetCriteria(Arrays.asList(criteria));
79+
80+
return this;
81+
}
82+
83+
/**
84+
* Sets conjunction (AND) criteria for index query.
85+
*
86+
* @param criteria Criteria to set.
87+
* @return {@code this} for chaining.
88+
*/
89+
public IndexQuery<K, V> setCriteria(List<IndexQueryCriterion> criteria) {
90+
validateAndSetCriteria(new ArrayList<>(criteria));
91+
92+
return this;
93+
}
94+
95+
/**
96+
* Index query criteria.
97+
*
98+
* @return List of criteria for this index query.
99+
*/
100+
public List<IndexQueryCriterion> getCriteria() {
101+
return criteria;
102+
}
103+
104+
/**
105+
* Cache Value type.
106+
*
107+
* @return Cache Value type.
108+
*/
109+
public String getValueType() {
110+
return valType;
111+
}
112+
113+
/**
114+
* Index name.
115+
*
116+
* @return Index name.
117+
*/
118+
public String getIndexName() {
119+
return idxName;
120+
}
121+
122+
/** */
123+
private void validateAndSetCriteria(List<IndexQueryCriterion> criteria) {
124+
A.notEmpty(criteria, "criteria");
125+
A.notNull(criteria.get(0), "criteria");
126+
127+
Class<?> critCls = criteria.get(0).getClass();
128+
129+
Set<String> fields = new HashSet<>();
130+
131+
for (IndexQueryCriterion c: criteria) {
132+
A.notNull(c, "criteria");
133+
A.ensure(c.getClass() == critCls,
134+
"Expect a the same criteria class for merging criteria. Exp=" + critCls + ", act=" + c.getClass());
135+
136+
A.ensure(!fields.contains(c.field()), "Duplicated field in criteria: " + c.field() + ".");
137+
138+
fields.add(c.field());
139+
}
140+
141+
this.criteria = Collections.unmodifiableList(criteria);
142+
}
143+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.ignite.cache.query;
19+
20+
import org.apache.ignite.internal.cache.query.RangeIndexQueryCriterion;
21+
import org.apache.ignite.internal.util.typedef.internal.A;
22+
23+
/**
24+
* Factory of {@link IndexQueryCriterion} for {@link IndexQuery}.
25+
*/
26+
public class IndexQueryCriteriaBuilder {
27+
/**
28+
* Equal To.
29+
*
30+
* @param field Index field to apply criterion.
31+
* @param val Strict equality value.
32+
* @return Criterion.
33+
*/
34+
public static IndexQueryCriterion eq(String field, Object val) {
35+
return between(field, val, val);
36+
}
37+
38+
/**
39+
* Less Then.
40+
*
41+
* @param field Index field to apply criterion.
42+
* @param val Exclusive upper bound.
43+
* @return Criterion.
44+
*/
45+
public static IndexQueryCriterion lt(String field, Object val) {
46+
A.notNullOrEmpty(field, "field");
47+
48+
RangeIndexQueryCriterion c = new RangeIndexQueryCriterion(field, null, val);
49+
c.lowerIncl(true);
50+
c.upperNull(val == null);
51+
52+
return c;
53+
}
54+
55+
/**
56+
* Less Then or Equal To.
57+
*
58+
* @param field Index field to apply criterion.
59+
* @param val Inclusive upper bound.
60+
* @return Criterion.
61+
*/
62+
public static IndexQueryCriterion lte(String field, Object val) {
63+
A.notNullOrEmpty(field, "field");
64+
65+
RangeIndexQueryCriterion c = new RangeIndexQueryCriterion(field, null, val);
66+
c.lowerIncl(true);
67+
c.upperIncl(true);
68+
c.upperNull(val == null);
69+
70+
return c;
71+
}
72+
73+
/**
74+
* Greater Then.
75+
*
76+
* @param field Index field to apply criterion.
77+
* @param val Exclusive lower bound.
78+
* @return Criterion.
79+
*/
80+
public static IndexQueryCriterion gt(String field, Object val) {
81+
A.notNullOrEmpty(field, "field");
82+
83+
RangeIndexQueryCriterion c = new RangeIndexQueryCriterion(field, val, null);
84+
c.upperIncl(true);
85+
c.lowerNull(val == null);
86+
87+
return c;
88+
}
89+
90+
/**
91+
* Greater Then or Equal To.
92+
*
93+
* @param field Index field to apply criterion.
94+
* @param val Inclusive lower bound.
95+
* @return Criterion.
96+
*/
97+
public static IndexQueryCriterion gte(String field, Object val) {
98+
A.notNullOrEmpty(field, "field");
99+
100+
RangeIndexQueryCriterion c = new RangeIndexQueryCriterion(field, val, null);
101+
c.lowerIncl(true);
102+
c.upperIncl(true);
103+
c.lowerNull(val == null);
104+
105+
return c;
106+
}
107+
108+
/**
109+
* Between.
110+
*
111+
* @param field Index field to apply criterion.
112+
* @param lower Inclusive lower bound.
113+
* @param upper Inclusive upper bound.
114+
* @return Criterion.
115+
*/
116+
public static IndexQueryCriterion between(String field, Object lower, Object upper) {
117+
A.notNullOrEmpty(field, "field");
118+
119+
RangeIndexQueryCriterion c = new RangeIndexQueryCriterion(field, lower, upper);
120+
c.lowerIncl(true);
121+
c.upperIncl(true);
122+
c.lowerNull(lower == null);
123+
c.upperNull(upper == null);
124+
125+
return c;
126+
}
127+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.ignite.cache.query;
19+
20+
import java.io.Serializable;
21+
22+
/**
23+
* Basic interface for {@link IndexQuery} criterion.
24+
*/
25+
public interface IndexQueryCriterion extends Serializable {
26+
/**
27+
* @return Index field name.
28+
*/
29+
public String field();
30+
}

0 commit comments

Comments
 (0)