73 lines
1.8 KiB
Nim
Raw Normal View History

2023-09-28 17:43:29 -07:00
import std/os
2023-09-20 17:12:19 -07:00
import std/options
2023-09-28 17:43:29 -07:00
import std/strutils
2022-09-19 17:13:20 -06:00
2023-09-20 17:12:19 -07:00
import pkg/questionable
import pkg/questionable/results
2023-09-28 17:43:29 -07:00
from pkg/stew/results as stewResults import get, isErr
2023-09-20 17:12:19 -07:00
import pkg/upraises
2023-09-28 17:40:19 -07:00
import pkg/chronos
import pkg/taskpools
import ./threads/sqlbackend
2023-09-28 18:13:37 -07:00
import ./threads/threadproxy
2023-09-28 17:40:19 -07:00
import ./datastore
2023-09-28 18:24:31 -07:00
export datastore, keys, query, Taskpool, Memory, DbExt
2023-09-20 17:12:19 -07:00
push: {.upraises: [].}
2023-09-21 17:52:06 -07:00
type
SQLiteDatastore* = ref object of Datastore
2023-09-28 18:19:05 -07:00
db: ThreadProxy[SQLiteBackend[KeyId, DataBuffer]]
2023-09-20 17:12:19 -07:00
2023-09-21 17:52:06 -07:00
proc path*(self: SQLiteDatastore): string =
2023-09-28 17:47:13 -07:00
self.db.backend.path()
2023-09-20 17:12:19 -07:00
2023-09-21 17:52:06 -07:00
proc readOnly*(self: SQLiteDatastore): bool =
2023-09-28 17:47:13 -07:00
self.db.backend.readOnly()
2023-09-20 17:12:19 -07:00
2023-09-25 20:37:34 -07:00
method has*(self: SQLiteDatastore,
key: Key): Future[?!bool] {.async.} =
2023-09-28 17:43:29 -07:00
await self.db.has(key)
2023-09-20 17:12:19 -07:00
2023-09-25 20:37:34 -07:00
method delete*(self: SQLiteDatastore,
key: Key): Future[?!void] {.async.} =
2023-09-28 17:43:29 -07:00
await self.db.delete(key)
2023-09-20 17:12:19 -07:00
2023-09-25 20:37:34 -07:00
method delete*(self: SQLiteDatastore,
keys: seq[Key]): Future[?!void] {.async.} =
2023-09-28 17:43:29 -07:00
await self.db.delete(keys)
2023-09-20 17:12:19 -07:00
2023-09-25 20:37:34 -07:00
method get*(self: SQLiteDatastore,
key: Key): Future[?!seq[byte]] {.async.} =
2023-09-28 17:43:29 -07:00
await self.db.get(key)
2023-09-20 17:12:19 -07:00
2023-09-25 20:37:34 -07:00
method put*(self: SQLiteDatastore,
key: Key,
data: seq[byte]): Future[?!void] {.async.} =
2023-09-28 17:43:29 -07:00
await self.db.put(key, data)
2023-09-20 17:12:19 -07:00
2023-09-25 20:37:34 -07:00
method put*(self: SQLiteDatastore,
batch: seq[BatchEntry]): Future[?!void] {.async.} =
2023-09-28 17:43:29 -07:00
await self.db.put(batch)
2023-09-20 17:12:19 -07:00
2023-09-21 17:52:06 -07:00
method close*(self: SQLiteDatastore): Future[?!void] {.async.} =
2023-09-28 17:43:29 -07:00
await self.db.close()
2023-09-20 17:12:19 -07:00
2023-09-28 17:43:29 -07:00
method query*(self: SQLiteDatastore,
q: Query): Future[?!QueryIter] {.async.} =
await self.db.query(q)
2023-09-20 17:12:19 -07:00
2023-09-21 17:52:06 -07:00
proc new*(
T: type SQLiteDatastore,
path: string,
2023-09-28 18:24:31 -07:00
tp: Taskpool = Taskpool.new(4),
2023-09-26 20:07:02 -07:00
readOnly = false): ?!SQLiteDatastore =
2023-09-20 17:12:19 -07:00
2023-09-28 18:02:21 -07:00
let
backend = ? newSQLiteBackend[KeyId, DataBuffer](path, readOnly)
2023-09-28 18:19:05 -07:00
db = ? ThreadProxy.new(backend, tp = tp)
2023-09-28 18:02:21 -07:00
success SQLiteDatastore(db: db)