Skip to content

Commit

Permalink
drop: Add support for s3 removal of index files
Browse files Browse the repository at this point in the history
  • Loading branch information
tontinton committed Jul 1, 2024
1 parent 04629c9 commit 0208282
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/commands/drop.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::path::Path;
use std::sync::Arc;

use color_eyre::{eyre::eyre, Result};
use color_eyre::Result;
use futures::future::join_all;
use sqlx::{query, query_as, PgPool};
use tokio::fs::remove_file;

use crate::args::DropArgs;

use super::get_index_path;
use super::{get_index_path, get_operator};

pub async fn run_drop(args: DropArgs, pool: &PgPool) -> Result<()> {
let base_path = get_index_path(&args.name, pool).await?;
Expand All @@ -18,21 +18,27 @@ pub async fn run_drop(args: DropArgs, pool: &PgPool) -> Result<()> {
.await?;
let file_names_len = file_names.len();

for (file_name,) in file_names {
let _ = remove_file(
Path::new(&base_path)
.join(file_name)
.to_str()
.ok_or_else(|| eyre!("failed to build index file path"))?,
)
.await;
}

query("DELETE FROM indexes WHERE name=$1")
.bind(&args.name)
.execute(pool)
.await?;

let op = Arc::new(get_operator(&base_path).await?);
join_all(
file_names
.into_iter()
.map(|(file_name,)| (file_name, op.clone()))
.map(|(file_name, op)| async move {
if let Err(e) = op.delete(&file_name).await {
warn!(
"Failed to delete index file '{file_name}': {e}.
Don't worry, this just means the file is leaked, but will never be read from again."
);
}
}),
)
.await;

info!(
"Dropped index: {} ({} number of index files)",
&args.name, file_names_len
Expand Down

0 comments on commit 0208282

Please sign in to comment.