v2_store.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 etcdserver
  14. import (
  15. "fmt"
  16. "time"
  17. "github.com/coreos/etcd/store"
  18. )
  19. const (
  20. stset = iota
  21. stcreate
  22. stupdate
  23. stcas
  24. stdelete
  25. stcad
  26. stqget
  27. stsync
  28. )
  29. func (p *participant) Set(key string, dir bool, value string, expireTime time.Time) (*store.Event, error) {
  30. set := &Cmd{Type: stset, Key: key, Dir: &dir, Value: &value, Time: mustMarshalTime(&expireTime)}
  31. return p.do(set)
  32. }
  33. func (p *participant) Create(key string, dir bool, value string, expireTime time.Time, unique bool) (*store.Event, error) {
  34. create := &Cmd{Type: stcreate, Key: key, Dir: &dir, Value: &value, Time: mustMarshalTime(&expireTime), Unique: &unique}
  35. return p.do(create)
  36. }
  37. func (p *participant) Update(key string, value string, expireTime time.Time) (*store.Event, error) {
  38. update := &Cmd{Type: stupdate, Key: key, Value: &value, Time: mustMarshalTime(&expireTime)}
  39. return p.do(update)
  40. }
  41. func (p *participant) CAS(key, value, prevValue string, prevIndex uint64, expireTime time.Time) (*store.Event, error) {
  42. cas := &Cmd{Type: stcas, Key: key, Value: &value, PrevValue: &prevValue, PrevIndex: &prevIndex, Time: mustMarshalTime(&expireTime)}
  43. return p.do(cas)
  44. }
  45. func (p *participant) Delete(key string, dir, recursive bool) (*store.Event, error) {
  46. d := &Cmd{Type: stdelete, Key: key, Dir: &dir, Recursive: &recursive}
  47. return p.do(d)
  48. }
  49. func (p *participant) CAD(key string, prevValue string, prevIndex uint64) (*store.Event, error) {
  50. cad := &Cmd{Type: stcad, Key: key, PrevValue: &prevValue, PrevIndex: &prevIndex}
  51. return p.do(cad)
  52. }
  53. func (p *participant) QuorumGet(key string, recursive, sorted bool) (*store.Event, error) {
  54. get := &Cmd{Type: stqget, Key: key, Recursive: &recursive, Sorted: &sorted}
  55. return p.do(get)
  56. }
  57. func (p *participant) do(c *Cmd) (*store.Event, error) {
  58. data, err := c.Marshal()
  59. if err != nil {
  60. panic(err)
  61. }
  62. pp := v2Proposal{
  63. data: data,
  64. ret: make(chan interface{}, 1),
  65. }
  66. select {
  67. case p.proposal <- pp:
  68. default:
  69. return nil, fmt.Errorf("unable to send out the proposal")
  70. }
  71. var ret interface{}
  72. select {
  73. case ret = <-pp.ret:
  74. case <-p.stopNotifyc:
  75. return nil, fmt.Errorf("stop serving")
  76. }
  77. switch t := ret.(type) {
  78. case *store.Event:
  79. return t, nil
  80. case error:
  81. return nil, t
  82. default:
  83. panic("server.do: unexpected return type")
  84. }
  85. }