MCPcopy
hub / github.com/etcd-io/etcd / ExampleKV_txn

Function ExampleKV_txn

client/v3/example_kv_test.go:266–307  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

264}
265
266func ExampleKV_txn() {
267 forUnitTestsRunInMockedContext(mockKV_txn, func() {
268 cli, err := clientv3.New(clientv3.Config{
269 Endpoints: exampleEndpoints(),
270 DialTimeout: dialTimeout,
271 })
272 if err != nil {
273 log.Fatal(err)
274 }
275 defer cli.Close()
276
277 kvc := clientv3.NewKV(cli)
278
279 _, err = kvc.Put(context.TODO(), "key", "xyz")
280 if err != nil {
281 log.Fatal(err)
282 }
283
284 ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
285 _, err = kvc.Txn(ctx).
286 // txn value comparisons are lexical
287 If(clientv3.Compare(clientv3.Value("key"), ">", "abc")).
288 // the "Then" runs, since "xyz" > "abc"
289 Then(clientv3.OpPut("key", "XYZ")).
290 // the "Else" does not run
291 Else(clientv3.OpPut("key", "ABC")).
292 Commit()
293 cancel()
294 if err != nil {
295 log.Fatal(err)
296 }
297
298 gresp, err := kvc.Get(context.TODO(), "key")
299 if err != nil {
300 log.Fatal(err)
301 }
302 for _, ev := range gresp.Kvs {
303 fmt.Printf("%s : %s\n", ev.Key, ev.Value)
304 }
305 })
306 // Output: key : XYZ
307}
308
309func mockKV_do() {}
310

Callers

nothing calls this directly

Calls 13

PutMethod · 0.95
TxnMethod · 0.95
GetMethod · 0.95
exampleEndpointsFunction · 0.70
FatalMethod · 0.65
CloseMethod · 0.65
CommitMethod · 0.65
ElseMethod · 0.65
ThenMethod · 0.65
IfMethod · 0.65
CompareMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…