Fixes key-not-found error type. Plugs into Codex for testing

This commit is contained in:
Ben 2024-05-06 16:07:22 +02:00
parent 32a380e442
commit 8dfb680d85
No known key found for this signature in database
GPG Key ID: 541B9D8C9F1426A1
2 changed files with 14 additions and 5 deletions

View File

@ -44,6 +44,7 @@ import ./utils/addrutils
import ./namespaces
import ./codextypes
import ./logutils
import ../leveldb/leveldbds
logScope:
topics = "codex node"
@ -232,7 +233,7 @@ proc new*(
let
discoveryStore = Datastore(
SQLiteDatastore.new(config.dataDir / CodexDhtProvidersNamespace)
LevelDbDatastore.new(config.dataDir / CodexDhtProvidersNamespace)
.expect("Should create discovery datastore!"))
discovery = Discovery.new(
@ -249,11 +250,10 @@ proc new*(
repoData = case config.repoKind
of repoFS: Datastore(FSDatastore.new($config.dataDir, depth = 5)
.expect("Should create repo file data store!"))
of repoSQLite: Datastore(SQLiteDatastore.new($config.dataDir)
of repoSQLite: Datastore(LevelDbDatastore.new($config.dataDir)
.expect("Should create repo SQLite data store!"))
metadataStore = Datastore(FSDatastore.new($config.metaDir, depth = 5)
.expect("Should create repo metadata store!"))
metadataStore = Datastore(LevelDbDatastore.new($(config.metaDir / "leveldb.ds")).expect("Should create metadata store!"))
repoStore = RepoStore.new(
repoDs = repoData,

View File

@ -1,6 +1,7 @@
import std/options
import std/tables
import pkg/chronicles
import pkg/chronos
import pkg/questionable
import pkg/questionable/results
@ -14,6 +15,9 @@ import ./src/leveldb
push: {.upraises: [].}
logScope:
topics = "LevelDB"
type
LevelDbDatastore* = ref object of Datastore
db: LevelDb
@ -49,16 +53,18 @@ method delete*(self: Datastore, keys: seq[Key]): Future[?!void] {.async, locks:
return success()
method get*(self: LevelDbDatastore, key: Key): Future[?!seq[byte]] {.async, locks: "unknown".} =
trace "Get", key
try:
let str = self.db.get($key)
if not str.isSome:
return failure("LevelDbDatastore.get: key not found " & $key)
return failure(newException(DatastoreKeyNotFound, "LevelDbDatastore.get: key not found " & $key))
let bytes = toByteSeq(str.get())
return success(bytes)
except LevelDbException as e:
return failure("LevelDbDatastore.get exception: " & $e.msg)
method put*(self: LevelDbDatastore, key: Key, data: seq[byte]): Future[?!void] {.async, locks: "unknown".} =
trace "Put", key
try:
let str = toString(data)
self.db.put($key, str)
@ -174,6 +180,8 @@ method modify*(
proc new*(
T: type LevelDbDatastore, dbName: string): ?!T =
try:
trace "Opening LevelDB", dbName
let db = leveldb.open(dbName)
success T(
@ -181,4 +189,5 @@ proc new*(
locks: newTable[Key, AsyncLock]()
)
except LevelDbException:
error "That didn't work"
return failure("exception open")