Add sorting and limit to store's List
parent
38e1fc44d3
commit
d216949d91
|
|
@ -32,7 +32,10 @@ func (bc *BoltClient) Set(namespace, key string, value []byte) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bc *BoltClient) List(namespace, key string) ([]string, error) {
|
func (bc *BoltClient) List(namespace, key string, asc bool, limit int) ([]string, error) {
|
||||||
|
if limit < 1 {
|
||||||
|
limit = 10000
|
||||||
|
}
|
||||||
found := []string{}
|
found := []string{}
|
||||||
err := bc.db.View(func(tx *bolt.Tx) error {
|
err := bc.db.View(func(tx *bolt.Tx) error {
|
||||||
bucket := tx.Bucket([]byte(namespace))
|
bucket := tx.Bucket([]byte(namespace))
|
||||||
|
|
@ -40,9 +43,15 @@ func (bc *BoltClient) List(namespace, key string) ([]string, error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
c := bucket.Cursor()
|
c := bucket.Cursor()
|
||||||
for k, _ := c.Seek([]byte(key)); k != nil; k, _ = c.Next() {
|
if asc {
|
||||||
|
for k, _ := c.Seek([]byte(key)); k != nil && len(found) < limit; k, _ = c.Next() {
|
||||||
found = append(found, string(k))
|
found = append(found, string(k))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
for k, _ := c.Seek([]byte(key)); k != nil && len(found) < limit; k, _ = c.Next() {
|
||||||
|
found = append(found, string(k))
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return found, err
|
return found, err
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ func Test_BoltSetListGet(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if results, err := sc.List(c.ns, c.search); err != nil {
|
if results, err := sc.List(c.ns, c.search, true, -1); err != nil {
|
||||||
t.Errorf("failed to list bolt: %v", err)
|
t.Errorf("failed to list bolt: %v", err)
|
||||||
} else if len(results) != c.results {
|
} else if len(results) != c.results {
|
||||||
t.Errorf("failed to list bolt: missing results for %q: %v, expceted %v", c.ns, len(results), len(c.keys))
|
t.Errorf("failed to list bolt: missing results for %q: %v, expceted %v", c.ns, len(results), len(c.keys))
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,6 @@ package store
|
||||||
type Client interface {
|
type Client interface {
|
||||||
Get(string, string) ([]byte, error)
|
Get(string, string) ([]byte, error)
|
||||||
Set(string, string, []byte) error
|
Set(string, string, []byte) error
|
||||||
List(string, string) ([]string, error)
|
List(string, string, bool, int) ([]string, error)
|
||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue