refactor add push/pop function
This commit is contained in:
@ -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!")
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
|
Reference in New Issue
Block a user