Skip to content
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

[repostore] Retrieve empty blocks #513

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions codex/stores/repostore.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ method getBlock*(self: RepoStore, cid: Cid): Future[?!Block] {.async.} =
## Get a block from the blockstore
##

if cid.isEmpty:
emizzle marked this conversation as resolved.
Show resolved Hide resolved
trace "Empty block, ignoring"
return success cid.emptyBlock

without key =? makePrefixKey(self.postFixLen, cid), err:
trace "Error getting key from provider", err = err.msg
return failure(err)
Expand Down Expand Up @@ -132,6 +136,10 @@ method putBlock*(
## Put a block to the blockstore
##

if blk.isEmpty:
trace "Empty block, ignoring"
return success()

without key =? makePrefixKey(self.postFixLen, blk.cid), err:
trace "Error getting key from provider", err = err.msg
return failure(err)
Expand Down Expand Up @@ -205,6 +213,10 @@ method delBlock*(self: RepoStore, cid: Cid): Future[?!void] {.async.} =

trace "Deleting block", cid

if cid.isEmpty:
trace "Empty block, ignoring"
return success()

if blk =? (await self.getBlock(cid)):
if key =? makePrefixKey(self.postFixLen, cid) and
err =? (await self.repoDs.delete(key)).errorOption:
Expand Down Expand Up @@ -233,6 +245,10 @@ method hasBlock*(self: RepoStore, cid: Cid): Future[?!bool] {.async.} =
## Check if the block exists in the blockstore
##

if cid.isEmpty:
trace "Empty block, ignoring"
return true.success

without key =? makePrefixKey(self.postFixLen, cid), err:
trace "Error getting key from provider", err = err.msg
return failure(err)
Expand Down Expand Up @@ -320,17 +336,6 @@ method close*(self: RepoStore): Future[void] {.async.} =

(await self.repoDs.close()).expect("Should close datastore")

proc hasBlock*(self: RepoStore, cid: Cid): Future[?!bool] {.async.} =
## Check if the block exists in the blockstore.
## Return false if error encountered
##

without key =? makePrefixKey(self.postFixLen, cid), err:
trace "Error getting key from provider", err = err.msg
return failure(err.msg)

return await self.repoDs.has(key)

proc reserve*(self: RepoStore, bytes: uint): Future[?!void] {.async.} =
## Reserve bytes
##
Expand Down
23 changes: 23 additions & 0 deletions tests/codex/stores/testrepostore.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import pkg/codex/clock

import ../helpers
import ../helpers/mockclock
import ../examples
import ./commonstoretests

checksuite "Test RepoStore start/stop":
Expand Down Expand Up @@ -283,6 +284,28 @@ asyncchecksuite "RepoStore":
check blockExpirations2.len == 1
assertExpiration(blockExpirations2[0], blk3)

test "should put empty blocks":
let blk = Cid.example.emptyBlock
check (await repo.putBlock(blk)).isOk

test "should get empty blocks":
let blk = Cid.example.emptyBlock

let got = await repo.getBlock(blk.cid)
check got.isOk
check got.get.cid == blk.cid

test "should delete empty blocks":
let blk = Cid.example.emptyBlock
check (await repo.delBlock(blk.cid)).isOk

test "should have empty block":
let blk = Cid.example.emptyBlock

let has = await repo.hasBlock(blk.cid)
check has.isOk
check has.get

commonBlockStoreTests(
"RepoStore Sql backend", proc: BlockStore =
BlockStore(
Expand Down
7 changes: 6 additions & 1 deletion tests/codex/testerasure.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import std/sequtils

import pkg/asynctest
import pkg/chronos
import pkg/datastore
import pkg/questionable/results

import pkg/codex/erasure
Expand All @@ -21,12 +22,16 @@ asyncchecksuite "Erasure encode/decode":
var manifest: Manifest
var store: BlockStore
var erasure: Erasure
var repoDs: Datastore
var metaDs: SQLiteDatastore

setup:
rng = Rng.instance()
chunker = RandomChunker.new(rng, size = dataSetSize, chunkSize = BlockSize)
manifest = !Manifest.new(blockSize = BlockSize)
store = CacheStore.new(cacheSize = (dataSetSize * 2), chunkSize = BlockSize)
repoDs = SQLiteDatastore.new(Memory).tryGet()
metaDs = SQLiteDatastore.new(Memory).tryGet()
store = RepoStore.new(repoDs, metaDs)
erasure = Erasure.new(store, leoEncoderProvider, leoDecoderProvider)

while (
Expand Down