Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
Qi Xiao committed Apr 8, 2019
1 parent 1a7002d commit 3c16e33
Showing 1 changed file with 26 additions and 40 deletions.
66 changes: 26 additions & 40 deletions sqlchain/observer/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,6 @@ const (
dbFileName = "observer.db3"
)

// Bucket stores transaction/block information as follows
/*
[root]
|
|--[height]-->[`dbID`]
| | |---> [hash] => height
| | \--> [hash] => height
| |
| [count2height]-->[`dbID`]
| | |---> [count] => height
| | \--> [count] => height
| |
| [block]-->[`dbID`]
| | |---> [height+hash+count] => block
| | \--> [height+hash+count] => block
| |
| [ack]-->[`dbID`]
| | |---> [hash] => height+offset
| | \--> [hash] => height+offset
| |
| [request]-->[`dbID`]
| | |---> [hash] => height+offset
| | \--> [hash] => height+offset
| |
| [response]-->[`dbID`]
| |---> [hash] => height+offset
| \--> [hash] => height+offset
|
\-> [subscription]
\---> [`dbID`] => height
*/

var (
// ErrStopped defines error on observer service has already stopped
ErrStopped = errors.New("observer service has stopped")
Expand Down Expand Up @@ -179,9 +147,11 @@ func NewService() (service *Service, err error) {
err = errors.Wrap(err, "query previous subscriptions failed")
return
}

defer func() {
_ = rows.Close()
}()

for rows.Next() {
var (
rawDBID string
Expand Down Expand Up @@ -262,7 +232,9 @@ func (s *Service) saveSubscriptionStatus(dbID proto.DatabaseID, count int32) (er
}

_, err = s.db.Writer().Exec(saveSubscriptionSQL, string(dbID), count)
err = errors.Wrapf(err, "save subscription failed: %s, %d", dbID, count)
if err != nil {
err = errors.Wrapf(err, "save subscription failed: %s, %d", dbID, count)
}
return
}

Expand All @@ -284,7 +256,9 @@ func (s *Service) addAck(dbID proto.DatabaseID, height int32, offset int32, ack

// store ack
_, err = s.db.Writer().Exec(saveAckSQL, string(dbID), ack.Hash().String(), height, offset)
err = errors.Wrapf(err, "save ack failed: %s, %s, %d", dbID, ack.Hash().String(), height)
if err != nil {
err = errors.Wrapf(err, "save ack failed: %s, %s, %d", dbID, ack.Hash().String(), height)
}
return
}

Expand Down Expand Up @@ -312,7 +286,9 @@ func (s *Service) addQueryTracker(dbID proto.DatabaseID, height int32, offset in
return
}
_, err = s.db.Writer().Exec(saveResponseSQL, string(dbID), qt.Response.Hash().String(), height, offset)
err = errors.Wrapf(err, "save response failed: %s, %s, %d", dbID, qt.Response.Hash().String(), height)
if err != nil {
err = errors.Wrapf(err, "save response failed: %s, %s, %d", dbID, qt.Response.Hash().String(), height)
}
return
}

Expand Down Expand Up @@ -588,7 +564,9 @@ func (s *Service) getHighestBlock(dbID proto.DatabaseID) (height int32, b *types
}

err = utils.DecodeMsgPack(blockData, &b)
err = errors.Wrapf(err, "decode block failed: %s", dbID)
if err != nil {
err = errors.Wrapf(err, "decode block failed: %s", dbID)
}

return
}
Expand All @@ -609,7 +587,9 @@ func (s *Service) getHighestBlockV2(
}

err = utils.DecodeMsgPack(blockData, &b)
err = errors.Wrapf(err, "decode block failed: %s", dbID)
if err != nil {
err = errors.Wrapf(err, "decode block failed: %s", dbID)
}

return
}
Expand All @@ -628,7 +608,9 @@ func (s *Service) getBlockByHeight(dbID proto.DatabaseID, height int32) (count i
}

err = utils.DecodeMsgPack(blockData, &b)
err = errors.Wrapf(err, "decode block failed: %s", dbID)
if err != nil {
err = errors.Wrapf(err, "decode block failed: %s", dbID)
}

return
}
Expand All @@ -649,7 +631,9 @@ func (s *Service) getBlockByCount(
}

err = utils.DecodeMsgPack(blockData, &b)
err = errors.Wrapf(err, "decode block failed: %s", dbID)
if err != nil {
err = errors.Wrapf(err, "decode block failed: %s", dbID)
}

return
}
Expand All @@ -668,7 +652,9 @@ func (s *Service) getBlock(dbID proto.DatabaseID, h *hash.Hash) (count int32, he
}

err = utils.DecodeMsgPack(blockData, &b)
err = errors.Wrapf(err, "decode block failed: %s", dbID)
if err != nil {
err = errors.Wrapf(err, "decode block failed: %s", dbID)
}

return
}
Expand Down

0 comments on commit 3c16e33

Please sign in to comment.