example_kv_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Copyright 2016 The etcd Authors
  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. "context"
  17. "fmt"
  18. "log"
  19. "go.etcd.io/etcd/clientv3"
  20. "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes"
  21. )
  22. func ExampleKV_put() {
  23. cli, err := clientv3.New(clientv3.Config{
  24. Endpoints: endpoints,
  25. DialTimeout: dialTimeout,
  26. })
  27. if err != nil {
  28. log.Fatal(err)
  29. }
  30. defer cli.Close()
  31. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  32. _, err = cli.Put(ctx, "sample_key", "sample_value")
  33. cancel()
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. }
  38. func ExampleKV_putErrorHandling() {
  39. cli, err := clientv3.New(clientv3.Config{
  40. Endpoints: endpoints,
  41. DialTimeout: dialTimeout,
  42. })
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. defer cli.Close()
  47. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  48. _, err = cli.Put(ctx, "", "sample_value")
  49. cancel()
  50. if err != nil {
  51. switch err {
  52. case context.Canceled:
  53. fmt.Printf("ctx is canceled by another routine: %v\n", err)
  54. case context.DeadlineExceeded:
  55. fmt.Printf("ctx is attached with a deadline is exceeded: %v\n", err)
  56. case rpctypes.ErrEmptyKey:
  57. fmt.Printf("client-side error: %v\n", err)
  58. default:
  59. fmt.Printf("bad cluster endpoints, which are not etcd servers: %v\n", err)
  60. }
  61. }
  62. // Output: client-side error: etcdserver: key is not provided
  63. }
  64. func ExampleKV_get() {
  65. cli, err := clientv3.New(clientv3.Config{
  66. Endpoints: endpoints,
  67. DialTimeout: dialTimeout,
  68. })
  69. if err != nil {
  70. log.Fatal(err)
  71. }
  72. defer cli.Close()
  73. _, err = cli.Put(context.TODO(), "foo", "bar")
  74. if err != nil {
  75. log.Fatal(err)
  76. }
  77. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  78. resp, err := cli.Get(ctx, "foo")
  79. cancel()
  80. if err != nil {
  81. log.Fatal(err)
  82. }
  83. for _, ev := range resp.Kvs {
  84. fmt.Printf("%s : %s\n", ev.Key, ev.Value)
  85. }
  86. // Output: foo : bar
  87. }
  88. func ExampleKV_getWithRev() {
  89. cli, err := clientv3.New(clientv3.Config{
  90. Endpoints: endpoints,
  91. DialTimeout: dialTimeout,
  92. })
  93. if err != nil {
  94. log.Fatal(err)
  95. }
  96. defer cli.Close()
  97. presp, err := cli.Put(context.TODO(), "foo", "bar1")
  98. if err != nil {
  99. log.Fatal(err)
  100. }
  101. _, err = cli.Put(context.TODO(), "foo", "bar2")
  102. if err != nil {
  103. log.Fatal(err)
  104. }
  105. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  106. resp, err := cli.Get(ctx, "foo", clientv3.WithRev(presp.Header.Revision))
  107. cancel()
  108. if err != nil {
  109. log.Fatal(err)
  110. }
  111. for _, ev := range resp.Kvs {
  112. fmt.Printf("%s : %s\n", ev.Key, ev.Value)
  113. }
  114. // Output: foo : bar1
  115. }
  116. func ExampleKV_getSortedPrefix() {
  117. cli, err := clientv3.New(clientv3.Config{
  118. Endpoints: endpoints,
  119. DialTimeout: dialTimeout,
  120. })
  121. if err != nil {
  122. log.Fatal(err)
  123. }
  124. defer cli.Close()
  125. for i := range make([]int, 3) {
  126. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  127. _, err = cli.Put(ctx, fmt.Sprintf("key_%d", i), "value")
  128. cancel()
  129. if err != nil {
  130. log.Fatal(err)
  131. }
  132. }
  133. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  134. resp, err := cli.Get(ctx, "key", clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
  135. cancel()
  136. if err != nil {
  137. log.Fatal(err)
  138. }
  139. for _, ev := range resp.Kvs {
  140. fmt.Printf("%s : %s\n", ev.Key, ev.Value)
  141. }
  142. // Output:
  143. // key_2 : value
  144. // key_1 : value
  145. // key_0 : value
  146. }
  147. func ExampleKV_delete() {
  148. cli, err := clientv3.New(clientv3.Config{
  149. Endpoints: endpoints,
  150. DialTimeout: dialTimeout,
  151. })
  152. if err != nil {
  153. log.Fatal(err)
  154. }
  155. defer cli.Close()
  156. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  157. defer cancel()
  158. // count keys about to be deleted
  159. gresp, err := cli.Get(ctx, "key", clientv3.WithPrefix())
  160. if err != nil {
  161. log.Fatal(err)
  162. }
  163. // delete the keys
  164. dresp, err := cli.Delete(ctx, "key", clientv3.WithPrefix())
  165. if err != nil {
  166. log.Fatal(err)
  167. }
  168. fmt.Println("Deleted all keys:", int64(len(gresp.Kvs)) == dresp.Deleted)
  169. // Output:
  170. // Deleted all keys: true
  171. }
  172. func ExampleKV_compact() {
  173. cli, err := clientv3.New(clientv3.Config{
  174. Endpoints: endpoints,
  175. DialTimeout: dialTimeout,
  176. })
  177. if err != nil {
  178. log.Fatal(err)
  179. }
  180. defer cli.Close()
  181. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  182. resp, err := cli.Get(ctx, "foo")
  183. cancel()
  184. if err != nil {
  185. log.Fatal(err)
  186. }
  187. compRev := resp.Header.Revision // specify compact revision of your choice
  188. ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
  189. _, err = cli.Compact(ctx, compRev)
  190. cancel()
  191. if err != nil {
  192. log.Fatal(err)
  193. }
  194. }
  195. func ExampleKV_txn() {
  196. cli, err := clientv3.New(clientv3.Config{
  197. Endpoints: endpoints,
  198. DialTimeout: dialTimeout,
  199. })
  200. if err != nil {
  201. log.Fatal(err)
  202. }
  203. defer cli.Close()
  204. kvc := clientv3.NewKV(cli)
  205. _, err = kvc.Put(context.TODO(), "key", "xyz")
  206. if err != nil {
  207. log.Fatal(err)
  208. }
  209. ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
  210. _, err = kvc.Txn(ctx).
  211. // txn value comparisons are lexical
  212. If(clientv3.Compare(clientv3.Value("key"), ">", "abc")).
  213. // the "Then" runs, since "xyz" > "abc"
  214. Then(clientv3.OpPut("key", "XYZ")).
  215. // the "Else" does not run
  216. Else(clientv3.OpPut("key", "ABC")).
  217. Commit()
  218. cancel()
  219. if err != nil {
  220. log.Fatal(err)
  221. }
  222. gresp, err := kvc.Get(context.TODO(), "key")
  223. if err != nil {
  224. log.Fatal(err)
  225. }
  226. for _, ev := range gresp.Kvs {
  227. fmt.Printf("%s : %s\n", ev.Key, ev.Value)
  228. }
  229. // Output: key : XYZ
  230. }
  231. func ExampleKV_do() {
  232. cli, err := clientv3.New(clientv3.Config{
  233. Endpoints: endpoints,
  234. DialTimeout: dialTimeout,
  235. })
  236. if err != nil {
  237. log.Fatal(err)
  238. }
  239. defer cli.Close()
  240. ops := []clientv3.Op{
  241. clientv3.OpPut("put-key", "123"),
  242. clientv3.OpGet("put-key"),
  243. clientv3.OpPut("put-key", "456")}
  244. for _, op := range ops {
  245. if _, err := cli.Do(context.TODO(), op); err != nil {
  246. log.Fatal(err)
  247. }
  248. }
  249. }