nim-datastore/datastore/filesystem_datastore.nim

164 lines
3.5 KiB
Nim
Raw Normal View History

2022-05-11 10:50:05 -05:00
import std/os
import pkg/chronos
2022-05-11 10:50:05 -05:00
import pkg/questionable
import pkg/questionable/results
from pkg/stew/results as stewResults import get, isErr
2022-05-11 10:50:05 -05:00
import pkg/upraises
import ./datastore
export datastore
push: {.upraises: [].}
type
FileSystemDatastore* = ref object of Datastore
root: string
const
objExt* = ".dsobject"
proc new*(
T: type FileSystemDatastore,
root = "data"): ?!T =
try:
let
root = if root.isAbsolute: root
else: getCurrentDir() / root
createDir(root)
success T(root: root)
except IOError as e:
failure e
except OSError as e:
failure e
proc root*(self: FileSystemDatastore): string =
self.root
proc path*(
self: FileSystemDatastore,
key: Key): string =
var
segments: seq[string]
for ns in key:
without field =? ns.field:
segments.add ns.value
continue
segments.add(field / ns.value)
# is it problematic that per this logic Key(/a:b) evaluates to the same path
# as Key(/a/b)? may need to check if/how other Datastore implementations
# distinguish them
self.root / joinPath(segments) & objExt
method contains*(
self: FileSystemDatastore,
key: Key): Future[?!bool] {.async, locks: "unknown".} =
2022-05-11 10:50:05 -05:00
return success fileExists(self.path(key))
2022-05-11 10:50:05 -05:00
method delete*(
self: FileSystemDatastore,
key: Key): Future[?!void] {.async, locks: "unknown".} =
2022-05-11 10:50:05 -05:00
let
path = self.path(key)
try:
removeFile(path)
return success()
2022-05-11 10:50:05 -05:00
# removing an empty directory might lead to surprising behavior depending
# on what the user specified as the `root` of the FileSystemDatastore, so
# until further consideration, empty directories will be left in place
except OSError as e:
return failure e
2022-05-11 10:50:05 -05:00
method get*(
self: FileSystemDatastore,
key: Key): Future[?!(?seq[byte])] {.async, locks: "unknown".} =
2022-05-11 10:50:05 -05:00
# to support finer control of memory allocation, maybe could/should change
# the signature of `get` so that it has a 3rd parameter
# `bytes: var openArray[byte]` and return type `?!bool`; this variant with
# return type `?!(?seq[byte])` would be a special case (convenience method)
# calling the former after allocating a seq with size automatically
# determined via `getFileSize`
let
path = self.path(key)
containsRes = await self.contains(key)
if containsRes.isErr: return failure containsRes.error.msg
2022-05-11 10:50:05 -05:00
if containsRes.get:
2022-05-11 10:50:05 -05:00
var
file: File
if not file.open(path):
return failure "unable to open file: " & path
else:
try:
let
size = file.getFileSize
var
bytes: seq[byte]
if size > 0:
newSeq(bytes, size)
let
bytesRead = file.readBytes(bytes, 0, size)
if bytesRead < size:
return failure $bytesRead & " bytes were read from " & path &
" but " & $size & " bytes were expected"
return success bytes.some
2022-05-11 10:50:05 -05:00
except IOError as e:
return failure e
2022-05-11 10:50:05 -05:00
finally:
file.close
else:
return success seq[byte].none
2022-05-11 10:50:05 -05:00
method put*(
self: FileSystemDatastore,
key: Key,
data: seq[byte]): Future[?!void] {.async, locks: "unknown".} =
2022-05-11 10:50:05 -05:00
let
path = self.path(key)
try:
createDir(parentDir(path))
if data.len > 0: writeFile(path, data)
else: writeFile(path, "")
return success()
2022-05-11 10:50:05 -05:00
except IOError as e:
return failure e
2022-05-11 10:50:05 -05:00
except OSError as e:
return failure e
2022-05-11 10:50:05 -05:00
# method query*(
# self: FileSystemDatastore,
# query: ...): Future[?!(?...)] {.async, locks: "unknown".} =
2022-05-11 10:50:05 -05:00
#
# return success ....some