Skip to content

Commit

Permalink
refactor add push/pop function
Browse files Browse the repository at this point in the history
  • Loading branch information
xiang90 committed Nov 5, 2013
1 parent c5a6f9b commit efe431e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 4 additions & 6 deletions store/heap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ func TestHeapPushPop(t *testing.T) {
path := fmt.Sprintf("%v", 10-i)
m := time.Duration(10 - i)
n := newKV(nil, path, path, 0, 0, nil, "", time.Now().Add(time.Second*m))
heap.Push(h, n)
h.push(n)
}

min := time.Now()

for i := 0; i < 10; i++ {
iNode := heap.Pop(h)
node, _ := iNode.(*Node)
node := h.pop()
if node.ExpireTime.Before(min) {
t.Fatal("heap sort wrong!")
}
Expand All @@ -45,7 +44,7 @@ func TestHeapUpdate(t *testing.T) {
m := time.Duration(10 - i)
n = newKV(nil, path, path, 0, 0, nil, "", time.Now().Add(time.Second*m))
kvs[i] = n
heap.Push(h, n)
h.push(n)
}

// Path 7
Expand All @@ -60,8 +59,7 @@ func TestHeapUpdate(t *testing.T) {
min := time.Now()

for i := 0; i < 10; i++ {
iNode := heap.Pop(h)
node, _ := iNode.(*Node)
node := h.pop()
if node.ExpireTime.Before(min) {
t.Fatal("heap sort wrong!")
}
Expand Down
10 changes: 10 additions & 0 deletions store/ttl_key_heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ func (h *TTLKeyHeap) Pop() interface{} {
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) {
index := h.Map[n]
heap.Remove(h, index)
Expand Down

0 comments on commit efe431e

Please sign in to comment.