Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.database.connector.firebird.metadata.data;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.OptionalInt;
import java.util.concurrent.ConcurrentHashMap;

/**
* Registry for Firebird sizes.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class FirebirdSizeRegistry {

private static final Map<String, Map<String, Integer>> COLUMN_SIZES = new ConcurrentHashMap<>();

/**
* Refresh column sizes for a table.
*
* @param schemaName schema name
* @param tableName table name
* @param columnSizes column sizes map
*/
public static void refreshTable(final String schemaName, final String tableName, final Map<String, Integer> columnSizes) {
if (null == tableName) {
return;
}
String tableKey = buildTableKey(schemaName, tableName);
if (columnSizes.isEmpty()) {
COLUMN_SIZES.remove(tableKey);
return;
}
Map<String, Integer> normalizedColumnSizes = new HashMap<>(columnSizes.size(), 1F);
for (Map.Entry<String, Integer> entry : columnSizes.entrySet()) {
if (null == entry.getKey()) {
continue;
}
normalizedColumnSizes.put(toKey(entry.getKey()), entry.getValue());
}
if (normalizedColumnSizes.isEmpty()) {
COLUMN_SIZES.remove(tableKey);
return;
}
COLUMN_SIZES.put(tableKey, Collections.unmodifiableMap(normalizedColumnSizes));
}

/**
* Find registered column size.
*
* @param schemaName schema name
* @param tableName table name
* @param columnName column name
* @return column size
*/
public static OptionalInt findColumnSize(final String schemaName, final String tableName, final String columnName) {
if (null == tableName || null == columnName) {
return OptionalInt.empty();
}
Map<String, Integer> tableSizes = COLUMN_SIZES.get(buildTableKey(schemaName, tableName));
if (null == tableSizes) {
return OptionalInt.empty();
}
Integer columnSize = tableSizes.get(toKey(columnName));
return null == columnSize ? OptionalInt.empty() : OptionalInt.of(columnSize);
}

private static String buildTableKey(final String schemaName, final String tableName) {
String schemaKey = null == schemaName ? "" : toKey(schemaName);
return schemaKey + "." + toKey(tableName);
}

private static String toKey(final String value) {
return value.toUpperCase(Locale.ENGLISH);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.database.connector.firebird.metadata.data.loader;

import org.apache.shardingsphere.database.connector.core.metadata.data.loader.MetaDataLoaderConnection;
import org.apache.shardingsphere.database.connector.core.metadata.data.loader.MetaDataLoaderMaterial;
import org.apache.shardingsphere.database.connector.core.type.DatabaseTypeRegistry;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

/**
* Loader for Firebird column sizes.
*/
final class FirebirdColumnSizeLoader {

private static final String LOAD_BLOB_SEGMENT_SIZES_SQL = "SELECT TRIM(rf.RDB$FIELD_NAME) AS COLUMN_NAME, "
+ "COALESCE(f.RDB$SEGMENT_LENGTH, 0) AS SEGMENT_SIZE "
+ "FROM RDB$RELATION_FIELDS rf "
+ "JOIN RDB$FIELDS f ON rf.RDB$FIELD_SOURCE = f.RDB$FIELD_NAME "
+ "WHERE TRIM(UPPER(rf.RDB$RELATION_NAME)) = ? "
+ "AND f.RDB$FIELD_TYPE = 261";

private final MetaDataLoaderMaterial material;

FirebirdColumnSizeLoader(final MetaDataLoaderMaterial material) {
this.material = material;
}

Map<String, Map<String, Integer>> load() throws SQLException {
if (material.getActualTableNames().isEmpty()) {
return Collections.emptyMap();
}
Map<String, Map<String, Integer>> result = new HashMap<>(material.getActualTableNames().size(), 1F);
DatabaseTypeRegistry databaseTypeRegistry = new DatabaseTypeRegistry(material.getStorageType());
try (MetaDataLoaderConnection connection = new MetaDataLoaderConnection(material.getStorageType(), material.getDataSource().getConnection())) {
for (String each : material.getActualTableNames()) {
String formattedTableName = databaseTypeRegistry.formatIdentifierPattern(each);
Map<String, Integer> columnSizes = loadTableColumnSizes(connection, formattedTableName);
result.put(each, columnSizes);
}
}
return result;
}

private Map<String, Integer> loadTableColumnSizes(final MetaDataLoaderConnection connection, final String formattedTableName) throws SQLException {
Map<String, Integer> result = new HashMap<>();
loadColumnSizesFromMetaData(connection, formattedTableName, result);
loadBlobSegmentSizes(connection, formattedTableName, result);
return result.isEmpty() ? Collections.emptyMap() : Collections.unmodifiableMap(result);
}

private void loadColumnSizesFromMetaData(final MetaDataLoaderConnection connection, final String formattedTableName, final Map<String, Integer> result) throws SQLException {
try (ResultSet resultSet = connection.getMetaData().getColumns(connection.getCatalog(), connection.getSchema(), formattedTableName, "%")) {
while (resultSet.next()) {
if (!Objects.equals(formattedTableName, resultSet.getString("TABLE_NAME"))) {
continue;
}
int dataType = resultSet.getInt("DATA_TYPE");
if (!isDynamicLengthType(dataType)) {
continue;
}
String columnName = resultSet.getString("COLUMN_NAME");
if (null != columnName) {
int columnSize = resultSet.getInt("COLUMN_SIZE");
if (!resultSet.wasNull() && columnSize > 0) {
result.put(columnName.toUpperCase(Locale.ENGLISH), columnSize);
}
}
}
}
}

private void loadBlobSegmentSizes(final MetaDataLoaderConnection connection, final String formattedTableName, final Map<String, Integer> result) throws SQLException {
try (PreparedStatement preparedStatement = connection.prepareStatement(LOAD_BLOB_SEGMENT_SIZES_SQL)) {
preparedStatement.setString(1, formattedTableName.toUpperCase(Locale.ENGLISH));
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
String columnName = resultSet.getString("COLUMN_NAME");
if (null == columnName) {
continue;
}
String trimmedColumnName = columnName.trim();
if (trimmedColumnName.isEmpty()) {
continue;
}
result.put(trimmedColumnName.toUpperCase(Locale.ENGLISH), resultSet.getInt("SEGMENT_SIZE"));
}
}
}
}

