@@ -43,29 +43,15 @@ type NewPrivacyMapDB func(groupID session.ID) PrivacyMapDB
43
43
// group ID key.
44
44
func (db * DB ) PrivacyDB (groupID session.ID ) PrivacyMapDB {
45
45
return & privacyMapDB {
46
- DB : db ,
46
+ db : db ,
47
47
groupID : groupID ,
48
48
}
49
49
}
50
50
51
51
// PrivacyMapDB provides an Update and View method that will allow the caller
52
52
// to perform atomic read and write transactions defined by PrivacyMapTx on the
53
53
// 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 ]
69
55
70
56
// PrivacyMapTx represents a db that can be used to create, store and fetch
71
57
// real-pseudo pairs.
@@ -88,23 +74,10 @@ type PrivacyMapTx interface {
88
74
89
75
// privacyMapDB is an implementation of PrivacyMapDB.
90
76
type privacyMapDB struct {
91
- * DB
77
+ db * DB
92
78
groupID session.ID
93
79
}
94
80
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
-
108
81
// Update opens a database read/write transaction and executes the function f
109
82
// with the transaction passed as a parameter. After f exits, if f did not
110
83
// error, the transaction is committed. Otherwise, if f did error, the
@@ -113,30 +86,17 @@ func (p *privacyMapDB) beginTx(writable bool) (*privacyMapTx, error) {
113
86
// returned.
114
87
//
115
88
// 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 ,
117
90
tx PrivacyMapTx ) error ) error {
118
91
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 ,
128
96
}
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
- }
138
97
139
- return tx .boltTx .Commit ()
98
+ return fn (ctx , boltTx )
99
+ })
140
100
}
141
101
142
102
// 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,
145
105
// occur).
146
106
//
147
107
// 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 ,
149
109
tx PrivacyMapTx ) error ) error {
150
110
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 ,
160
115
}
161
- }()
162
116
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
+ })
173
119
}
174
120
175
121
// privacyMapTx is an implementation of PrivacyMapTx.
0 commit comments