aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorr <r@freesoftwareextremist.com>2021-01-30 04:13:22 +0000
committerr <r@freesoftwareextremist.com>2021-01-30 04:13:22 +0000
commit9f34b607498c09b4a21bdcc82b3295f6c94bd058 (patch)
treec5a528cad05227f15f30b07f6d0056045176166b /util
parentc3f39210d8451d65f3ecaa88046380f42dc536a1 (diff)
parent3ac95ab3b117ee8867a30c8e4b30ab37411e5ccf (diff)
downloadbloat-9f34b607498c09b4a21bdcc82b3295f6c94bd058.tar.gz
bloat-9f34b607498c09b4a21bdcc82b3295f6c94bd058.zip
Merge branch 'master' into absolute_fluoride
Diffstat (limited to 'util')
-rw-r--r--util/kv.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/util/kv.go b/util/kv.go
new file mode 100644
index 0000000..df61654
--- /dev/null
+++ b/util/kv.go
@@ -0,0 +1,91 @@
+package util
+
+import (
+ "errors"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "strings"
+ "sync"
+)
+
+var (
+ errInvalidKey = errors.New("invalid key")
+ errNoSuchKey = errors.New("no such key")
+)
+
+type Database struct {
+ cache map[string][]byte
+ basedir string
+ m sync.RWMutex
+}
+
+func NewDatabse(basedir string) (db *Database, err error) {
+ err = os.Mkdir(basedir, 0755)
+ if err != nil && !os.IsExist(err) {
+ return
+ }
+
+ return &Database{
+ cache: make(map[string][]byte),
+ basedir: basedir,
+ }, nil
+}
+
+func (db *Database) Set(key string, val []byte) (err error) {
+ if len(key) < 1 || strings.ContainsRune(key, os.PathSeparator) {
+ return errInvalidKey
+ }
+
+ err = ioutil.WriteFile(filepath.Join(db.basedir, key), val, 0644)
+ if err != nil {
+ return
+ }
+
+ db.m.Lock()
+ db.cache[key] = val
+ db.m.Unlock()
+
+ return
+}
+
+func (db *Database) Get(key string) (val []byte, err error) {
+ if len(key) < 1 || strings.ContainsRune(key, os.PathSeparator) {
+ return nil, errInvalidKey
+ }
+
+ db.m.RLock()
+ data, ok := db.cache[key]
+ db.m.RUnlock()
+
+ if !ok {
+ data, err = ioutil.ReadFile(filepath.Join(db.basedir, key))
+ if err != nil {
+ err = errNoSuchKey
+ return nil, err
+ }
+
+ db.m.Lock()
+ db.cache[key] = data
+ db.m.Unlock()
+ }
+
+ val = make([]byte, len(data))
+ copy(val, data)
+
+ return
+}
+
+func (db *Database) Remove(key string) {
+ if len(key) < 1 || strings.ContainsRune(key, os.PathSeparator) {
+ return
+ }
+
+ os.Remove(filepath.Join(db.basedir, key))
+
+ db.m.Lock()
+ delete(db.cache, key)
+ db.m.Unlock()
+
+ return
+}