private boolean isDynamicLengthType(final int dataType) {
switch (dataType) {
case Types.CHAR:
case Types.NCHAR:
case Types.VARCHAR:
case Types.NVARCHAR:
case Types.LONGVARCHAR:
case Types.LONGNVARCHAR:
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
return true;
default:
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.database.connector.firebird.metadata.data.loader;

import org.apache.shardingsphere.database.connector.core.metadata.data.loader.DialectMetaDataLoader;
import org.apache.shardingsphere.database.connector.core.metadata.data.loader.MetaDataLoaderMaterial;
import org.apache.shardingsphere.database.connector.core.metadata.data.loader.type.TableMetaDataLoader;
import org.apache.shardingsphere.database.connector.core.metadata.data.model.SchemaMetaData;
import org.apache.shardingsphere.database.connector.core.metadata.data.model.TableMetaData;
import org.apache.shardingsphere.database.connector.firebird.metadata.data.FirebirdSizeRegistry;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Map;

/**
* Meta data loader for Firebird.
*/
public final class FirebirdMetaDataLoader implements DialectMetaDataLoader {

@Override
public Collection<SchemaMetaData> load(final MetaDataLoaderMaterial material) throws SQLException {
Collection<TableMetaData> tableMetaData = new LinkedList<>();
for (String each : material.getActualTableNames()) {
TableMetaDataLoader.load(material.getDataSource(), each, material.getStorageType()).ifPresent(tableMetaData::add);
}
Map<String, Map<String, Integer>> columnSizes = new FirebirdColumnSizeLoader(material).load();
for (String each : material.getActualTableNames()) {
Map<String, Integer> tableSizes = columnSizes.getOrDefault(each, Collections.emptyMap());
FirebirdSizeRegistry.refreshTable(material.getDefaultSchemaName(), each, tableSizes);
}
return Collections.singleton(new SchemaMetaData(material.getDefaultSchemaName(), tableMetaData));
}

@Override
public String getDatabaseType() {
return "Firebird";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

org.apache.shardingsphere.database.connector.firebird.metadata.data.loader.FirebirdMetaDataLoader
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.database.connector.firebird.metadata.data;

import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.OptionalInt;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class FirebirdSizeRegistryTest {

@Test
void assertRefreshAndFindColumnSize() {
Map<String, Integer> columnSizes = Collections.singletonMap("varchar_col", 64);
FirebirdSizeRegistry.refreshTable("schema_a", "table_a", columnSizes);
OptionalInt actual = FirebirdSizeRegistry.findColumnSize("schema_a", "table_a", "VARCHAR_COL");
assertTrue(actual.isPresent());
assertThat(actual.getAsInt(), is(64));
FirebirdSizeRegistry.refreshTable("schema_a", "table_a", Collections.emptyMap());
}

@Test
void assertRefreshTableRemovesEntryWhenEmptyColumnSizesProvided() {
FirebirdSizeRegistry.refreshTable("schema_b", "table_b", Collections.singletonMap("col", 32));
FirebirdSizeRegistry.refreshTable("schema_b", "table_b", Collections.emptyMap());
assertFalse(FirebirdSizeRegistry.findColumnSize("schema_b", "table_b", "COL").isPresent());
}

@Test
void assertRefreshTableSkipsNullColumnNames() {
Map<String, Integer> columnSizes = new HashMap<>(2, 1F);
columnSizes.put("valid", 12);
columnSizes.put(null, 24);
FirebirdSizeRegistry.refreshTable("schema_c", "table_c", columnSizes);
OptionalInt actual = FirebirdSizeRegistry.findColumnSize("schema_c", "table_c", "VaLiD");
assertTrue(actual.isPresent());
assertThat(actual.getAsInt(), is(12));
assertFalse(FirebirdSizeRegistry.findColumnSize("schema_c", "table_c", null).isPresent());
FirebirdSizeRegistry.refreshTable("schema_c", "table_c", Collections.emptyMap());
}

@Test
void assertRefreshTableRemovesWhenAllColumnsInvalid() {
Map<String, Integer> columnSizes = new HashMap<>(1, 1F);
columnSizes.put(null, 48);
FirebirdSizeRegistry.refreshTable("schema_d", "table_d", columnSizes);
assertFalse(FirebirdSizeRegistry.findColumnSize("schema_d", "table_d", "any").isPresent());
}
}
Loading