2022-05-01 12:28:58 +00:00
|
|
|
package ydb
|
|
|
|
|
2022-05-01 21:07:47 +00:00
|
|
|
import (
|
|
|
|
"github.com/ydb-platform/ydb-go-sdk/v3/table"
|
2022-05-02 07:42:20 +00:00
|
|
|
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
|
2022-05-01 21:07:47 +00:00
|
|
|
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
|
|
|
|
)
|
|
|
|
|
2022-05-01 12:28:58 +00:00
|
|
|
//go:generate ydbgen
|
|
|
|
|
|
|
|
//ydb:gen
|
|
|
|
type FileMeta struct {
|
|
|
|
DirHash int64 `ydb:"type:int64"`
|
|
|
|
Name string `ydb:"type:utf8"`
|
|
|
|
Directory string `ydb:"type:utf8"`
|
2022-05-02 19:11:37 +00:00
|
|
|
Meta []byte `ydb:"type:string"`
|
2022-05-01 12:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//ydb:gen scan,value
|
|
|
|
type FileMetas []FileMeta
|
2022-05-01 21:07:47 +00:00
|
|
|
|
2022-05-03 12:52:23 +00:00
|
|
|
func (fm *FileMeta) queryParameters(ttlSec int32) *table.QueryParameters {
|
|
|
|
var expireAtValue types.Value
|
|
|
|
if ttlSec > 0 {
|
|
|
|
expireAtValue = types.Uint32Value(uint32(ttlSec))
|
|
|
|
} else {
|
|
|
|
expireAtValue = types.NullValue(types.TypeUint32)
|
|
|
|
}
|
2022-05-01 21:07:47 +00:00
|
|
|
return table.NewQueryParameters(
|
|
|
|
table.ValueParam("$dir_hash", types.Int64Value(fm.DirHash)),
|
|
|
|
table.ValueParam("$directory", types.UTF8Value(fm.Directory)),
|
2022-05-03 10:18:28 +00:00
|
|
|
table.ValueParam("$name", types.UTF8Value(fm.Name)),
|
2022-05-03 12:52:23 +00:00
|
|
|
table.ValueParam("$meta", types.StringValue(fm.Meta)),
|
|
|
|
table.ValueParam("$expire_at", expireAtValue))
|
2022-05-01 21:07:47 +00:00
|
|
|
}
|
2022-05-02 07:42:20 +00:00
|
|
|
|
|
|
|
func createTableOptions() []options.CreateTableOption {
|
2022-05-03 12:52:23 +00:00
|
|
|
columnUnit := options.TimeToLiveUnitSeconds
|
2022-05-02 07:42:20 +00:00
|
|
|
return []options.CreateTableOption{
|
2022-05-02 19:11:37 +00:00
|
|
|
options.WithColumn("dir_hash", types.Optional(types.TypeInt64)),
|
2022-05-02 17:23:07 +00:00
|
|
|
options.WithColumn("directory", types.Optional(types.TypeUTF8)),
|
2022-05-03 10:18:28 +00:00
|
|
|
options.WithColumn("name", types.Optional(types.TypeUTF8)),
|
2022-05-02 17:23:07 +00:00
|
|
|
options.WithColumn("meta", types.Optional(types.TypeString)),
|
2022-05-03 12:52:23 +00:00
|
|
|
options.WithColumn("expire_at", types.Optional(types.TypeUint32)),
|
2022-05-02 07:42:20 +00:00
|
|
|
options.WithPrimaryKeyColumn("dir_hash", "name"),
|
2022-05-03 12:52:23 +00:00
|
|
|
options.WithTimeToLiveSettings(options.TimeToLiveSettings{
|
|
|
|
ColumnName: "expire_at",
|
|
|
|
ColumnUnit: &columnUnit,
|
|
|
|
Mode: options.TimeToLiveModeValueSinceUnixEpoch},
|
|
|
|
),
|
2022-05-02 07:42:20 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-02 10:33:29 +00:00
|
|
|
func withPragma(prefix string, query string) string {
|
2022-05-02 07:42:20 +00:00
|
|
|
return `PRAGMA TablePathPrefix("` + prefix + `");` + query
|
|
|
|
}
|