OSDN Git Service

update SetAccount
authorChengcheng Zhang <943420582@qq.com>
Sat, 29 Jun 2019 04:00:57 +0000 (12:00 +0800)
committerChengcheng Zhang <943420582@qq.com>
Sat, 29 Jun 2019 04:00:57 +0000 (12:00 +0800)
account/accounts.go
account/image.go
account/store.go
database/account_store.go

index 7b822d4..c66aa59 100644 (file)
@@ -116,8 +116,14 @@ func (m *Manager) saveAccount(account *Account, updateIndex bool) error {
        m.store.InitBatch()
        defer m.store.CommitBatch()
 
-       if err := m.store.SetAccount(account, updateIndex); err != nil {
-               return err
+       if updateIndex {
+               if err := m.store.SetAccountIndex(account); err != nil {
+                       return err
+               }
+       } else {
+               if err := m.store.SetAccount(account); err != nil {
+                       return err
+               }
        }
 
        return nil
@@ -195,7 +201,7 @@ func (m *Manager) UpdateAccountAlias(accountID string, newAlias string) error {
        defer m.store.CommitBatch()
 
        m.store.DeleteAccountByAccountAlias(oldAlias)
-       if err := m.store.SetAccount(account, false); err != nil {
+       if err := m.store.SetAccount(account); err != nil {
                return err
        }
 
index 8c0301a..8fa2f3f 100644 (file)
@@ -61,7 +61,7 @@ func (m *Manager) Restore(image *Image) error {
                        return ErrDuplicateAlias
                }
 
-               if err := m.store.SetAccount(slice.Account, false); err != nil {
+               if err := m.store.SetAccount(slice.Account); err != nil {
                        return err
                }
        }
index edf2e80..8cfaa02 100644 (file)
@@ -10,7 +10,8 @@ import (
 type AccountStorer interface {
        InitBatch()
        CommitBatch()
-       SetAccount(*Account, bool) error
+       SetAccount(*Account) error
+       SetAccountIndex(*Account) error
        GetAccountIDByAccountAlias(string) string
        GetAccountByAccountID(string) (*Account, error)
        GetAccountIndex([]chainkd.XPub) uint64
index 8326167..272d1f7 100644 (file)
@@ -42,7 +42,7 @@ func (store *AccountStore) CommitBatch() {
 }
 
 // SetAccount set account account ID, account alias and raw account.
-func (store *AccountStore) SetAccount(account *acc.Account, updateIndex bool) error {
+func (store *AccountStore) SetAccount(account *acc.Account) error {
        rawAccount, err := json.Marshal(account)
        if err != nil {
                return acc.ErrMarshalAccount
@@ -52,11 +52,31 @@ func (store *AccountStore) SetAccount(account *acc.Account, updateIndex bool) er
        if store.batch != nil {
                batch = store.batch
        }
+
        batch.Set(AccountIDKey(account.ID), rawAccount)
        batch.Set(accountAliasKey(account.Alias), []byte(account.ID))
-       if updateIndex {
-               batch.Set(accountIndexKey(account.XPubs), common.Unit64ToBytes(account.KeyIndex))
+
+       if store.batch == nil {
+               batch.Write()
        }
+       return nil
+}
+
+// SetAccountIndex set account account ID, account alias and raw account.
+func (store *AccountStore) SetAccountIndex(account *acc.Account) error {
+       rawAccount, err := json.Marshal(account)
+       if err != nil {
+               return acc.ErrMarshalAccount
+       }
+
+       batch := store.accountDB.NewBatch()
+       if store.batch != nil {
+               batch = store.batch
+       }
+
+       batch.Set(AccountIDKey(account.ID), rawAccount)
+       batch.Set(accountAliasKey(account.Alias), []byte(account.ID))
+       batch.Set(accountIndexKey(account.XPubs), common.Unit64ToBytes(account.KeyIndex))
 
        if store.batch == nil {
                batch.Write()