99package org .opensearch .cache .store .disk ;
1010
1111import org .opensearch .cache .EhcacheDiskCacheSettings ;
12+ import org .opensearch .common .Randomness ;
1213import org .opensearch .common .cache .CacheType ;
1314import org .opensearch .common .cache .ICache ;
1415import org .opensearch .common .cache .LoadAwareCacheLoader ;
1516import org .opensearch .common .cache .RemovalListener ;
1617import org .opensearch .common .cache .RemovalNotification ;
18+ import org .opensearch .common .cache .serializer .BytesReferenceSerializer ;
19+ import org .opensearch .common .cache .serializer .Serializer ;
1720import org .opensearch .common .cache .store .config .CacheConfig ;
1821import org .opensearch .common .metrics .CounterMetric ;
1922import org .opensearch .common .settings .Settings ;
2023import 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 ;
2127import org .opensearch .env .NodeEnvironment ;
2228import org .opensearch .test .OpenSearchSingleNodeTestCase ;
2329
2430import java .io .IOException ;
31+ import java .nio .charset .Charset ;
32+ import java .nio .charset .StandardCharsets ;
2533import java .util .ArrayList ;
2634import java .util .HashMap ;
2735import java .util .Iterator ;
2836import java .util .List ;
2937import java .util .Map ;
38+ import java .util .Random ;
3039import java .util .UUID ;
3140import java .util .concurrent .CopyOnWriteArrayList ;
3241import java .util .concurrent .CountDownLatch ;
@@ -51,6 +60,8 @@ public void testBasicGetAndPut() throws IOException {
5160 .setIsEventListenerModeSync (true )
5261 .setKeyType (String .class )
5362 .setValueType (String .class )
63+ .setKeySerializer (new StringSerializer ())
64+ .setValueSerializer (new StringSerializer ())
5465 .setCacheType (CacheType .INDICES_REQUEST_CACHE )
5566 .setSettings (settings )
5667 .setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -89,6 +100,8 @@ public void testBasicGetAndPutUsingFactory() throws IOException {
89100 new CacheConfig .Builder <String , String >().setValueType (String .class )
90101 .setKeyType (String .class )
91102 .setRemovalListener (removalListener )
103+ .setKeySerializer (new StringSerializer ())
104+ .setValueSerializer (new StringSerializer ())
92105 .setSettings (
93106 Settings .builder ()
94107 .put (
@@ -149,6 +162,8 @@ public void testConcurrentPut() throws Exception {
149162 .setIsEventListenerModeSync (true ) // For accurate count
150163 .setKeyType (String .class )
151164 .setValueType (String .class )
165+ .setKeySerializer (new StringSerializer ())
166+ .setValueSerializer (new StringSerializer ())
152167 .setCacheType (CacheType .INDICES_REQUEST_CACHE )
153168 .setSettings (settings )
154169 .setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -194,6 +209,8 @@ public void testEhcacheParallelGets() throws Exception {
194209 .setIsEventListenerModeSync (true ) // For accurate count
195210 .setKeyType (String .class )
196211 .setValueType (String .class )
212+ .setKeySerializer (new StringSerializer ())
213+ .setValueSerializer (new StringSerializer ())
197214 .setCacheType (CacheType .INDICES_REQUEST_CACHE )
198215 .setSettings (settings )
199216 .setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -237,6 +254,8 @@ public void testEhcacheKeyIterator() throws Exception {
237254 .setIsEventListenerModeSync (true )
238255 .setKeyType (String .class )
239256 .setValueType (String .class )
257+ .setKeySerializer (new StringSerializer ())
258+ .setValueSerializer (new StringSerializer ())
240259 .setCacheType (CacheType .INDICES_REQUEST_CACHE )
241260 .setSettings (settings )
242261 .setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -274,6 +293,8 @@ public void testEvictions() throws Exception {
274293 .setThreadPoolAlias ("ehcacheTest" )
275294 .setKeyType (String .class )
276295 .setValueType (String .class )
296+ .setKeySerializer (new StringSerializer ())
297+ .setValueSerializer (new StringSerializer ())
277298 .setCacheType (CacheType .INDICES_REQUEST_CACHE )
278299 .setSettings (settings )
279300 .setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -304,6 +325,8 @@ public void testComputeIfAbsentConcurrently() throws Exception {
304325 .setThreadPoolAlias ("ehcacheTest" )
305326 .setKeyType (String .class )
306327 .setValueType (String .class )
328+ .setKeySerializer (new StringSerializer ())
329+ .setValueSerializer (new StringSerializer ())
307330 .setCacheType (CacheType .INDICES_REQUEST_CACHE )
308331 .setSettings (settings )
309332 .setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -373,6 +396,8 @@ public void testComputeIfAbsentConcurrentlyAndThrowsException() throws Exception
373396 .setThreadPoolAlias ("ehcacheTest" )
374397 .setKeyType (String .class )
375398 .setValueType (String .class )
399+ .setKeySerializer (new StringSerializer ())
400+ .setValueSerializer (new StringSerializer ())
376401 .setCacheType (CacheType .INDICES_REQUEST_CACHE )
377402 .setSettings (settings )
378403 .setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -430,6 +455,8 @@ public void testComputeIfAbsentWithNullValueLoading() throws Exception {
430455 .setStoragePath (env .nodePaths ()[0 ].indicesPath .toString () + "/request_cache" )
431456 .setKeyType (String .class )
432457 .setValueType (String .class )
458+ .setKeySerializer (new StringSerializer ())
459+ .setValueSerializer (new StringSerializer ())
433460 .setCacheType (CacheType .INDICES_REQUEST_CACHE )
434461 .setSettings (settings )
435462 .setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -491,6 +518,8 @@ public void testEhcacheKeyIteratorWithRemove() throws IOException {
491518 .setIsEventListenerModeSync (true )
492519 .setKeyType (String .class )
493520 .setValueType (String .class )
521+ .setKeySerializer (new StringSerializer ())
522+ .setValueSerializer (new StringSerializer ())
494523 .setCacheType (CacheType .INDICES_REQUEST_CACHE )
495524 .setSettings (settings )
496525 .setExpireAfterAccess (TimeValue .MAX_VALUE )
@@ -525,6 +554,50 @@ public void testEhcacheKeyIteratorWithRemove() throws IOException {
525554
526555 }
527556
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+
528601 private static String generateRandomString (int length ) {
529602 String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ;
530603 StringBuilder randomString = new StringBuilder (length );
@@ -546,4 +619,25 @@ public void onRemoval(RemovalNotification<K, V> notification) {
546619 evictionMetric .inc ();
547620 }
548621 }
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+ }
549643}
0 commit comments