Skip to content

Commit

Permalink
Fix primary database not filter
Browse files Browse the repository at this point in the history
  • Loading branch information
flaming-archer committed Sep 26, 2023
1 parent 88cc45a commit 602f261
Showing 1 changed file with 55 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public StaticDatabaseMappingService(
QueryMapping queryMapping) {
this.metaStoreMappingFactory = metaStoreMappingFactory;
this.queryMapping = queryMapping;
mappingsByMetaStoreName = Collections.synchronizedMap(new LinkedHashMap<>());
mappingsByDatabaseName = Collections.synchronizedMap(new LinkedHashMap<>());
databaseMappingToDatabaseList = new ConcurrentHashMap<>();
databaseToTableAllowList = new ConcurrentHashMap<>();

primaryDatabasesCache = CacheBuilder
.newBuilder()
.expireAfterAccess(1, TimeUnit.MINUTES)
Expand All @@ -94,17 +99,14 @@ public StaticDatabaseMappingService(
@Override
public List<String> load(String key) throws Exception {
if (primaryDatabaseMapping != null) {
return primaryDatabaseMapping.getClient().get_all_databases();
return new StaticDatabaseMappingPanopticOperationHandler()
.getPrimaryAllDatabases();
} else {
return Lists.newArrayList();
}
}
});

mappingsByMetaStoreName = Collections.synchronizedMap(new LinkedHashMap<>());
mappingsByDatabaseName = Collections.synchronizedMap(new LinkedHashMap<>());
databaseMappingToDatabaseList = new ConcurrentHashMap<>();
databaseToTableAllowList = new ConcurrentHashMap<>();
for (AbstractMetaStore federatedMetaStore : initialMetastores) {
add(federatedMetaStore);
}
Expand Down Expand Up @@ -363,47 +365,64 @@ private boolean databaseAndTableAllowed(String database, String table, DatabaseM

@Override
public PanopticOperationHandler getPanopticOperationHandler() {
return new PanopticOperationHandler() {
return new StaticDatabaseMappingPanopticOperationHandler();
}

@Override
public List<TableMeta> getTableMeta(String db_patterns, String tbl_patterns, List<String> tbl_types) {
class StaticDatabaseMappingPanopticOperationHandler extends PanopticOperationHandler {

BiFunction<TableMeta, DatabaseMapping, Boolean> filter = (tableMeta, mapping) ->
databaseAndTableAllowed(tableMeta.getDbName(), tableMeta.getTableName(), mapping);
@Override
public List<TableMeta> getTableMeta(String db_patterns, String tbl_patterns,
List<String> tbl_types) {

Map<DatabaseMapping, String> mappingsForPattern = new LinkedHashMap<>();
for (DatabaseMapping mapping : getAvailableDatabaseMappings()) {
mappingsForPattern.put(mapping, db_patterns);
}
return super.getTableMeta(tbl_patterns, tbl_types, mappingsForPattern, filter);
BiFunction<TableMeta, DatabaseMapping, Boolean> filter = (tableMeta, mapping) ->
databaseAndTableAllowed(tableMeta.getDbName(), tableMeta.getTableName(), mapping);

Map<DatabaseMapping, String> mappingsForPattern = new LinkedHashMap<>();
for (DatabaseMapping mapping : getAvailableDatabaseMappings()) {
mappingsForPattern.put(mapping, db_patterns);
}
return super.getTableMeta(tbl_patterns, tbl_types, mappingsForPattern, filter);
}

@Override
public List<String> getAllDatabases(String pattern) {
BiFunction<String, DatabaseMapping, Boolean> filter = (database, mapping) -> mappingsByDatabaseName
.containsKey(database);
@Override
public List<String> getAllDatabases(String pattern) {
BiFunction<String, DatabaseMapping, Boolean> filter = getFilter();

BiFunction<String, DatabaseMapping, Boolean> filter1 = (database, mapping) -> filter.apply(database, mapping)
&& databaseMappingToDatabaseList.get(mapping.getMetastoreMappingName()).contains(database);
Map<DatabaseMapping, String> mappingsForPattern = new LinkedHashMap<>();
for (DatabaseMapping mapping : getAllDatabaseMappings()) {
mappingsForPattern.put(mapping, pattern);
}

Map<DatabaseMapping, String> mappingsForPattern = new LinkedHashMap<>();
for (DatabaseMapping mapping : getAllDatabaseMappings()) {
mappingsForPattern.put(mapping, pattern);
}
return super.getAllDatabases(mappingsForPattern, filter);
}

return super.getAllDatabases(mappingsForPattern, filter1);
}
private BiFunction<String, DatabaseMapping, Boolean> getFilter() {
BiFunction<String, DatabaseMapping, Boolean> filter =
(database, mapping) -> mappingsByDatabaseName.containsKey(database);

@Override
public List<String> getAllDatabases() {
return new ArrayList<>(mappingsByDatabaseName.keySet());
}
return (database, mapping) -> filter.apply(database, mapping)
&& databaseMappingToDatabaseList.get(mapping.getMetastoreMappingName())
.contains(database);
}

@Override
protected PanopticOperationExecutor getPanopticOperationExecutor() {
return new PanopticConcurrentOperationExecutor();
}
};
@Override
public List<String> getAllDatabases() {
return new ArrayList<>(mappingsByDatabaseName.keySet());
}

public List<String> getPrimaryAllDatabases() {
BiFunction<String, DatabaseMapping, Boolean> filter = getFilter();

Map<DatabaseMapping, String> mappingsForPattern = new LinkedHashMap<>();
mappingsForPattern.put(primaryDatabaseMapping, "*");

return super.getAllDatabases(mappingsForPattern, filter);
}

@Override
protected PanopticOperationExecutor getPanopticOperationExecutor() {
return new PanopticConcurrentOperationExecutor();
}
}

@Override
Expand Down

0 comments on commit 602f261

Please sign in to comment.