Browse Source

refactor add push/pop function

Xiang Li 12 years ago
parent
commit
efe431ead0
2 changed files with 14 additions and 6 deletions
  1. 4 6
      store/heap_test.go
  2. 10 0
      store/ttl_key_heap.go

+ 4 - 6
store/heap_test.go

@@ -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, _ := iNode.(*Node)
+		node := h.pop()
 		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, _ := iNode.(*Node)
+		node := h.pop()
 		if node.ExpireTime.Before(min) {
 		if node.ExpireTime.Before(min) {
 			t.Fatal("heap sort wrong!")
 			t.Fatal("heap sort wrong!")
 		}
 		}

+ 10 - 0
store/ttl_key_heap.go

@@ -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)