Skip to content

Commit 62723bd

Browse files
committed
firewalldb: use DBExecutor for PrivacyMapDB
In this commit, we delete the PrivacyMapDB interface definition and instead use the generic DBExecutor interface parameterised by a PrivacyMapTx to define the PrivacyMapDB interface.
1 parent ef93611 commit 62723bd

File tree

1 file changed

+17
-71
lines changed

1 file changed

+17
-71
lines changed

firewalldb/privacy_mapper.go

Lines changed: 17 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,15 @@ type NewPrivacyMapDB func(groupID session.ID) PrivacyMapDB
4343
// group ID key.
4444
func (db *DB) PrivacyDB(groupID session.ID) PrivacyMapDB {
4545
return &privacyMapDB{
46-
DB: db,
46+
db: db,
4747
groupID: groupID,
4848
}
4949
}
5050

5151
// PrivacyMapDB provides an Update and View method that will allow the caller
5252
// to perform atomic read and write transactions defined by PrivacyMapTx on the
5353
// underlying DB.
54-
type PrivacyMapDB interface {
55-
// Update opens a database read/write transaction and executes the
56-
// function f with the transaction passed as a parameter. After f exits,
57-
// if f did not error, the transaction is committed. Otherwise, if f did
58-
// error, the transaction is rolled back. If the rollback fails, the
59-
// original error returned by f is still returned. If the commit fails,
60-
// the commit error is returned.
61-
Update(context.Context, func(context.Context, PrivacyMapTx) error) error
62-
63-
// View opens a database read transaction and executes the function f
64-
// with the transaction passed as a parameter. After f exits, the
65-
// transaction is rolled back. If f errors, its error is returned, not a
66-
// rollback error (if any occur).
67-
View(context.Context, func(context.Context, PrivacyMapTx) error) error
68-
}
54+
type PrivacyMapDB = DBExecutor[PrivacyMapTx]
6955

7056
// PrivacyMapTx represents a db that can be used to create, store and fetch
7157
// real-pseudo pairs.
@@ -88,23 +74,10 @@ type PrivacyMapTx interface {
8874

8975
// privacyMapDB is an implementation of PrivacyMapDB.
9076
type privacyMapDB struct {
91-
*DB
77+
db *DB
9278
groupID session.ID
9379
}
9480

95-
// beginTx starts db transaction. The transaction will be a read or read-write
96-
// transaction depending on the value of the `writable` parameter.
97-
func (p *privacyMapDB) beginTx(writable bool) (*privacyMapTx, error) {
98-
boltTx, err := p.Begin(writable)
99-
if err != nil {
100-
return nil, err
101-
}
102-
return &privacyMapTx{
103-
privacyMapDB: p,
104-
boltTx: boltTx,
105-
}, nil
106-
}
107-
10881
// Update opens a database read/write transaction and executes the function f
10982
// with the transaction passed as a parameter. After f exits, if f did not
11083
// error, the transaction is committed. Otherwise, if f did error, the
@@ -113,30 +86,17 @@ func (p *privacyMapDB) beginTx(writable bool) (*privacyMapTx, error) {
11386
// returned.
11487
//
11588
// NOTE: this is part of the PrivacyMapDB interface.
116-
func (p *privacyMapDB) Update(ctx context.Context, f func(ctx context.Context,
89+
func (p *privacyMapDB) Update(ctx context.Context, fn func(ctx context.Context,
11790
tx PrivacyMapTx) error) error {
11891

119-
tx, err := p.beginTx(true)
120-
if err != nil {
121-
return err
122-
}
123-
124-
// Make sure the transaction rolls back in the event of a panic.
125-
defer func() {
126-
if tx != nil {
127-
_ = tx.boltTx.Rollback()
92+
return p.db.Update(func(tx *bbolt.Tx) error {
93+
boltTx := &privacyMapTx{
94+
privacyMapDB: p,
95+
boltTx: tx,
12896
}
129-
}()
130-
131-
err = f(ctx, tx)
132-
if err != nil {
133-
// Want to return the original error, not a rollback error if
134-
// any occur.
135-
_ = tx.boltTx.Rollback()
136-
return err
137-
}
13897

139-
return tx.boltTx.Commit()
98+
return fn(ctx, boltTx)
99+
})
140100
}
141101

142102
// View opens a database read transaction and executes the function f with the
@@ -145,31 +105,17 @@ func (p *privacyMapDB) Update(ctx context.Context, f func(ctx context.Context,
145105
// occur).
146106
//
147107
// NOTE: this is part of the PrivacyMapDB interface.
148-
func (p *privacyMapDB) View(ctx context.Context, f func(ctx context.Context,
108+
func (p *privacyMapDB) View(ctx context.Context, fn func(ctx context.Context,
149109
tx PrivacyMapTx) error) error {
150110

151-
tx, err := p.beginTx(false)
152-
if err != nil {
153-
return err
154-
}
155-
156-
// Make sure the transaction rolls back in the event of a panic.
157-
defer func() {
158-
if tx != nil {
159-
_ = tx.boltTx.Rollback()
111+
return p.db.View(func(tx *bbolt.Tx) error {
112+
boltTx := &privacyMapTx{
113+
privacyMapDB: p,
114+
boltTx: tx,
160115
}
161-
}()
162116

163-
err = f(ctx, tx)
164-
rollbackErr := tx.boltTx.Rollback()
165-
if err != nil {
166-
return err
167-
}
168-
169-
if rollbackErr != nil {
170-
return rollbackErr
171-
}
172-
return nil
117+
return fn(ctx, boltTx)
118+
})
173119
}
174120

175121
// privacyMapTx is an implementation of PrivacyMapTx.

0 commit comments

Comments
 (0)