Skip to content

Commit c6641c6

Browse files
committed
[extension/dbstorage] Add DB Transactions to dbstorage.Batch() method
1 parent 1f8c1ee commit c6641c6

File tree

8 files changed

+388
-27
lines changed

8 files changed

+388
-27
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: dbstorageextension
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add DB Transactions to dbstorage.Batch() method as it is expected by Storage API
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [37805]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [api]

extension/storage/dbstorage/client.go

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type dbStorageClient struct {
3333

3434
func newClient(ctx context.Context, driverName string, db *sql.DB, tableName string) (*dbStorageClient, error) {
3535
createTableSQL := createTable
36-
if driverName == "sqlite" {
36+
if driverName == driverSqlite {
3737
createTableSQL = createTableSqlite
3838
}
3939
var err error
@@ -59,45 +59,37 @@ func newClient(ctx context.Context, driverName string, db *sql.DB, tableName str
5959

6060
// Get will retrieve data from storage that corresponds to the specified key
6161
func (c *dbStorageClient) Get(ctx context.Context, key string) ([]byte, error) {
62-
rows, err := c.getQuery.QueryContext(ctx, key)
63-
if err != nil {
64-
return nil, err
65-
}
66-
if !rows.Next() {
67-
return nil, nil
68-
}
69-
var result []byte
70-
err = rows.Scan(&result)
71-
if err != nil {
72-
return result, err
73-
}
74-
err = rows.Close()
75-
return result, err
62+
return c.get(ctx, key, nil)
7663
}
7764

7865
// Set will store data. The data can be retrieved using the same key
7966
func (c *dbStorageClient) Set(ctx context.Context, key string, value []byte) error {
80-
_, err := c.setQuery.ExecContext(ctx, key, value, value)
81-
return err
67+
return c.set(ctx, key, value, nil)
8268
}
8369

8470
// Delete will delete data associated with the specified key
8571
func (c *dbStorageClient) Delete(ctx context.Context, key string) error {
86-
_, err := c.deleteQuery.ExecContext(ctx, key)
87-
return err
72+
return c.delete(ctx, key, nil)
8873
}
8974

9075
// Batch executes the specified operations in order. Get operation results are updated in place
9176
func (c *dbStorageClient) Batch(ctx context.Context, ops ...*storage.Operation) error {
92-
var err error
77+
// Start a new transaction
78+
tx, err := c.db.BeginTx(ctx, nil)
79+
if err != nil {
80+
return err
81+
}
82+
//nolint:errcheck
83+
defer tx.Rollback()
84+
9385
for _, op := range ops {
9486
switch op.Type {
9587
case storage.Get:
96-
op.Value, err = c.Get(ctx, op.Key)
88+
op.Value, err = c.get(ctx, op.Key, tx)
9789
case storage.Set:
98-
err = c.Set(ctx, op.Key, op.Value)
90+
err = c.set(ctx, op.Key, op.Value, tx)
9991
case storage.Delete:
100-
err = c.Delete(ctx, op.Key)
92+
err = c.delete(ctx, op.Key, tx)
10193
default:
10294
return errors.New("wrong operation type")
10395
}
@@ -106,7 +98,8 @@ func (c *dbStorageClient) Batch(ctx context.Context, ops ...*storage.Operation)
10698
return err
10799
}
108100
}
109-
return err
101+
102+
return tx.Commit()
110103
}
111104

112105
// Close will close the database
@@ -119,3 +112,39 @@ func (c *dbStorageClient) Close(_ context.Context) error {
119112
}
120113
return c.getQuery.Close()
121114
}
115+
116+
func (c *dbStorageClient) get(ctx context.Context, key string, tx *sql.Tx) ([]byte, error) {
117+
rows, err := c.wrapTx(c.getQuery, tx).QueryContext(ctx, key)
118+
if err != nil {
119+
return nil, err
120+
}
121+
122+
if !rows.Next() {
123+
return nil, nil
124+
}
125+
126+
var result []byte
127+
if err := rows.Scan(&result); err != nil {
128+
return result, err
129+
}
130+
131+
return result, rows.Close()
132+
}
133+
134+
func (c *dbStorageClient) set(ctx context.Context, key string, value []byte, tx *sql.Tx) error {
135+
_, err := c.wrapTx(c.setQuery, tx).ExecContext(ctx, key, value, value)
136+
return err
137+
}
138+
139+
func (c *dbStorageClient) delete(ctx context.Context, key string, tx *sql.Tx) error {
140+
_, err := c.wrapTx(c.deleteQuery, tx).ExecContext(ctx, key)
141+
return err
142+
}
143+
144+
func (c *dbStorageClient) wrapTx(stmt *sql.Stmt, tx *sql.Tx) *sql.Stmt {
145+
if tx != nil {
146+
return tx.Stmt(stmt)
147+
}
148+
149+
return stmt
150+
}

0 commit comments

Comments
 (0)