v2_store.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright 2014 CoreOS Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcd
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "time"
  18. "github.com/coreos/etcd/store"
  19. )
  20. type cmd struct {
  21. Type string
  22. Key string
  23. Value string
  24. PrevValue string
  25. PrevIndex uint64
  26. Dir bool
  27. Recursive bool
  28. Unique bool
  29. Time time.Time
  30. }
  31. func (p *participant) Set(key string, dir bool, value string, expireTime time.Time) (*store.Event, error) {
  32. set := &cmd{Type: "set", Key: key, Dir: dir, Value: value, Time: expireTime}
  33. return p.do(set)
  34. }
  35. func (p *participant) Create(key string, dir bool, value string, expireTime time.Time, unique bool) (*store.Event, error) {
  36. create := &cmd{Type: "create", Key: key, Dir: dir, Value: value, Time: expireTime, Unique: unique}
  37. return p.do(create)
  38. }
  39. func (p *participant) Update(key string, value string, expireTime time.Time) (*store.Event, error) {
  40. update := &cmd{Type: "update", Key: key, Value: value, Time: expireTime}
  41. return p.do(update)
  42. }
  43. func (p *participant) CAS(key, value, prevValue string, prevIndex uint64, expireTime time.Time) (*store.Event, error) {
  44. cas := &cmd{Type: "cas", Key: key, Value: value, PrevValue: prevValue, PrevIndex: prevIndex, Time: expireTime}
  45. return p.do(cas)
  46. }
  47. func (p *participant) Delete(key string, dir, recursive bool) (*store.Event, error) {
  48. d := &cmd{Type: "delete", Key: key, Dir: dir, Recursive: recursive}
  49. return p.do(d)
  50. }
  51. func (p *participant) CAD(key string, prevValue string, prevIndex uint64) (*store.Event, error) {
  52. cad := &cmd{Type: "cad", Key: key, PrevValue: prevValue, PrevIndex: prevIndex}
  53. return p.do(cad)
  54. }
  55. func (p *participant) do(c *cmd) (*store.Event, error) {
  56. data, err := json.Marshal(c)
  57. if err != nil {
  58. panic(err)
  59. }
  60. pp := v2Proposal{
  61. data: data,
  62. ret: make(chan interface{}, 1),
  63. }
  64. select {
  65. case p.proposal <- pp:
  66. default:
  67. return nil, fmt.Errorf("unable to send out the proposal")
  68. }
  69. var ret interface{}
  70. select {
  71. case ret = <-pp.ret:
  72. case <-p.stopc:
  73. return nil, fmt.Errorf("stop serving")
  74. }
  75. switch t := ret.(type) {
  76. case *store.Event:
  77. return t, nil
  78. case error:
  79. return nil, t
  80. default:
  81. panic("server.do: unexpected return type")
  82. }
  83. }