diff options
author | Julio Capote <jcapote@gmail.com> | 2023-01-01 21:48:37 +0000 |
---|---|---|
committer | Julio Capote <jcapote@gmail.com> | 2023-01-01 21:48:37 +0000 |
commit | b3f22c698739bdd57dab77af45a0b8a43da72ca4 (patch) | |
tree | 07d4bca01bc49d71d2ddceaa89f51487f0768047 /models | |
parent | 71495ac61587e3380f413b4d34669a68b5cd8068 (diff) | |
download | communique-b3f22c698739bdd57dab77af45a0b8a43da72ca4.tar.gz |
working ids and activity lookups
Diffstat (limited to 'models')
-rw-r--r-- | models/model.go | 1 | ||||
-rw-r--r-- | models/outbox_item.go | 11 | ||||
-rw-r--r-- | models/persister.go | 26 |
3 files changed, 32 insertions, 6 deletions
diff --git a/models/model.go b/models/model.go index 56f1680..354b3bd 100644 --- a/models/model.go +++ b/models/model.go @@ -5,4 +5,5 @@ import "github.com/dgraph-io/badger/v3" type model interface { Save(txn *badger.Txn) error Keybase() string + Key() string } diff --git a/models/outbox_item.go b/models/outbox_item.go index 9950d9c..9d9de97 100644 --- a/models/outbox_item.go +++ b/models/outbox_item.go @@ -18,7 +18,7 @@ type OutboxItem struct { CreatedAt time.Time } -// used for lookup purposes (count, collect) +// used for lookup purposes (count, collect, find) func NewOutboxItem(h config.Handler) *OutboxItem { aso := &OutboxItem{Handler: h} return aso @@ -31,14 +31,13 @@ func CreateOutboxItem(h config.Handler, content []byte) *OutboxItem { Handler: h, CreatedAt: t, Content: content, - Id: k.Bytes(), + Id: []byte(k.String()), // NOTE: we want the bytes of the string representation of a hash, NOT a binary hash } return aso } -func (a *OutboxItem) keyName() []byte { - key := fmt.Sprintf("%s:%s", a.Keybase(), a.Id) - return []byte(key) +func (a *OutboxItem) Key() string { + return fmt.Sprintf("%s:%s", a.Keybase(), a.Id) } func (a *OutboxItem) Keybase() string { @@ -59,6 +58,6 @@ func (a *OutboxItem) Save(txn *badger.Txn) error { if err != nil { return fmt.Errorf("could not encode outbox item: %w", err) } - e := badger.NewEntry(a.keyName(), network.Bytes()) + e := badger.NewEntry([]byte(a.Key()), network.Bytes()) return txn.SetEntry(e) } diff --git a/models/persister.go b/models/persister.go index 669480e..69e79f4 100644 --- a/models/persister.go +++ b/models/persister.go @@ -60,3 +60,29 @@ func (p *Persister) Collect(model model) ([][]byte, error) { }) return result, err } + +func (p *Persister) Find(model model) ([]byte, error) { + var result []byte + var item *badger.Item + err := p.db.View(func(txn *badger.Txn) error { + var getErr error + item, getErr = txn.Get([]byte(model.Key())) + if getErr != nil { + return getErr + } + return nil + }) + if err != nil { + return nil, err + } + err = item.Value(func(v []byte) error { + result = v + return nil + }) + + if err != nil { + return nil, err + } + + return result, nil +} |