From fa52883f424c95dcde16e9056631041af4c479c7 Mon Sep 17 00:00:00 2001 From: Julio Capote Date: Fri, 6 Jan 2023 14:16:35 -0500 Subject: callback support for deduped save --- models/persister.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'models/persister.go') diff --git a/models/persister.go b/models/persister.go index 5f79e4e..f877488 100644 --- a/models/persister.go +++ b/models/persister.go @@ -21,7 +21,15 @@ func NewPersister(log *zap.SugaredLogger, db *badger.DB) *Persister { return aso } -func (p *Persister) Store(model model) error { +func (p *Persister) StoreWithCallback(m model, onSave func()) error { + return p.storeModel(m, onSave) +} + +func (p *Persister) Store(m model) error { + return p.storeModel(m, func() {}) +} + +func (p *Persister) storeModel(model model, onSave func()) error { log := p.log.With("model", model.Name()).With("DedupKey", model.DedupKey()).With("Key", model.Key()) log.Debug("Store()") err := p.db.View(func(txn *badger.Txn) error { @@ -35,8 +43,9 @@ func (p *Persister) Store(model model) error { if updateErr != nil { return updateErr } - log.Debug("saving") + log.Debug("saving and calling storeCallback") updateErr = p.db.Update(model.SaveDedup) // stores the sha256 + onSave() } return updateErr }) -- cgit v1.2.3