Skip to content

Commit

Permalink
use defer
Browse files Browse the repository at this point in the history
  • Loading branch information
akiozihao committed Dec 18, 2023
1 parent b0636fd commit 55ad47a
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ func (db *DB) waitMemtableSpace() error {
// 5. Delete the wal.
func (db *DB) flushMemtable(table *memtable) {
db.flushLock.Lock()
defer db.flushLock.Unlock()
sklIter := table.skl.NewIterator()
var deletedKeys [][]byte
var logRecords []*ValueLogRecord
Expand All @@ -357,14 +358,12 @@ func (db *DB) flushMemtable(table *memtable) {
keyPos, err := db.vlog.writeBatch(logRecords)
if err != nil {
log.Println("vlog writeBatch failed:", err)
db.flushLock.Unlock()
return
}

// sync the value log
if err := db.vlog.sync(); err != nil {
log.Println("vlog sync failed:", err)
db.flushLock.Unlock()
return
}

Expand All @@ -378,7 +377,6 @@ func (db *DB) flushMemtable(table *memtable) {
}
if err := db.index.PutBatch(keyPos, putMatchKeys...); err != nil {
log.Println("index PutBatch failed:", err)
db.flushLock.Unlock()
return
}
// delete the deleted keys from index
Expand All @@ -391,20 +389,17 @@ func (db *DB) flushMemtable(table *memtable) {
}
if err := db.index.DeleteBatch(deletedKeys, deleteMatchKeys...); err != nil {
log.Println("index DeleteBatch failed:", err)
db.flushLock.Unlock()
return
}
// sync the index
if err := db.index.Sync(); err != nil {
log.Println("index sync failed:", err)
db.flushLock.Unlock()
return
}

// delete the wal
if err := table.deleteWAl(); err != nil {
log.Println("delete wal failed:", err)
db.flushLock.Unlock()
return
}

Expand All @@ -428,8 +423,6 @@ func (db *DB) flushMemtable(table *memtable) {
}

db.mu.Unlock()

db.flushLock.Unlock()
}

func (db *DB) listenMemtableFlush() {
Expand Down Expand Up @@ -482,7 +475,7 @@ func (db *DB) Compact() error {

validRecords := make([]*ValueLogRecord, 0, db.vlog.options.compactBatchCount)
reader := db.vlog.walFiles[part].NewReader()
var count = 0
count := 0
// iterate all records in wal, find the valid records
for {
count++
Expand Down

0 comments on commit 55ad47a

Please sign in to comment.