From 91aed9e232f89031978c078989cc50ed87b62283 Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Wed, 21 Aug 2013 14:21:17 +0300 Subject: [PATCH 1/3] Fix error code in README.md There is no error code with number 404. It returns 100 when no key exist. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 259b0462a..04ccae37f 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ Now you can try to get the key by sending: curl -L http://127.0.0.1:4001/v1/keys/foo ``` -If the TTL has expired, the key will be deleted, and you will be returned a 404. +If the TTL has expired, the key will be deleted, and you will be returned a 100. ```json {"errorCode":100,"message":"Key Not Found","cause":"/foo"} From 6345e02d20189d90240e2e41c51718c493b043c4 Mon Sep 17 00:00:00 2001 From: "Fabrizio (Misto) Milo" Date: Thu, 22 Aug 2013 15:04:17 -0700 Subject: [PATCH 2/3] test and set creates the key if key does not exists. fixes #96 --- store/store.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/store/store.go b/store/store.go index d37345f4d..c1da0fc27 100644 --- a/store/store.go +++ b/store/store.go @@ -465,12 +465,16 @@ func (s *Store) TestAndSet(key string, prevValue string, value string, expireTim resp := s.internalGet(key) if resp == nil { - return nil, etcdErr.NewError(100, "testandset: "+key) + if prevValue != "" { + errmsg := fmt.Sprintf("TestAndSet: key not found and previousValue is not empty %s:%s ", key, prevValue) + return nil, etcdErr.NewError(100, errmsg) + } + return s.internalSet(key, value, expireTime, index) } if resp.Value == prevValue { - // If test success, do set + // If test succeed, do set return s.internalSet(key, value, expireTime, index) } else { From a543d644b401b3aaa36d7219700de595853bb874 Mon Sep 17 00:00:00 2001 From: "Fabrizio (Misto) Milo" Date: Wed, 21 Aug 2013 16:39:23 -0700 Subject: [PATCH 3/3] Split raw get into rawGetNode and rawGetNodeList --- store/store.go | 194 +++++++++++++++++++++++++------------------------ 1 file changed, 99 insertions(+), 95 deletions(-) diff --git a/store/store.go b/store/store.go index c1da0fc27..e4eb79db0 100644 --- a/store/store.go +++ b/store/store.go @@ -325,6 +325,62 @@ func (s *Store) Get(key string) ([]byte, error) { return json.Marshal(resps) } +func (s *Store) rawGetNode(key string, node *Node) ([]*Response, error) { + resps := make([]*Response, 1) + + isExpire := !node.ExpireTime.Equal(PERMANENT) + + resps[0] = &Response{ + Action: "GET", + Index: s.Index, + Key: key, + Value: node.Value, + } + + // Update ttl + if isExpire { + TTL := int64(node.ExpireTime.Sub(time.Now()) / time.Second) + resps[0].Expiration = &node.ExpireTime + resps[0].TTL = TTL + } + + return resps, nil +} + +func (s *Store) rawGetNodeList(key string, keys []string, nodes []*Node) ([]*Response, error) { + resps := make([]*Response, len(nodes)) + + // TODO: check if nodes and keys are the same length + for i := 0; i < len(nodes); i++ { + var TTL int64 + var isExpire bool = false + + isExpire = !nodes[i].ExpireTime.Equal(PERMANENT) + + resps[i] = &Response{ + Action: "GET", + Index: s.Index, + Key: path.Join(key, keys[i]), + } + + if len(nodes[i].Value) != 0 { + resps[i].Value = nodes[i].Value + } else { + resps[i].Dir = true + } + + // Update ttl + if isExpire { + TTL = int64(nodes[i].ExpireTime.Sub(time.Now()) / time.Second) + resps[i].Expiration = &nodes[i].ExpireTime + resps[i].TTL = TTL + } + + } + + return resps, nil +} + func (s *Store) RawGet(key string) ([]*Response, error) { // Update stats s.BasicStats.Gets++ @@ -332,68 +388,18 @@ func (s *Store) RawGet(key string) ([]*Response, error) { key = path.Clean("/" + key) nodes, keys, ok := s.Tree.list(key) - - if ok { - - node, ok := nodes.(*Node) - - if ok { - resps := make([]*Response, 1) - - isExpire := !node.ExpireTime.Equal(PERMANENT) - - resps[0] = &Response{ - Action: "GET", - Index: s.Index, - Key: key, - Value: node.Value, - } - - // Update ttl - if isExpire { - TTL := int64(node.ExpireTime.Sub(time.Now()) / time.Second) - resps[0].Expiration = &node.ExpireTime - resps[0].TTL = TTL - } - - return resps, nil - } - - nodes, _ := nodes.([]*Node) - - resps := make([]*Response, len(nodes)) - for i := 0; i < len(nodes); i++ { - - var TTL int64 - var isExpire bool = false - - isExpire = !nodes[i].ExpireTime.Equal(PERMANENT) - - resps[i] = &Response{ - Action: "GET", - Index: s.Index, - Key: path.Join(key, keys[i]), - } - - if len(nodes[i].Value) != 0 { - resps[i].Value = nodes[i].Value - } else { - resps[i].Dir = true - } - - // Update ttl - if isExpire { - TTL = int64(nodes[i].ExpireTime.Sub(time.Now()) / time.Second) - resps[i].Expiration = &nodes[i].ExpireTime - resps[i].TTL = TTL - } - - } - - return resps, nil + if !ok { + return nil, etcdErr.NewError(100, "get: "+key) } - return nil, etcdErr.NewError(100, "get: "+key) + switch node := nodes.(type) { + case *Node: + return s.rawGetNode(key, node) + case []*Node: + return s.rawGetNodeList(key, keys, node) + default: + panic("invalid cast ") + } } func (s *Store) Delete(key string, index uint64) ([]byte, error) { @@ -415,43 +421,41 @@ func (s *Store) internalDelete(key string, index uint64) ([]byte, error) { node, ok := s.Tree.get(key) - if ok { - - resp := Response{ - Action: "DELETE", - Key: key, - PrevValue: node.Value, - Index: index, - } - - if node.ExpireTime.Equal(PERMANENT) { - - s.Tree.delete(key) - - } else { - resp.Expiration = &node.ExpireTime - // Kill the expire go routine - node.update <- PERMANENT - s.Tree.delete(key) - - } - - msg, err := json.Marshal(resp) - - s.watcher.notify(resp) - - // notify the messager - if s.messager != nil && err == nil { - s.messager <- string(msg) - } - - s.addToResponseMap(index, &resp) - - return msg, err - - } else { + if !ok { return nil, etcdErr.NewError(100, "delete: "+key) } + + resp := Response{ + Action: "DELETE", + Key: key, + PrevValue: node.Value, + Index: index, + } + + if node.ExpireTime.Equal(PERMANENT) { + + s.Tree.delete(key) + + } else { + resp.Expiration = &node.ExpireTime + // Kill the expire go routine + node.update <- PERMANENT + s.Tree.delete(key) + + } + + msg, err := json.Marshal(resp) + + s.watcher.notify(resp) + + // notify the messager + if s.messager != nil && err == nil { + s.messager <- string(msg) + } + + s.addToResponseMap(index, &resp) + + return msg, err } // Set the value of the key to the value if the given prevValue is equal to the value of the key