85
85
import java .util .Optional ;
86
86
import java .util .Set ;
87
87
import java .util .TimeZone ;
88
+ import java .util .concurrent .ConcurrentHashMap ;
89
+ import java .util .concurrent .ConcurrentMap ;
88
90
import java .util .stream .Stream ;
89
91
90
92
import static com .facebook .presto .hive .HiveStatisticsUtil .createPartitionStatistics ;
@@ -152,6 +154,7 @@ public class IcebergHiveMetadata
152
154
private final HdfsEnvironment hdfsEnvironment ;
153
155
private final DateTimeZone timeZone = DateTimeZone .forTimeZone (TimeZone .getTimeZone (ZoneId .of (TimeZone .getDefault ().getID ())));
154
156
private final IcebergHiveTableOperationsConfig hiveTableOeprationsConfig ;
157
+ private final ConcurrentMap <SchemaTableName , Optional <Table >> tablesCache = new ConcurrentHashMap <>();
155
158
156
159
public IcebergHiveMetadata (
157
160
ExtendedHiveMetastore metastore ,
@@ -191,8 +194,7 @@ protected View getIcebergView(ConnectorSession session, SchemaTableName schemaTa
191
194
@ Override
192
195
protected boolean tableExists (ConnectorSession session , SchemaTableName schemaTableName )
193
196
{
194
- IcebergTableName name = IcebergTableName .from (schemaTableName .getTableName ());
195
- Optional <Table > hiveTable = metastore .getTable (getMetastoreContext (session ), schemaTableName .getSchemaName (), name .getTableName ());
197
+ Optional <Table > hiveTable = getHiveTable (session , schemaTableName );
196
198
if (!hiveTable .isPresent ()) {
197
199
return false ;
198
200
}
@@ -202,6 +204,14 @@ protected boolean tableExists(ConnectorSession session, SchemaTableName schemaTa
202
204
return true ;
203
205
}
204
206
207
+ private Optional <Table > getHiveTable (ConnectorSession session , SchemaTableName schemaTableName )
208
+ {
209
+ IcebergTableName name = IcebergTableName .from (schemaTableName .getTableName ());
210
+ return tablesCache .computeIfAbsent (
211
+ schemaTableName ,
212
+ ignored -> metastore .getTable (getMetastoreContext (session ), schemaTableName .getSchemaName (), name .getTableName ()));
213
+ }
214
+
205
215
@ Override
206
216
public List <String > listSchemaNames (ConnectorSession session )
207
217
{
@@ -369,7 +379,7 @@ public void createView(ConnectorSession session, ConnectorTableMetadata viewMeta
369
379
encodeViewData (viewData ));
370
380
PrincipalPrivileges principalPrivileges = buildInitialPrivilegeSet (session .getUser ());
371
381
372
- Optional <Table > existing = metastore . getTable ( metastoreContext , viewName . getSchemaName (), viewName . getTableName () );
382
+ Optional <Table > existing = getHiveTable ( session , viewName );
373
383
if (existing .isPresent ()) {
374
384
if (!replace || !isPrestoView (existing .get ())) {
375
385
throw new ViewAlreadyExistsException (viewName );
@@ -413,7 +423,7 @@ public Map<SchemaTableName, ConnectorViewDefinition> getViews(ConnectorSession s
413
423
}
414
424
MetastoreContext metastoreContext = getMetastoreContext (session );
415
425
for (SchemaTableName schemaTableName : tableNames ) {
416
- Optional <Table > table = metastore . getTable ( metastoreContext , schemaTableName . getSchemaName (), schemaTableName . getTableName () );
426
+ Optional <Table > table = getHiveTable ( session , schemaTableName );
417
427
if (table .isPresent () && isPrestoView (table .get ())) {
418
428
verifyAndPopulateViews (table .get (), schemaTableName , decodeViewData (table .get ().getViewOriginalText ().get ()), views );
419
429
}
@@ -527,7 +537,7 @@ public void finishStatisticsCollection(ConnectorSession session, ConnectorTableH
527
537
{
528
538
IcebergTableHandle icebergTableHandle = (IcebergTableHandle ) tableHandle ;
529
539
MetastoreContext metastoreContext = getMetastoreContext (session );
530
- Table table = metastore . getTable ( metastoreContext , icebergTableHandle .getSchemaTableName (). getSchemaName (), icebergTableHandle . getSchemaTableName (). getTableName ())
540
+ Table table = getHiveTable ( session , icebergTableHandle .getSchemaTableName ())
531
541
.orElseThrow (() -> new TableNotFoundException (icebergTableHandle .getSchemaTableName ()));
532
542
533
543
List <Column > partitionColumns = table .getPartitionColumns ();
0 commit comments