Skip to content

Commit

Permalink
Extract a method to decide deleting database data in Hive metastore
Browse files Browse the repository at this point in the history
  • Loading branch information
ebyhr authored and findepi committed Aug 8, 2023
1 parent a327d63 commit 9ef6e5a
Showing 1 changed file with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -469,29 +469,33 @@ private static boolean isCreatedBy(Database database, String queryId)
}

public synchronized void dropDatabase(ConnectorSession session, String schemaName)
{
setExclusive((delegate, hdfsEnvironment) -> {
boolean deleteData = shouldDeleteDatabaseData(session, schemaName);
delegate.dropDatabase(schemaName, deleteData);
});
}

public boolean shouldDeleteDatabaseData(ConnectorSession session, String schemaName)
{
Optional<Path> location = delegate.getDatabase(schemaName)
.orElseThrow(() -> new SchemaNotFoundException(schemaName))
.getLocation()
.map(Path::new);

setExclusive((delegate, hdfsEnvironment) -> {
// If we see files in the schema location, don't delete it.
// If we see no files, request deletion.
// If we fail to check the schema location, behave according to fallback.
boolean deleteData = location.map(path -> {
try {
return !hdfsEnvironment.getFileSystem(new HdfsContext(session), path)
.listLocatedStatus(path).hasNext();
}
catch (IOException | RuntimeException e) {
log.warn(e, "Could not check schema directory '%s'", path);
return deleteSchemaLocationsFallback;
}
}).orElse(deleteSchemaLocationsFallback);

delegate.dropDatabase(schemaName, deleteData);
});
// If we see files in the schema location, don't delete it.
// If we see no files, request deletion.
// If we fail to check the schema location, behave according to fallback.
return location.map(path -> {
try {
return !hdfsEnvironment.getFileSystem(new HdfsContext(session), path)
.listLocatedStatus(path).hasNext();
}
catch (IOException | RuntimeException e) {
log.warn(e, "Could not check schema directory '%s'", path);
return deleteSchemaLocationsFallback;
}
}).orElse(deleteSchemaLocationsFallback);
}

public synchronized void renameDatabase(String source, String target)
Expand Down

0 comments on commit 9ef6e5a

Please sign in to comment.