-
Notifications
You must be signed in to change notification settings - Fork 688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tikv: update the usage example for Txn KV API #487
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,17 +142,26 @@ To use the Transactional Key-Value API in applications developed by golang, take | |
1. Install the necessary packages. | ||
|
||
```bash | ||
go get -v -u github.com/juju/errors | ||
go get -v -u github.com/pingcap/tidb/kv | ||
go get -v -u github.com/pingcap/tidb/store/tikv | ||
go get -v -u golang.org/x/net/context | ||
``` | ||
|
||
2. Import the dependency packages. | ||
|
||
```bash | ||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/juju/errors" | ||
"github.com/pingcap/tidb/kv" | ||
"github.com/pingcap/tidb/store/tikv" | ||
"fmt" | ||
"github.com/pingcap/tidb/terror" | ||
|
||
goctx "golang.org/x/net/context" | ||
) | ||
``` | ||
|
||
|
@@ -184,112 +193,146 @@ To use the Transactional Key-Value API in applications developed by golang, take | |
package main | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code fencing should be |
||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"strconv" | ||
"os" | ||
|
||
"github.com/juju/errors" | ||
"github.com/pingcap/tidb/kv" | ||
"github.com/pingcap/tidb/store/tikv" | ||
"github.com/pingcap/tidb/terror" | ||
|
||
goctx "golang.org/x/net/context" | ||
) | ||
|
||
type KV struct { | ||
K, V []byte | ||
} | ||
|
||
func (kv KV) String() string { | ||
return fmt.Sprintf("%s => %s (%v)", kv.K, kv.V, kv.V) | ||
} | ||
|
||
var ( | ||
store kv.Storage | ||
pdAddr = flag.String("pd", "192.168.199.113:2379", "pd address:192.168.199.113:2379") | ||
) | ||
|
||
// if key not found, set value to zero | ||
// else increase the value | ||
func increase(storage kv.Storage, key []byte) error { | ||
txn, err := storage.Begin() | ||
// Init initializes information. | ||
func initStore() { | ||
driver := tikv.Driver{} | ||
var err error | ||
store, err = driver.Open(fmt.Sprintf("tikv://%s", *pdAddr)) | ||
terror.MustNil(err) | ||
} | ||
|
||
// key1 val1 key2 val2 ... | ||
func puts(args ...[]byte) error { | ||
tx, err := store.Begin() | ||
if err != nil { | ||
return err | ||
return errors.Trace(err) | ||
} | ||
defer txn.Rollback() | ||
var oldValue int | ||
val, err := txn.Get(key) | ||
if err != nil { | ||
if !kv.ErrNotExist.Equal(err) { | ||
return err | ||
} | ||
} else { | ||
oldValue, err = strconv.Atoi(string(val)) | ||
|
||
for i := 0; i < len(args); i += 2 { | ||
key, val := args[i], args[i+1] | ||
err := tx.Set(key, val) | ||
if err != nil { | ||
return err | ||
return errors.Trace(err) | ||
} | ||
} | ||
|
||
err = txn.Set(key, []byte(strconv.Itoa(oldValue+1))) | ||
err = tx.Commit(goctx.Background()) | ||
if err != nil { | ||
return err | ||
return errors.Trace(err) | ||
} | ||
err = txn.Commit(context.Background()) | ||
|
||
return nil | ||
} | ||
|
||
// lookup value for key | ||
func lookup(storage kv.Storage, key []byte) (int, error) { | ||
var value int | ||
txn, err := storage.Begin() | ||
if err != nil { | ||
return value, err | ||
} | ||
defer txn.Rollback() | ||
val, err := txn.Get(key) | ||
func get(k []byte) (KV, error) { | ||
tx, err := store.Begin() | ||
if err != nil { | ||
return value, err | ||
return KV{}, errors.Trace(err) | ||
} | ||
value, err = strconv.Atoi(string(val)) | ||
v, err := tx.Get(k) | ||
if err != nil { | ||
return value, err | ||
return KV{}, errors.Trace(err) | ||
} | ||
return value, nil | ||
return KV{K: k, V: v}, nil | ||
} | ||
|
||
func main() { | ||
driver := tikv.Driver{} | ||
storage, err := driver.Open("tikv://192.168.199.113:2379") | ||
func dels(keys ...[]byte) error { | ||
tx, err := store.Begin() | ||
if err != nil { | ||
panic(err) | ||
return errors.Trace(err) | ||
} | ||
defer storage.Close() | ||
|
||
key := []byte("Account") | ||
// lookup account | ||
account, err := lookup(storage, key) | ||
for _, key := range keys { | ||
err := tx.Delete(key) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
} | ||
err = tx.Commit(goctx.Background()) | ||
if err != nil { | ||
fmt.Printf("failed to lookup key %s: %v\n", key, err) | ||
} else { | ||
fmt.Printf("Account is %d\n", account) | ||
return errors.Trace(err) | ||
} | ||
return nil | ||
} | ||
|
||
// increase account | ||
err = increase(storage, key) | ||
func scan(keyPrefix []byte, limit int) ([]KV, error) { | ||
tx, err := store.Begin() | ||
if err != nil { | ||
panic(err) | ||
return nil, errors.Trace(err) | ||
} | ||
|
||
// lookup account again | ||
account, err = lookup(storage, key) | ||
it, err := tx.Seek(keyPrefix) | ||
if err != nil { | ||
fmt.Printf("failed to lookup key %s: %v\n", key, err) | ||
} else { | ||
fmt.Printf("Account increased to %d\n", account) | ||
return nil, errors.Trace(err) | ||
} | ||
defer it.Close() | ||
var ret []KV | ||
for it.Valid() && limit > 0 { | ||
ret = append(ret, KV{K: it.Key()[:], V: it.Value()[:]}) | ||
limit-- | ||
it.Next() | ||
} | ||
return ret, nil | ||
} | ||
|
||
func main() { | ||
pdAddr := os.Getenv("PD_ADDR") | ||
if pdAddr != "" { | ||
os.Args = append(os.Args, "-pd", pdAddr) | ||
} | ||
flag.Parse() | ||
initStore() | ||
|
||
// set | ||
err := puts([]byte("key1"), []byte("value1"), []byte("key2"), []byte("value2")) | ||
terror.MustNil(err) | ||
|
||
// get | ||
kv, err := get([]byte("key1")) | ||
terror.MustNil(err) | ||
fmt.Println(kv) | ||
|
||
// scan | ||
ret, err := scan([]byte("key"), 10) | ||
for _, kv := range ret { | ||
fmt.Println(kv) | ||
} | ||
|
||
// delete | ||
err = dels([]byte("key1"), []byte("key2")) | ||
terror.MustNil(err) | ||
} | ||
``` | ||
|
||
The result is like: | ||
|
||
```bash | ||
INFO[0000] [pd] create pd client with endpoints [192.168.199.113:2379] | ||
INFO[0000] [pd] leader switches to: http://127.0.0.1:2379, previous: | ||
INFO[0000] [pd] init cluster id 6554145799874853483 | ||
INFO[0000] [kv] Rollback txn 400197262324006914 | ||
failed to lookup key Account: [kv:2]Error: key not exist | ||
INFO[0000] [kv] Rollback txn 400197262324006917 | ||
Account increased to 1 | ||
|
||
# run the program again | ||
INFO[0000] [pd] create pd client with endpoints [192.168.199.113:2379] | ||
INFO[0000] [pd] leader switches to: http://127.0.0.1:2379, previous: | ||
INFO[0000] [pd] init cluster id 6554145799874853483 | ||
INFO[0000] [kv] Rollback txn 400198364324954114 | ||
Account is 1 | ||
INFO[0000] [kv] Rollback txn 400198364324954117 | ||
Account increased to 2 | ||
INFO[0000] [pd] leader switches to: http://192.168.199.113:2379, previous: | ||
INFO[0000] [pd] init cluster id 6563858376412119197 | ||
key1 => value1 ([118 97 108 117 101 49]) | ||
key1 => value1 ([118 97 108 117 101 49]) | ||
key2 => value2 ([118 97 108 117 101 50]) | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bash ^^