fix checking wrong index in watcher.go

This commit is contained in:
Xiang Li 2013-06-29 22:20:18 -07:00
parent 56244a6d40
commit b9a30986bb
2 changed files with 9 additions and 11 deletions

View File

@ -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) s.Responses[index - s.ResponseStartIndex] = resp
fmt.Println(len(s.Responses), " ")
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)
} }

View File

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