-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[ENH] Add support for delete in blockfile #1957
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ use super::sparse_index::SparseIndex; | |
use crate::blockstore::arrow_blockfile::block::delta::BlockDelta; | ||
use crate::blockstore::BlockfileError; | ||
use crate::errors::ChromaError; | ||
use crate::errors::IntoResult; | ||
use parking_lot::Mutex; | ||
use std::sync::Arc; | ||
use thiserror::Error; | ||
|
@@ -94,6 +95,48 @@ impl Blockfile for ArrowBlockfile { | |
} | ||
} | ||
|
||
fn delete(&mut self, key: BlockfileKey) -> Result<(), Box<dyn ChromaError>> { | ||
if !self.in_transaction() { | ||
return Err(Box::new(BlockfileError::TransactionNotInProgress)); | ||
} | ||
|
||
self.validate_key(&key)?; | ||
|
||
let transaction_state = match &self.transaction_state { | ||
None => return Err(Box::new(BlockfileError::TransactionNotInProgress)), | ||
Some(transaction_state) => transaction_state, | ||
}; | ||
|
||
// Note: The code to get the target block as well as get or create the delta is duplicated | ||
// in the set and delete methods. This is due to the lock management making it clunky | ||
// to abstract this into a separate method. This likely should be refactored | ||
// for cleaner reuse, but for now its ok to repeat ourselves. | ||
|
||
// Get the target block id for the key | ||
let transaction_sparse_index = transaction_state.sparse_index.lock(); | ||
let target_block_id = match *transaction_sparse_index { | ||
None => self.sparse_index.lock().get_target_block_id(&key), | ||
Some(ref index) => index.lock().get_target_block_id(&key), | ||
}; | ||
|
||
// See if a delta for the target block already exists, if not create a new one and add it to the transaction state | ||
// Creating a delta loads the block entirely into memory | ||
let delta = match transaction_state.get_delta_for_block(&target_block_id) { | ||
None => { | ||
let target_block = match self.block_provider.get_block(&target_block_id) { | ||
None => return Err(Box::new(ArrowBlockfileError::BlockNotFoundError)), | ||
Some(block) => block, | ||
}; | ||
let delta = BlockDelta::from(target_block); | ||
transaction_state.add_delta(delta.clone()); | ||
delta | ||
} | ||
Some(delta) => delta, | ||
}; | ||
|
||
delta.delete(key).into_result() | ||
} | ||
|
||
fn get_by_prefix( | ||
&self, | ||
prefix: String, | ||
|
@@ -144,29 +187,7 @@ impl Blockfile for ArrowBlockfile { | |
return Err(Box::new(BlockfileError::TransactionNotInProgress)); | ||
} | ||
|
||
// Validate key type | ||
match key.key { | ||
Key::String(_) => { | ||
if self.key_type != KeyType::String { | ||
return Err(Box::new(BlockfileError::InvalidKeyType)); | ||
} | ||
} | ||
Key::Float(_) => { | ||
if self.key_type != KeyType::Float { | ||
return Err(Box::new(BlockfileError::InvalidKeyType)); | ||
} | ||
} | ||
Key::Bool(_) => { | ||
if self.key_type != KeyType::Bool { | ||
return Err(Box::new(BlockfileError::InvalidKeyType)); | ||
} | ||
} | ||
Key::Uint(_) => { | ||
if self.key_type != KeyType::Uint { | ||
return Err(Box::new(BlockfileError::InvalidKeyType)); | ||
} | ||
} | ||
} | ||
self.validate_key(&key)?; | ||
|
||
// Validate value type | ||
match value { | ||
|
@@ -388,6 +409,32 @@ impl ArrowBlockfile { | |
fn in_transaction(&self) -> bool { | ||
self.transaction_state.is_some() | ||
} | ||
|
||
fn validate_key(&self, key: &BlockfileKey) -> Result<(), Box<dyn ChromaError>> { | ||
match key.key { | ||
Key::String(_) => { | ||
if self.key_type != KeyType::String { | ||
return Err(Box::new(BlockfileError::InvalidKeyType)); | ||
} | ||
} | ||
Key::Float(_) => { | ||
if self.key_type != KeyType::Float { | ||
return Err(Box::new(BlockfileError::InvalidKeyType)); | ||
} | ||
} | ||
Key::Bool(_) => { | ||
if self.key_type != KeyType::Bool { | ||
return Err(Box::new(BlockfileError::InvalidKeyType)); | ||
} | ||
} | ||
Key::Uint(_) => { | ||
if self.key_type != KeyType::Uint { | ||
return Err(Box::new(BlockfileError::InvalidKeyType)); | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
|
@@ -605,4 +652,48 @@ mod tests { | |
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_delete() { | ||
let block_provider = ArrowBlockProvider::new(); | ||
let mut blockfile = | ||
ArrowBlockfile::new(KeyType::String, ValueType::Int32Array, block_provider); | ||
|
||
blockfile.begin_transaction().unwrap(); | ||
let key1 = BlockfileKey::new("key".to_string(), Key::String("zzzz".to_string())); | ||
blockfile | ||
.set( | ||
key1.clone(), | ||
Value::Int32ArrayValue(Int32Array::from(vec![1, 2, 3])), | ||
) | ||
.unwrap(); | ||
let key2 = BlockfileKey::new("key".to_string(), Key::String("aaaa".to_string())); | ||
blockfile | ||
.set( | ||
key2.clone(), | ||
Value::Int32ArrayValue(Int32Array::from(vec![4, 5, 6])), | ||
) | ||
.unwrap(); | ||
blockfile.commit_transaction().unwrap(); | ||
|
||
blockfile.begin_transaction().unwrap(); | ||
blockfile.delete(key1.clone()).unwrap(); | ||
blockfile.commit_transaction().unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we test rollback here as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rollback is unimplemented in this PR. i have a stale stacked PR with proptests |
||
|
||
let res = blockfile.get(key1); | ||
match res { | ||
Err(err) => { | ||
assert_eq!(err.code(), crate::errors::ErrorCodes::NotFound); | ||
} | ||
_ => panic!("Expected not found error"), | ||
} | ||
|
||
let res = blockfile.get(key2); | ||
match res { | ||
Ok(_) => {} | ||
Err(err) => { | ||
panic!("Expected key2 to be found, got error: {:?}", err); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not familiar with the rollback mechanism here. What happens if we need to rollback the delete?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it discards the whole in-memory state