example_kv_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright 2016 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package clientv3_test
  15. import (
  16. "fmt"
  17. "log"
  18. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  19. "github.com/coreos/etcd/clientv3"
  20. )
  21. func ExampleKV_put() {
  22. cli, err := clientv3.New(clientv3.Config{
  23. Endpoints: endpoints,
  24. DialTimeout: dialTimeout,
  25. })
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. defer cli.Close()
  30. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  31. resp, err := cli.Put(ctx, "sample_key", "sample_value")
  32. cancel()
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. fmt.Println("current revision:", resp.Header.Revision) // revision start at 1
  37. // current revision: 2
  38. }
  39. func ExampleKV_get() {
  40. cli, err := clientv3.New(clientv3.Config{
  41. Endpoints: endpoints,
  42. DialTimeout: dialTimeout,
  43. })
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. defer cli.Close()
  48. _, err = cli.Put(context.TODO(), "foo", "bar")
  49. if err != nil {
  50. log.Fatal(err)
  51. }
  52. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  53. resp, err := cli.Get(ctx, "foo")
  54. cancel()
  55. if err != nil {
  56. log.Fatal(err)
  57. }
  58. for _, ev := range resp.Kvs {
  59. fmt.Printf("%s : %s\n", ev.Key, ev.Value)
  60. }
  61. // foo : bar
  62. }
  63. func ExampleKV_getSortedPrefix() {
  64. cli, err := clientv3.New(clientv3.Config{
  65. Endpoints: endpoints,
  66. DialTimeout: dialTimeout,
  67. })
  68. if err != nil {
  69. log.Fatal(err)
  70. }
  71. defer cli.Close()
  72. for i := range make([]int, 3) {
  73. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  74. _, err = cli.Put(ctx, fmt.Sprintf("key_%d", i), "value")
  75. cancel()
  76. if err != nil {
  77. log.Fatal(err)
  78. }
  79. }
  80. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  81. resp, err := cli.Get(ctx, "key", clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
  82. cancel()
  83. if err != nil {
  84. log.Fatal(err)
  85. }
  86. for _, ev := range resp.Kvs {
  87. fmt.Printf("%s : %s\n", ev.Key, ev.Value)
  88. }
  89. // key_2 : value
  90. // key_1 : value
  91. // key_0 : value
  92. }
  93. func ExampleKV_delete() {
  94. cli, err := clientv3.New(clientv3.Config{
  95. Endpoints: endpoints,
  96. DialTimeout: dialTimeout,
  97. })
  98. if err != nil {
  99. log.Fatal(err)
  100. }
  101. defer cli.Close()
  102. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  103. resp, err := cli.Delete(ctx, "key", clientv3.WithPrefix())
  104. cancel()
  105. if err != nil {
  106. log.Fatal(err)
  107. }
  108. fmt.Println("Deleted", resp.Deleted, "keys")
  109. // Deleted n keys
  110. }
  111. func ExampleKV_compact() {
  112. cli, err := clientv3.New(clientv3.Config{
  113. Endpoints: endpoints,
  114. DialTimeout: dialTimeout,
  115. })
  116. if err != nil {
  117. log.Fatal(err)
  118. }
  119. defer cli.Close()
  120. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  121. resp, err := cli.Get(ctx, "foo")
  122. cancel()
  123. if err != nil {
  124. log.Fatal(err)
  125. }
  126. compRev := resp.Header.Revision // specify compact revision of your choice
  127. ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
  128. err = cli.Compact(ctx, compRev)
  129. cancel()
  130. if err != nil {
  131. log.Fatal(err)
  132. }
  133. }
  134. func ExampleKV_txn() {
  135. cli, err := clientv3.New(clientv3.Config{
  136. Endpoints: endpoints,
  137. DialTimeout: dialTimeout,
  138. })
  139. if err != nil {
  140. log.Fatal(err)
  141. }
  142. defer cli.Close()
  143. kvc := clientv3.NewKV(cli)
  144. _, err = kvc.Put(context.TODO(), "key", "xyz")
  145. if err != nil {
  146. log.Fatal(err)
  147. }
  148. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  149. _, err = kvc.Txn(ctx).
  150. If(clientv3.Compare(clientv3.Value("key"), ">", "abc")). // txn value comparisons are lexical
  151. Then(clientv3.OpPut("key", "XYZ")). // this runs, since 'xyz' > 'abc'
  152. Else(clientv3.OpPut("key", "ABC")).
  153. Commit()
  154. cancel()
  155. if err != nil {
  156. log.Fatal(err)
  157. }
  158. gresp, err := kvc.Get(context.TODO(), "key")
  159. cancel()
  160. if err != nil {
  161. log.Fatal(err)
  162. }
  163. for _, ev := range gresp.Kvs {
  164. fmt.Printf("%s : %s\n", ev.Key, ev.Value)
  165. }
  166. // key : XYZ
  167. }
  168. func ExampleKV_do() {
  169. cli, err := clientv3.New(clientv3.Config{
  170. Endpoints: endpoints,
  171. DialTimeout: dialTimeout,
  172. })
  173. if err != nil {
  174. log.Fatal(err)
  175. }
  176. defer cli.Close()
  177. ops := []clientv3.Op{
  178. clientv3.OpPut("put-key", "123"),
  179. clientv3.OpGet("put-key"),
  180. clientv3.OpPut("put-key", "456")}
  181. for _, op := range ops {
  182. if _, err := cli.Do(context.TODO(), op); err != nil {
  183. log.Fatal(err)
  184. }
  185. }
  186. }