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
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
}
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
}
// 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
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()