11package com .scalar .db .common ;
22
3- import com .google .common .cache .CacheBuilder ;
4- import com .google .common .cache .CacheLoader ;
5- import com .google .common .cache .LoadingCache ;
3+ import com .github .benmanes .caffeine .cache .CacheLoader ;
4+ import com .github .benmanes .caffeine .cache .Caffeine ;
5+ import com .github .benmanes .caffeine .cache .LoadingCache ;
6+ import com .google .common .annotations .VisibleForTesting ;
67import com .scalar .db .api .Admin ;
78import com .scalar .db .api .Operation ;
89import com .scalar .db .api .TableMetadata ;
910import com .scalar .db .exception .storage .ExecutionException ;
1011import com .scalar .db .util .ScalarDbUtils ;
1112import com .scalar .db .util .ThrowableFunction ;
1213import java .util .Objects ;
13- import java .util .Optional ;
14+ import java .util .concurrent . CompletionException ;
1415import java .util .concurrent .TimeUnit ;
1516import javax .annotation .Nonnull ;
17+ import javax .annotation .Nullable ;
1618import javax .annotation .concurrent .ThreadSafe ;
1719
1820/** A class that manages and caches table metadata */
1921@ ThreadSafe
2022public class TableMetadataManager {
2123
22- private final LoadingCache <TableKey , Optional < TableMetadata > > tableMetadataCache ;
24+ private final LoadingCache <TableKey , TableMetadata > tableMetadataCache ;
2325
2426 public TableMetadataManager (Admin admin , long cacheExpirationTimeSecs ) {
25- this (
26- key -> Optional .ofNullable (admin .getTableMetadata (key .namespace , key .table )),
27- cacheExpirationTimeSecs );
27+ this (key -> admin .getTableMetadata (key .namespace , key .table ), cacheExpirationTimeSecs );
2828 }
2929
3030 public TableMetadataManager (
31- ThrowableFunction <TableKey , Optional < TableMetadata > , Exception > getTableMetadataFunc ,
31+ ThrowableFunction <TableKey , TableMetadata , Exception > getTableMetadataFunc ,
3232 long cacheExpirationTimeSecs ) {
33- CacheBuilder <Object , Object > builder = CacheBuilder .newBuilder ();
33+ Caffeine <Object , Object > builder = Caffeine .newBuilder ();
3434 if (cacheExpirationTimeSecs >= 0 ) {
3535 builder .expireAfterWrite (cacheExpirationTimeSecs , TimeUnit .SECONDS );
3636 }
3737 tableMetadataCache =
3838 builder .build (
39- new CacheLoader <TableKey , Optional < TableMetadata > >() {
40- @ Nonnull
39+ new CacheLoader <TableKey , TableMetadata >() {
40+ @ Nullable
4141 @ Override
42- public Optional < TableMetadata > load (@ Nonnull TableKey key ) throws Exception {
42+ public TableMetadata load (@ Nonnull TableKey key ) throws Exception {
4343 return getTableMetadataFunc .apply (key );
4444 }
4545 });
@@ -61,26 +61,28 @@ public TableMetadata getTableMetadata(Operation operation) throws ExecutionExcep
6161 }
6262
6363 /**
64- * Returns a table metadata corresponding to the specified operation .
64+ * Returns a table metadata corresponding to the specified namespace and table .
6565 *
6666 * @param namespace a namespace to retrieve
6767 * @param table a table to retrieve
6868 * @return a table metadata. null if the table is not found.
6969 * @throws ExecutionException if the operation fails
7070 */
71+ @ Nullable
7172 public TableMetadata getTableMetadata (String namespace , String table ) throws ExecutionException {
7273 try {
7374 TableKey key = new TableKey (namespace , table );
74- return tableMetadataCache .get (key ). orElse ( null ) ;
75- } catch (java . util . concurrent . ExecutionException e ) {
75+ return tableMetadataCache .get (key );
76+ } catch (CompletionException e ) {
7677 throw new ExecutionException (
7778 CoreError .GETTING_TABLE_METADATA_FAILED .buildMessage (
7879 ScalarDbUtils .getFullTableName (namespace , table )),
79- e );
80+ e . getCause () );
8081 }
8182 }
8283
83- public static class TableKey {
84+ @ VisibleForTesting
85+ static class TableKey {
8486 public final String namespace ;
8587 public final String table ;
8688
0 commit comments