aboutsummaryrefslogtreecommitdiff
path: root/models/persister.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--models/persister.go22
1 files changed, 20 insertions, 2 deletions
diff --git a/models/persister.go b/models/persister.go
index 69e79f4..60c25b5 100644
--- a/models/persister.go
+++ b/models/persister.go
@@ -16,12 +16,30 @@ type PersisterResult struct {
}
func NewPersister(log *zap.SugaredLogger, db *badger.DB) *Persister {
- aso := &Persister{log: log, db: db}
+ logger := log.With("type", "db")
+ aso := &Persister{log: logger, db: db}
return aso
}
func (p *Persister) Store(model model) error {
- err := p.db.Update(model.Save)
+ log := p.log.With("model", model.Name())
+ log.Debug("store")
+ err := p.db.View(func(txn *badger.Txn) error {
+ var getErr error
+ var updateErr error
+ log.With("DedupKey", model.DedupKey()).Debug("checking")
+ _, getErr = txn.Get([]byte(model.DedupKey()))
+ if getErr == badger.ErrKeyNotFound {
+ log.With("DedupKey", model.DedupKey()).Debug("not found")
+ updateErr = p.db.Update(model.Save) // stores the outbox item
+ if updateErr != nil {
+ return updateErr
+ }
+ log.With("DedupKey", model.DedupKey()).Debug("saving")
+ updateErr = p.db.Update(model.SaveDedup) // stores the sha256
+ }
+ return updateErr
+ })
return err
}