Browse Source

fix queries

pull/2996/head
Konstantin Lebedev 3 years ago
parent
commit
7640e650e5
  1. 18
      weed/filer/ydb/ydb_queries.go
  2. 4
      weed/filer/ydb/ydb_store.go
  3. 8
      weed/filer/ydb/ydb_store_kv.go
  4. 4
      weed/filer/ydb/ydb_types.go

18
weed/filer/ydb/ydb_queries.go

@ -4,7 +4,7 @@ import asql "github.com/chrislusf/seaweedfs/weed/filer/abstract_sql"
const ( const (
insertQuery = ` insertQuery = `
DECLARE $dir_hash int64;
DECLARE $dir_hash AS int64;
DECLARE $name AS Utf8; DECLARE $name AS Utf8;
DECLARE $directory AS Utf8; DECLARE $directory AS Utf8;
DECLARE $meta AS String; DECLARE $meta AS String;
@ -15,7 +15,7 @@ const (
($dir_hash, $name, $directory, $meta);` ($dir_hash, $name, $directory, $meta);`
updateQuery = ` updateQuery = `
DECLARE $dir_hash int64;
DECLARE $dir_hash AS int64;
DECLARE $name AS Utf8; DECLARE $name AS Utf8;
DECLARE $directory AS Utf8; DECLARE $directory AS Utf8;
DECLARE $meta AS String; DECLARE $meta AS String;
@ -27,7 +27,7 @@ const (
COMMIT;` COMMIT;`
deleteQuery = ` deleteQuery = `
DECLARE $dir_hash int64;
DECLARE $dir_hash AS int64;
DECLARE $name AS Utf8; DECLARE $name AS Utf8;
DELETE FROM ` + asql.DEFAULT_TABLE + ` DELETE FROM ` + asql.DEFAULT_TABLE + `
@ -35,15 +35,15 @@ const (
COMMIT;` COMMIT;`
findQuery = ` findQuery = `
DECLARE $dir_hash int64;
DECLARE $dir_hash AS int64;
DECLARE $name AS Utf8; DECLARE $name AS Utf8;
SELECT meta SELECT meta
FROM file_meta
FROM ` + asql.DEFAULT_TABLE + `
WHERE dir_hash == $dir_hash AND name == $name;` WHERE dir_hash == $dir_hash AND name == $name;`
deleteFolderChildrenQuery = ` deleteFolderChildrenQuery = `
DECLARE $dir_hash int64;
DECLARE $dir_hash AS int64;
DECLARE $directory AS Utf8; DECLARE $directory AS Utf8;
DELETE FROM ` + asql.DEFAULT_TABLE + ` DELETE FROM ` + asql.DEFAULT_TABLE + `
@ -51,14 +51,14 @@ const (
COMMIT;` COMMIT;`
listDirectoryQuery = ` listDirectoryQuery = `
DECLARE $dir_hash int64;
DECLARE $dir_hash AS int64;
DECLARE $directory AS Utf8; DECLARE $directory AS Utf8;
DECLARE $start_name AS Utf8; DECLARE $start_name AS Utf8;
DECLARE $prefix AS Utf8; DECLARE $prefix AS Utf8;
DECLARE $limit AS int64;
DECLARE $limit AS Uint64;
SELECT name, meta SELECT name, meta
FROM ` + asql.DEFAULT_TABLE + ` FROM ` + asql.DEFAULT_TABLE + `
WHERE dir_hash == $dir_hash AND directory == $directory and name %s $start_name and name LIKE '$prefix%%'
WHERE dir_hash == $dir_hash AND directory == $directory and name %s $start_name and name LIKE $prefix
ORDER BY name ASC LIMIT $limit;` ORDER BY name ASC LIMIT $limit;`
) )

4
weed/filer/ydb/ydb_store.go

@ -228,8 +228,8 @@ func (store *YdbStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPath
table.ValueParam("$dir_hash", types.Int64Value(util.HashStringToLong(dir))), table.ValueParam("$dir_hash", types.Int64Value(util.HashStringToLong(dir))),
table.ValueParam("$directory", types.UTF8Value(dir)), table.ValueParam("$directory", types.UTF8Value(dir)),
table.ValueParam("$start_name", types.UTF8Value(startFileName)), table.ValueParam("$start_name", types.UTF8Value(startFileName)),
table.ValueParam("$prefix", types.UTF8Value(prefix)),
table.ValueParam("$limit", types.Int64Value(limit)),
table.ValueParam("$prefix", types.UTF8Value(prefix+"%")),
table.ValueParam("$limit", types.Uint64Value(uint64(limit))),
) )
err = store.doTxOrDB(ctx, &queryWithPragma, queryParams, roTX, func(res result.Result) error { err = store.doTxOrDB(ctx, &queryWithPragma, queryParams, roTX, func(res result.Result) error {
defer func() { defer func() {

8
weed/filer/ydb/ydb_store_kv.go

@ -17,11 +17,11 @@ func (store *YdbStore) KvPut(ctx context.Context, key []byte, value []byte) (err
return store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) (err error) { return store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) (err error) {
stmt, err := s.Prepare(ctx, withPragma(store.getPrefix(ctx, dirStr), insertQuery)) stmt, err := s.Prepare(ctx, withPragma(store.getPrefix(ctx, dirStr), insertQuery))
if err != nil { if err != nil {
return fmt.Errorf("Prepare %s: %v", util.NewFullPath(dirStr, name).Name(), err)
return fmt.Errorf("kv put prepare %s: %v", util.NewFullPath(dirStr, name).Name(), err)
} }
_, _, err = stmt.Execute(ctx, rwTX, fileMeta.queryParameters()) _, _, err = stmt.Execute(ctx, rwTX, fileMeta.queryParameters())
if err != nil { if err != nil {
return fmt.Errorf("kv put %s: %v", util.NewFullPath(dirStr, name).Name(), err)
return fmt.Errorf("kv put execute %s: %v", util.NewFullPath(dirStr, name).Name(), err)
} }
return nil return nil
}) })
@ -33,13 +33,13 @@ func (store *YdbStore) KvGet(ctx context.Context, key []byte) (value []byte, err
err = store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) error { err = store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) error {
stmt, err := s.Prepare(ctx, withPragma(store.getPrefix(ctx, dirStr), findQuery)) stmt, err := s.Prepare(ctx, withPragma(store.getPrefix(ctx, dirStr), findQuery))
if err != nil { if err != nil {
return fmt.Errorf("Prepare %s: %v", util.NewFullPath(dirStr, name), err)
return fmt.Errorf("kv get prepare %s: %v", util.NewFullPath(dirStr, name), err)
} }
_, res, err := stmt.Execute(ctx, roTX, table.NewQueryParameters( _, res, err := stmt.Execute(ctx, roTX, table.NewQueryParameters(
table.ValueParam("$dir_hash", types.Int64Value(dirHash)), table.ValueParam("$dir_hash", types.Int64Value(dirHash)),
table.ValueParam("$name", types.UTF8Value(name)))) table.ValueParam("$name", types.UTF8Value(name))))
if err != nil { if err != nil {
return fmt.Errorf("kv get %s: %v", util.NewFullPath(dirStr, name).Name(), err)
return fmt.Errorf("kv get execute %s: %v", util.NewFullPath(dirStr, name).Name(), err)
} }
defer func() { _ = res.Close() }() defer func() { _ = res.Close() }()
for res.NextRow() { for res.NextRow() {

4
weed/filer/ydb/ydb_types.go

@ -13,7 +13,7 @@ type FileMeta struct {
DirHash int64 `ydb:"type:int64"` DirHash int64 `ydb:"type:int64"`
Name string `ydb:"type:utf8"` Name string `ydb:"type:utf8"`
Directory string `ydb:"type:utf8"` Directory string `ydb:"type:utf8"`
Meta []byte `ydb:"-"`
Meta []byte `ydb:"type:string"`
} }
//ydb:gen scan,value //ydb:gen scan,value
@ -29,7 +29,7 @@ func (fm *FileMeta) queryParameters() *table.QueryParameters {
func createTableOptions() []options.CreateTableOption { func createTableOptions() []options.CreateTableOption {
return []options.CreateTableOption{ return []options.CreateTableOption{
options.WithColumn("dir_hash", types.Optional(types.TypeUint64)),
options.WithColumn("dir_hash", types.Optional(types.TypeInt64)),
options.WithColumn("name", types.Optional(types.TypeUTF8)), options.WithColumn("name", types.Optional(types.TypeUTF8)),
options.WithColumn("directory", types.Optional(types.TypeUTF8)), options.WithColumn("directory", types.Optional(types.TypeUTF8)),
options.WithColumn("meta", types.Optional(types.TypeString)), options.WithColumn("meta", types.Optional(types.TypeString)),

Loading…
Cancel
Save