From f064fb24429854a2b1cb6d19050ae44f2cffb306 Mon Sep 17 00:00:00 2001 From: Julio Capote Date: Sun, 8 Jan 2023 10:08:13 -0500 Subject: only return most recent 50 items for outbox --- models/persister.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'models') 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 -- cgit v1.2.3