Skip to content

Commit 7d660fa

Browse files
committed
Add handle delete prefix
1 parent 1bcfe8d commit 7d660fa

File tree

11 files changed

+50
-9
lines changed

11 files changed

+50
-9
lines changed

go/cpossum/c-possum.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,7 @@ func WriterRename(w Writer, v Value, newKey []byte) {
264264
func HandleMovePrefix(h *Handle, from, to []byte) error {
265265
return mapError(C.possum_handle_move_prefix(h, BufFromBytes(from), BufFromBytes(to)))
266266
}
267+
268+
func HandleDeletePrefix(h *Handle, prefix []byte) error {
269+
return mapError(C.possum_handle_delete_prefix(h, BufFromBytes(prefix)))
270+
}

go/cpossum/possum.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,5 @@ PossumError possum_single_delete(const Handle *handle, PossumBuf key, PossumStat
129129
PossumError possum_reader_new(const Handle *handle, PossumReader **reader);
130130

131131
PossumError possum_handle_move_prefix(Handle *handle, PossumBuf from, PossumBuf to);
132+
133+
PossumError possum_handle_delete_prefix(Handle *handle, PossumBuf prefix);

go/handle.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,7 @@ func (me Handle) CleanupSnapshots() error {
8383
func (me Handle) MovePrefix(from, to []byte) error {
8484
return possumC.HandleMovePrefix(me.cHandle, from, to)
8585
}
86+
87+
func (me Handle) DeletePrefix(prefix []byte) error {
88+
return possumC.HandleDeletePrefix(me.cHandle, prefix)
89+
}

go/justfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test:
2+
cargo build
3+
make -C ..
4+
CGO_LDFLAGS=../target/debug/libpossum.a go test -race ./...

go/resource/resource.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ func (p Provider) NewInstance(s string) (resource.Instance, error) {
2121
}, nil
2222
}
2323

24+
func (p Provider) DeletePrefix(prefix string) error {
25+
return p.Handle.DeletePrefix([]byte(prefix))
26+
}
27+
2428
var _ resource.Provider = Provider{}
2529

2630
type instance struct {

src/c_api/ext_fns/handle.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,16 @@ pub extern "C" fn possum_handle_move_prefix(
173173
.map_err(Into::into)
174174
})
175175
}
176+
177+
#[no_mangle]
178+
pub extern "C" fn possum_handle_delete_prefix(
179+
handle: *mut Handle,
180+
prefix: PossumBuf,
181+
) -> PossumError {
182+
let handle = unsafe { &mut *handle };
183+
with_residual(|| {
184+
handle
185+
.delete_prefix(prefix.as_ref())
186+
.map_err(Into::into)
187+
})
188+
}

src/handle.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ impl Handle {
304304
// Maybe it's okay just to commit anyway, since we have a deferred transaction and sqlite
305305
// might know nothing has changed.
306306
if deleted.is_some() {
307-
tx.commit(())?.complete()?;
307+
tx.commit(())?.complete();
308308
}
309309
Ok(deleted)
310310
}
@@ -321,7 +321,7 @@ impl Handle {
321321
pub fn rename_item(&mut self, from: &[u8], to: &[u8]) -> PubResult<Timestamp> {
322322
let mut tx = self.start_immediate_transaction()?;
323323
let last_used = tx.rename_item(from, to)?;
324-
Ok(tx.commit(last_used)?.complete()?)
324+
Ok(tx.commit(last_used)?.complete())
325325
}
326326

327327
/// Walks the underlying files in the possum directory.
@@ -455,7 +455,17 @@ impl Handle {
455455
to_vec.extend_from_slice(item.key.strip_prefix(from).unwrap());
456456
tx.rename_item(&item.key, &to_vec)?;
457457
}
458-
tx.commit(())?.complete()
458+
tx.commit(())?.complete();
459+
Ok(())
460+
}
461+
462+
pub fn delete_prefix(&self, prefix: &[u8]) -> PubResult<()> {
463+
let mut tx = self.start_deferred_transaction()?;
464+
for item in tx.read().list_items(prefix)? {
465+
tx.delete_key(&item.key)?;
466+
}
467+
tx.commit(())?.complete();
468+
Ok(())
459469
}
460470
}
461471

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ impl<'handle> BatchWriter<'handle> {
349349
.context("commit transaction")?;
350350

351351
self.flush_exclusive_files();
352-
work.complete()
352+
Ok(work.complete())
353353
}
354354

355355
/// Flush Writer's exclusive files and return them to the Handle pool.

src/reader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<'a> Reader<'a> {
1818
file_offset,
1919
length,
2020
file_id,
21-
}) = value.location.clone()
21+
}) = value.location
2222
{
2323
let file = self.reads.entry(file_id);
2424
file.or_default().insert(ReadExtent {
@@ -39,7 +39,7 @@ impl<'a> Reader<'a> {
3939
self.owned_tx
4040
.commit(())
4141
.context("committing transaction")?
42-
.complete()?;
42+
.complete();
4343
Ok(Snapshot { file_clones })
4444
}
4545

src/testing/torrent_storage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ fn torrent_storage_inner_run(inner: &TorrentStorageInner) -> anyhow::Result<()>
154154
|offset| format!("verified/{piece_data_hash:016x}/{offset}").into_bytes();
155155
if opts.rename_values {
156156
for (offset, value) in values {
157-
snapshot.value(value.clone()).view(|bytes| {
157+
snapshot.value(value).view(|bytes| {
158158
stored_hash.write(bytes);
159159
compare_reads(bytes, io::repeat(byte).take(chunk_size as u64)).unwrap();
160160
writer.rename_value(value, make_verified_key(offset))

src/tx.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ where
147147
}
148148

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

0 commit comments

Comments
 (0)