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

Supporting invalid path in BackupStorageUtils #2989

Merged
merged 4 commits into from
Nov 11, 2024
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 @@ -26,7 +26,7 @@ private val log = getLogger {}
*
* @param storageName
* @param commonPrefix
* @param s3KeyValidator
* @param s3KeyValidator accepts S3 keys and returns `true` for **unexpected** ones.
* @return [CompletableFuture] without body
*/
fun S3Operations.backupUnexpectedKeys(
Expand Down Expand Up @@ -63,7 +63,7 @@ fun S3Operations.backupUnexpectedKeys(
*
* @param storageName
* @param commonPrefix
* @param s3KeyValidator
* @param s3KeyValidator accepts S3 keys and returns `true` for **unexpected** ones.
* @return [CompletableFuture] without body
*/
fun S3Operations.deleteUnexpectedKeys(
Expand Down Expand Up @@ -95,9 +95,27 @@ fun S3Operations.deleteUnexpectedKeys(
s3KeyValidator = s3KeyManager.asS3KeyValidator(),
)

/**
* @return the lambda, which accepts an _S3 key_ (in the form of `path/to/data/<id>`)
* and returns `true` if the key is _invalid_.
*/
private fun AbstractS3KeyDatabaseManager<*, *, *>.asS3KeyValidator(): (String) -> Boolean = { s3Key ->
val id = s3Key.removePrefix(commonPrefix).toLong()
findKeyByEntityId(id) == null
/*-
* S3 "folders", similarly to "files", also have keys.
*
* In our case, such a folder name may be the same as the prefix (e.g.:
* `path/to/data/`), that's why we use `toLongOrNull()` rather than
* `toLong()` here.
*
* The key to a folder which has the same name as the prefix (basically,
* the containing folder) is considered to be a *valid* key (the validator
* returning `false`).
*/
s3Key.removePrefix(commonPrefix)
.toLongOrNull()
?.let { id ->
findKeyByEntityId(id) == null
} == true
}

private fun S3Operations.doBackupUnexpectedKeys(
Expand Down Expand Up @@ -146,6 +164,9 @@ private fun S3Operations.doDeleteUnexpectedKeys(
}
}

/**
* @param s3KeyValidator accepts S3 keys and returns `true` for **unexpected** ones.
*/
private fun S3Operations.detectUnexpectedKeys(
commonPrefix: String,
s3KeyValidator: (String) -> Boolean,
Expand Down
Loading