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

Function ExampleKV_getSortedPrefix

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

Source from the content-addressed store, hash-verified

157}
158
159func ExampleKV_getSortedPrefix() {
160 forUnitTestsRunInMockedContext(mockKV_getSortedPrefix, func() {
161 cli, err := clientv3.New(clientv3.Config{
162 Endpoints: exampleEndpoints(),
163 DialTimeout: dialTimeout,
164 })
165 if err != nil {
166 log.Fatal(err)
167 }
168 defer cli.Close()
169
170 for i := range make([]int, 3) {
171 ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
172 _, err = cli.Put(ctx, fmt.Sprintf("key_%d", i), "value")
173 cancel()
174 if err != nil {
175 log.Fatal(err)
176 }
177 }
178
179 ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
180 resp, err := cli.Get(ctx, "key", clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
181 cancel()
182 if err != nil {
183 log.Fatal(err)
184 }
185 for _, ev := range resp.Kvs {
186 fmt.Printf("%s : %s\n", ev.Key, ev.Value)
187 }
188 })
189 // Output:
190 // key_2 : value
191 // key_1 : value
192 // key_0 : value
193}
194
195func mockKV_delete() {
196 fmt.Println("Deleted all keys: true")

Callers

nothing calls this directly

Calls 7

WithPrefixMethod · 0.80
exampleEndpointsFunction · 0.70
FatalMethod · 0.65
CloseMethod · 0.65
PutMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…