2022-06-27 02:57:24 +00:00
|
|
|
//go:build tikv
|
|
|
|
// +build tikv
|
|
|
|
|
2021-08-26 07:20:18 +00:00
|
|
|
package tikv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
2021-08-26 07:20:18 +00:00
|
|
|
"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 08:20:35 +00:00
|
|
|
if isNotExists(err) {
|
|
|
|
return data, filer.ErrKvNotFound
|
|
|
|
}
|
2021-08-26 07:20:18 +00: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)
|
|
|
|
})
|
|
|
|
}
|