From db727b5bb7be41778b1437b9f9a0c3acb0c4a450 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Sat, 14 Aug 2021 12:57:59 +0200 Subject: [PATCH] Export meta types and some standard procs --- src/tkrzw.nim | 42 +++++++++++++++++++++++++++++++----------- tkrzw.nimble | 2 +- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/tkrzw.nim b/src/tkrzw.nim index 636257d..a707a33 100644 --- a/src/tkrzw.nim +++ b/src/tkrzw.nim @@ -52,9 +52,9 @@ type CacheDBM* = ref CacheDBMObj ## On-memory database manager implementation with LRU deletion. - FileDBM = HashDBM | TreeDBM | SkipDBM - MemoryDBM = TinyDBM | BabyDBM | CacheDBM - DBM = FileDBM | MemoryDBM + FileDBM* = HashDBM | TreeDBM | SkipDBM + MemoryDBM* = TinyDBM | BabyDBM | CacheDBM + DBM* = FileDBM | MemoryDBM using dbm: DBM @@ -97,9 +97,12 @@ template importDBM(T: untyped): untyped = proc MakeIterator(dbm: T): Iterator {.importcpp: "#->MakeIterator()".} proc Synchronize(dbm: T; hard: bool): Status {. importcpp: "#->Synchronize(@)".} + proc Count(dbm: T; count: ptr int64): Status {.importcpp: "#->Count(@)".} + proc GetFileSize(dbm: T; size: ptr int64): Status {.importcpp: "#->GetFileSize(@)".} + proc Clear(dbm: T): Status {.importcpp: "#->Clear()".} proc Rebuild(dbm: T): Status {.importcpp: "#->Rebuild()".} proc ShouldBeRebuilt(dbm: T; toBe: ptr bool): Status {. - importcpp: "#->ShouldBeRebuilt()".} + importcpp: "#->ShouldBeRebuilt(@)".} proc IsOpen(dbm: T): bool {.importcpp: "#->IsOpen()".} proc IsWritable(dbm: T): bool {.importcpp: "#->IsWritable()".} proc IsHealthy(dbm: T): bool {.importcpp: "#->IsHealthy()".} @@ -116,12 +119,15 @@ proc construct[T](): T = new(result) {.emit: "new ((void*)result) tkrzw::" & $T & "();".} +proc newDbm*[T: DBM](path: string; rw: RW; options: set[OpenOption] = {}): T = + var opts: int32 + for o in options.items: opts.inc o.int32 + result = construct[T]() + result.Open(path, rw == writeable, opts) + template declareNewFile(T: untyped): untyped = proc `new T`*(path: string; rw: RW; options: set[OpenOption] = {}): T = - var opts: int32 - for o in options.items: opts.inc o.int32 - result = construct[T]() - result.Open(path, rw == writeable, opts) + newDBM[T](path, rw, options) template declareNewMem(T: untyped): untyped = proc `new T`*(): T = construct[T]() @@ -177,14 +183,28 @@ proc synchronize*(dbm; hard: bool) = ## Synchronizes the content of the database to the file system. check dbm.Synchronize(hard) -proc shouldBeRebuilt*(dbm): bool = - ## Checks whether the database should be rebuilt, in a simple way. - discard dbm.ShouldBeRebuilt(addr result) +proc len*(dbm): int64 = + ## Gets the number of records. + var st = dbm.Count(addr result) + check st + +proc fileSize*(dbm): int64 = + ## Gets the current file size of the database. + var st = dbm.GetFileSize(addr result) + check st + +proc clear*(dbm) = + ## Removes all records. + check dbm.Clear() proc rebuild*(dbm) = ## Rebuilds the entire database. check dbm.Rebuild() +proc shouldBeRebuilt*(dbm): bool = + ## Checks whether the database should be rebuilt, in a simple way. + check dbm.ShouldBeRebuilt(addr result) + proc rebuild*(dbm: TinyDBM; numBuckets = -1) = ## Rebuilds the entire database. ## When `numBuckets` is calculated implicitly when -1. diff --git a/tkrzw.nimble b/tkrzw.nimble index a29f3de..42d4923 100644 --- a/tkrzw.nimble +++ b/tkrzw.nimble @@ -1,6 +1,6 @@ # Package -version = "0.1.0" +version = "0.1.1" author = "Emery Hemingway" description = "Wrapper of the Tkrzw key-value database library" license = "Apache-2.0" -- 2.45.2