From 6288d01c0eec76a7939f1baaa6273b91e1f1da66 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Wed, 18 Jun 2014 20:48:07 -0700 Subject: [PATCH] . --- blocks/account.go | 2 +- blocks/adjustment.go | 2 +- blocks/tx.go | 2 +- common/debounce.go | 8 +++++++- common/heap.go | 2 +- common/panic.go | 2 +- merkle/iavl.go | 8 ++++++-- 7 files changed, 18 insertions(+), 8 deletions(-) diff --git a/blocks/account.go b/blocks/account.go index 0758d7b1d..e7b2c5950 100644 --- a/blocks/account.go +++ b/blocks/account.go @@ -27,7 +27,7 @@ func ReadAccountId(r io.Reader) AccountId { case ACCOUNT_TYPE_BOTH: return AccountId{t, ReadUInt64(r), ReadByteSlice(r)} default: - panicf("Unknown AccountId type %x", t) + Panicf("Unknown AccountId type %x", t) return AccountId{} } } diff --git a/blocks/adjustment.go b/blocks/adjustment.go index 548ee60c6..ecac58961 100644 --- a/blocks/adjustment.go +++ b/blocks/adjustment.go @@ -54,7 +54,7 @@ func ReadAdjustment(r io.Reader) Adjustment { VoteB: ReadVote(r), } default: - panicf("Unknown Adjustment type %x", t) + Panicf("Unknown Adjustment type %x", t) return nil } } diff --git a/blocks/tx.go b/blocks/tx.go index 4a982618a..4ae1a2b84 100644 --- a/blocks/tx.go +++ b/blocks/tx.go @@ -47,7 +47,7 @@ func ReadTx(r io.Reader) Tx { Signature: ReadSignature(r), } default: - panicf("Unknown Tx type %x", t) + Panicf("Unknown Tx type %x", t) return nil } } diff --git a/common/debounce.go b/common/debounce.go index 6f5755243..cf777b7ef 100644 --- a/common/debounce.go +++ b/common/debounce.go @@ -2,6 +2,7 @@ package common import ( "time" + "sync" ) /* Debouncer */ @@ -9,6 +10,7 @@ type Debouncer struct { Ch chan struct{} quit chan struct{} dur time.Duration + mtx sync.Mutex timer *time.Timer } @@ -16,6 +18,7 @@ func NewDebouncer(dur time.Duration) *Debouncer { var timer *time.Timer var ch = make(chan struct{}) var quit = make(chan struct{}) + var mtx sync.Mutex fire := func() { go func() { select { @@ -23,17 +26,20 @@ func NewDebouncer(dur time.Duration) *Debouncer { case <-quit: } }() + mtx.Lock(); defer mtx.Unlock() timer.Reset(dur) } timer = time.AfterFunc(dur, fire) - return &Debouncer{Ch:ch, dur:dur, quit:quit, timer:timer} + return &Debouncer{Ch:ch, dur:dur, quit:quit, mtx:mtx, timer:timer} } func (d *Debouncer) Reset() { + d.mtx.Lock(); defer d.mtx.Unlock() d.timer.Reset(d.dur) } func (d *Debouncer) Stop() bool { + d.mtx.Lock(); defer d.mtx.Unlock() close(d.quit) return d.timer.Stop() } diff --git a/common/heap.go b/common/heap.go index dd2497b70..265ff4e4e 100644 --- a/common/heap.go +++ b/common/heap.go @@ -78,7 +78,7 @@ func (pq *priorityQueue) Pop() interface{} { return item } -func (pq *priorityQueue) Update(item *pqItem, value ByteSlice, priority int) { +func (pq *priorityQueue) Update(item *pqItem, value interface{}, priority int) { heap.Remove(pq, item.index) item.value = value item.priority = priority diff --git a/common/panic.go b/common/panic.go index b6cf4f328..14f63d2e9 100644 --- a/common/panic.go +++ b/common/panic.go @@ -4,6 +4,6 @@ import ( "fmt" ) -func panicf(s string, args ...interface{}) { +func Panicf(s string, args ...interface{}) { panic(fmt.Sprintf(s, args...)) } diff --git a/merkle/iavl.go b/merkle/iavl.go index 4fc0444d0..696d672ef 100644 --- a/merkle/iavl.go +++ b/merkle/iavl.go @@ -95,6 +95,9 @@ func (t *IAVLTree) Copy() Tree { return &IAVLTree{db:t.db, root:t.root} } +// Traverses all the nodes of the tree in prefix order. +// return true from cb to halt iteration. +// node.Height() == 0 if you just want a value node. func (t *IAVLTree) Traverse(cb func(Node) bool) { if t.root == nil { return } t.root.traverse(t.db, cb) @@ -104,11 +107,12 @@ func (t *IAVLTree) Values() <-chan Value { root := t.root ch := make(chan Value) go func() { - root.traverse(func(n Node) { + root.traverse(t.db, func(n Node) bool { if n.Height() == 0 { ch <- n.Value() } + return true }) close(ch) - } + }() return ch }