diff options
Diffstat (limited to '')
-rw-r--r-- | controller/controller.go | 2 | ||||
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 4 | ||||
-rw-r--r-- | models/persister.go | 43 |
4 files changed, 50 insertions, 1 deletions
diff --git a/controller/controller.go b/controller/controller.go index 91ac267..7b063e6 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -154,7 +154,7 @@ func (r *Controller) OutboxCollection(name string) (map[string]interface{}, erro return nil, nil } aso := models.NewOutboxItem(handler.handlerCfg) - page, err := r.persister.Collect(aso) + page, err := r.persister.CollectRecent(aso, 50) if err != nil { return nil, err } @@ -29,6 +29,8 @@ require ( github.com/golang/snappy v0.0.4 // indirect github.com/google/flatbuffers v22.11.23+incompatible // indirect github.com/gorilla/css v1.0.0 // indirect + github.com/hashicorp/golang-lru v1.0.1 // indirect + github.com/hashicorp/golang-lru/v2 v2.0.1 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.15.13 // indirect github.com/leodido/go-urn v1.2.1 // indirect @@ -104,6 +104,10 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY= github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c= +github.com/hashicorp/golang-lru v1.0.1 h1:5KzQ9DWj9u/NZIuatPgGU/H7bIxFbUta+iD5OQ/aLxo= +github.com/hashicorp/golang-lru v1.0.1/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/golang-lru/v2 v2.0.1 h1:5pv5N1lT1fjLg2VQ5KWc7kmucp2x/kvFOnxuVTqZ6x4= +github.com/hashicorp/golang-lru/v2 v2.0.1/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 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 |