Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#471] Fix table name truncate: make jdbc table 63 charter #473

Merged
merged 2 commits into from
Feb 3, 2025
Merged
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
Expand Up @@ -11,10 +11,13 @@
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions Copyright [year] [name of copyright owner]".
*
* Copyright 2024 3A Systems, LLC.
* Copyright 2024-2025 3A Systems, LLC.
*/
package org.opends.server.backends.jdbc;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.forgerock.opendj.config.server.ConfigChangeResult;
Expand All @@ -31,8 +34,11 @@
import org.opends.server.types.RestoreConfig;
import org.opends.server.util.BackupManager;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.sql.*;
import java.util.*;
import java.util.concurrent.ExecutionException;

import static org.opends.server.backends.pluggable.spi.StorageUtils.addErrorMessage;
import static org.opends.server.util.StaticUtils.stackTraceToSingleLineString;
Expand Down Expand Up @@ -108,9 +114,28 @@ public void close() {
storageStatus = StorageStatus.lockedDown(LocalizableMessage.raw("closed"));
}

LoadingCache<TreeName,String> tree2table= CacheBuilder.newBuilder()
.build(new CacheLoader<TreeName, String>() {
@Override
public String load(TreeName treeName) throws Exception {
final MessageDigest md = MessageDigest.getInstance("SHA-224");
final byte[] messageDigest = md.digest(treeName.toString().getBytes());
final BigInteger no = new BigInteger(1, messageDigest);
String hashtext = no.toString(16);
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
return "opendj_"+hashtext;
}
});

String getTableName(TreeName treeName) {
return "\"OpenDJ"+treeName.toString()+"\"";
}
try {
return tree2table.get(treeName);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}

@Override
public void removeStorageFiles() throws StorageRuntimeException {
Expand Down Expand Up @@ -215,10 +240,23 @@ public WriteableTransactionTransactionImpl(Connection con) {
isReadOnly=false;
}

boolean isExistsTable(TreeName treeName) {
try(final ResultSet rs = con.getMetaData().getTables(null, null, tree2table.get(treeName), new String[]{"TABLE"})) {
while (rs.next()) {
if (tree2table.get(treeName).equals(rs.getString("TABLE_NAME"))){
return true;
}
}
} catch (Exception e) {
throw new StorageRuntimeException(e);
}
return false;
}

@Override
public void openTree(TreeName treeName, boolean createOnDemand) {
if (createOnDemand) {
try (final PreparedStatement statement=con.prepareStatement("create table if not exists "+getTableName(treeName)+" (k bytea primary key,v bytea)")){
if (createOnDemand && !isExistsTable(treeName)) {
try (final PreparedStatement statement=con.prepareStatement("create table "+getTableName(treeName)+" (k bytea primary key,v bytea)")){
execute(statement);
con.commit();
}catch (SQLException e) {
Expand All @@ -238,11 +276,13 @@ public void clearTree(TreeName treeName) {

@Override
public void deleteTree(TreeName treeName) {
try (final PreparedStatement statement=con.prepareStatement("drop table "+getTableName(treeName))){
execute(statement);
con.commit();
}catch (SQLException e) {
throw new StorageRuntimeException(e);
if (isExistsTable(treeName)) {
try (final PreparedStatement statement = con.prepareStatement("drop table " + getTableName(treeName))) {
execute(statement);
con.commit();
} catch (SQLException e) {
throw new StorageRuntimeException(e);
}
}
}

Expand Down Expand Up @@ -459,18 +499,8 @@ public boolean positionToIndex(int index) {

@Override
public Set<TreeName> listTrees() {
final Set<TreeName> res=new HashSet<>();
try(final Connection con=getConnection();
final ResultSet rs = con.getMetaData().getTables(null, null, "OpenDJ%", new String[]{"TABLE"})) {
while (rs.next()) {
res.add(TreeName.valueOf(rs.getString("TABLE_NAME").substring(6)));
}
} catch (Exception e) {
throw new StorageRuntimeException(e);
}
return res;
return tree2table.asMap().keySet();
}


private final class ImporterImpl implements Importer {
final Connection con;
Expand Down