test repairDb

This commit is contained in:
Xie Yanbo 2020-02-12 23:29:11 +08:00
parent 3c0b6d9b62
commit 74ae934f94
2 changed files with 12 additions and 2 deletions

View File

@ -2,6 +2,7 @@ import options, strutils, leveldb/raw
type
LevelDb* = ref object
path*: string
db: ptr leveldb_t
syncWriteOptions: ptr leveldb_writeoptions_t
asyncWriteOptions: ptr leveldb_writeoptions_t
@ -61,6 +62,7 @@ proc open*(path: string, cacheCapacity = 0): LevelDb =
result.cache = cache
var errPtr: cstring = nil
result.path = path
result.db = leveldb_open(options, path, addr errPtr)
checkError(errPtr)
@ -189,7 +191,7 @@ proc removeDb*(name: string) =
leveldb_destroy_db(options, name, addr err)
checkError(err)
proc repaireDb*(name: string) =
proc repairDb*(name: string) =
let options = leveldb_options_create()
leveldb_options_set_create_if_missing(options, 0)
leveldb_options_set_error_if_exists(options, 0)
@ -322,4 +324,4 @@ when isMainModule:
db.delete(key)
db.close()
elif args[0] == "repair":
repaireDb(dbPath)
repairDb(dbPath)

View File

@ -77,6 +77,11 @@ suite "leveldb":
db.put("z2\0", "ff\0")
check(toSeq(db.iter()) == @[("\0z1", "\0ff"), ("z2\0", "ff\0")])
test "repair database":
initData(db)
db.close()
repairDb(dbName)
test "batch":
db.put("a", "1")
db.put("b", "2")
@ -107,5 +112,8 @@ suite "leveldb":
test "open with cache":
let ldb = leveldb.open(dbName & "-cache", cacheCapacity = 100000)
defer:
ldb.close()
removeDb(ldb.path)
ldb.put("a", "1")
check(toSeq(ldb.iter()) == @[("a", "1")])