aboutsummaryrefslogtreecommitdiff
path: root/models/persister.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/persister.go')
-rw-r--r--models/persister.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/models/persister.go b/models/persister.go
index b4b9c49..94992de 100644
--- a/models/persister.go
+++ b/models/persister.go
@@ -2,6 +2,8 @@ package models
import (
"github.com/dgraph-io/badger/v3"
+ lru "github.com/hashicorp/golang-lru/v2"
+
"go.uber.org/zap"
)
@@ -115,6 +117,47 @@ func (p *Persister) CollectKeys(model model) ([][]byte, error) {
return keys, err
}
+// CollectRecent grabs the last n from a collection in an efficient way
+func (p *Persister) CollectRecent(model model, n int) ([][]byte, error) {
+ keys, err := lru.New[string, string](n)
+ if err != nil {
+ return nil, err
+ }
+
+ err = p.db.View(func(txn *badger.Txn) error {
+ opts := badger.DefaultIteratorOptions
+ opts.PrefetchValues = false
+ it := txn.NewIterator(opts)
+ defer it.Close()
+ prefix := []byte(model.Keybase())
+ for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
+ keys.Add(string(it.Item().Key()), "")
+ }
+ return nil
+ })
+
+ var results [][]byte
+ for _, k := range keys.Keys() {
+ err := p.db.View(func(txn *badger.Txn) error {
+ var getErr error
+ item, getErr := txn.Get([]byte(k))
+ if getErr != nil {
+ return getErr
+ }
+ getErr = item.Value(func(v []byte) error {
+ results = append(results, v)
+ return nil
+ })
+ return nil
+ })
+ if err != nil {
+ return nil, err
+ }
+ }
+
+ return results, err
+}
+
func (p *Persister) Find(model model) ([]byte, error) {
var result []byte
var item *badger.Item