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 .Caffeine ;
4+ import com .github .benmanes .caffeine .cache .LoadingCache ;
65import com .scalar .db .api .Admin ;
76import com .scalar .db .api .Operation ;
87import com .scalar .db .api .TableMetadata ;
98import com .scalar .db .exception .storage .ExecutionException ;
109import com .scalar .db .util .ScalarDbUtils ;
1110import com .scalar .db .util .ThrowableFunction ;
1211import java .util .Objects ;
13- import java .util .Optional ;
12+ import java .util .concurrent . CompletionException ;
1413import java .util .concurrent .TimeUnit ;
15- import javax .annotation .Nonnull ;
14+ import javax .annotation .Nullable ;
1615import javax .annotation .concurrent .ThreadSafe ;
1716
1817/** A class that manages and caches table metadata */
1918@ ThreadSafe
2019public class TableMetadataManager {
2120
22- private final LoadingCache <TableKey , Optional < TableMetadata > > tableMetadataCache ;
21+ private final LoadingCache <TableKey , TableMetadata > tableMetadataCache ;
2322
2423 public TableMetadataManager (Admin admin , long cacheExpirationTimeSecs ) {
25- this (
26- key -> Optional .ofNullable (admin .getTableMetadata (key .namespace , key .table )),
27- cacheExpirationTimeSecs );
24+ this (key -> admin .getTableMetadata (key .namespace , key .table ), cacheExpirationTimeSecs );
2825 }
2926
3027 public TableMetadataManager (
31- ThrowableFunction <TableKey , Optional < TableMetadata > , Exception > getTableMetadataFunc ,
28+ ThrowableFunction <TableKey , TableMetadata , Exception > getTableMetadataFunc ,
3229 long cacheExpirationTimeSecs ) {
33- CacheBuilder <Object , Object > builder = CacheBuilder .newBuilder ();
30+ Caffeine <Object , Object > builder = Caffeine .newBuilder ();
3431 if (cacheExpirationTimeSecs >= 0 ) {
3532 builder .expireAfterWrite (cacheExpirationTimeSecs , TimeUnit .SECONDS );
3633 }
37- tableMetadataCache =
38- builder .build (
39- new CacheLoader <TableKey , Optional <TableMetadata >>() {
40- @ Nonnull
41- @ Override
42- public Optional <TableMetadata > load (@ Nonnull TableKey key ) throws Exception {
43- return getTableMetadataFunc .apply (key );
44- }
45- });
34+ tableMetadataCache = builder .build (getTableMetadataFunc ::apply );
4635 }
4736
4837 /**
@@ -61,22 +50,23 @@ public TableMetadata getTableMetadata(Operation operation) throws ExecutionExcep
6150 }
6251
6352 /**
64- * Returns a table metadata corresponding to the specified operation .
53+ * Returns a table metadata corresponding to the specified namespace and table .
6554 *
6655 * @param namespace a namespace to retrieve
6756 * @param table a table to retrieve
6857 * @return a table metadata. null if the table is not found.
6958 * @throws ExecutionException if the operation fails
7059 */
60+ @ Nullable
7161 public TableMetadata getTableMetadata (String namespace , String table ) throws ExecutionException {
7262 try {
7363 TableKey key = new TableKey (namespace , table );
74- return tableMetadataCache .get (key ). orElse ( null ) ;
75- } catch (java . util . concurrent . ExecutionException e ) {
64+ return tableMetadataCache .get (key );
65+ } catch (CompletionException e ) {
7666 throw new ExecutionException (
7767 CoreError .GETTING_TABLE_METADATA_FAILED .buildMessage (
7868 ScalarDbUtils .getFullTableName (namespace , table )),
79- e );
69+ e . getCause () );
8070 }
8171 }
8272
0 commit comments