refactor add push/pop function

This commit is contained in:
Xiang Li
2013-11-04 21:33:23 -08:00
parent c5a6f9bb6b
commit efe431ead0
2 changed files with 14 additions and 6 deletions

View File

@ -16,14 +16,13 @@ func TestHeapPushPop(t *testing.T) {
path := fmt.Sprintf("%v", 10-i) path := fmt.Sprintf("%v", 10-i)
m := time.Duration(10 - i) m := time.Duration(10 - i)
n := newKV(nil, path, path, 0, 0, nil, "", time.Now().Add(time.Second*m)) n := newKV(nil, path, path, 0, 0, nil, "", time.Now().Add(time.Second*m))
heap.Push(h, n) h.push(n)
} }
min := time.Now() min := time.Now()
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
iNode := heap.Pop(h) node := h.pop()
node, _ := iNode.(*Node)
if node.ExpireTime.Before(min) { if node.ExpireTime.Before(min) {
t.Fatal("heap sort wrong!") t.Fatal("heap sort wrong!")
} }
@ -45,7 +44,7 @@ func TestHeapUpdate(t *testing.T) {
m := time.Duration(10 - i) m := time.Duration(10 - i)
n = newKV(nil, path, path, 0, 0, nil, "", time.Now().Add(time.Second*m)) n = newKV(nil, path, path, 0, 0, nil, "", time.Now().Add(time.Second*m))
kvs[i] = n kvs[i] = n
heap.Push(h, n) h.push(n)
} }
// Path 7 // Path 7
@ -60,8 +59,7 @@ func TestHeapUpdate(t *testing.T) {
min := time.Now() min := time.Now()
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
iNode := heap.Pop(h) node := h.pop()
node, _ := iNode.(*Node)
if node.ExpireTime.Before(min) { if node.ExpireTime.Before(min) {
t.Fatal("heap sort wrong!") t.Fatal("heap sort wrong!")
} }

View File

@ -48,6 +48,16 @@ func (h *TTLKeyHeap) Pop() interface{} {
return x return x
} }
func (h *TTLKeyHeap) pop() *Node {
x := heap.Pop(h)
n, _ := x.(*Node)
return n
}
func (h *TTLKeyHeap) push(x interface{}) {
heap.Push(h, x)
}
func (h *TTLKeyHeap) update(n *Node) { func (h *TTLKeyHeap) update(n *Node) {
index := h.Map[n] index := h.Map[n]
heap.Remove(h, index) heap.Remove(h, index)