2022-05-01 16:20:37 +00:00
|
|
|
package ydb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer/abstract_sql"
|
2022-05-01 21:07:47 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
"github.com/ydb-platform/ydb-go-sdk/v3/table"
|
|
|
|
"github.com/ydb-platform/ydb-go-sdk/v3/table/result/named"
|
|
|
|
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
|
2022-05-01 16:20:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (store *YdbStore) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
|
|
|
|
dirStr, dirHash, name := abstract_sql.GenDirAndName(key)
|
|
|
|
fileMeta := FileMeta{dirHash, name, dirStr, value}
|
2022-05-01 21:07:47 +00:00
|
|
|
return store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) (err error) {
|
2022-05-02 07:42:20 +00:00
|
|
|
stmt, err := s.Prepare(ctx, withPragma(store.getPrefix(ctx, dirStr), insertQuery))
|
2022-05-01 21:07:47 +00:00
|
|
|
if err != nil {
|
2022-05-02 19:11:37 +00:00
|
|
|
return fmt.Errorf("kv put prepare %s: %v", util.NewFullPath(dirStr, name).Name(), err)
|
2022-05-01 21:07:47 +00:00
|
|
|
}
|
2022-05-02 07:42:20 +00:00
|
|
|
_, _, err = stmt.Execute(ctx, rwTX, fileMeta.queryParameters())
|
2022-05-01 21:07:47 +00:00
|
|
|
if err != nil {
|
2022-05-02 19:11:37 +00:00
|
|
|
return fmt.Errorf("kv put execute %s: %v", util.NewFullPath(dirStr, name).Name(), err)
|
2022-05-01 21:07:47 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-05-01 16:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *YdbStore) KvGet(ctx context.Context, key []byte) (value []byte, err error) {
|
|
|
|
dirStr, dirHash, name := abstract_sql.GenDirAndName(key)
|
2022-05-01 21:07:47 +00:00
|
|
|
valueFound := false
|
|
|
|
err = store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) error {
|
2022-05-02 07:42:20 +00:00
|
|
|
stmt, err := s.Prepare(ctx, withPragma(store.getPrefix(ctx, dirStr), findQuery))
|
2022-05-01 21:07:47 +00:00
|
|
|
if err != nil {
|
2022-05-02 19:11:37 +00:00
|
|
|
return fmt.Errorf("kv get prepare %s: %v", util.NewFullPath(dirStr, name), err)
|
2022-05-01 21:07:47 +00:00
|
|
|
}
|
|
|
|
_, res, err := stmt.Execute(ctx, roTX, table.NewQueryParameters(
|
|
|
|
table.ValueParam("$dir_hash", types.Int64Value(dirHash)),
|
|
|
|
table.ValueParam("$name", types.UTF8Value(name))))
|
|
|
|
if err != nil {
|
2022-05-02 19:11:37 +00:00
|
|
|
return fmt.Errorf("kv get execute %s: %v", util.NewFullPath(dirStr, name).Name(), err)
|
2022-05-01 21:07:47 +00:00
|
|
|
}
|
|
|
|
defer func() { _ = res.Close() }()
|
2022-05-01 16:20:37 +00:00
|
|
|
for res.NextRow() {
|
2022-05-01 21:07:47 +00:00
|
|
|
if err := res.ScanNamed(named.Required("meta", &value)); err != nil {
|
2022-05-02 17:23:07 +00:00
|
|
|
return fmt.Errorf("scanNamed %s : %v", util.NewFullPath(dirStr, name).Name(), err)
|
2022-05-01 16:20:37 +00:00
|
|
|
}
|
2022-05-01 21:07:47 +00:00
|
|
|
valueFound = true
|
|
|
|
return nil
|
2022-05-01 16:20:37 +00:00
|
|
|
}
|
2022-05-01 21:07:47 +00:00
|
|
|
return res.Err()
|
|
|
|
})
|
|
|
|
|
|
|
|
if !valueFound {
|
|
|
|
return nil, filer.ErrKvNotFound
|
2022-05-01 16:20:37 +00:00
|
|
|
}
|
2022-05-01 21:07:47 +00:00
|
|
|
|
|
|
|
return value, nil
|
2022-05-01 16:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *YdbStore) KvDelete(ctx context.Context, key []byte) (err error) {
|
|
|
|
dirStr, dirHash, name := abstract_sql.GenDirAndName(key)
|
2022-05-01 21:07:47 +00:00
|
|
|
return store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) (err error) {
|
2022-05-02 07:42:20 +00:00
|
|
|
stmt, err := s.Prepare(ctx, withPragma(store.getPrefix(ctx, dirStr), insertQuery))
|
2022-05-01 21:07:47 +00:00
|
|
|
if err != nil {
|
2022-05-02 17:23:07 +00:00
|
|
|
return fmt.Errorf("Prepare %s: %v", util.NewFullPath(dirStr, name).Name(), err)
|
2022-05-01 21:07:47 +00:00
|
|
|
}
|
|
|
|
_, _, err = stmt.Execute(ctx, rwTX, table.NewQueryParameters(
|
|
|
|
table.ValueParam("$dir_hash", types.Int64Value(dirHash)),
|
|
|
|
table.ValueParam("$name", types.UTF8Value(name))))
|
|
|
|
if err != nil {
|
2022-05-02 17:23:07 +00:00
|
|
|
return fmt.Errorf("kv delete %s: %v", util.NewFullPath(dirStr, name).Name(), err)
|
2022-05-01 21:07:47 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-05-01 16:20:37 +00:00
|
|
|
}
|