diff --git a/store/bolt.go b/store/bolt.go index 7d81758..d3a7681 100644 --- a/store/bolt.go +++ b/store/bolt.go @@ -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{} err := bc.db.View(func(tx *bolt.Tx) error { bucket := tx.Bucket([]byte(namespace)) @@ -40,8 +43,14 @@ func (bc *BoltClient) List(namespace, key string) ([]string, error) { return nil } c := bucket.Cursor() - for k, _ := c.Seek([]byte(key)); k != nil; k, _ = c.Next() { - found = append(found, string(k)) + if asc { + for k, _ := c.Seek([]byte(key)); k != nil && len(found) < limit; k, _ = c.Next() { + 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 }) diff --git a/store/bolt_test.go b/store/bolt_test.go index 4bb1086..ff42999 100644 --- a/store/bolt_test.go +++ b/store/bolt_test.go @@ -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) } 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)) diff --git a/store/store.go b/store/store.go index 3150ef7..ebea7e6 100644 --- a/store/store.go +++ b/store/store.go @@ -3,6 +3,6 @@ package store type Client interface { Get(string, string) ([]byte, error) Set(string, string, []byte) error - List(string, string) ([]string, error) + List(string, string, bool, int) ([]string, error) Close() error }