aboutsummaryrefslogtreecommitdiff
path: root/models/persister.go
diff options
context:
space:
mode:
authorJulio Capote <jcapote@gmail.com>2023-01-02 00:38:31 +0000
committerJulio Capote <jcapote@gmail.com>2023-01-02 00:38:31 +0000
commita69f9cfc6ba6ff332d1e2d8303020d49443ca8cb (patch)
treea487561c677a1c54fecae70fa5f46294449bd1b4 /models/persister.go
parentbce250b0e75812c4f61b925d898c320d6ef11c5c (diff)
downloadcommunique-a69f9cfc6ba6ff332d1e2d8303020d49443ca8cb.tar.gz
working dedup
Diffstat (limited to 'models/persister.go')
-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
}