Skip to content

Commit 6fc298d

Browse files
IGNITE-29199 Fix local IndexQuery keep-binary mode (#13744)
1 parent 15380a5 commit 6fc298d

2 files changed

Lines changed: 101 additions & 1 deletion

File tree

‎modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/GridCacheQueryManager.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1161,9 +1161,11 @@ public GridCloseableIterator indexQueryLocal(final CacheQuery qry) throws Ignite
11611161

11621162
@Override protected Object onNext() throws IgniteCheckedException {
11631163
IgniteBiTuple<K, V> entry = iter.nextX();
1164+
K key = (K)cctx.unwrapBinaryIfNeeded(entry.getKey(), qry.keepBinary(), false, null);
1165+
V val = (V)cctx.unwrapBinaryIfNeeded(entry.getValue(), qry.keepBinary(), false, null);
11641166
cnt++;
11651167

1166-
return new CacheEntryImpl<>(entry.getKey(), entry.getValue());
1168+
return new CacheEntryImpl<>(key, val);
11671169
}
11681170

11691171
@Override protected boolean onHasNext() throws IgniteCheckedException {

‎modules/indexing/src/test/java/org/apache/ignite/cache/query/IndexQueryLocalTest.java‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717

1818
package org.apache.ignite.cache.query;
1919

20+
import java.util.List;
2021
import java.util.Objects;
22+
import javax.cache.Cache;
2123
import org.apache.ignite.Ignite;
2224
import org.apache.ignite.IgniteCache;
2325
import org.apache.ignite.IgniteDataStreamer;
2426
import org.apache.ignite.IgniteException;
27+
import org.apache.ignite.binary.BinaryObject;
2528
import org.apache.ignite.cache.CacheMode;
2629
import org.apache.ignite.cache.query.annotations.QuerySqlField;
2730
import org.apache.ignite.configuration.CacheConfiguration;
@@ -99,6 +102,51 @@ public void testServerNodePartitionedCache() {
99102
assertEquals(CNT / 2, resultSize);
100103
}
101104

105+
/** Should return deserialized values for a cache that is not in keep-binary mode. */
106+
@Test
107+
public void testLocalQueryReturnsDeserializedValues() {
108+
IgniteCache<PersonId, Person> cache = crd.createCache(ccfgObjectKey(CacheMode.REPLICATED));
109+
110+
insertDataWithPersonIdKey(crd, cache);
111+
112+
IndexQuery<PersonId, Person> qry = new IndexQuery<PersonId, Person>(Person.class, IDX)
113+
.setCriteria(lt("id", CNT / 2));
114+
115+
List<Cache.Entry<PersonId, Person>> res = cache.query(qry.setLocal(true)).getAll();
116+
117+
for (Cache.Entry<PersonId, Person> e : res) {
118+
assertEquals(new PersonId(e.getValue().id), e.getKey());
119+
assertEquals(new Person(e.getKey().id), e.getValue());
120+
}
121+
122+
assertEquals(CNT / 2, res.size());
123+
}
124+
125+
/** Should keep binary keys and values for a cache in keep-binary mode. */
126+
@Test
127+
public void testLocalQueryKeepBinaryReturnsBinaryValues() {
128+
IgniteCache<PersonId, Person> cache = crd.createCache(ccfgObjectKey(CacheMode.REPLICATED));
129+
130+
insertDataWithPersonIdKey(crd, cache);
131+
132+
IgniteCache<BinaryObject, BinaryObject> binCache = cache.withKeepBinary();
133+
134+
IndexQuery<BinaryObject, BinaryObject> qry = new IndexQuery<BinaryObject, BinaryObject>(Person.class, IDX)
135+
.setCriteria(lt("id", CNT / 2));
136+
137+
List<Cache.Entry<BinaryObject, BinaryObject>> res = binCache.query(qry.setLocal(true)).getAll();
138+
139+
for (Cache.Entry<BinaryObject, BinaryObject> e : res) {
140+
PersonId key = e.getKey().deserialize();
141+
Person val = e.getValue().deserialize();
142+
143+
assertEquals(new PersonId(val.id), key);
144+
assertEquals(new Person(key.id), val);
145+
}
146+
147+
assertEquals(CNT / 2, res.size());
148+
}
149+
102150
/** Should fail as no data on nodes. */
103151
@Test
104152
public void testClientNodeReplicatedCache() throws Exception {
@@ -136,6 +184,14 @@ private void insertData(Ignite ignite, IgniteCache<Long, Person> cache) {
136184
}
137185
}
138186

187+
/** */
188+
private void insertDataWithPersonIdKey(Ignite ignite, IgniteCache<PersonId, Person> cache) {
189+
try (IgniteDataStreamer<PersonId, Person> streamer = ignite.dataStreamer(cache.getName())) {
190+
for (int i = 0; i < CNT; i++)
191+
streamer.addData(new PersonId(i), new Person(i));
192+
}
193+
}
194+
139195
/** */
140196
private CacheConfiguration<Long, Person> ccfg(CacheMode cacheMode) {
141197
return new CacheConfiguration<Long, Person>()
@@ -145,6 +201,15 @@ private CacheConfiguration<Long, Person> ccfg(CacheMode cacheMode) {
145201
.setBackups(2);
146202
}
147203

204+
/** */
205+
private CacheConfiguration<PersonId, Person> ccfgObjectKey(CacheMode cacheMode) {
206+
return new CacheConfiguration<PersonId, Person>()
207+
.setName(CACHE)
208+
.setIndexedTypes(PersonId.class, Person.class)
209+
.setCacheMode(cacheMode)
210+
.setBackups(2);
211+
}
212+
148213
/** */
149214
private static class Person {
150215
/** */
@@ -178,4 +243,37 @@ private static class Person {
178243
return Objects.hash(id);
179244
}
180245
}
246+
247+
/** */
248+
private static class PersonId {
249+
/** */
250+
final int id;
251+
252+
/** */
253+
PersonId(int id) {
254+
this.id = id;
255+
}
256+
257+
/** {@inheritDoc} */
258+
@Override public String toString() {
259+
return "PersonId[id=" + id + "]";
260+
}
261+
262+
/** {@inheritDoc} */
263+
@Override public boolean equals(Object o) {
264+
if (this == o)
265+
return true;
266+
if (o == null || getClass() != o.getClass())
267+
return false;
268+
269+
PersonId personId = (PersonId)o;
270+
271+
return Objects.equals(id, personId.id);
272+
}
273+
274+
/** {@inheritDoc} */
275+
@Override public int hashCode() {
276+
return Objects.hash(id);
277+
}
278+
}
181279
}

0 commit comments

Comments
 (0)