Skip to content

Commit 775d573

Browse files
authored
Merge branch 'master' into fix-CoreError-message-parameter-mismatches
2 parents 3cc9cc9 + a117037 commit 775d573

File tree

20 files changed

+186
-168
lines changed

20 files changed

+186
-168
lines changed

core/src/integration-test/java/com/scalar/db/storage/jdbc/JdbcAdminTestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private void execute(String sql) throws SQLException {
9090
@Override
9191
public boolean tableExists(String namespace, String table) throws Exception {
9292
String fullTableName = rdbEngine.encloseFullTableName(namespace, table);
93-
String sql = rdbEngine.tableExistsInternalTableCheckSql(fullTableName);
93+
String sql = rdbEngine.internalTableExistsCheckSql(fullTableName);
9494
try (Connection connection = dataSource.getConnection();
9595
Statement statement = connection.createStatement()) {
9696
statement.execute(sql);

core/src/integration-test/java/com/scalar/db/storage/multistorage/MultiStorageAdminTestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private boolean tableExistsOnCassandra(String namespace, String table) {
227227

228228
private boolean tableExistsOnJdbc(String namespace, String table) throws Exception {
229229
String fullTableName = rdbEngine.encloseFullTableName(namespace, table);
230-
String sql = rdbEngine.tableExistsInternalTableCheckSql(fullTableName);
230+
String sql = rdbEngine.internalTableExistsCheckSql(fullTableName);
231231
try (Connection connection = dataSource.getConnection();
232232
Statement statement = connection.createStatement()) {
233233
statement.execute(sql);

core/src/main/java/com/scalar/db/api/AuthAdmin.java

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -198,23 +198,23 @@ default void dropRole(String roleName) throws ExecutionException {
198198
}
199199

200200
/**
201-
* Retrieves a list of {@link RoleDetail}s.
201+
* Retrieves a list of {@link Role}s.
202202
*
203-
* @return a list of {@link RoleDetail}s
203+
* @return a list of {@link Role}s
204204
* @throws ExecutionException if the operation fails
205205
*/
206-
default List<RoleDetail> getRoles() throws ExecutionException {
206+
default List<Role> getRoles() throws ExecutionException {
207207
throw new UnsupportedOperationException(CoreError.AUTH_NOT_ENABLED.buildMessage());
208208
}
209209

210210
/**
211-
* Retrieves a list of {@link UserRoleDetail}s for the given user.
211+
* Retrieves a list of {@link RoleForUser}s for the given user.
212212
*
213213
* @param username the username
214-
* @return a list of {@link UserRoleDetail}s for the given user
214+
* @return a list of {@link RoleForUser}s for the given user
215215
* @throws ExecutionException if the operation fails
216216
*/
217-
default List<UserRoleDetail> getRolesForUser(String username) throws ExecutionException {
217+
default List<RoleForUser> getRolesForUser(String username) throws ExecutionException {
218218
throw new UnsupportedOperationException(CoreError.AUTH_NOT_ENABLED.buildMessage());
219219
}
220220

@@ -258,13 +258,13 @@ default void revokeAdminOptionFromUser(String username, String roleName)
258258
}
259259

260260
/**
261-
* Retrieves a list of {@link UserRole}s for the given role.
261+
* Retrieves a list of {@link GranteeUserRef}s for the given role.
262262
*
263263
* @param roleName the role name
264-
* @return a list of {@link UserRole}s for the given role
264+
* @return a list of {@link GranteeUserRef}s for the given role
265265
* @throws ExecutionException if the operation fails
266266
*/
267-
default List<UserRole> getUsersForRole(String roleName) throws ExecutionException {
267+
default List<GranteeUserRef> getGranteeUsersForRole(String roleName) throws ExecutionException {
268268
throw new UnsupportedOperationException(CoreError.AUTH_NOT_ENABLED.buildMessage());
269269
}
270270

@@ -399,53 +399,83 @@ default void revokePrivilegeFromRole(
399399

400400
/** Represents a user. */
401401
interface User {
402+
/**
403+
* Returns the username.
404+
*
405+
* @return the username
406+
*/
402407
String getName();
403408

409+
/**
410+
* Returns whether the user is a superuser.
411+
*
412+
* @return whether the user is a superuser
413+
*/
404414
boolean isSuperuser();
405415
}
406416

407-
/** Represents a role. */
417+
/** Represents a role, including its granted roles. */
408418
interface Role {
419+
/**
420+
* Returns the role name.
421+
*
422+
* @return the role name
423+
*/
409424
String getName();
410-
}
411-
412-
/** Represents a role with its hierarchy information. */
413-
interface RoleDetail {
414-
Role getRole();
415425

416-
List<RoleHierarchy> getRoleHierarchies();
426+
/**
427+
* Returns the roles granted to the role.
428+
*
429+
* @return the roles granted to the role
430+
*/
431+
List<GrantedRoleRef> getGrantedRoles();
417432
}
418433

419434
/**
420-
* Represents a role detail for a specific user, including whether the user has admin option for
435+
* Represents a role granted to a specific user, including whether the user has admin option for
421436
* this role.
422437
*/
423-
interface UserRoleDetail extends RoleDetail {
438+
interface RoleForUser extends Role {
424439
/**
425440
* Returns whether the user has admin option for this role. This is distinct from the admin
426441
* option in role hierarchies, which applies to role-to-role grants.
442+
*
443+
* @return whether the user has admin option for this role
427444
*/
428445
boolean hasAdminOptionOnUser();
429446
}
430447

431-
/** Represents a user-role assignment. */
432-
interface UserRole {
433-
String getUsername();
434-
435-
String getRoleName();
448+
/** A reference to a grantee user of a role. */
449+
interface GranteeUserRef {
450+
/**
451+
* Returns the username.
452+
*
453+
* @return the username
454+
*/
455+
String getName();
436456

457+
/**
458+
* Returns whether admin option is granted for this assignment.
459+
*
460+
* @return whether admin option is granted for this assignment
461+
*/
437462
boolean hasAdminOption();
438463
}
439464

440-
/** Represents a role hierarchy (role-to-role assignment). */
441-
interface RoleHierarchy {
442-
/** Returns the role name. */
443-
String getRoleName();
444-
445-
/** Returns the member role name granted to the role. */
446-
String getMemberRoleName();
465+
/** A reference to a granted role. */
466+
interface GrantedRoleRef {
467+
/**
468+
* Returns the granted role name.
469+
*
470+
* @return the granted role name
471+
*/
472+
String getName();
447473

448-
/** Returns whether admin option is granted for this hierarchy. */
474+
/**
475+
* Returns whether admin option is granted for this role grant.
476+
*
477+
* @return whether admin option is granted for this role grant
478+
*/
449479
boolean hasAdminOption();
450480
}
451481

core/src/main/java/com/scalar/db/common/DecoratedDistributedTransactionAdmin.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,12 @@ public void dropRole(String roleName) throws ExecutionException {
394394
}
395395

396396
@Override
397-
public List<RoleDetail> getRoles() throws ExecutionException {
397+
public List<Role> getRoles() throws ExecutionException {
398398
return distributedTransactionAdmin.getRoles();
399399
}
400400

401401
@Override
402-
public List<UserRoleDetail> getRolesForUser(String username) throws ExecutionException {
402+
public List<RoleForUser> getRolesForUser(String username) throws ExecutionException {
403403
return distributedTransactionAdmin.getRolesForUser(username);
404404
}
405405

@@ -421,8 +421,8 @@ public void revokeAdminOptionFromUser(String username, String roleName)
421421
}
422422

423423
@Override
424-
public List<UserRole> getUsersForRole(String roleName) throws ExecutionException {
425-
return distributedTransactionAdmin.getUsersForRole(roleName);
424+
public List<GranteeUserRef> getGranteeUsersForRole(String roleName) throws ExecutionException {
425+
return distributedTransactionAdmin.getGranteeUsersForRole(roleName);
426426
}
427427

428428
@Override

core/src/main/java/com/scalar/db/storage/dynamo/DynamoAdmin.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ private void createTableInternal(
304304
requestBuilder.tableName(getFullTableName(namespace, table));
305305

306306
try {
307-
if (!(ifNotExists && tableExistsInternal(namespace, table))) {
307+
if (!(ifNotExists && internalTableExists(namespace, table))) {
308308
client.createTable(requestBuilder.build());
309309
waitForTableCreation(namespace, table);
310310
}
@@ -543,14 +543,14 @@ private void createMetadataTableIfNotExists(boolean noBackup) throws ExecutionEx
543543
}
544544

545545
private boolean metadataTableExists() throws ExecutionException {
546-
return tableExistsInternal(Namespace.of(metadataNamespace), METADATA_TABLE);
546+
return internalTableExists(Namespace.of(metadataNamespace), METADATA_TABLE);
547547
}
548548

549549
private boolean namespacesTableExists() throws ExecutionException {
550-
return tableExistsInternal(Namespace.of(metadataNamespace), NAMESPACES_TABLE);
550+
return internalTableExists(Namespace.of(metadataNamespace), NAMESPACES_TABLE);
551551
}
552552

553-
private boolean tableExistsInternal(Namespace namespace, String table) throws ExecutionException {
553+
private boolean internalTableExists(Namespace namespace, String table) throws ExecutionException {
554554
try {
555555
client.describeTable(
556556
DescribeTableRequest.builder().tableName(getFullTableName(namespace, table)).build());

core/src/main/java/com/scalar/db/storage/jdbc/JdbcAdmin.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class JdbcAdmin implements DistributedStorageAdmin {
5959

6060
private final RdbEngineStrategy rdbEngine;
6161
private final BasicDataSource dataSource;
62+
private final String metadataSchema;
6263
private final TableMetadataService tableMetadataService;
6364
private final NamespaceMetadataService namespaceMetadataService;
6465

@@ -67,16 +68,18 @@ public JdbcAdmin(DatabaseConfig databaseConfig) {
6768
JdbcConfig config = new JdbcConfig(databaseConfig);
6869
rdbEngine = RdbEngineFactory.create(config);
6970
dataSource = JdbcUtils.initDataSourceForAdmin(config, rdbEngine);
70-
tableMetadataService = new TableMetadataService(config.getMetadataSchema(), rdbEngine);
71-
namespaceMetadataService = new NamespaceMetadataService(config.getMetadataSchema(), rdbEngine);
71+
metadataSchema = config.getMetadataSchema();
72+
tableMetadataService = new TableMetadataService(metadataSchema, rdbEngine);
73+
namespaceMetadataService = new NamespaceMetadataService(metadataSchema, rdbEngine);
7274
}
7375

7476
@SuppressFBWarnings("EI_EXPOSE_REP2")
7577
public JdbcAdmin(BasicDataSource dataSource, JdbcConfig config) {
7678
rdbEngine = RdbEngineFactory.create(config);
7779
this.dataSource = dataSource;
78-
tableMetadataService = new TableMetadataService(config.getMetadataSchema(), rdbEngine);
79-
namespaceMetadataService = new NamespaceMetadataService(config.getMetadataSchema(), rdbEngine);
80+
metadataSchema = config.getMetadataSchema();
81+
tableMetadataService = new TableMetadataService(metadataSchema, rdbEngine);
82+
namespaceMetadataService = new NamespaceMetadataService(metadataSchema, rdbEngine);
8083
}
8184

8285
private static boolean hasDescClusteringOrder(TableMetadata metadata) {
@@ -105,6 +108,7 @@ public void createNamespace(String namespace, Map<String, String> options)
105108

106109
try (Connection connection = dataSource.getConnection()) {
107110
execute(connection, rdbEngine.createSchemaSqls(namespace));
111+
createMetadataSchemaIfNotExists(connection);
108112
createNamespacesTableIfNotExists(connection);
109113
namespaceMetadataService.insertIntoNamespacesTable(connection, namespace);
110114
} catch (SQLException e) {
@@ -117,6 +121,7 @@ public void createTable(
117121
String namespace, String table, TableMetadata metadata, Map<String, String> options)
118122
throws ExecutionException {
119123
try (Connection connection = dataSource.getConnection()) {
124+
createMetadataSchemaIfNotExists(connection);
120125
createTableInternal(connection, namespace, table, metadata, false);
121126
addTableMetadata(connection, namespace, table, metadata, true, false);
122127
} catch (SQLException e) {
@@ -221,6 +226,7 @@ public void dropNamespace(String namespace) throws ExecutionException {
221226
execute(connection, rdbEngine.dropNamespaceSql(namespace));
222227
namespaceMetadataService.deleteFromNamespacesTable(connection, namespace);
223228
namespaceMetadataService.deleteNamespacesTableIfEmpty(connection);
229+
deleteMetadataSchemaIfEmpty(connection);
224230
} catch (SQLException e) {
225231
rdbEngine.dropNamespaceTranslateSQLException(e, namespace);
226232
}
@@ -282,7 +288,7 @@ TableMetadata getImportTableMetadata(
282288
String catalogName = rdbEngine.getCatalogName(namespace);
283289
String schemaName = rdbEngine.getSchemaName(namespace);
284290

285-
if (!tableExistsInternal(connection, namespace, table)) {
291+
if (!internalTableExists(connection, namespace, table)) {
286292
throw new IllegalArgumentException(
287293
CoreError.TABLE_NOT_FOUND.buildMessage(getFullTableName(namespace, table)));
288294
}
@@ -337,6 +343,7 @@ public void importTable(
337343

338344
try (Connection connection = dataSource.getConnection()) {
339345
TableMetadata tableMetadata = getImportTableMetadata(namespace, table, overrideColumnsType);
346+
createMetadataSchemaIfNotExists(connection);
340347
createNamespacesTableIfNotExists(connection);
341348
upsertIntoNamespacesTable(connection, namespace);
342349
addTableMetadata(connection, namespace, table, tableMetadata, true, false);
@@ -473,6 +480,7 @@ public void repairNamespace(String namespace, Map<String, String> options)
473480

474481
try (Connection connection = dataSource.getConnection()) {
475482
createSchemaIfNotExists(connection, namespace);
483+
createMetadataSchemaIfNotExists(connection);
476484
createNamespacesTableIfNotExists(connection);
477485
upsertIntoNamespacesTable(connection, namespace);
478486
} catch (SQLException e) {
@@ -485,6 +493,7 @@ public void repairTable(
485493
String namespace, String table, TableMetadata metadata, Map<String, String> options)
486494
throws ExecutionException {
487495
try (Connection connection = dataSource.getConnection()) {
496+
createMetadataSchemaIfNotExists(connection);
488497
createTableInternal(connection, namespace, table, metadata, true);
489498
addTableMetadata(connection, namespace, table, metadata, true, true);
490499
} catch (SQLException e) {
@@ -720,6 +729,7 @@ public void upgrade(Map<String, String> options) throws ExecutionException {
720729
return;
721730
}
722731

732+
createMetadataSchemaIfNotExists(connection);
723733
createNamespacesTableIfNotExists(connection);
724734
for (String namespace : namespaceNamesOfExistingTables) {
725735
upsertIntoNamespacesTable(connection, namespace);
@@ -762,6 +772,20 @@ void upsertIntoNamespacesTable(Connection connection, String namespace) throws S
762772
namespaceMetadataService.upsertIntoNamespacesTable(connection, namespace);
763773
}
764774

775+
private void createMetadataSchemaIfNotExists(Connection connection) throws SQLException {
776+
createSchemaIfNotExists(connection, metadataSchema);
777+
}
778+
779+
private void deleteMetadataSchemaIfEmpty(Connection connection) throws SQLException {
780+
Set<String> internalTableNames = getInternalTableNames(connection, metadataSchema);
781+
if (!internalTableNames.isEmpty()) {
782+
return;
783+
}
784+
785+
String sql = rdbEngine.deleteMetadataSchemaSql(metadataSchema);
786+
execute(connection, sql);
787+
}
788+
765789
private void createTable(Connection connection, String createTableStatement, boolean ifNotExists)
766790
throws SQLException {
767791
String stmt = createTableStatement;
@@ -778,10 +802,10 @@ private void createTable(Connection connection, String createTableStatement, boo
778802
}
779803
}
780804

781-
private boolean tableExistsInternal(Connection connection, String namespace, String table)
805+
private boolean internalTableExists(Connection connection, String namespace, String table)
782806
throws ExecutionException {
783807
String fullTableName = encloseFullTableName(namespace, table);
784-
String sql = rdbEngine.tableExistsInternalTableCheckSql(fullTableName);
808+
String sql = rdbEngine.internalTableExistsCheckSql(fullTableName);
785809
try {
786810
execute(connection, sql);
787811
return true;

0 commit comments

Comments
 (0)