Skip to content

Commit

Permalink
Add handle delete prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Mar 9, 2024
1 parent 1bcfe8d commit 7d660fa
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 9 deletions.
4 changes: 4 additions & 0 deletions go/cpossum/c-possum.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,7 @@ func WriterRename(w Writer, v Value, newKey []byte) {
func HandleMovePrefix(h *Handle, from, to []byte) error {
return mapError(C.possum_handle_move_prefix(h, BufFromBytes(from), BufFromBytes(to)))
}

func HandleDeletePrefix(h *Handle, prefix []byte) error {
return mapError(C.possum_handle_delete_prefix(h, BufFromBytes(prefix)))
}
2 changes: 2 additions & 0 deletions go/cpossum/possum.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,5 @@ PossumError possum_single_delete(const Handle *handle, PossumBuf key, PossumStat
PossumError possum_reader_new(const Handle *handle, PossumReader **reader);

PossumError possum_handle_move_prefix(Handle *handle, PossumBuf from, PossumBuf to);

PossumError possum_handle_delete_prefix(Handle *handle, PossumBuf prefix);
4 changes: 4 additions & 0 deletions go/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,7 @@ func (me Handle) CleanupSnapshots() error {
func (me Handle) MovePrefix(from, to []byte) error {
return possumC.HandleMovePrefix(me.cHandle, from, to)
}

func (me Handle) DeletePrefix(prefix []byte) error {
return possumC.HandleDeletePrefix(me.cHandle, prefix)
}
4 changes: 4 additions & 0 deletions go/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test:
cargo build
make -C ..
CGO_LDFLAGS=../target/debug/libpossum.a go test -race ./...
4 changes: 4 additions & 0 deletions go/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func (p Provider) NewInstance(s string) (resource.Instance, error) {
}, nil
}

func (p Provider) DeletePrefix(prefix string) error {
return p.Handle.DeletePrefix([]byte(prefix))
}

var _ resource.Provider = Provider{}

type instance struct {
Expand Down
13 changes: 13 additions & 0 deletions src/c_api/ext_fns/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,16 @@ pub extern "C" fn possum_handle_move_prefix(
.map_err(Into::into)
})
}

#[no_mangle]
pub extern "C" fn possum_handle_delete_prefix(
handle: *mut Handle,
prefix: PossumBuf,
) -> PossumError {
let handle = unsafe { &mut *handle };
with_residual(|| {
handle
.delete_prefix(prefix.as_ref())
.map_err(Into::into)
})
}
16 changes: 13 additions & 3 deletions src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ impl Handle {
// Maybe it's okay just to commit anyway, since we have a deferred transaction and sqlite
// might know nothing has changed.
if deleted.is_some() {
tx.commit(())?.complete()?;
tx.commit(())?.complete();
}
Ok(deleted)
}
Expand All @@ -321,7 +321,7 @@ impl Handle {
pub fn rename_item(&mut self, from: &[u8], to: &[u8]) -> PubResult<Timestamp> {
let mut tx = self.start_immediate_transaction()?;
let last_used = tx.rename_item(from, to)?;
Ok(tx.commit(last_used)?.complete()?)
Ok(tx.commit(last_used)?.complete())
}

/// Walks the underlying files in the possum directory.
Expand Down Expand Up @@ -455,7 +455,17 @@ impl Handle {
to_vec.extend_from_slice(item.key.strip_prefix(from).unwrap());
tx.rename_item(&item.key, &to_vec)?;
}
tx.commit(())?.complete()
tx.commit(())?.complete();
Ok(())
}

pub fn delete_prefix(&self, prefix: &[u8]) -> PubResult<()> {
let mut tx = self.start_deferred_transaction()?;
for item in tx.read().list_items(prefix)? {
tx.delete_key(&item.key)?;
}
tx.commit(())?.complete();
Ok(())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ impl<'handle> BatchWriter<'handle> {
.context("commit transaction")?;

self.flush_exclusive_files();
work.complete()
Ok(work.complete())
}

/// Flush Writer's exclusive files and return them to the Handle pool.
Expand Down
4 changes: 2 additions & 2 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<'a> Reader<'a> {
file_offset,
length,
file_id,
}) = value.location.clone()
}) = value.location
{
let file = self.reads.entry(file_id);
file.or_default().insert(ReadExtent {
Expand All @@ -39,7 +39,7 @@ impl<'a> Reader<'a> {
self.owned_tx
.commit(())
.context("committing transaction")?
.complete()?;
.complete();
Ok(Snapshot { file_clones })
}

Expand Down
2 changes: 1 addition & 1 deletion src/testing/torrent_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ fn torrent_storage_inner_run(inner: &TorrentStorageInner) -> anyhow::Result<()>
|offset| format!("verified/{piece_data_hash:016x}/{offset}").into_bytes();
if opts.rename_values {
for (offset, value) in values {
snapshot.value(value.clone()).view(|bytes| {
snapshot.value(value).view(|bytes| {
stored_hash.write(bytes);
compare_reads(bytes, io::repeat(byte).take(chunk_size as u64)).unwrap();
writer.rename_value(value, make_verified_key(offset))
Expand Down
4 changes: 2 additions & 2 deletions src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ where
}

impl<'h, T> PostCommitWork<'h, T> {
pub fn complete(self) -> Result<T> {
pub fn complete(self) -> T {
// This has to happen after exclusive files are flushed or there's a tendency for hole
// punches to not persist. It doesn't fix the problem, but it significantly reduces it.
if !self.handle.instance_limits.disable_hole_punching {
Expand All @@ -157,7 +157,7 @@ impl<'h, T> PostCommitWork<'h, T> {
for file_id in self.altered_files {
self.handle.clones.lock().unwrap().remove(&file_id);
}
Ok(self.reward)
self.reward
}
}

Expand Down

0 comments on commit 7d660fa

Please sign in to comment.