aboutsummaryrefslogtreecommitdiff
path: root/models
diff options
context:
space:
mode:
authorJulio Capote <jcapote@gmail.com>2023-01-08 15:08:13 +0000
committerJulio Capote <jcapote@gmail.com>2023-01-08 15:08:13 +0000
commitf064fb24429854a2b1cb6d19050ae44f2cffb306 (patch)
tree9b654316030b4857f4422d17c475faf9fbbafb9f /models
parent2ac280ccd497f36caee2038fa1a2bcf29f9f9931 (diff)
downloadcommunique-f064fb24429854a2b1cb6d19050ae44f2cffb306.tar.gz
only return most recent 50 items for outbox
Diffstat (limited to 'models')
-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