Skip to content

Commit

Permalink
Unified ShardingSphereData metadata api (apache#23851)
Browse files Browse the repository at this point in the history
* Unified ShardingSphereData metadata api

* Fix checkstyle
  • Loading branch information
zhaojinchao95 authored Jan 31, 2023
1 parent c66ee26 commit 630f0fb
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,43 @@
public final class ShardingSphereData {

private final Map<String, ShardingSphereDatabaseData> databaseData = new LinkedHashMap<>();

/**
* Get ShardingSphere database.
*
* @param databaseName database name
* @return ShardingSphere database data
*/
public ShardingSphereDatabaseData getDatabase(final String databaseName) {
return databaseData.get(databaseName.toLowerCase());
}

/**
* Put ShardingSphere database.
*
* @param databaseName database name
* @param database ShardingSphere database
*/
public void putDatabase(final String databaseName, final ShardingSphereDatabaseData database) {
databaseData.put(databaseName.toLowerCase(), database);
}

/**
* Drop ShardingSphere database.
*
* @param databaseName database name
*/
public void dropDatabase(final String databaseName) {
databaseData.remove(databaseName.toLowerCase());
}

/**
* Judge contains ShardingSphere database from meta data or not.
*
* @param databaseName database name
* @return contains ShardingSphere database from meta data or not
*/
public boolean containsDatabase(final String databaseName) {
return databaseData.containsKey(databaseName.toLowerCase());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,43 @@
public final class ShardingSphereDatabaseData {

private final Map<String, ShardingSphereSchemaData> schemaData = new LinkedHashMap<>();

/**
* Get ShardingSphere schema data.
*
* @param schemaName schema name
* @return ShardingSphere schema data
*/
public ShardingSphereSchemaData getSchema(final String schemaName) {
return schemaData.get(schemaName.toLowerCase());
}

/**
* Put ShardingSphere schema data.
*
* @param schemaName schema name
* @param schema ShardingSphere schema data
*/
public void putSchema(final String schemaName, final ShardingSphereSchemaData schema) {
schemaData.put(schemaName.toLowerCase(), schema);
}

/**
* Remove ShardingSphere schema data.
*
* @param schemaName schema name
*/
public void removeSchema(final String schemaName) {
schemaData.remove(schemaName.toLowerCase());
}

/**
* Judge contains ShardingSphere schema from ShardingSphere database or not.
*
* @param schemaName schema name
* @return Contains schema from database or not
*/
public boolean containsSchema(final String schemaName) {
return schemaData.containsKey(schemaName.toLowerCase());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,43 @@
public final class ShardingSphereSchemaData {

private final Map<String, ShardingSphereTableData> tableData = new LinkedHashMap<>();

/**
* Get ShardingSphere table meta data via table name.
*
* @param tableName tableName table name
* @return ShardingSphere table data
*/
public ShardingSphereTableData getTable(final String tableName) {
return tableData.get(tableName.toLowerCase());
}

/**
* Add ShardingSphere table data.
*
* @param tableName table name
* @param table ShardingSphere table data
*/
public void putTable(final String tableName, final ShardingSphereTableData table) {
tableData.put(tableName.toLowerCase(), table);
}

/**
* Remove ShardingSphere table meta data.
*
* @param tableName table name
*/
public void removeTable(final String tableName) {
tableData.remove(tableName.toLowerCase());
}

/**
* Judge contains ShardingSphere table from table metadata or not.
*
* @param tableName table name
* @return contains ShardingSphere table from table metadata or not
*/
public boolean containsTable(final String tableName) {
return tableData.containsKey(tableName.toLowerCase());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.data.ShardingSphereDatabaseData;
import org.apache.shardingsphere.infra.metadata.data.ShardingSphereRowData;
import org.apache.shardingsphere.infra.metadata.data.ShardingSphereSchemaData;
import org.apache.shardingsphere.infra.metadata.data.ShardingSphereTableData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
Expand Down Expand Up @@ -563,21 +562,21 @@ private synchronized void reloadTable(final String databaseName, final String sc
* @param databaseName database name
*/
public synchronized void addShardingSphereDatabaseData(final String databaseName) {
if (metaDataContexts.getShardingSphereData().getDatabaseData().containsKey(databaseName)) {
if (metaDataContexts.getShardingSphereData().containsDatabase(databaseName)) {
return;
}
metaDataContexts.getShardingSphereData().getDatabaseData().put(databaseName, new ShardingSphereDatabaseData());
metaDataContexts.getShardingSphereData().putDatabase(databaseName, new ShardingSphereDatabaseData());
}

/**
* Drop ShardingSphere data database.
* @param databaseName database name
*/
public synchronized void dropShardingSphereDatabaseData(final String databaseName) {
if (!metaDataContexts.getShardingSphereData().getDatabaseData().containsKey(databaseName.toLowerCase())) {
if (!metaDataContexts.getShardingSphereData().containsDatabase(databaseName)) {
return;
}
metaDataContexts.getShardingSphereData().getDatabaseData().remove(databaseName);
metaDataContexts.getShardingSphereData().dropDatabase(databaseName);
}

/**
Expand All @@ -587,10 +586,10 @@ public synchronized void dropShardingSphereDatabaseData(final String databaseNam
* @param schemaName schema name
*/
public synchronized void addShardingSphereSchemaData(final String databaseName, final String schemaName) {
if (metaDataContexts.getShardingSphereData().getDatabaseData().get(databaseName).getSchemaData().containsKey(schemaName)) {
if (metaDataContexts.getShardingSphereData().getDatabase(databaseName).containsSchema(schemaName)) {
return;
}
metaDataContexts.getShardingSphereData().getDatabaseData().get(databaseName).getSchemaData().put(schemaName, new ShardingSphereSchemaData());
metaDataContexts.getShardingSphereData().getDatabase(databaseName).putSchema(schemaName, new ShardingSphereSchemaData());
}

/**
Expand All @@ -600,11 +599,11 @@ public synchronized void addShardingSphereSchemaData(final String databaseName,
* @param schemaName schema name
*/
public synchronized void dropShardingSphereSchemaData(final String databaseName, final String schemaName) {
ShardingSphereDatabaseData databaseData = metaDataContexts.getShardingSphereData().getDatabaseData().get(databaseName);
if (null == databaseData || !databaseData.getSchemaData().containsKey(schemaName)) {
ShardingSphereDatabaseData databaseData = metaDataContexts.getShardingSphereData().getDatabase(databaseName);
if (null == databaseData || !databaseData.containsSchema(schemaName)) {
return;
}
databaseData.getSchemaData().remove(schemaName);
databaseData.removeSchema(schemaName);
}

/**
Expand All @@ -615,15 +614,13 @@ public synchronized void dropShardingSphereSchemaData(final String databaseName,
* @param tableName table name
*/
public synchronized void addShardingSphereTableData(final String databaseName, final String schemaName, final String tableName) {
if (!metaDataContexts.getShardingSphereData().getDatabaseData().containsKey(databaseName)
|| !metaDataContexts.getShardingSphereData().getDatabaseData().get(databaseName).getSchemaData().containsKey(schemaName)) {
if (!metaDataContexts.getShardingSphereData().containsDatabase(databaseName) || !metaDataContexts.getShardingSphereData().getDatabase(databaseName).containsSchema(schemaName)) {
return;
}
if (metaDataContexts.getShardingSphereData().getDatabaseData().get(databaseName).getSchemaData().get(schemaName).getTableData().containsKey(tableName)) {
if (metaDataContexts.getShardingSphereData().getDatabase(databaseName).getSchema(schemaName).containsTable(tableName)) {
return;
}
ShardingSphereDatabaseData database = metaDataContexts.getShardingSphereData().getDatabaseData().get(databaseName);
database.getSchemaData().get(schemaName).getTableData().put(tableName, new ShardingSphereTableData(tableName));
metaDataContexts.getShardingSphereData().getDatabase(databaseName).getSchema(schemaName).putTable(tableName, new ShardingSphereTableData(tableName));
}

/**
Expand All @@ -634,11 +631,10 @@ public synchronized void addShardingSphereTableData(final String databaseName, f
* @param tableName table name
*/
public synchronized void dropShardingSphereTableData(final String databaseName, final String schemaName, final String tableName) {
if (!metaDataContexts.getShardingSphereData().getDatabaseData().containsKey(databaseName)
|| !metaDataContexts.getShardingSphereData().getDatabaseData().get(databaseName).getSchemaData().containsKey(schemaName)) {
if (!metaDataContexts.getShardingSphereData().containsDatabase(databaseName) || !metaDataContexts.getShardingSphereData().getDatabase(databaseName).containsSchema(schemaName)) {
return;
}
metaDataContexts.getShardingSphereData().getDatabaseData().get(databaseName).getSchemaData().get(schemaName).getTableData().remove(tableName);
metaDataContexts.getShardingSphereData().getDatabase(databaseName).getSchema(schemaName).removeTable(tableName);
}

/**
Expand All @@ -650,19 +646,17 @@ public synchronized void dropShardingSphereTableData(final String databaseName,
* @param yamlRowData yaml row data
*/
public synchronized void alterShardingSphereRowData(final String databaseName, final String schemaName, final String tableName, final YamlShardingSphereRowData yamlRowData) {
if (!metaDataContexts.getShardingSphereData().getDatabaseData().containsKey(databaseName)
|| !metaDataContexts.getShardingSphereData().getDatabaseData().get(databaseName).getSchemaData().containsKey(schemaName)
|| !metaDataContexts.getShardingSphereData().getDatabaseData().get(databaseName).getSchemaData().get(schemaName).getTableData().containsKey(tableName)) {
if (!metaDataContexts.getShardingSphereData().containsDatabase(databaseName) || !metaDataContexts.getShardingSphereData().getDatabase(databaseName).containsSchema(schemaName)
|| !metaDataContexts.getShardingSphereData().getDatabase(databaseName).getSchema(schemaName).containsTable(tableName)) {
return;
}
if (!metaDataContexts.getMetaData().containsDatabase(databaseName) || !metaDataContexts.getMetaData().getDatabase(databaseName).containsSchema(schemaName)
|| !metaDataContexts.getMetaData().getDatabase(databaseName).getSchema(schemaName).containsTable(tableName)) {
return;
}
ShardingSphereTableData tableData = metaDataContexts.getShardingSphereData().getDatabaseData().get(databaseName).getSchemaData().get(schemaName).getTableData().get(tableName);
ShardingSphereTableData tableData = metaDataContexts.getShardingSphereData().getDatabase(databaseName).getSchema(schemaName).getTable(tableName);
List<ShardingSphereColumn> columns = new ArrayList<>(metaDataContexts.getMetaData().getDatabase(databaseName).getSchema(schemaName).getTable(tableName).getColumns().values());
ShardingSphereRowData rowData = new YamlShardingSphereRowDataSwapper(columns).swapToObject(yamlRowData);
tableData.getRows().add(rowData);
tableData.getRows().add(new YamlShardingSphereRowDataSwapper(columns).swapToObject(yamlRowData));
}

/**
Expand All @@ -674,13 +668,11 @@ public synchronized void alterShardingSphereRowData(final String databaseName, f
* @param uniqueKey row uniqueKey
*/
public synchronized void deleteShardingSphereRowData(final String databaseName, final String schemaName, final String tableName, final String uniqueKey) {
if (!metaDataContexts.getShardingSphereData().getDatabaseData().containsKey(databaseName)
|| !metaDataContexts.getShardingSphereData().getDatabaseData().get(databaseName).getSchemaData().containsKey(schemaName)
|| !metaDataContexts.getShardingSphereData().getDatabaseData().get(databaseName).getSchemaData().get(schemaName).getTableData().containsKey(tableName)) {
if (!metaDataContexts.getShardingSphereData().containsDatabase(databaseName) || !metaDataContexts.getShardingSphereData().getDatabase(databaseName).containsSchema(schemaName)
|| !metaDataContexts.getShardingSphereData().getDatabase(databaseName).getSchema(schemaName).containsTable(tableName)) {
return;
}
ShardingSphereTableData tableData = metaDataContexts.getShardingSphereData().getDatabaseData().get(databaseName).getSchemaData().get(schemaName).getTableData().get(tableName);
tableData.getRows().removeIf(each -> uniqueKey.equals(each.getUniqueKey()));
metaDataContexts.getShardingSphereData().getDatabase(databaseName).getSchema(schemaName).getTable(tableName).getRows().removeIf(each -> uniqueKey.equals(each.getUniqueKey()));
}

@Override
Expand Down

0 comments on commit 630f0fb

Please sign in to comment.