Skip to content

Commit 5750451

Browse files
committed
firewalldb: introduce DBExecutor and use it for KVStores
Here we introduce a generic DBExecutor interface with methods that take a generic T transaction. This is in preparation for the introducing a SQL transaction instead of a bolt one.
1 parent 9316a59 commit 5750451

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

firewalldb/interface.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,24 @@ type SessionDB interface {
1414
// GetSession returns the session for a specific id.
1515
GetSession(context.Context, session.ID) (*session.Session, error)
1616
}
17+
18+
// DBExecutor provides an Update and View method that will allow the caller
19+
// to perform atomic read and write transactions defined by PrivacyMapTx on the
20+
// underlying BoltDB.
21+
type DBExecutor[T any] interface {
22+
// Update opens a database read/write transaction and executes the
23+
// function f with the transaction passed as a parameter. After f exits,
24+
// if f did not error, the transaction is committed. Otherwise, if f did
25+
// error, the transaction is rolled back. If the rollback fails, the
26+
// original error returned by f is still returned. If the commit fails,
27+
// the commit error is returned.
28+
Update(ctx context.Context, f func(ctx context.Context,
29+
tx T) error) error
30+
31+
// View opens a database read transaction and executes the function f
32+
// with the transaction passed as a parameter. After f exits, the
33+
// transaction is rolled back. If f errors, its error is returned, not a
34+
// rollback error (if any occur).
35+
View(ctx context.Context, f func(ctx context.Context,
36+
tx T) error) error
37+
}

firewalldb/kvstores.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,7 @@ var (
5454
// KVStores provides an Update and View method that will allow the caller to
5555
// perform atomic read and write transactions on and of the key value stores
5656
// offered the KVStoreTx.
57-
type KVStores interface {
58-
// Update opens a database read/write transaction and executes the
59-
// function f with the transaction passed as a parameter. After f exits,
60-
// if f did not error, the transaction is committed. Otherwise, if f did
61-
// error, the transaction is rolled back. If the rollback fails, the
62-
// original error returned by f is still returned. If the commit fails,
63-
// the commit error is returned.
64-
Update(ctx context.Context, f func(ctx context.Context,
65-
tx KVStoreTx) error) error
66-
67-
// View opens a database read transaction and executes the function f
68-
// with the transaction passed as a parameter. After f exits, the
69-
// transaction is rolled back. If f errors, its error is returned, not a
70-
// rollback error (if any occur).
71-
View(ctx context.Context, f func(ctx context.Context,
72-
tx KVStoreTx) error) error
73-
}
57+
type KVStores = DBExecutor[KVStoreTx]
7458

7559
// KVStoreTx represents a database transaction that can be used for both read
7660
// and writes of the various different key value stores offered for the rule.

0 commit comments

Comments
 (0)