unified-storage: add Delete support to the sqlkv implementation (#115443)

This commit is contained in:
Renato Costa
2025-12-16 16:19:45 -05:00
committed by GitHub
parent f2c30cbbd1
commit c647140475
5 changed files with 87 additions and 33 deletions
+54 -14
View File
@@ -33,9 +33,13 @@ func mustTemplate(filename string) *template.Template {
// Templates.
var (
sqlKVGet = mustTemplate("sqlkv_get.sql")
sqlKVGet = mustTemplate("sqlkv_get.sql")
sqlKVDelete = mustTemplate("sqlkv_delete.sql")
)
// sqlKVSection can be embedded in structs used when rendering query templates
// for queries that reference a particular section. The section will be validated,
// and the template can directly reference the `TableName`.
type sqlKVSection struct {
Section string
}
@@ -60,18 +64,15 @@ func (req sqlKVSection) TableName() string {
return "resource_events"
}
type sqlKVGetRequest struct {
sqltemplate.SQLTemplate
// sqlKVSectionKey can be embedded in structs used when rendering query templates
// for queries that reference both a section and a particular key. The `key` is
// validated, and the template can reference the corresponding `KeyPath`.
type sqlKVSectionKey struct {
sqlKVSection
Key string
*sqlKVGetResponse
}
type sqlKVGetResponse struct {
Value []byte
}
func (req sqlKVGetRequest) Validate() error {
func (req sqlKVSectionKey) Validate() error {
if err := req.sqlKVSection.Validate(); err != nil {
return err
}
@@ -82,12 +83,35 @@ func (req sqlKVGetRequest) Validate() error {
return nil
}
func (req sqlKVSectionKey) KeyPath() string {
return req.Section + "/" + req.Key
}
type sqlKVGetRequest struct {
sqltemplate.SQLTemplate
sqlKVSectionKey
*sqlKVGetResponse
}
type sqlKVGetResponse struct {
Value []byte
}
func (req sqlKVGetRequest) Validate() error {
return req.sqlKVSectionKey.Validate()
}
func (req sqlKVGetRequest) Results() ([]byte, error) {
return req.Value, nil
}
func (req sqlKVGetRequest) KeyPath() string {
return req.Section + "/" + req.Key
type sqlKVDeleteRequest struct {
sqltemplate.SQLTemplate
sqlKVSectionKey
}
func (req sqlKVDeleteRequest) Validate() error {
return req.sqlKVSectionKey.Validate()
}
var _ KV = &sqlKV{}
@@ -134,8 +158,7 @@ func (k *sqlKV) Keys(ctx context.Context, section string, opt ListOptions) iter.
func (k *sqlKV) Get(ctx context.Context, section string, key string) (io.ReadCloser, error) {
value, err := dbutil.QueryRow(ctx, k.db, sqlKVGet, sqlKVGetRequest{
SQLTemplate: sqltemplate.New(k.dialect),
sqlKVSection: sqlKVSection{section},
Key: key,
sqlKVSectionKey: sqlKVSectionKey{sqlKVSection{section}, key},
sqlKVGetResponse: new(sqlKVGetResponse),
})
if err != nil {
@@ -171,7 +194,24 @@ func (k *sqlKV) Save(ctx context.Context, section string, key string) (io.WriteC
}
func (k *sqlKV) Delete(ctx context.Context, section string, key string) error {
panic("not implemented!")
res, err := dbutil.Exec(ctx, k.db, sqlKVDelete, sqlKVDeleteRequest{
SQLTemplate: sqltemplate.New(k.dialect),
sqlKVSectionKey: sqlKVSectionKey{sqlKVSection{section}, key},
})
if err != nil {
return fmt.Errorf("failed to delete key: %w", err)
}
rows, err := res.RowsAffected()
if err != nil {
return fmt.Errorf("failed to validate delete: %w", err)
}
if rows == 0 {
return ErrNotFound
}
return nil
}
func (k *sqlKV) BatchDelete(ctx context.Context, section string, keys []string) error {