Browse Source

fix checking wrong index in watcher.go

Xiang Li 12 years ago
parent
commit
b9a30986bb
2 changed files with 9 additions and 11 deletions
  1. 8 7
      store/store.go
  2. 1 4
      store/watcher.go

+ 8 - 7
store/store.go

@@ -29,7 +29,7 @@ type Store struct {
 	messager *chan string
 	messager *chan string
 
 
 	// previous responses
 	// previous responses
-	Responses []Response
+	Responses [1024]Response
 
 
 	// at some point, we may need to compact the Response
 	// at some point, we may need to compact the Response
 	ResponseStartIndex uint64
 	ResponseStartIndex uint64
@@ -70,13 +70,13 @@ type Response struct {
 func init() {
 func init() {
 	s = createStore()
 	s = createStore()
 	s.messager = nil
 	s.messager = nil
+	s.ResponseStartIndex = 0
 }
 }
 
 
 // make a new stroe
 // make a new stroe
 func createStore() *Store {
 func createStore() *Store {
 	s := new(Store)
 	s := new(Store)
 	s.Nodes = make(map[string]Node)
 	s.Nodes = make(map[string]Node)
-	s.Responses = make([]Response, 0)
 	s.ResponseStartIndex = 0
 	s.ResponseStartIndex = 0
 	return s
 	return s
 }
 }
@@ -155,7 +155,7 @@ func Set(key string, value string, expireTime time.Time, index uint64) ([]byte,
 			*s.messager <- string(msg)
 			*s.messager <- string(msg)
 		}
 		}
 
 
-		s.Responses = append(s.Responses, resp)
+		s.Responses[index - s.ResponseStartIndex] = resp
 
 
 		return msg, err
 		return msg, err
 
 
@@ -183,8 +183,9 @@ func Set(key string, value string, expireTime time.Time, index uint64) ([]byte,
 			*s.messager <- string(msg)
 			*s.messager <- string(msg)
 		}
 		}
 		
 		
-		s.Responses = append(s.Responses, resp)
-		fmt.Println(len(s.Responses), " ")
+		s.Responses[index - s.ResponseStartIndex] = resp
+		
+		fmt.Println(index - s.ResponseStartIndex)
 		return msg, err
 		return msg, err
 	}
 	}
 }
 }
@@ -295,13 +296,13 @@ func Delete(key string, index uint64) ([]byte, error) {
 			*s.messager <- string(msg)
 			*s.messager <- string(msg)
 		}
 		}
 
 
-		s.Responses = append(s.Responses, resp)
+		s.Responses[index - s.ResponseStartIndex] = resp
 		return msg, err
 		return msg, err
 
 
 	} else {
 	} else {
 
 
 		resp := Response{DELETE, key, "", "", false, time.Unix(0, 0), 0, index}
 		resp := Response{DELETE, key, "", "", false, time.Unix(0, 0), 0, index}
-		s.Responses = append(s.Responses, resp)
+		s.Responses[index - s.ResponseStartIndex] = resp
 
 
 		return json.Marshal(resp)
 		return json.Marshal(resp)
 	}
 	}

+ 1 - 4
store/watcher.go

@@ -39,8 +39,7 @@ func AddWatcher(prefix string, c chan Response, sinceIndex uint64) error {
 	prefix = "/" + path.Clean(prefix)
 	prefix = "/" + path.Clean(prefix)
 
 
 	if sinceIndex != 0 && sinceIndex >= s.ResponseStartIndex {
 	if sinceIndex != 0 && sinceIndex >= s.ResponseStartIndex {
-
-		for i := sinceIndex; i < s.Index; i++ {
+		for i := sinceIndex; i <= s.Index; i++ {
 			if check(prefix, i) {
 			if check(prefix, i) {
 				c <- s.Responses[i]
 				c <- s.Responses[i]
 				return nil
 				return nil
@@ -77,9 +76,7 @@ func check(prefix string, index uint64) bool {
 	}
 	}
 
 
 	path := s.Responses[index].Key
 	path := s.Responses[index].Key
-	fmt.Println("checking ", path, " ", prefix)
 	if strings.HasPrefix(path, prefix) {
 	if strings.HasPrefix(path, prefix) {
-		fmt.Println("checking found")
 		prefixLen := len(prefix)
 		prefixLen := len(prefix)
 		if len(path) == prefixLen || path[prefixLen] == '/' {
 		if len(path) == prefixLen || path[prefixLen] == '/' {
 			return true
 			return true