2022-06-27 10:57:24 +08:00
|
|
|
//go:build tikv
|
|
|
|
// +build tikv
|
|
|
|
|
2021-08-26 15:20:18 +08:00
|
|
|
package tikv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
|
|
|
"github.com/tikv/client-go/v2/txnkv"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (store *TikvStore) KvPut(ctx context.Context, key []byte, value []byte) error {
|
|
|
|
tw, err := store.getTxn(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tw.RunInTxn(func(txn *txnkv.KVTxn) error {
|
|
|
|
return txn.Set(key, value)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *TikvStore) KvGet(ctx context.Context, key []byte) ([]byte, error) {
|
|
|
|
tw, err := store.getTxn(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var data []byte = nil
|
|
|
|
err = tw.RunInTxn(func(txn *txnkv.KVTxn) error {
|
|
|
|
val, err := txn.Get(context.TODO(), key)
|
|
|
|
if err == nil {
|
|
|
|
data = val
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
2021-08-26 16:20:35 +08:00
|
|
|
if isNotExists(err) {
|
|
|
|
return data, filer.ErrKvNotFound
|
|
|
|
}
|
2021-08-26 15:20:18 +08:00
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *TikvStore) KvDelete(ctx context.Context, key []byte) error {
|
|
|
|
tw, err := store.getTxn(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tw.RunInTxn(func(txn *txnkv.KVTxn) error {
|
|
|
|
return txn.Delete(key)
|
|
|
|
})
|
|
|
|
}
|