1
1
/*
2
- * Copyright (c) 2000, 2024 , Oracle and/or its affiliates.
2
+ * Copyright (c) 2000, 2025 , Oracle and/or its affiliates.
3
3
*
4
4
* Licensed under the Universal Permissive License v 1.0 as shown at
5
5
* https://oss.oracle.com/licenses/upl.
6
6
*/
7
7
8
8
package filter ;
9
9
10
+ import com .oracle .coherence .common .collections .NullableSortedMap ;
10
11
import com .oracle .coherence .testing .AbstractFunctionalTest ;
11
- import com .tangosol .coherence .config .Config ;
12
+
13
+ import com .tangosol .util .Binary ;
14
+ import com .tangosol .util .ChainedCollection ;
15
+ import com .tangosol .util .SubSet ;
12
16
import com .tangosol .util .comparator .SafeComparator ;
13
17
import com .tangosol .util .filter .AlwaysFilter ;
14
18
import com .tangosol .util .filter .LimitFilter ;
15
- import data .Person ;
16
19
17
20
import com .tangosol .net .CacheFactory ;
18
21
import com .tangosol .net .NamedCache ;
24
27
import com .tangosol .util .filter .AllFilter ;
25
28
import com .tangosol .util .filter .BetweenFilter ;
26
29
30
+ import data .Person ;
31
+
32
+ import java .nio .charset .StandardCharsets ;
33
+
34
+ import java .util .ArrayList ;
35
+ import java .util .Collection ;
36
+ import java .util .Collections ;
27
37
import java .util .Comparator ;
28
38
import java .util .HashMap ;
39
+ import java .util .HashSet ;
40
+ import java .util .Iterator ;
41
+ import java .util .List ;
29
42
import java .util .Map ;
30
-
43
+ import java . util . Set ;
31
44
import java .util .SortedSet ;
45
+
46
+ import org .junit .Assert ;
32
47
import org .junit .BeforeClass ;
33
48
import org .junit .Test ;
34
49
35
50
import static junit .framework .Assert .fail ;
51
+
36
52
import static org .junit .Assert .assertEquals ;
37
53
38
54
public class MiscTests
@@ -58,6 +74,19 @@ public static void _startup()
58
74
}
59
75
60
76
// ----- test methods ---------------------------------------------------
77
+
78
+ @ Test
79
+ public void testRetainAll ()
80
+ {
81
+ testSubset (true );
82
+ }
83
+
84
+ @ Test
85
+ public void testRemoveAll ()
86
+ {
87
+ testSubset (false );
88
+ }
89
+
61
90
@ Test
62
91
public void testNullFirst ()
63
92
{
@@ -85,8 +114,6 @@ public void testNullFirst()
85
114
assertEquals (s .last ().getValue ().getName (), null );
86
115
}
87
116
88
-
89
-
90
117
@ Test
91
118
public void testBetweenFilter ()
92
119
{
@@ -185,5 +212,99 @@ public void testBetweenFilter()
185
212
{
186
213
fail ("Test run interrupted" );
187
214
}
215
+
216
+ cache .removeIndex (extrFirst );
217
+ cache .removeIndex (extrLast );
218
+ cache .removeIndex (extrYear );
219
+ }
220
+
221
+ // ----- helper methods -------------------------------------------------
222
+
223
+ /**
224
+ * Test the performance of retainAll() and removeAll() as any deviation
225
+ * can result in query timeouts
226
+ *
227
+ * @param fRetain Whether retainAll() or removeAll() should be tested
228
+ */
229
+ public void testSubset (boolean fRetain )
230
+ {
231
+ SubSet <Binary > setKeys = new SubSet <>(createSet ());
232
+ SubSet <Binary > setKeys2 = new SubSet <>(setKeys );
233
+ NullableSortedMap <Integer , Set <Binary >> mapTail = new NullableSortedMap <>();
234
+ for (int i = 0 ; i < 43000 ; i ++)
235
+ {
236
+ mapTail .put (i , Collections .singleton (createBinary (i )));
237
+ }
238
+
239
+ // new Style
240
+ long start = System .currentTimeMillis ();
241
+ List <Set <?>> listGT = new ArrayList <>(mapTail .size ());
242
+ for (Object o : mapTail .values ())
243
+ {
244
+ Set set = (Set ) o ;
245
+ listGT .add (ensureSafeSet (set ));
246
+ }
247
+ Collection <Object > c = new ChainedCollection <>(listGT .toArray (Set []::new ));
248
+
249
+ if (fRetain )
250
+ {
251
+ // test against passed set
252
+ setKeys .retainAll (c );
253
+ // test against "original" set
254
+ setKeys .retainAll (c );
255
+ }
256
+ else
257
+ {
258
+ // test against passed set
259
+ setKeys .removeAll (c );
260
+ // test against "original" set
261
+ setKeys .removeAll (c );
262
+ }
263
+
264
+ long newStyle = System .currentTimeMillis () - start ;
265
+
266
+ // old style:
267
+ start = System .currentTimeMillis ();
268
+ NullableSortedMap <Integer , Set <Binary >> mapGE = new NullableSortedMap <>(mapTail );
269
+ Set setGT = new HashSet ();
270
+ for (Iterator iterGE = mapGE .values ().iterator (); iterGE .hasNext (); )
271
+ {
272
+ Set set = (Set ) iterGE .next ();
273
+ setGT .addAll (ensureSafeSet (set ));
274
+ }
275
+ if (fRetain )
276
+ {
277
+ setKeys2 .retainAll (setGT );
278
+ }
279
+ else
280
+ {
281
+ setKeys2 .removeAll (setGT );
282
+ }
283
+ long oldStyle = System .currentTimeMillis () - start ;
284
+ System .out .println ("Time: new style(partitioned index):" + newStyle + " ms, old style(monolithic index):" + oldStyle + " ms" );
285
+ Assert .assertEquals (setKeys , setKeys2 );
286
+ // new style called twice ~ as good as old style called once
287
+ Assert .assertTrue ((newStyle * .25 ) <= oldStyle );
288
+ }
289
+
290
+ private Set <Binary > createSet ()
291
+ {
292
+ Set <Binary > s = new HashSet <>();
293
+ for (int i = 0 ; i < 30000 ; i ++)
294
+ {
295
+ s .add (createBinary (i + 15000 ));
296
+ }
297
+ return s ;
298
+ }
299
+
300
+ protected static Set ensureSafeSet (Set set )
301
+ {
302
+ return set == null ? Collections .emptySet () : set ;
303
+ }
304
+
305
+ private Binary createBinary (int i )
306
+ {
307
+ String s = "randomkey_no_test_gldfkjglkdfgjdflkgjdflkgjlkdfjglkdfjglkdfjglkdf" + i ;
308
+ return new Binary (s .getBytes (StandardCharsets .UTF_8 ));
188
309
}
189
310
}
0 commit comments