mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
211 lines
4.7 KiB
Go
211 lines
4.7 KiB
Go
// Copyright 2016 CoreOS, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package clientv3_test
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/coreos/etcd/clientv3"
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
func ExampleKV_put() {
|
|
cli, err := clientv3.New(clientv3.Config{
|
|
Endpoints: endpoints,
|
|
DialTimeout: dialTimeout,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer cli.Close()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
|
resp, err := cli.Put(ctx, "sample_key", "sample_value")
|
|
cancel()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Println("current revision:", resp.Header.Revision) // revision start at 1
|
|
// current revision: 2
|
|
}
|
|
|
|
func ExampleKV_get() {
|
|
cli, err := clientv3.New(clientv3.Config{
|
|
Endpoints: endpoints,
|
|
DialTimeout: dialTimeout,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer cli.Close()
|
|
|
|
_, err = cli.Put(context.TODO(), "foo", "bar")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
|
resp, err := cli.Get(ctx, "foo")
|
|
cancel()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
for _, ev := range resp.Kvs {
|
|
fmt.Printf("%s : %s\n", ev.Key, ev.Value)
|
|
}
|
|
// foo : bar
|
|
}
|
|
|
|
func ExampleKV_getSortedPrefix() {
|
|
cli, err := clientv3.New(clientv3.Config{
|
|
Endpoints: endpoints,
|
|
DialTimeout: dialTimeout,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer cli.Close()
|
|
|
|
for i := range make([]int, 3) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
|
_, err = cli.Put(ctx, fmt.Sprintf("key_%d", i), "value")
|
|
cancel()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
|
resp, err := cli.Get(ctx, "key", clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
|
|
cancel()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
for _, ev := range resp.Kvs {
|
|
fmt.Printf("%s : %s\n", ev.Key, ev.Value)
|
|
}
|
|
// key_2 : value
|
|
// key_1 : value
|
|
// key_0 : value
|
|
}
|
|
|
|
func ExampleKV_delete() {
|
|
cli, err := clientv3.New(clientv3.Config{
|
|
Endpoints: endpoints,
|
|
DialTimeout: dialTimeout,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer cli.Close()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
|
resp, err := cli.Delete(ctx, "key", clientv3.WithPrefix())
|
|
cancel()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Println("Deleted", resp.Deleted, "keys")
|
|
// Deleted n keys
|
|
}
|
|
|
|
func ExampleKV_compact() {
|
|
cli, err := clientv3.New(clientv3.Config{
|
|
Endpoints: endpoints,
|
|
DialTimeout: dialTimeout,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer cli.Close()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
|
resp, err := cli.Get(ctx, "foo")
|
|
cancel()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
compRev := resp.Header.Revision // specify compact revision of your choice
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
|
|
err = cli.Compact(ctx, compRev)
|
|
cancel()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func ExampleKV_txn() {
|
|
cli, err := clientv3.New(clientv3.Config{
|
|
Endpoints: endpoints,
|
|
DialTimeout: dialTimeout,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer cli.Close()
|
|
|
|
kvc := clientv3.NewKV(cli)
|
|
|
|
_, err = kvc.Put(context.TODO(), "key", "xyz")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
|
_, err = kvc.Txn(ctx).
|
|
If(clientv3.Compare(clientv3.Value("key"), ">", "abc")). // txn value comparisons are lexical
|
|
Then(clientv3.OpPut("key", "XYZ")). // this runs, since 'xyz' > 'abc'
|
|
Else(clientv3.OpPut("key", "ABC")).
|
|
Commit()
|
|
cancel()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
gresp, err := kvc.Get(context.TODO(), "key")
|
|
cancel()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
for _, ev := range gresp.Kvs {
|
|
fmt.Printf("%s : %s\n", ev.Key, ev.Value)
|
|
}
|
|
// key : XYZ
|
|
}
|
|
|
|
func ExampleKV_do() {
|
|
cli, err := clientv3.New(clientv3.Config{
|
|
Endpoints: endpoints,
|
|
DialTimeout: dialTimeout,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer cli.Close()
|
|
|
|
ops := []clientv3.Op{
|
|
clientv3.OpPut("put-key", "123"),
|
|
clientv3.OpGet("put-key"),
|
|
clientv3.OpPut("put-key", "456")}
|
|
|
|
for _, op := range ops {
|
|
if _, err := cli.Do(context.TODO(), op); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
}
|