From dc3aa70fb6150e2bf677acdb348355e1d08b6408 Mon Sep 17 00:00:00 2001 From: Martin Martinez Rivera Date: Thu, 30 Jan 2020 15:02:25 -0800 Subject: [PATCH 1/4] Change else-if statements to idiomatic switch statements. --- db.go | 21 ++++++++++++--------- levels.go | 30 ++++++++++++++++++------------ table/merge_iterator.go | 29 ++++++++++++++++------------- value.go | 35 ++++++++++++++++++++--------------- 4 files changed, 66 insertions(+), 49 deletions(-) diff --git a/db.go b/db.go index 03e9e05b4..d9e96cb75 100644 --- a/db.go +++ b/db.go @@ -146,7 +146,8 @@ func (db *DB) replayFunction() func(Entry, valuePointer) error { ExpiresAt: e.ExpiresAt, } - if e.meta&bitFinTxn > 0 { + switch { + case e.meta&bitFinTxn > 0: txnTs, err := strconv.ParseUint(string(e.Value), 10, 64) if err != nil { return errors.Wrapf(err, "Unable to parse txn fin: %q", e.Value) @@ -160,7 +161,7 @@ func (db *DB) replayFunction() func(Entry, valuePointer) error { txn = txn[:0] lastCommit = 0 - } else if e.meta&bitTxn > 0 { + case e.meta&bitTxn > 0: txnTs := y.ParseTs(nk) if lastCommit == 0 { lastCommit = txnTs @@ -174,7 +175,7 @@ func (db *DB) replayFunction() func(Entry, valuePointer) error { te := txnEntry{nk: nk, v: v} txn = append(txn, te) - } else { + default: // This entry is from a rewrite. toLSM(nk, v) @@ -1055,9 +1056,10 @@ func (db *DB) calculateSize() { return err } ext := filepath.Ext(path) - if ext == ".sst" { + switch ext { + case ".sst": lsmSize += info.Size() - } else if ext == ".vlog" { + case ".vlog": vlogSize += info.Size() } return nil @@ -1214,11 +1216,10 @@ func (seq *Sequence) Release() error { func (seq *Sequence) updateLease() error { return seq.db.Update(func(txn *Txn) error { item, err := txn.Get(seq.key) - if err == ErrKeyNotFound { + switch err { + case ErrKeyNotFound: seq.next = 0 - } else if err != nil { - return err - } else { + case nil: var num uint64 if err := item.Value(func(v []byte) error { num = binary.BigEndian.Uint64(v) @@ -1227,6 +1228,8 @@ func (seq *Sequence) updateLease() error { return err } seq.next = num + default: + return err } lease := seq.next + seq.bandwidth diff --git a/levels.go b/levels.go index ece4a5900..41faf6e0b 100644 --- a/levels.go +++ b/levels.go @@ -84,12 +84,13 @@ func newLevelsController(db *DB, mf *Manifest) (*levelsController, error) { for i := 0; i < db.opt.MaxLevels; i++ { s.levels[i] = newLevelHandler(db, i) - if i == 0 { + switch i { + case 0: // Do nothing. - } else if i == 1 { + case 1: // Level 1 probably shouldn't be too much bigger than level 0. s.levels[i].maxTotalSize = db.opt.LevelOneSize - } else { + default: s.levels[i].maxTotalSize = s.levels[i-1].maxTotalSize * int64(db.opt.LevelSizeMultiplier) } s.cstatus.levels[i] = new(levelCompactStatus) @@ -360,12 +361,15 @@ func (s *levelsController) runWorker(lc *y.Closer) { // Can add a done channel or other stuff. case <-ticker.C: prios := s.pickCompactLevels() + loop: for _, p := range prios { - if err := s.doCompact(p); err == nil { - break - } else if err == errFillTables { + err := s.doCompact(p) + switch err { + case nil: + break loop + case errFillTables: // pass - } else { + default: s.kv.opt.Warningf("While running doCompact: %v\n", err) } } @@ -475,9 +479,10 @@ func (s *levelsController) compactBuildTables( // Create iterators across all the tables involved first. var iters []y.Iterator - if lev == 0 { + switch { + case lev == 0: iters = appendIteratorsReversed(iters, topTables, false) - } else if len(topTables) > 0 { + case len(topTables) > 0: y.AssertTrue(len(topTables) == 1) iters = []y.Iterator{topTables[0].NewIterator(false)} } @@ -579,14 +584,15 @@ func (s *levelsController) compactBuildTables( // versions. Ensure that we're only removing versions below readTs. skipKey = y.SafeCopy(skipKey, it.Key()) - if lastValidVersion { + switch { + case lastValidVersion: // Add this key. We have set skipKey, so the following key versions // would be skipped. - } else if hasOverlap { + case hasOverlap: // If this key range has overlap with lower levels, then keep the deletion // marker with the latest version, discarding the rest. We have set skipKey, // so the following key versions would be skipped. - } else { + default: // If no overlap, we can skip all the versions, by continuing here. numSkips++ updateStats(vs) diff --git a/table/merge_iterator.go b/table/merge_iterator.go index e93edbbb9..e1809e027 100644 --- a/table/merge_iterator.go +++ b/table/merge_iterator.go @@ -55,17 +55,18 @@ func (n *node) setIterator(iter y.Iterator) { } func (n *node) setKey() { - if n.merge != nil { + switch { + case n.merge != nil: n.valid = n.merge.small.valid if n.valid { n.key = n.merge.small.key } - } else if n.concat != nil { + case n.concat != nil: n.valid = n.concat.Valid() if n.valid { n.key = n.concat.Key() } - } else { + default: n.valid = n.iter.Valid() if n.valid { n.key = n.iter.Key() @@ -74,11 +75,12 @@ func (n *node) setKey() { } func (n *node) next() { - if n.merge != nil { + switch { + case n.merge != nil: n.merge.Next() - } else if n.concat != nil { + case n.concat != nil: n.concat.Next() - } else { + default: n.iter.Next() } n.setKey() @@ -103,22 +105,22 @@ func (mi *MergeIterator) fix() { return } cmp := y.CompareKeys(mi.small.key, mi.bigger().key) - // Both the keys are equal. - if cmp == 0 { + switch { + case cmp == 0: // Both the keys are equal. // In case of same keys, move the right iterator ahead. mi.right.next() if &mi.right == mi.small { mi.swapSmall() } return - } else if cmp < 0 { // Small is less than bigger(). + case cmp < 0: // Small is less than bigger(). if mi.reverse { mi.swapSmall() } else { // we don't need to do anything. Small already points to the smallest. } return - } else { // bigger() is less than small. + default: // bigger() is less than small. if mi.reverse { // Do nothing since we're iterating in reverse. Small currently points to // the bigger key and that's okay in reverse iteration. @@ -206,11 +208,12 @@ func (mi *MergeIterator) Close() error { // NewMergeIterator creates a merge iterator. func NewMergeIterator(iters []y.Iterator, reverse bool) y.Iterator { - if len(iters) == 0 { + switch len(iters) { + case 0: return nil - } else if len(iters) == 1 { + case 1: return iters[0] - } else if len(iters) == 2 { + case 2: mi := &MergeIterator{ reverse: reverse, } diff --git a/value.go b/value.go index 1f1c9434e..45de9ba4a 100644 --- a/value.go +++ b/value.go @@ -436,16 +436,19 @@ func (vlog *valueLog) iterate(lf *logFile, offset uint32, fn logEntry) (uint32, var lastCommit uint64 var validEndOffset uint32 = offset + +loop: for { e, err := read.Entry(reader) - if err == io.EOF { - break - } else if err == io.ErrUnexpectedEOF || err == errTruncate { - break - } else if err != nil { - return 0, err - } else if e == nil { + switch err { + case io.EOF: + break loop + case io.ErrUnexpectedEOF, errTruncate: + break loop + case nil: continue + default: + return 0, err } var vp valuePointer @@ -455,29 +458,30 @@ func (vlog *valueLog) iterate(lf *logFile, offset uint32, fn logEntry) (uint32, vp.Offset = e.offset vp.Fid = lf.fid - if e.meta&bitTxn > 0 { + switch { + case e.meta&bitTxn > 0: txnTs := y.ParseTs(e.Key) if lastCommit == 0 { lastCommit = txnTs } if lastCommit != txnTs { - break + break loop } - } else if e.meta&bitFinTxn > 0 { + case e.meta&bitFinTxn > 0: txnTs, err := strconv.ParseUint(string(e.Value), 10, 64) if err != nil || lastCommit != txnTs { - break + break loop } // Got the end of txn. Now we can store them. lastCommit = 0 validEndOffset = read.recordOffset - } else { + default: if lastCommit != 0 { // This is most likely an entry which was moved as part of GC. // We shouldn't get this entry in the middle of a transaction. - break + break loop } validEndOffset = read.recordOffset } @@ -1537,10 +1541,11 @@ func (vlog *valueLog) pickLog(head valuePointer, tr trace.Trace) (files []*logFi vlog.filesLock.RLock() defer vlog.filesLock.RUnlock() fids := vlog.sortedFids() - if len(fids) <= 1 { + switch { + case len(fids) <= 1: tr.LazyPrintf("Only one or less value log file.") return nil - } else if head.Fid == 0 { + case head.Fid == 0: tr.LazyPrintf("Head pointer is at zero.") return nil } From 94694f814bd16627835be72cd2c97f911dddb353 Mon Sep 17 00:00:00 2001 From: Martin Martinez Rivera Date: Thu, 30 Jan 2020 15:25:45 -0800 Subject: [PATCH 2/4] Don't reverse order of the statements. --- db.go | 10 +++++----- test.sh | 6 +++--- value.go | 12 ++++++------ 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/db.go b/db.go index d9e96cb75..a40aa8d92 100644 --- a/db.go +++ b/db.go @@ -1216,10 +1216,12 @@ func (seq *Sequence) Release() error { func (seq *Sequence) updateLease() error { return seq.db.Update(func(txn *Txn) error { item, err := txn.Get(seq.key) - switch err { - case ErrKeyNotFound: + switch { + case err == ErrKeyNotFound: seq.next = 0 - case nil: + case err != nil: + return err + default: var num uint64 if err := item.Value(func(v []byte) error { num = binary.BigEndian.Uint64(v) @@ -1228,8 +1230,6 @@ func (seq *Sequence) updateLease() error { return err } seq.next = num - default: - return err } lease := seq.next + seq.bandwidth diff --git a/test.sh b/test.sh index b4e40601a..b986edba9 100755 --- a/test.sh +++ b/test.sh @@ -15,9 +15,9 @@ pushd badger go build -v . popd -# Run the memory intensive tests first. -go test -v -run='TestBigKeyValuePairs$' --manual=true -go test -v -run='TestPushValueLogLimit' --manual=true +# # Run the memory intensive tests first. +# go test -v -run='TestBigKeyValuePairs$' --manual=true +# go test -v -run='TestPushValueLogLimit' --manual=true # Run the special Truncate test. rm -rf p diff --git a/value.go b/value.go index 45de9ba4a..094615d8a 100644 --- a/value.go +++ b/value.go @@ -440,15 +440,15 @@ func (vlog *valueLog) iterate(lf *logFile, offset uint32, fn logEntry) (uint32, loop: for { e, err := read.Entry(reader) - switch err { - case io.EOF: + switch { + case err == io.EOF: break loop - case io.ErrUnexpectedEOF, errTruncate: + case err == io.ErrUnexpectedEOF || err == errTruncate: break loop - case nil: - continue - default: + case err != nil: return 0, err + default: + continue } var vp valuePointer From e1c43b73702cf44a16bae65ec8097a2eda537819 Mon Sep 17 00:00:00 2001 From: Martin Martinez Rivera Date: Thu, 30 Jan 2020 15:32:42 -0800 Subject: [PATCH 3/4] Fix bug. --- value.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/value.go b/value.go index 094615d8a..b0d94ad86 100644 --- a/value.go +++ b/value.go @@ -447,7 +447,7 @@ loop: break loop case err != nil: return 0, err - default: + case e == nil: continue } From 8c85ee367dc54f18a74d88f37efa623096e8cc3e Mon Sep 17 00:00:00 2001 From: Martin Martinez Rivera Date: Thu, 30 Jan 2020 15:33:24 -0800 Subject: [PATCH 4/4] Revert changes to test.sh --- test.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test.sh b/test.sh index b986edba9..b4e40601a 100755 --- a/test.sh +++ b/test.sh @@ -15,9 +15,9 @@ pushd badger go build -v . popd -# # Run the memory intensive tests first. -# go test -v -run='TestBigKeyValuePairs$' --manual=true -# go test -v -run='TestPushValueLogLimit' --manual=true +# Run the memory intensive tests first. +go test -v -run='TestBigKeyValuePairs$' --manual=true +go test -v -run='TestPushValueLogLimit' --manual=true # Run the special Truncate test. rm -rf p