index controlled block deleting

This commit is contained in:
Ben 2024-05-15 14:13:20 +02:00
parent c7fdf4ca9e
commit 72ae2ceabc
No known key found for this signature in database
GPG Key ID: 541B9D8C9F1426A1
2 changed files with 9 additions and 9 deletions

View File

@ -514,7 +514,7 @@ proc requestStorage*(
let purchase = await contracts.purchasing.purchase(request)
success purchase.id
proc debugDelete*(self: CodexNodeRef, cid: Cid, nBlocks: int): Future[?!int] {.async.} =
proc debugDelete*(self: CodexNodeRef, cid: Cid, blockIndices: seq[int]): Future[?!int] {.async.} =
without manifest =? (await self.fetchManifest(cid)), err:
trace "Unable to fetch manifest for cid", cid, err = err.msg
return failure(err)
@ -524,15 +524,15 @@ proc debugDelete*(self: CodexNodeRef, cid: Cid, nBlocks: int): Future[?!int] {.a
treeCid = manifest.treeCid
var
index = -1
deleted = 0
while deleted < nBlocks and index < totalBlocks:
inc index
for index in blockIndices:
if index >= totalBlocks:
return failure("Index out of range. Index: " & $index & " totalBlocks: " & $totalBlocks)
without hasBlock =? (await self.networkStore.hasBlock(treeCid, index)), err:
return failure(err)
if not hasBlock:
continue
return failure("Tree does not have this index: " & $index & " totalBlocks: " & $totalBlocks)
else:
if err =? (await self.networkStore.delBlock(treeCid, index)).errorOption:
return failure(err)

View File

@ -640,7 +640,7 @@ proc initDebugApi(node: CodexNodeRef, conf: CodexConf, router: var RestRouter) =
when codex_enable_api_debug_delete:
type
DebugDeleteParams = object
nBlocks* {.serialize.}: int
blockIndices* {.serialize.}: seq[int]
router.rawApi(
MethodPost,
@ -653,12 +653,12 @@ proc initDebugApi(node: CodexNodeRef, conf: CodexConf, router: var RestRouter) =
without params =? DebugDeleteParams.fromJson(body), error:
return RestApiResponse.error(Http400, error.msg)
warn "debug/delete is deleting blocks", cid = $cid, nBlocks = params.nBlocks
warn "debug/delete is deleting blocks", cid = $cid, nBlocks = params.blockIndices.len
without deleted =? (await node.debugDelete(cid, params.nBlocks)), err:
without deleted =? (await node.debugDelete(cid, params.blockIndices)), err:
return RestApiResponse.error(Http500, err.msg)
warn "debug/delete has deleted blocks", cid = $cid, nBlocks = params.nBlocks, deleted
warn "debug/delete has deleted blocks", cid = $cid, deleted
return RestApiResponse.response($deleted)
except CatchableError as exc: