9
9
package org .opensearch .cache .store .disk ;
10
10
11
11
import org .opensearch .cache .EhcacheDiskCacheSettings ;
12
+ import org .opensearch .common .Randomness ;
12
13
import org .opensearch .common .cache .CacheType ;
13
14
import org .opensearch .common .cache .ICache ;
14
15
import org .opensearch .common .cache .LoadAwareCacheLoader ;
15
16
import org .opensearch .common .cache .RemovalListener ;
16
17
import org .opensearch .common .cache .RemovalNotification ;
18
+ import org .opensearch .common .cache .serializer .BytesReferenceSerializer ;
19
+ import org .opensearch .common .cache .serializer .Serializer ;
17
20
import org .opensearch .common .cache .store .config .CacheConfig ;
18
21
import org .opensearch .common .metrics .CounterMetric ;
19
22
import org .opensearch .common .settings .Settings ;
20
23
import org .opensearch .common .unit .TimeValue ;
24
+ import org .opensearch .core .common .bytes .BytesArray ;
25
+ import org .opensearch .core .common .bytes .BytesReference ;
26
+ import org .opensearch .core .common .bytes .CompositeBytesReference ;
21
27
import org .opensearch .env .NodeEnvironment ;
22
28
import org .opensearch .test .OpenSearchSingleNodeTestCase ;
23
29
24
30
import java .io .IOException ;
31
+ import java .nio .charset .Charset ;
32
+ import java .nio .charset .StandardCharsets ;
25
33
import java .util .ArrayList ;
26
34
import java .util .HashMap ;
27
35
import java .util .Iterator ;
28
36
import java .util .List ;
29
37
import java .util .Map ;
38
+ import java .util .Random ;
30
39
import java .util .UUID ;
31
40
import java .util .concurrent .CopyOnWriteArrayList ;
32
41
import java .util .concurrent .CountDownLatch ;
@@ -51,6 +60,8 @@ public void testBasicGetAndPut() throws IOException {
51
60
.setIsEventListenerModeSync (true )
52
61
.setKeyType (String .class )
53
62
.setValueType (String .class )
63
+ .setKeySerializer (new StringSerializer ())
64
+ .setValueSerializer (new StringSerializer ())
54
65
.setCacheType (CacheType .INDICES_REQUEST_CACHE )
55
66
.setSettings (settings )
56
67
.setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -89,6 +100,8 @@ public void testBasicGetAndPutUsingFactory() throws IOException {
89
100
new CacheConfig .Builder <String , String >().setValueType (String .class )
90
101
.setKeyType (String .class )
91
102
.setRemovalListener (removalListener )
103
+ .setKeySerializer (new StringSerializer ())
104
+ .setValueSerializer (new StringSerializer ())
92
105
.setSettings (
93
106
Settings .builder ()
94
107
.put (
@@ -149,6 +162,8 @@ public void testConcurrentPut() throws Exception {
149
162
.setIsEventListenerModeSync (true ) // For accurate count
150
163
.setKeyType (String .class )
151
164
.setValueType (String .class )
165
+ .setKeySerializer (new StringSerializer ())
166
+ .setValueSerializer (new StringSerializer ())
152
167
.setCacheType (CacheType .INDICES_REQUEST_CACHE )
153
168
.setSettings (settings )
154
169
.setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -194,6 +209,8 @@ public void testEhcacheParallelGets() throws Exception {
194
209
.setIsEventListenerModeSync (true ) // For accurate count
195
210
.setKeyType (String .class )
196
211
.setValueType (String .class )
212
+ .setKeySerializer (new StringSerializer ())
213
+ .setValueSerializer (new StringSerializer ())
197
214
.setCacheType (CacheType .INDICES_REQUEST_CACHE )
198
215
.setSettings (settings )
199
216
.setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -237,6 +254,8 @@ public void testEhcacheKeyIterator() throws Exception {
237
254
.setIsEventListenerModeSync (true )
238
255
.setKeyType (String .class )
239
256
.setValueType (String .class )
257
+ .setKeySerializer (new StringSerializer ())
258
+ .setValueSerializer (new StringSerializer ())
240
259
.setCacheType (CacheType .INDICES_REQUEST_CACHE )
241
260
.setSettings (settings )
242
261
.setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -274,6 +293,8 @@ public void testEvictions() throws Exception {
274
293
.setThreadPoolAlias ("ehcacheTest" )
275
294
.setKeyType (String .class )
276
295
.setValueType (String .class )
296
+ .setKeySerializer (new StringSerializer ())
297
+ .setValueSerializer (new StringSerializer ())
277
298
.setCacheType (CacheType .INDICES_REQUEST_CACHE )
278
299
.setSettings (settings )
279
300
.setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -304,6 +325,8 @@ public void testComputeIfAbsentConcurrently() throws Exception {
304
325
.setThreadPoolAlias ("ehcacheTest" )
305
326
.setKeyType (String .class )
306
327
.setValueType (String .class )
328
+ .setKeySerializer (new StringSerializer ())
329
+ .setValueSerializer (new StringSerializer ())
307
330
.setCacheType (CacheType .INDICES_REQUEST_CACHE )
308
331
.setSettings (settings )
309
332
.setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -373,6 +396,8 @@ public void testComputeIfAbsentConcurrentlyAndThrowsException() throws Exception
373
396
.setThreadPoolAlias ("ehcacheTest" )
374
397
.setKeyType (String .class )
375
398
.setValueType (String .class )
399
+ .setKeySerializer (new StringSerializer ())
400
+ .setValueSerializer (new StringSerializer ())
376
401
.setCacheType (CacheType .INDICES_REQUEST_CACHE )
377
402
.setSettings (settings )
378
403
.setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -430,6 +455,8 @@ public void testComputeIfAbsentWithNullValueLoading() throws Exception {
430
455
.setStoragePath (env .nodePaths ()[0 ].indicesPath .toString () + "/request_cache" )
431
456
.setKeyType (String .class )
432
457
.setValueType (String .class )
458
+ .setKeySerializer (new StringSerializer ())
459
+ .setValueSerializer (new StringSerializer ())
433
460
.setCacheType (CacheType .INDICES_REQUEST_CACHE )
434
461
.setSettings (settings )
435
462
.setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -491,6 +518,8 @@ public void testEhcacheKeyIteratorWithRemove() throws IOException {
491
518
.setIsEventListenerModeSync (true )
492
519
.setKeyType (String .class )
493
520
.setValueType (String .class )
521
+ .setKeySerializer (new StringSerializer ())
522
+ .setValueSerializer (new StringSerializer ())
494
523
.setCacheType (CacheType .INDICES_REQUEST_CACHE )
495
524
.setSettings (settings )
496
525
.setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -525,6 +554,50 @@ public void testEhcacheKeyIteratorWithRemove() throws IOException {
525
554
526
555
}
527
556
557
+ public void testBasicGetAndPutBytesReference () throws Exception {
558
+ Settings settings = Settings .builder ().build ();
559
+ try (NodeEnvironment env = newNodeEnvironment (settings )) {
560
+ ICache <String , BytesReference > ehCacheDiskCachingTier = new EhcacheDiskCache .Builder <String , BytesReference >()
561
+ .setThreadPoolAlias ("ehcacheTest" )
562
+ .setStoragePath (env .nodePaths ()[0 ].indicesPath .toString () + "/request_cache" )
563
+ .setKeySerializer (new StringSerializer ())
564
+ .setValueSerializer (new BytesReferenceSerializer ())
565
+ .setKeyType (String .class )
566
+ .setValueType (BytesReference .class )
567
+ .setCacheType (CacheType .INDICES_REQUEST_CACHE )
568
+ .setSettings (settings )
569
+ .setMaximumWeightInBytes (CACHE_SIZE_IN_BYTES * 20 ) // bigger so no evictions happen
570
+ .setExpireAfterAccess (TimeValue .MAX_VALUE )
571
+ .setRemovalListener (new MockRemovalListener <>())
572
+ .build ();
573
+ int randomKeys = randomIntBetween (10 , 100 );
574
+ int valueLength = 100 ;
575
+ Random rand = Randomness .get ();
576
+ Map <String , BytesReference > keyValueMap = new HashMap <>();
577
+ for (int i = 0 ; i < randomKeys ; i ++) {
578
+ byte [] valueBytes = new byte [valueLength ];
579
+ rand .nextBytes (valueBytes );
580
+ keyValueMap .put (UUID .randomUUID ().toString (), new BytesArray (valueBytes ));
581
+
582
+ // Test a non-BytesArray implementation of BytesReference.
583
+ byte [] compositeBytes1 = new byte [valueLength ];
584
+ byte [] compositeBytes2 = new byte [valueLength ];
585
+ rand .nextBytes (compositeBytes1 );
586
+ rand .nextBytes (compositeBytes2 );
587
+ BytesReference composite = CompositeBytesReference .of (new BytesArray (compositeBytes1 ), new BytesArray (compositeBytes2 ));
588
+ keyValueMap .put (UUID .randomUUID ().toString (), composite );
589
+ }
590
+ for (Map .Entry <String , BytesReference > entry : keyValueMap .entrySet ()) {
591
+ ehCacheDiskCachingTier .put (entry .getKey (), entry .getValue ());
592
+ }
593
+ for (Map .Entry <String , BytesReference > entry : keyValueMap .entrySet ()) {
594
+ BytesReference value = ehCacheDiskCachingTier .get (entry .getKey ());
595
+ assertEquals (entry .getValue (), value );
596
+ }
597
+ ehCacheDiskCachingTier .close ();
598
+ }
599
+ }
600
+
528
601
private static String generateRandomString (int length ) {
529
602
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ;
530
603
StringBuilder randomString = new StringBuilder (length );
@@ -546,4 +619,25 @@ public void onRemoval(RemovalNotification<K, V> notification) {
546
619
evictionMetric .inc ();
547
620
}
548
621
}
622
+
623
+ static class StringSerializer implements Serializer <String , byte []> {
624
+ private final Charset charset = StandardCharsets .UTF_8 ;
625
+
626
+ @ Override
627
+ public byte [] serialize (String object ) {
628
+ return object .getBytes (charset );
629
+ }
630
+
631
+ @ Override
632
+ public String deserialize (byte [] bytes ) {
633
+ if (bytes == null ) {
634
+ return null ;
635
+ }
636
+ return new String (bytes , charset );
637
+ }
638
+
639
+ public boolean equals (String object , byte [] bytes ) {
640
+ return object .equals (deserialize (bytes ));
641
+ }
642
+ }
549
643
}
0 commit comments