v2_store.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package etcd
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. "github.com/coreos/etcd/store"
  7. )
  8. type cmd struct {
  9. Type string
  10. Key string
  11. Value string
  12. PrevValue string
  13. PrevIndex uint64
  14. Dir bool
  15. Recursive bool
  16. Unique bool
  17. Time time.Time
  18. }
  19. func (p *participant) Set(key string, dir bool, value string, expireTime time.Time) (*store.Event, error) {
  20. set := &cmd{Type: "set", Key: key, Dir: dir, Value: value, Time: expireTime}
  21. return p.do(set)
  22. }
  23. func (p *participant) Create(key string, dir bool, value string, expireTime time.Time, unique bool) (*store.Event, error) {
  24. create := &cmd{Type: "create", Key: key, Dir: dir, Value: value, Time: expireTime, Unique: unique}
  25. return p.do(create)
  26. }
  27. func (p *participant) Update(key string, value string, expireTime time.Time) (*store.Event, error) {
  28. update := &cmd{Type: "update", Key: key, Value: value, Time: expireTime}
  29. return p.do(update)
  30. }
  31. func (p *participant) CAS(key, value, prevValue string, prevIndex uint64, expireTime time.Time) (*store.Event, error) {
  32. cas := &cmd{Type: "cas", Key: key, Value: value, PrevValue: prevValue, PrevIndex: prevIndex, Time: expireTime}
  33. return p.do(cas)
  34. }
  35. func (p *participant) Delete(key string, dir, recursive bool) (*store.Event, error) {
  36. d := &cmd{Type: "delete", Key: key, Dir: dir, Recursive: recursive}
  37. return p.do(d)
  38. }
  39. func (p *participant) CAD(key string, prevValue string, prevIndex uint64) (*store.Event, error) {
  40. cad := &cmd{Type: "cad", Key: key, PrevValue: prevValue, PrevIndex: prevIndex}
  41. return p.do(cad)
  42. }
  43. func (p *participant) do(c *cmd) (*store.Event, error) {
  44. data, err := json.Marshal(c)
  45. if err != nil {
  46. panic(err)
  47. }
  48. pp := v2Proposal{
  49. data: data,
  50. ret: make(chan interface{}, 1),
  51. }
  52. select {
  53. case p.proposal <- pp:
  54. default:
  55. return nil, fmt.Errorf("unable to send out the proposal")
  56. }
  57. switch t := (<-pp.ret).(type) {
  58. case *store.Event:
  59. return t, nil
  60. case error:
  61. return nil, t
  62. default:
  63. panic("server.do: unexpected return type")
  64. }
  65. }