|
1 | 1 | package firewalldb |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | 4 | "crypto/rand" |
6 | 5 | "encoding/binary" |
7 | 6 | "encoding/hex" |
@@ -42,9 +41,12 @@ type NewPrivacyMapDB func(groupID session.ID) PrivacyMapDB |
42 | 41 | // PrivacyDB constructs a PrivacyMapDB that will be indexed under the given |
43 | 42 | // group ID key. |
44 | 43 | func (db *DB) PrivacyDB(groupID session.ID) PrivacyMapDB { |
45 | | - return &privacyMapDB{ |
46 | | - db: db, |
47 | | - groupID: groupID, |
| 44 | + return &kvdbExecutor[PrivacyMapTx]{ |
| 45 | + db: db.DB, |
| 46 | + wrapper: &privacyMapDB{ |
| 47 | + db: db, |
| 48 | + groupID: groupID, |
| 49 | + }, |
48 | 50 | } |
49 | 51 | } |
50 | 52 |
|
@@ -78,44 +80,14 @@ type privacyMapDB struct { |
78 | 80 | groupID session.ID |
79 | 81 | } |
80 | 82 |
|
81 | | -// Update opens a database read/write transaction and executes the function f |
82 | | -// with the transaction passed as a parameter. After f exits, if f did not |
83 | | -// error, the transaction is committed. Otherwise, if f did error, the |
84 | | -// transaction is rolled back. If the rollback fails, the original error |
85 | | -// returned by f is still returned. If the commit fails, the commit error is |
86 | | -// returned. |
| 83 | +// wrap returns a new PrivacyMapTx that wraps the given bolt transaction. |
87 | 84 | // |
88 | | -// NOTE: this is part of the PrivacyMapDB interface. |
89 | | -func (p *privacyMapDB) Update(ctx context.Context, fn func(ctx context.Context, |
90 | | - tx PrivacyMapTx) error) error { |
91 | | - |
92 | | - return p.db.Update(func(tx *bbolt.Tx) error { |
93 | | - boltTx := &privacyMapTx{ |
94 | | - privacyMapDB: p, |
95 | | - boltTx: tx, |
96 | | - } |
97 | | - |
98 | | - return fn(ctx, boltTx) |
99 | | - }) |
100 | | -} |
101 | | - |
102 | | -// View opens a database read transaction and executes the function f with the |
103 | | -// transaction passed as a parameter. After f exits, the transaction is rolled |
104 | | -// back. If f errors, its error is returned, not a rollback error (if any |
105 | | -// occur). |
106 | | -// |
107 | | -// NOTE: this is part of the PrivacyMapDB interface. |
108 | | -func (p *privacyMapDB) View(ctx context.Context, fn func(ctx context.Context, |
109 | | - tx PrivacyMapTx) error) error { |
110 | | - |
111 | | - return p.db.View(func(tx *bbolt.Tx) error { |
112 | | - boltTx := &privacyMapTx{ |
113 | | - privacyMapDB: p, |
114 | | - boltTx: tx, |
115 | | - } |
116 | | - |
117 | | - return fn(ctx, boltTx) |
118 | | - }) |
| 85 | +// NOTE: this is part of the txWrapper interface. |
| 86 | +func (p *privacyMapDB) wrap(tx *bbolt.Tx) PrivacyMapTx { |
| 87 | + return &privacyMapTx{ |
| 88 | + boltTx: tx, |
| 89 | + privacyMapDB: p, |
| 90 | + } |
119 | 91 | } |
120 | 92 |
|
121 | 93 | // privacyMapTx is an implementation of PrivacyMapTx. |
|
0 commit comments