diff options
author | r <r@freesoftwareextremist.com> | 2020-11-22 17:29:58 +0000 |
---|---|---|
committer | r <r@freesoftwareextremist.com> | 2020-11-22 17:29:58 +0000 |
commit | fa27d9c6eb525b2e55c3faab5dd8a3e0e9658536 (patch) | |
tree | 15561e426c76d5fab44b5e401c2e81b77c0d7553 /kv | |
parent | 37b1c750454a780ef31a26d19005e5c94a39ae9b (diff) | |
download | bloat-fa27d9c6eb525b2e55c3faab5dd8a3e0e9658536.tar.gz bloat-fa27d9c6eb525b2e55c3faab5dd8a3e0e9658536.zip |
Refactor things
- Remove separate auth/logging and merge them into transport.go
- Add helper function for http handlers
Diffstat (limited to 'kv')
-rw-r--r-- | kv/kv.go | 91 |
1 files changed, 0 insertions, 91 deletions
diff --git a/kv/kv.go b/kv/kv.go deleted file mode 100644 index 0f51e07..0000000 --- a/kv/kv.go +++ /dev/null @@ -1,91 +0,0 @@ -package kv - -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 -} |