You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. //go:build ydb
  2. // +build ydb
  3. package ydb
  4. import (
  5. "context"
  6. "fmt"
  7. "github.com/seaweedfs/seaweedfs/weed/filer"
  8. "github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
  9. "github.com/seaweedfs/seaweedfs/weed/util"
  10. "github.com/ydb-platform/ydb-go-sdk/v3/table"
  11. "github.com/ydb-platform/ydb-go-sdk/v3/table/result/named"
  12. "github.com/ydb-platform/ydb-go-sdk/v3/table/types"
  13. )
  14. func (store *YdbStore) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
  15. dirStr, dirHash, name := abstract_sql.GenDirAndName(key)
  16. fileMeta := FileMeta{dirHash, name, dirStr, value}
  17. return store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) (err error) {
  18. _, _, err = s.Execute(ctx, rwTX, *withPragma(&store.tablePathPrefix, upsertQuery),
  19. fileMeta.queryParameters(0))
  20. if err != nil {
  21. return fmt.Errorf("kv put execute %s: %v", util.NewFullPath(dirStr, name).Name(), err)
  22. }
  23. return nil
  24. })
  25. }
  26. func (store *YdbStore) KvGet(ctx context.Context, key []byte) (value []byte, err error) {
  27. dirStr, dirHash, name := abstract_sql.GenDirAndName(key)
  28. valueFound := false
  29. err = store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) error {
  30. _, res, err := s.Execute(ctx, roTX, *withPragma(&store.tablePathPrefix, findQuery),
  31. table.NewQueryParameters(
  32. table.ValueParam("$dir_hash", types.Int64Value(dirHash)),
  33. table.ValueParam("$name", types.UTF8Value(name))))
  34. if err != nil {
  35. return fmt.Errorf("kv get execute %s: %v", util.NewFullPath(dirStr, name).Name(), err)
  36. }
  37. defer func() { _ = res.Close() }()
  38. if !res.NextResultSet(ctx) || !res.HasNextRow() {
  39. return nil
  40. }
  41. for res.NextRow() {
  42. if err := res.ScanNamed(named.OptionalWithDefault("meta", &value)); err != nil {
  43. return fmt.Errorf("scanNamed %s : %v", util.NewFullPath(dirStr, name).Name(), err)
  44. }
  45. valueFound = true
  46. return nil
  47. }
  48. return res.Err()
  49. })
  50. if !valueFound {
  51. return nil, filer.ErrKvNotFound
  52. }
  53. return value, nil
  54. }
  55. func (store *YdbStore) KvDelete(ctx context.Context, key []byte) (err error) {
  56. dirStr, dirHash, name := abstract_sql.GenDirAndName(key)
  57. return store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) (err error) {
  58. _, _, err = s.Execute(ctx, rwTX, *withPragma(&store.tablePathPrefix, deleteQuery),
  59. table.NewQueryParameters(
  60. table.ValueParam("$dir_hash", types.Int64Value(dirHash)),
  61. table.ValueParam("$name", types.UTF8Value(name))))
  62. if err != nil {
  63. return fmt.Errorf("kv delete %s: %v", util.NewFullPath(dirStr, name).Name(), err)
  64. }
  65. return nil
  66. })
  67. }