Skip to content

Commit

Permalink
add column filter to row delete
Browse files Browse the repository at this point in the history
  • Loading branch information
marvin-j97 committed Jan 26, 2024
1 parent b1027ff commit de812d8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resolver = "2"
members = ["server", "smoltable"]

[workspace.package]
version = "1.0.0"
version = "0.0.1"
edition = "2021"
license = "MIT OR Apache-2.0"
rust-version = "1.74.0"
Expand Down
14 changes: 9 additions & 5 deletions server/src/api/delete_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use actix_web::{
};
use serde::Deserialize;
use serde_json::json;
use smoltable::TableWriter;
use smoltable::{ColumnFilter, TableWriter};

#[derive(Debug, Deserialize)]
pub struct Input {
row_key: String,
// column_filter: Option<ColumnKey>,
column_filter: Option<ColumnFilter>,
}

// TODO: change input format to Vec, atomic multi-row deletes...?
Expand Down Expand Up @@ -51,13 +51,17 @@ pub async fn handler(
));
}

let req_body = req_body.into_inner();

if let Some(table) = tables.get(&table_name) {
let count = {
let table = table.clone();

tokio::task::spawn_blocking(move || table.delete_row(req_body.row_key.clone()))
.await
.expect("should join")
tokio::task::spawn_blocking(move || {
table.delete_row(req_body.row_key, req_body.column_filter)
})
.await
.expect("should join")
}?;

let micros_total = before.elapsed().as_micros();
Expand Down
14 changes: 11 additions & 3 deletions smoltable/src/table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use self::row_reader::SingleRowReader;
use crate::{
query::{
row::{
Input as QueryRowInput, Output as QueryRowOutput, RowOptions as QueryRowInputRowOptions,
ColumnOptions as QueryRowColumnOptions, Input as QueryRowInput,
Output as QueryRowOutput, RowOptions as QueryRowInputRowOptions,
},
scan::{Input as QueryPrefixInput, Output as QueryPrefixOutput, ScanMode},
},
Expand Down Expand Up @@ -444,7 +445,11 @@ impl Smoltable {
// TODO: delete row thrashes block cache

// TODO: allow deleting specific columns -> DeleteRowInput, also batch + limit it?
pub fn delete_row(&self, row_key: String) -> crate::Result<u64> {
pub fn delete_row(
&self,
row_key: String,
column_filter: Option<ColumnFilter>,
) -> crate::Result<u64> {
let mut count = 0;

let mut reader = SingleRowReader::new(
Expand All @@ -455,7 +460,10 @@ impl Smoltable {
key: row_key,
cell_limit: None,
},
column: None,
column: column_filter.map(|cf| QueryRowColumnOptions {
cell_limit: None,
filter: Some(cf),
}),
},
)?;

Expand Down
6 changes: 3 additions & 3 deletions smoltable/tests/write_count_after_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ pub fn write_count_after_delete() -> smoltable::Result<()> {
assert_eq!(2, row_count);
assert_eq!(4, cell_count);

table.delete_row("test".to_string())?;
table.delete_row("test".to_string(), None)?;

let (row_count, cell_count) = table.count()?;
assert_eq!(1, row_count);
assert_eq!(2, cell_count);

table.delete_row("test".to_string())?;
table.delete_row("test".to_string(), None)?;

let (row_count, cell_count) = table.count()?;
assert_eq!(1, row_count);
assert_eq!(2, cell_count);

table.delete_row("test2".to_string())?;
table.delete_row("test2".to_string(), None)?;

let (row_count, cell_count) = table.count()?;
assert_eq!(0, row_count);
Expand Down

0 comments on commit de812d8

Please sign in to comment.