From f647664b549281c715d209e29c38ea6abdafbc95 Mon Sep 17 00:00:00 2001 From: bel Date: Thu, 5 Aug 2021 21:51:50 -0600 Subject: [PATCH] stub namespaces now that i realize file trees arent namespaces --- db.go | 1 + files.go | 5 +++++ map.go | 8 +++++++ rclone/rclone.go | 5 +++++ redis.go | 55 +++++++++++++++++++++++++++++++++------------- resolve/resolve.go | 4 ++++ 6 files changed, 63 insertions(+), 15 deletions(-) diff --git a/db.go b/db.go index 9bdcf3a..b869d58 100755 --- a/db.go +++ b/db.go @@ -17,6 +17,7 @@ type DB interface { Set(string, []byte, ...string) error List([]string, ...string) ([]string, error) Close() error + //Namespaces() ([][]string, error) } func New(key Type, params ...string) (db DB, err error) { diff --git a/files.go b/files.go index 463b4ee..7ebb51e 100755 --- a/files.go +++ b/files.go @@ -2,6 +2,7 @@ package storage import ( "bytes" + "errors" "io" "io/ioutil" "local/storage/resolve" @@ -34,6 +35,10 @@ func NewFiles(root string) (*Files, error) { }, os.MkdirAll(root, os.ModePerm) } +func (b *Files) Namespaces() ([][]string, error) { + return nil, errors.New("not impl") +} + func (b *Files) List(ns []string, limits ...string) ([]string, error) { namespace := resolve.Namespace(ns) limits = resolve.Limits(limits) diff --git a/map.go b/map.go index 7b3c87e..b5be964 100755 --- a/map.go +++ b/map.go @@ -38,6 +38,14 @@ func (m *Map) Close() error { return nil } +func (m *Map) Namespaces() ([][]string, error) { + keys := [][]string{} + for key := range m.m { + keys = append(keys, resolve.UnNamespace(key)) + } + return keys, nil +} + func (m *Map) List(ns []string, limits ...string) ([]string, error) { m.lock.RLock() defer m.lock.RUnlock() diff --git a/rclone/rclone.go b/rclone/rclone.go index aaf9435..67a447b 100755 --- a/rclone/rclone.go +++ b/rclone/rclone.go @@ -2,6 +2,7 @@ package rclone import ( "bytes" + "errors" "io" "io/ioutil" "local/storage/resolve" @@ -110,6 +111,10 @@ func (rc *RClone) Close() error { return nil } +func (rc *RClone) Namespaces() ([][]string, error) { + return nil, errors.New("not impl") +} + func (rc *RClone) List(ns []string, limits ...string) ([]string, error) { namespace := rc.ns if len(ns) > 0 { diff --git a/redis.go b/redis.go index 0c02298..f7508f7 100755 --- a/redis.go +++ b/redis.go @@ -33,6 +33,31 @@ func (m *Redis) Close() error { return m.client.Close() } +func (m *Redis) Namespaces() ([][]string, error) { + resp, err := m.client.Do("KEYS", "*") + if err != nil { + return nil, err + } + keys := map[string][]string{} + results, ok := resp.([]interface{}) + if !ok { + return nil, ErrNotFound + } + for _, key := range results { + pieces := resolve.UnNamespace(fmt.Sprintf("%s", key)) + if len(pieces) < 2 { + continue + } + pieces = pieces[:len(pieces)-1] + keys[fmt.Sprint(pieces)] = pieces + } + result := make([][]string, 0, len(keys)) + for _, v := range keys { + result = append(result, v) + } + return result, nil +} + func (m *Redis) List(ns []string, limits ...string) ([]string, error) { limits = resolve.Limits(limits) limits[0] = resolve.Namespace(append(ns, limits[0])) @@ -44,23 +69,23 @@ func (m *Redis) List(ns []string, limits ...string) ([]string, error) { } keys := []string{} - if results, ok := resp.([]interface{}); !ok { + results, ok := resp.([]interface{}) + if !ok { return nil, ErrNotFound - } else { - for i := range results { - _, ok := results[i].([]uint8) - if !ok { - return nil, errors.New("not a []byte key") - } - k := fmt.Sprintf("%s", results[i]) - if k < limits[0] { - continue - } - if k > limits[1] { - break - } - keys = append(keys, k) + } + for i := range results { + _, ok := results[i].([]uint8) + if !ok { + return nil, errors.New("not a []byte key") } + k := fmt.Sprintf("%s", results[i]) + if k < limits[0] { + continue + } + if k > limits[1] { + break + } + keys = append(keys, k) } return keys, nil } diff --git a/resolve/resolve.go b/resolve/resolve.go index 488e283..e41b2b8 100755 --- a/resolve/resolve.go +++ b/resolve/resolve.go @@ -7,6 +7,10 @@ import ( var DefaultNamespace = "namespace" +func UnNamespace(ns string) []string { + return strings.Split(ns, ".") +} + func Namespace(ns []string) string { namespace := DefaultNamespace if len(ns) > 0 {