Browse Source

add watchHistory clone

Xiang Li 12 years ago
parent
commit
33e010ebd8
3 changed files with 30 additions and 4 deletions
  1. 22 0
      store/event.go
  2. 3 3
      store/store_test.go
  3. 5 1
      store/watcher.go

+ 22 - 0
store/event.go

@@ -151,3 +151,25 @@ func (eh *EventHistory) scan(prefix string, index uint64) (*Event, error) {
 	}
 
 }
+
+// clone will be protected by a stop-world lock
+// do not need to obtain internal lock
+func (eh *EventHistory) clone() *EventHistory {
+
+	clonedQueue := eventQueue{
+		Capacity: eh.Queue.Capacity,
+		Events:   make([]*Event, eh.Queue.Capacity),
+		Size:     eh.Queue.Size,
+		Front:    eh.Queue.Front,
+	}
+
+	for i, e := range eh.Queue.Events {
+		clonedQueue.Events[i] = e
+	}
+
+	return &EventHistory{
+		StartIndex: eh.StartIndex,
+		Queue:      clonedQueue,
+	}
+
+}

+ 3 - 3
store/store_test.go

@@ -247,7 +247,7 @@ func TestExpire(t *testing.T) {
 
 	s.Create("/foo", "bar", expire, 1, 1)
 
-	_, err := s.InternalGet("/foo", 1, 1)
+	_, err := s.internalGet("/foo", 1, 1)
 
 	if err != nil {
 		t.Fatalf("can not get the node")
@@ -255,7 +255,7 @@ func TestExpire(t *testing.T) {
 
 	time.Sleep(time.Second * 2)
 
-	_, err = s.InternalGet("/foo", 1, 1)
+	_, err = s.internalGet("/foo", 1, 1)
 
 	if err == nil {
 		t.Fatalf("can get the node after expiration time")
@@ -266,7 +266,7 @@ func TestExpire(t *testing.T) {
 	s.Create("/foo", "bar", expire, 1, 1)
 
 	time.Sleep(time.Millisecond * 50)
-	_, err = s.InternalGet("/foo", 1, 1)
+	_, err = s.internalGet("/foo", 1, 1)
 
 	if err != nil {
 		t.Fatalf("cannot get the node before expiration", err.Error())

+ 5 - 1
store/watcher.go

@@ -118,5 +118,9 @@ func (wh *watcherHub) notify(e *Event) {
 }
 
 func (wh *watcherHub) clone() *watcherHub {
-	return &watcherHub{}
+	clonedHistory := wh.EventHistory.clone()
+
+	return &watcherHub{
+		EventHistory: clonedHistory,
+	}
 }