Create dbstream interface

This commit is contained in:
Bel LaPointe
2021-02-07 12:53:56 -06:00
parent c0d561aa50
commit dcf1594e17
4 changed files with 64 additions and 9 deletions

View File

@@ -1,6 +1,8 @@
package storage
import (
"bytes"
"io"
"io/ioutil"
"os"
"path"
@@ -49,15 +51,31 @@ func (b *Files) List(ns []string, limits ...string) ([]string, error) {
}
func (b *Files) Get(key string, ns ...string) ([]byte, error) {
r, err := b.GetStream(key, ns...)
if err != nil {
return nil, err
}
return ioutil.ReadAll(r)
}
func (b *Files) GetStream(key string, ns ...string) (io.Reader, error) {
namespace := resolveNamespace(ns)
path := path.Join(b.root, namespace, key)
return ioutil.ReadFile(path)
return os.Open(path)
}
func (b *Files) Set(key string, value []byte, ns ...string) error {
r := bytes.NewReader(value)
if value == nil {
r = nil
}
return b.SetStream(key, r, ns...)
}
func (b *Files) SetStream(key string, r io.Reader, ns ...string) error {
namespace := resolveNamespace(ns)
dir := path.Join(b.root, namespace)
if value == nil {
if r == nil {
err := os.Remove(path.Join(dir, key))
if os.IsNotExist(err) {
err = nil
@@ -68,7 +86,13 @@ func (b *Files) Set(key string, value []byte, ns ...string) error {
return err
}
path := path.Join(dir, key)
return ioutil.WriteFile(path, value, os.ModePerm)
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, r)
return err
}
func (b *Files) Close() error {