v2_store.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. var ret interface{}
  58. select {
  59. case ret = <-pp.ret:
  60. case <-p.stopc:
  61. return nil, fmt.Errorf("stop serving")
  62. }
  63. switch t := ret.(type) {
  64. case *store.Event:
  65. return t, nil
  66. case error:
  67. return nil, t
  68. default:
  69. panic("server.do: unexpected return type")
  70. }
  71. }