move some locks, nbd

This commit is contained in:
Matthew Jaffee 2021-12-17 14:50:08 -06:00
parent 1fd872b126
commit 0799862266
2 changed files with 7 additions and 3 deletions

View file

@ -380,6 +380,9 @@ func (db *DB) checkpoint() (err error) {
db.afterCurrentTx(func() {
defer db.rwmu.Unlock()
db.baseWALID = readMetaWALID(db.data)
db.mu.Unlock()
defer db.mu.Lock()
if err = db.walFile.Truncate(0); err != nil {
db.logger.Errorf("truncate wal file: %w", err)
@ -389,7 +392,6 @@ func (db *DB) checkpoint() (err error) {
db.logger.Errorf("seek wal file: %w", err)
}
db.baseWALID = readMetaWALID(db.data)
})
return nil

View file

@ -102,8 +102,6 @@ func (tx *Tx) Commit() error {
// If any pages have been written, ensure we write a new meta page with
// the commit flag to mark the end of the transaction.
tx.db.mu.Lock()
defer tx.db.mu.Unlock()
if tx.dirty() {
if err := tx.flush(); err != nil {
return err
@ -120,11 +118,15 @@ func (tx *Tx) Commit() error {
// the lock, because we need removeTx to grab the lock to
// work, but if it wants to checkpoint, it wants to be able to return
// to us here and still be holding the lock.
tx.db.mu.Lock()
tx.db.rootRecords = tx.rootRecords
tx.db.pageMap = tx.pageMap
tx.db.walPageN = tx.walPageN
tx.db.mu.Unlock()
}
tx.db.mu.Lock()
defer tx.db.mu.Unlock()
// Disconnect transaction from DB.
return tx.db.removeTx(tx)
}