From c5a6f9bb6b40e16fa9190947f75e3a1be8838d2d Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Mon, 4 Nov 2013 21:22:22 -0800 Subject: [PATCH] fix iszero --- store/heap_test.go | 4 ++-- store/node.go | 33 +++++++++++++++++++++++++++++++-- store/store.go | 3 +++ store/ttl_key_heap.go | 7 ++++++- 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/store/heap_test.go b/store/heap_test.go index 1da81d312..f55413ee1 100644 --- a/store/heap_test.go +++ b/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() diff --git a/store/node.go b/store/node.go index b95a46e87..50518f926 100644 --- a/store/node.go +++ b/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 diff --git a/store/store.go b/store/store.go index 93278bf31..aae06f92d 100644 --- a/store/store.go +++ b/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() } diff --git a/store/ttl_key_heap.go b/store/ttl_key_heap.go index 23a9997f0..9694a6abe 100644 --- a/store/ttl_key_heap.go +++ b/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) +}