From a69f9cfc6ba6ff332d1e2d8303020d49443ca8cb Mon Sep 17 00:00:00 2001 From: Julio Capote Date: Sun, 1 Jan 2023 19:38:31 -0500 Subject: working dedup --- models/persister.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'models/persister.go') 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 } -- cgit v1.2.3