commit: add locks

This commit is contained in:
Tzu-Jung Lee 2018-08-14 19:46:51 -07:00
parent c53f755595
commit 372cd7a72f

View file

@ -3,6 +3,7 @@ package claimtrie
import ( import (
"bytes" "bytes"
"encoding/gob" "encoding/gob"
"sync"
"github.com/lbryio/claimtrie/claim" "github.com/lbryio/claimtrie/claim"
"github.com/lbryio/claimtrie/trie" "github.com/lbryio/claimtrie/trie"
@ -35,6 +36,8 @@ type Commit struct {
// CommitMgr ... // CommitMgr ...
type CommitMgr struct { type CommitMgr struct {
sync.RWMutex
db *leveldb.DB db *leveldb.DB
commits []*Commit commits []*Commit
head *Commit head *Commit
@ -53,6 +56,8 @@ func NewCommitMgr(db *leveldb.DB) *CommitMgr {
// Head ... // Head ...
func (cm *CommitMgr) Head() *Commit { func (cm *CommitMgr) Head() *Commit {
cm.RLock()
defer cm.RUnlock()
return cm.head return cm.head
} }
@ -61,6 +66,8 @@ func (cm *CommitMgr) Commit(ht claim.Height, merkle *chainhash.Hash) {
if ht == 0 { if ht == 0 {
return return
} }
cm.Lock()
defer cm.Unlock()
c := newCommit(cm.head, CommitMeta{ht}, merkle) c := newCommit(cm.head, CommitMeta{ht}, merkle)
cm.commits = append(cm.commits, c) cm.commits = append(cm.commits, c)
cm.head = c cm.head = c
@ -68,6 +75,8 @@ func (cm *CommitMgr) Commit(ht claim.Height, merkle *chainhash.Hash) {
// Reset ... // Reset ...
func (cm *CommitMgr) Reset(ht claim.Height) { func (cm *CommitMgr) Reset(ht claim.Height) {
cm.Lock()
defer cm.Unlock()
for i := len(cm.commits) - 1; i >= 0; i-- { for i := len(cm.commits) - 1; i >= 0; i-- {
c := cm.commits[i] c := cm.commits[i]
if c.Meta.Height <= ht { if c.Meta.Height <= ht {
@ -84,6 +93,8 @@ func (cm *CommitMgr) Reset(ht claim.Height) {
// Save ... // Save ...
func (cm *CommitMgr) Save() error { func (cm *CommitMgr) Save() error {
cm.Lock()
defer cm.Unlock()
exported := struct { exported := struct {
Commits []*Commit Commits []*Commit
Head *Commit Head *Commit
@ -104,13 +115,17 @@ func (cm *CommitMgr) Save() error {
// Load ... // Load ...
func (cm *CommitMgr) Load() error { func (cm *CommitMgr) Load() error {
cm.Lock()
defer cm.Unlock()
exported := struct { exported := struct {
Commits []*Commit Commits []*Commit
Head *Commit Head *Commit
}{} }{}
data, err := cm.db.Get([]byte("CommitMgr"), nil) data, err := cm.db.Get([]byte("CommitMgr"), nil)
if err != nil { if err == leveldb.ErrNotFound {
return nil
} else if err != nil {
return errors.Wrapf(err, "db.Get(CommitMgr)") return errors.Wrapf(err, "db.Get(CommitMgr)")
} }
if err := gob.NewDecoder(bytes.NewBuffer(data)).Decode(&exported); err != nil { if err := gob.NewDecoder(bytes.NewBuffer(data)).Decode(&exported); err != nil {
@ -123,6 +138,8 @@ func (cm *CommitMgr) Load() error {
// Log ... // Log ...
func (cm *CommitMgr) Log(ht claim.Height, visit CommitVisit) { func (cm *CommitMgr) Log(ht claim.Height, visit CommitVisit) {
cm.RLock()
defer cm.RUnlock()
for i := len(cm.commits) - 1; i >= 0; i-- { for i := len(cm.commits) - 1; i >= 0; i-- {
c := cm.commits[i] c := cm.commits[i]
if c.Meta.Height > ht { if c.Meta.Height > ht {