@@ -70,9 +70,8 @@ type Database struct {
70
70
seekCompGauge metrics.Gauge // Gauge for tracking the number of table compaction caused by read opt
71
71
manualMemAllocGauge metrics.Gauge // Gauge for tracking amount of non-managed memory currently allocated
72
72
73
- quitLock sync.RWMutex // Mutex protecting the quit channel and the closed flag
73
+ quitLock sync.Mutex // Mutex protecting the quit channel access
74
74
quitChan chan chan error // Quit channel to stop the metrics collection before closing the database
75
- closed bool // keep track of whether we're Closed
76
75
77
76
log log.Logger // Contextual logger tracking the database path
78
77
@@ -222,29 +221,23 @@ func New(file string, cache int, handles int, namespace string, readonly bool) (
222
221
func (d * Database ) Close () error {
223
222
d .quitLock .Lock ()
224
223
defer d .quitLock .Unlock ()
224
+
225
225
// Allow double closing, simplifies things
226
- if d .closed {
226
+ if d .quitChan == nil {
227
227
return nil
228
228
}
229
- d .closed = true
230
- if d .quitChan != nil {
231
- errc := make (chan error )
232
- d .quitChan <- errc
233
- if err := <- errc ; err != nil {
234
- d .log .Error ("Metrics collection failed" , "err" , err )
235
- }
236
- d .quitChan = nil
229
+ errc := make (chan error )
230
+ d .quitChan <- errc
231
+ if err := <- errc ; err != nil {
232
+ d .log .Error ("Metrics collection failed" , "err" , err )
237
233
}
234
+ d .quitChan = nil
235
+
238
236
return d .db .Close ()
239
237
}
240
238
241
239
// Has retrieves if a key is present in the key-value store.
242
240
func (d * Database ) Has (key []byte ) (bool , error ) {
243
- d .quitLock .RLock ()
244
- defer d .quitLock .RUnlock ()
245
- if d .closed {
246
- return false , pebble .ErrClosed
247
- }
248
241
_ , closer , err := d .db .Get (key )
249
242
if err == pebble .ErrNotFound {
250
243
return false , nil
@@ -257,11 +250,6 @@ func (d *Database) Has(key []byte) (bool, error) {
257
250
258
251
// Get retrieves the given key if it's present in the key-value store.
259
252
func (d * Database ) Get (key []byte ) ([]byte , error ) {
260
- d .quitLock .RLock ()
261
- defer d .quitLock .RUnlock ()
262
- if d .closed {
263
- return nil , pebble .ErrClosed
264
- }
265
253
dat , closer , err := d .db .Get (key )
266
254
if err != nil {
267
255
return nil , err
@@ -274,30 +262,19 @@ func (d *Database) Get(key []byte) ([]byte, error) {
274
262
275
263
// Put inserts the given value into the key-value store.
276
264
func (d * Database ) Put (key []byte , value []byte ) error {
277
- d .quitLock .RLock ()
278
- defer d .quitLock .RUnlock ()
279
- if d .closed {
280
- return pebble .ErrClosed
281
- }
282
265
return d .db .Set (key , value , pebble .NoSync )
283
266
}
284
267
285
268
// Delete removes the key from the key-value store.
286
269
func (d * Database ) Delete (key []byte ) error {
287
- d .quitLock .RLock ()
288
- defer d .quitLock .RUnlock ()
289
- if d .closed {
290
- return pebble .ErrClosed
291
- }
292
270
return d .db .Delete (key , nil )
293
271
}
294
272
295
273
// NewBatch creates a write-only key-value store that buffers changes to its host
296
274
// database until a final write is called.
297
275
func (d * Database ) NewBatch () ethdb.Batch {
298
276
return & batch {
299
- b : d .db .NewBatch (),
300
- db : d ,
277
+ b : d .db .NewBatch (),
301
278
}
302
279
}
303
280
@@ -504,7 +481,6 @@ func (d *Database) meter(refresh time.Duration) {
504
481
// when Write is called. A batch cannot be used concurrently.
505
482
type batch struct {
506
483
b * pebble.Batch
507
- db * Database
508
484
size int
509
485
}
510
486
@@ -529,11 +505,6 @@ func (b *batch) ValueSize() int {
529
505
530
506
// Write flushes any accumulated data to disk.
531
507
func (b * batch ) Write () error {
532
- b .db .quitLock .RLock ()
533
- defer b .db .quitLock .RUnlock ()
534
- if b .db .closed {
535
- return pebble .ErrClosed
536
- }
537
508
return b .b .Commit (pebble .NoSync )
538
509
}
539
510
0 commit comments