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.

76 lines
2.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package ydb
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/ydb-platform/ydb-go-sdk/v3/table/options"
  6. "github.com/chrislusf/seaweedfs/weed/filer"
  7. "github.com/chrislusf/seaweedfs/weed/filer/abstract_sql"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. "github.com/ydb-platform/ydb-go-sdk/v3/table"
  10. "github.com/ydb-platform/ydb-go-sdk/v3/table/result/named"
  11. "github.com/ydb-platform/ydb-go-sdk/v3/table/types"
  12. )
  13. func (store *YdbStore) KvPut(ctx context.Context, key []byte, value []byte) (err error) {
  14. dirStr, dirHash, name := abstract_sql.GenDirAndName(key)
  15. fileMeta := FileMeta{dirHash, name, dirStr, value}
  16. return store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) (err error) {
  17. _, _, err = s.Execute(ctx, rwTX, *withPragma(&store.tablePathPrefix, insertQuery),
  18. fileMeta.queryParameters(0),
  19. options.WithQueryCachePolicy(options.WithQueryCachePolicyKeepInCache()))
  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. options.WithQueryCachePolicy(options.WithQueryCachePolicyKeepInCache()))
  35. if err != nil {
  36. return fmt.Errorf("kv get execute %s: %v", util.NewFullPath(dirStr, name).Name(), err)
  37. }
  38. defer func() { _ = res.Close() }()
  39. for res.NextResultSet(ctx) {
  40. for res.NextRow() {
  41. if err := res.ScanNamed(named.OptionalWithDefault("meta", &value)); err != nil {
  42. return fmt.Errorf("scanNamed %s : %v", util.NewFullPath(dirStr, name).Name(), err)
  43. }
  44. valueFound = true
  45. return nil
  46. }
  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, insertQuery),
  59. table.NewQueryParameters(
  60. table.ValueParam("$dir_hash", types.Int64Value(dirHash)),
  61. table.ValueParam("$name", types.UTF8Value(name))),
  62. options.WithQueryCachePolicy(options.WithQueryCachePolicyKeepInCache()))
  63. if err != nil {
  64. return fmt.Errorf("kv delete %s: %v", util.NewFullPath(dirStr, name).Name(), err)
  65. }
  66. return nil
  67. })
  68. }