add memory (test) ds

This commit is contained in:
Jaremy Creechley 2023-08-28 19:41:37 -07:00 committed by Dmitriy Ryajov
parent 985697b1bb
commit a7f6794f7e
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
2 changed files with 19 additions and 0 deletions

View File

@ -1,5 +1,8 @@
# import std/atomics
import threading/smartptrs
import std/hashes
export hashes
type
DataBufferHolder* = object
@ -29,6 +32,16 @@ proc len*(a: DataBuffer): int = a[].size
proc isNil*(a: DataBuffer): bool = smartptrs.isNil(a)
proc hash*(a: DataBuffer): Hash =
a[].buf.toOpenArray(0, a[].size-1).hash()
proc `==`*(a, b: DataBuffer): bool =
if a.isNil and b.isNil: return true
elif a.isNil or b.isNil: return false
elif a[].size != b[].size: return false
elif a[].buf == b[].buf: return true
else: a.hash() == b.hash()
proc new*(tp: typedesc[DataBuffer], size: int = 0): DataBuffer =
## allocate new buffer with given size
newSharedPtr(DataBufferHolder(

View File

@ -49,12 +49,18 @@ method delete*(
return success()
import pretty
method get*(
self: MemoryDatastore,
key: Key
): Future[?!seq[byte]] {.async.} =
let dk = KeyBuffer.new(key)
echo "getting: ", key
for k, v in self.store.pairs():
print "get: ", k.toString()
if self.store.hasKey(dk):
let res = self.store[dk]
return success res.toSeq(byte)