Browse Source

fix iszero

Xiang Li 12 years ago
parent
commit
c5a6f9bb6b
4 changed files with 42 additions and 5 deletions
  1. 2 2
      store/heap_test.go
  2. 31 2
      store/node.go
  3. 3 0
      store/store.go
  4. 6 1
      store/ttl_key_heap.go

+ 2 - 2
store/heap_test.go

@@ -54,8 +54,8 @@ func TestHeapUpdate(t *testing.T) {
 	// Path 5
 	kvs[5].ExpireTime = time.Now().Add(time.Second * 12)
 
-	h.Update(kvs[3])
-	h.Update(kvs[5])
+	h.update(kvs[3])
+	h.update(kvs[5])
 
 	min := time.Now()
 

+ 31 - 2
store/node.go

@@ -1,6 +1,7 @@
 package store
 
 import (
+	"container/heap"
 	"path"
 	"sort"
 	"sync"
@@ -95,7 +96,7 @@ func (n *Node) IsHidden() bool {
 
 // IsPermanent function checks if the node is a permanent one.
 func (n *Node) IsPermanent() bool {
-	return !n.ExpireTime.IsZero()
+	return n.ExpireTime.IsZero()
 }
 
 // IsExpired function checks if the node has been expired.
@@ -144,7 +145,7 @@ func (n *Node) Write(value string, index uint64, term uint64) *etcdErr.Error {
 }
 
 func (n *Node) ExpirationAndTTL() (*time.Time, int64) {
-	if n.IsPermanent() {
+	if !n.IsPermanent() {
 		return &n.ExpireTime, int64(n.ExpireTime.Sub(time.Now())/time.Second) + 1
 	}
 	return nil, 0
@@ -239,6 +240,10 @@ func (n *Node) internalRemove(recursive bool, callback func(path string)) {
 			callback(n.Path)
 		}
 
+		if !n.IsPermanent() {
+			n.store.TTLKeyHeap.remove(n)
+		}
+
 		// the stop channel has a buffer. just send to it!
 		n.stopExpire <- true
 		return
@@ -257,6 +262,10 @@ func (n *Node) internalRemove(recursive bool, callback func(path string)) {
 			callback(n.Path)
 		}
 
+		if !n.IsPermanent() {
+			n.store.TTLKeyHeap.remove(n)
+		}
+
 		n.stopExpire <- true
 	}
 }
@@ -362,6 +371,26 @@ func (n *Node) Pair(recurisive, sorted bool) KeyValuePair {
 }
 
 func (n *Node) UpdateTTL(expireTime time.Time) {
+
+	if !n.IsPermanent() {
+		if expireTime.IsZero() {
+			// from ttl to permanent
+			// remove from ttl heap
+			n.store.TTLKeyHeap.remove(n)
+		} else {
+			// update ttl
+			// update ttl heap
+			n.store.TTLKeyHeap.update(n)
+		}
+
+	} else {
+		if !expireTime.IsZero() {
+			// from permanent to ttl
+			// push into ttl heap
+			heap.Push(n.store.TTLKeyHeap, n)
+		}
+	}
+
 	if !n.IsPermanent() {
 		// check if the node has been expired
 		// if the node is not expired, we need to stop the go routine associated with

+ 3 - 0
store/store.go

@@ -1,6 +1,7 @@
 package store
 
 import (
+	"container/heap"
 	"encoding/json"
 	"fmt"
 	"path"
@@ -393,6 +394,8 @@ func (s *store) internalCreate(nodePath string, value string, unique bool, repla
 
 	// Node with TTL
 	if !n.IsPermanent() {
+		heap.Push(s.TTLKeyHeap, n)
+
 		n.Expire()
 		e.Expiration, e.TTL = n.ExpirationAndTTL()
 	}

+ 6 - 1
store/ttl_key_heap.go

@@ -48,8 +48,13 @@ func (h *TTLKeyHeap) Pop() interface{} {
 	return x
 }
 
-func (h *TTLKeyHeap) Update(n *Node) {
+func (h *TTLKeyHeap) update(n *Node) {
 	index := h.Map[n]
 	heap.Remove(h, index)
 	heap.Push(h, n)
 }
+
+func (h *TTLKeyHeap) remove(n *Node) {
+	index := h.Map[n]
+	heap.Remove(h, index)
+}