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

View File

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