1
0
mirror of https://github.com/seaweedfs/seaweedfs.git synced 2024-12-31 05:27:51 +08:00
seaweedfs/weed/filer/ydb/ydb_store_kv.go

76 lines
2.5 KiB
Go
Raw Normal View History

//go:build ydb
// +build ydb
2022-05-02 00:20:37 +08:00
package ydb
import (
"context"
"fmt"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
"github.com/seaweedfs/seaweedfs/weed/util"
2022-05-02 05:07:47 +08:00
"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-02 00:20:37 +08: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-02 05:07:47 +08:00
return store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) (err error) {
_, _, err = s.Execute(ctx, rwTX, *withPragma(&store.tablePathPrefix, upsertQuery),
fileMeta.queryParameters(0))
2022-05-02 05:07:47 +08:00
if err != nil {
2022-05-03 03:11:37 +08:00
return fmt.Errorf("kv put execute %s: %v", util.NewFullPath(dirStr, name).Name(), err)
2022-05-02 05:07:47 +08:00
}
return nil
})
2022-05-02 00:20:37 +08:00
}
func (store *YdbStore) KvGet(ctx context.Context, key []byte) (value []byte, err error) {
dirStr, dirHash, name := abstract_sql.GenDirAndName(key)
2022-05-02 05:07:47 +08:00
valueFound := false
err = store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) error {
2022-05-03 23:16:00 +08:00
_, res, err := s.Execute(ctx, roTX, *withPragma(&store.tablePathPrefix, findQuery),
table.NewQueryParameters(
table.ValueParam("$dir_hash", types.Int64Value(dirHash)),
2022-05-10 15:02:23 +08:00
table.ValueParam("$name", types.UTF8Value(name))))
2022-05-02 05:07:47 +08:00
if err != nil {
2022-05-03 03:11:37 +08:00
return fmt.Errorf("kv get execute %s: %v", util.NewFullPath(dirStr, name).Name(), err)
2022-05-02 05:07:47 +08:00
}
defer func() { _ = res.Close() }()
if !res.NextResultSet(ctx) || !res.HasNextRow() {
return nil
}
for res.NextRow() {
if err := res.ScanNamed(named.OptionalWithDefault("meta", &value)); err != nil {
return fmt.Errorf("scanNamed %s : %v", util.NewFullPath(dirStr, name).Name(), err)
2022-05-02 00:20:37 +08:00
}
valueFound = true
return nil
2022-05-02 00:20:37 +08:00
}
2022-05-02 05:07:47 +08:00
return res.Err()
})
if !valueFound {
return nil, filer.ErrKvNotFound
2022-05-02 00:20:37 +08:00
}
2022-05-02 05:07:47 +08:00
return value, nil
2022-05-02 00:20:37 +08:00
}
func (store *YdbStore) KvDelete(ctx context.Context, key []byte) (err error) {
dirStr, dirHash, name := abstract_sql.GenDirAndName(key)
2022-05-02 05:07:47 +08:00
return store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) (err error) {
_, _, err = s.Execute(ctx, rwTX, *withPragma(&store.tablePathPrefix, deleteQuery),
table.NewQueryParameters(
table.ValueParam("$dir_hash", types.Int64Value(dirHash)),
table.ValueParam("$name", types.UTF8Value(name))))
2022-05-02 05:07:47 +08:00
if err != nil {
2022-05-03 01:23:07 +08:00
return fmt.Errorf("kv delete %s: %v", util.NewFullPath(dirStr, name).Name(), err)
2022-05-02 05:07:47 +08:00
}
return nil
})
2022-05-02 00:20:37 +08:00
}