@@ 6,8 6,6 @@
{.passC: staticExec("pkg-config --cflags tkrzw").}
{.passL: staticExec("pkg-config --libs tkrzw").}
-from macros import genSym, strVal
-
type
RW* = enum readonly, writeable
OpenOption* = enum
@@ 146,20 144,23 @@ proc close*(dbm: DBM) =
## Closes the database file.
dbm.Close()
-proc get*[A,B](dbm; key: A; result: var B) =
- var
- buf: CppString
- st = dbm.Get(key.toStringView, addr buf)
- if not IsOK(st):
- raise newException(KeyError, $GetMessage(st))
- result.setLen(buf.len)
- copyMem(addr result[0], buf.data, result.len)
- # TODO: is this copy avoidable?
+proc get*[A,B](dbm; key: A; dst: var B): bool =
+ var src: CppString
+ if IsOK dbm.Get(key.toStringView, addr src):
+ result = true
+ dst.setLen(src.len)
+ if dst.len > 0:
+ copyMem(addr dst[0], src.data, dst.len)
+
+proc hasKey*[A](dbm; key: A): bool =
+ dbm.Get(key.toStringView, nil).IsOK
proc set*[A,B](dbm; key: A; value: B; overwrite = true) =
dbm.Set(key.toStringView, value.toStringView, overwrite)
-proc `[]`*(dbm; key: string): string {.inline.} = get(dbm, key, result)
+proc `[]`*(dbm; key: string): string {.inline.} =
+ if not get(dbm, key, result):
+ raise newException(KeyError, "")
proc `[]=`*(dbm; key, value: string) = set(dbm, key, value)
@@ 1,6 1,6 @@
# Package
-version = "0.1.2"
+version = "20220830"
author = "Emery Hemingway"
description = "Wrapper of the Tkrzw key-value database library"
license = "Apache-2.0"