annotate semicongine/storage.nim @ 1138:02e1d2658ff5

did: more renaming
author sam <sam@basx.dev>
date Tue, 04 Jun 2024 22:08:48 +0700
parents f888ac4825b8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
984
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
1 import std/marshal
988
46778940c1d7 tmp: move stuff to notebook
sam <sam@basx.dev>
parents: 987
diff changeset
2 import std/tables
984
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
3 import std/strformat
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
4 import std/paths
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
5 import std/os
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
6
1013
967b8fa81b6b add: thirdparty libraries into repo
sam <sam@basx.dev>
parents: 990
diff changeset
7 import ./thirdparty/db_connector/db_sqlite
984
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
8
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
9 import ./core
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
10
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
11 const STORAGE_NAME = Path("storage.db")
1022
f888ac4825b8 did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents: 1013
diff changeset
12 const DEFAULT_KEY_VALUE_TABLE_NAME = "shelf"
984
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
13
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
14 type
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
15 StorageType* = enum
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
16 SystemStorage
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
17 UserStorage
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
18 # ? level storage type ?
988
46778940c1d7 tmp: move stuff to notebook
sam <sam@basx.dev>
parents: 987
diff changeset
19
46778940c1d7 tmp: move stuff to notebook
sam <sam@basx.dev>
parents: 987
diff changeset
20 var db: Table[StorageType, DbConn]
46778940c1d7 tmp: move stuff to notebook
sam <sam@basx.dev>
parents: 987
diff changeset
21
984
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
22 proc path(storageType: StorageType): Path =
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
23 case storageType:
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
24 of SystemStorage:
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
25 Path(getAppDir()) / STORAGE_NAME
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
26 of UserStorage:
986
5db7721395fc add: final (for now) storage API
sam <sam@basx.dev>
parents: 985
diff changeset
27 string(Path(getDataDir()) / Path(AppName())).createDir()
984
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
28 Path(getDataDir()) / Path(AppName()) / STORAGE_NAME
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
29
1022
f888ac4825b8 did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents: 1013
diff changeset
30 proc ensureExists(storageType: StorageType) =
988
46778940c1d7 tmp: move stuff to notebook
sam <sam@basx.dev>
parents: 987
diff changeset
31 if storageType in db:
46778940c1d7 tmp: move stuff to notebook
sam <sam@basx.dev>
parents: 987
diff changeset
32 return
46778940c1d7 tmp: move stuff to notebook
sam <sam@basx.dev>
parents: 987
diff changeset
33 db[storageType] = open(string(storageType.path), "", "", "")
1022
f888ac4825b8 did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents: 1013
diff changeset
34
f888ac4825b8 did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents: 1013
diff changeset
35 proc ensureExists(storageType: StorageType, table: string) =
f888ac4825b8 did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents: 1013
diff changeset
36 storageType.ensureExists()
f888ac4825b8 did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents: 1013
diff changeset
37 db[storageType].exec(sql(&"""CREATE TABLE IF NOT EXISTS {table} (
984
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
38 key TEXT NOT NULL UNIQUE,
985
0c02ca5de8e6 do: intermediate save before doing shit from scratch
sam <sam@basx.dev>
parents: 984
diff changeset
39 value TEXT NOT NULL
984
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
40 )"""))
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
41
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1022
diff changeset
42 proc Store*[T](storageType: StorageType, key: string, value: T, table = DEFAULT_KEY_VALUE_TABLE_NAME) =
1022
f888ac4825b8 did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents: 1013
diff changeset
43 storageType.ensureExists(table)
f888ac4825b8 did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents: 1013
diff changeset
44 db[storageType].exec(sql(&"""INSERT INTO {table} VALUES(?, ?)
984
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
45 ON CONFLICT(key) DO UPDATE SET value=excluded.value
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
46 """), key, $$value)
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
47
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1022
diff changeset
48 proc Load*[T](storageType: StorageType, key: string, default: T, table = DEFAULT_KEY_VALUE_TABLE_NAME): T =
1022
f888ac4825b8 did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents: 1013
diff changeset
49 storageType.ensureExists(table)
f888ac4825b8 did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents: 1013
diff changeset
50 let dbResult = db[storageType].getValue(sql(&"""SELECT value FROM {table} WHERE key = ? """), key)
984
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
51 if dbResult == "":
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
52 return default
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
53 return to[T](dbResult)
4e487f06378b add: inital version of storage api (untested)
sam <sam@basx.dev>
parents:
diff changeset
54
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1022
diff changeset
55 proc List*[T](storageType: StorageType, table = DEFAULT_KEY_VALUE_TABLE_NAME): seq[string] =
1022
f888ac4825b8 did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents: 1013
diff changeset
56 storageType.ensureExists(table)
f888ac4825b8 did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents: 1013
diff changeset
57 for row in db[storageType].fastRows(sql(&"""SELECT key FROM {table}""")):
f888ac4825b8 did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents: 1013
diff changeset
58 result.add row[0]
f888ac4825b8 did: refactor input system, did some renaming, add quering of keys in key-value-store
sam <sam@basx.dev>
parents: 1013
diff changeset
59
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1022
diff changeset
60 proc Purge*(storageType: StorageType) =
988
46778940c1d7 tmp: move stuff to notebook
sam <sam@basx.dev>
parents: 987
diff changeset
61 storageType.path().string.removeFile()