From 79b99f052bef09046f90b1f853f482cd7721269e Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Wed, 29 Jan 2020 10:14:32 +0400 Subject: [PATCH] lite2: batch save & delete operations in DB store (#4345) Closes #4330 --- lite2/store/db/db.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/lite2/store/db/db.go b/lite2/store/db/db.go index 4f534cd11..32c5a4d50 100644 --- a/lite2/store/db/db.go +++ b/lite2/store/db/db.go @@ -1,11 +1,11 @@ package db import ( - "errors" "fmt" "regexp" "strconv" + "github.com/pkg/errors" "github.com/tendermint/go-amino" dbm "github.com/tendermint/tm-db" @@ -38,20 +38,21 @@ func (s *dbs) SaveSignedHeaderAndNextValidatorSet(sh *types.SignedHeader, valSet panic("negative or zero height") } - // TODO: batch - bz, err := s.cdc.MarshalBinaryLengthPrefixed(sh) + shBz, err := s.cdc.MarshalBinaryLengthPrefixed(sh) if err != nil { - return err + return errors.Wrap(err, "marshalling header") } - s.db.Set(s.shKey(sh.Height), bz) - - bz, err = s.cdc.MarshalBinaryLengthPrefixed(valSet) + valSetBz, err := s.cdc.MarshalBinaryLengthPrefixed(valSet) if err != nil { - return err + return errors.Wrap(err, "marshalling validator set") } - s.db.Set(s.vsKey(sh.Height+1), bz) - return nil + b := s.db.NewBatch() + b.Set(s.shKey(sh.Height), shBz) + b.Set(s.vsKey(sh.Height+1), valSetBz) + err = b.WriteSync() + b.Close() + return err } // DeleteSignedHeaderAndNextValidatorSet deletes SignedHeader and ValidatorSet @@ -61,11 +62,12 @@ func (s *dbs) DeleteSignedHeaderAndNextValidatorSet(height int64) error { panic("negative or zero height") } - // TODO: batch - s.db.Delete(s.shKey(height)) - s.db.Delete(s.vsKey(height + 1)) - - return nil + b := s.db.NewBatch() + b.Delete(s.shKey(height)) + b.Delete(s.vsKey(height + 1)) + err := b.WriteSync() + b.Close() + return err } // SignedHeader loads SignedHeader at the given height.