X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=blobdiff_plain;f=account%2Fimage.go;h=b3ce30e14d4ab4ea904bc99c8db621f7bceb7aea;hp=0a7b115643ec06e492c2815b2c5892da516acb77;hb=refs%2Fheads%2Ffix_log;hpb=08281341c2cb02ba11d4218576256688854790fc diff --git a/account/image.go b/account/image.go index 0a7b1156..b3ce30e1 100644 --- a/account/image.go +++ b/account/image.go @@ -2,8 +2,6 @@ package account import ( - "encoding/json" - log "github.com/sirupsen/logrus" ) @@ -27,17 +25,15 @@ func (m *Manager) Backup() (*Image, error) { Slice: []*ImageSlice{}, } - accountIter := m.db.IteratorPrefix(accountPrefix) - defer accountIter.Release() - for accountIter.Next() { - a := &Account{} - if err := json.Unmarshal(accountIter.Value(), a); err != nil { - return nil, err - } + accounts, err := m.store.ListAccounts("") + if err != nil { + return nil, err + } + for _, account := range accounts { image.Slice = append(image.Slice, &ImageSlice{ - Account: a, - ContractIndex: m.GetContractIndex(a.ID), + Account: account, + ContractIndex: m.store.GetContractIndex(account.ID), }) } return image, nil @@ -48,28 +44,30 @@ func (m *Manager) Restore(image *Image) error { m.accountMu.Lock() defer m.accountMu.Unlock() - storeBatch := m.db.NewBatch() + newStore := m.store.InitBatch() + for _, slice := range image.Slice { - if existed := m.db.Get(Key(slice.Account.ID)); existed != nil { + if _, err := newStore.GetAccountByID(slice.Account.ID); err == nil { log.WithFields(log.Fields{ - "alias": slice.Account.Alias, - "id": slice.Account.ID, + "module": logModule, + "alias": slice.Account.Alias, + "id": slice.Account.ID, }).Warning("skip restore account due to already existed") continue + } else if err != ErrFindAccount { + return err } - if existed := m.db.Get(aliasKey(slice.Account.Alias)); existed != nil { + + if _, err := newStore.GetAccountByAlias(slice.Account.Alias); err == nil { return ErrDuplicateAlias + } else if err != ErrFindAccount { + return err } - rawAccount, err := json.Marshal(slice.Account) - if err != nil { - return ErrMarshalAccount + if err := newStore.SetAccount(slice.Account); err != nil { + return err } - - storeBatch.Set(Key(slice.Account.ID), rawAccount) - storeBatch.Set(aliasKey(slice.Account.Alias), []byte(slice.Account.ID)) } - storeBatch.Write() - return nil + return newStore.CommitBatch() }