mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
return command index
This commit is contained in:
parent
c2f436a58b
commit
5681e1a9d0
@ -33,7 +33,7 @@ func (c *SetCommand) CommandName() string {
|
|||||||
|
|
||||||
// Set the value of key to value
|
// Set the value of key to value
|
||||||
func (c *SetCommand) Apply(server *raft.Server) (interface{}, error) {
|
func (c *SetCommand) Apply(server *raft.Server) (interface{}, error) {
|
||||||
return store.Set(c.Key, c.Value, c.ExpireTime)
|
return store.Set(c.Key, c.Value, c.ExpireTime, server.CommittedIndex())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the path for http request
|
// Get the path for http request
|
||||||
@ -73,7 +73,7 @@ func (c *DeleteCommand) CommandName() string {
|
|||||||
|
|
||||||
// Delete the key
|
// Delete the key
|
||||||
func (c *DeleteCommand) Apply(server *raft.Server) (interface{}, error) {
|
func (c *DeleteCommand) Apply(server *raft.Server) (interface{}, error) {
|
||||||
return store.Delete(c.Key)
|
return store.Delete(c.Key, server.CommittedIndex())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Watch command
|
// Watch command
|
||||||
|
@ -51,6 +51,8 @@ type Response struct {
|
|||||||
Exist bool `json:"exist"`
|
Exist bool `json:"exist"`
|
||||||
|
|
||||||
Expiration time.Time `json:"expiration"`
|
Expiration time.Time `json:"expiration"`
|
||||||
|
|
||||||
|
Index uint64 `json:"index"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -76,7 +78,7 @@ func (s *Store) SetMessager(messager *chan string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// set the key to value, return the old value if the key exists
|
// set the key to value, return the old value if the key exists
|
||||||
func Set(key string, value string, expireTime time.Time) ([]byte, error) {
|
func Set(key string, value string, expireTime time.Time, index uint64) ([]byte, error) {
|
||||||
|
|
||||||
key = path.Clean(key)
|
key = path.Clean(key)
|
||||||
|
|
||||||
@ -88,7 +90,7 @@ func Set(key string, value string, expireTime time.Time) ([]byte, error) {
|
|||||||
// the key may be expired, we should not add the node
|
// the key may be expired, we should not add the node
|
||||||
// also if the node exist, we need to delete the node
|
// also if the node exist, we need to delete the node
|
||||||
if isExpire && expireTime.Sub(time.Now()) < 0 {
|
if isExpire && expireTime.Sub(time.Now()) < 0 {
|
||||||
return Delete(key)
|
return Delete(key, index)
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the node
|
// get the node
|
||||||
@ -113,7 +115,7 @@ func Set(key string, value string, expireTime time.Time) ([]byte, error) {
|
|||||||
// update the information of the node
|
// update the information of the node
|
||||||
s.Nodes[key] = Node{value, expireTime, node.update}
|
s.Nodes[key] = Node{value, expireTime, node.update}
|
||||||
|
|
||||||
resp := Response{SET, key, node.Value, value, true, expireTime}
|
resp := Response{SET, key, node.Value, value, true, expireTime, index}
|
||||||
|
|
||||||
msg, err := json.Marshal(resp)
|
msg, err := json.Marshal(resp)
|
||||||
|
|
||||||
@ -138,7 +140,7 @@ func Set(key string, value string, expireTime time.Time) ([]byte, error) {
|
|||||||
go expire(key, update, expireTime)
|
go expire(key, update, expireTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp := Response{SET, key, "", value, false, expireTime}
|
resp := Response{SET, key, "", value, false, expireTime, index}
|
||||||
|
|
||||||
msg, err := json.Marshal(resp)
|
msg, err := json.Marshal(resp)
|
||||||
|
|
||||||
@ -170,7 +172,7 @@ func expire(key string, update chan time.Time, expireTime time.Time) {
|
|||||||
|
|
||||||
delete(s.Nodes, key)
|
delete(s.Nodes, key)
|
||||||
|
|
||||||
resp := Response{DELETE, key, node.Value, "", true, node.ExpireTime}
|
resp := Response{DELETE, key, node.Value, "", true, node.ExpireTime, 0}
|
||||||
|
|
||||||
msg, err := json.Marshal(resp)
|
msg, err := json.Marshal(resp)
|
||||||
|
|
||||||
@ -207,14 +209,14 @@ func Get(key string) Response {
|
|||||||
node, ok := s.Nodes[key]
|
node, ok := s.Nodes[key]
|
||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
return Response{GET, key, node.Value, node.Value, true, node.ExpireTime}
|
return Response{GET, key, node.Value, node.Value, true, node.ExpireTime, 0}
|
||||||
} else {
|
} else {
|
||||||
return Response{GET, key, "", "", false, time.Unix(0, 0)}
|
return Response{GET, key, "", "", false, time.Unix(0, 0), 0}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete the key
|
// delete the key
|
||||||
func Delete(key string) ([]byte, error) {
|
func Delete(key string, index uint64) ([]byte, error) {
|
||||||
key = path.Clean(key)
|
key = path.Clean(key)
|
||||||
|
|
||||||
node, ok := s.Nodes[key]
|
node, ok := s.Nodes[key]
|
||||||
@ -233,7 +235,7 @@ func Delete(key string) ([]byte, error) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resp := Response{DELETE, key, node.Value, "", true, node.ExpireTime}
|
resp := Response{DELETE, key, node.Value, "", true, node.ExpireTime, index}
|
||||||
|
|
||||||
msg, err := json.Marshal(resp)
|
msg, err := json.Marshal(resp)
|
||||||
|
|
||||||
@ -249,7 +251,7 @@ func Delete(key string) ([]byte, error) {
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
return json.Marshal(Response{DELETE, key, "", "", false, time.Unix(0, 0)})
|
return json.Marshal(Response{DELETE, key, "", "", false, time.Unix(0, 0), index})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user