Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change else-if statements to idiomatic switch statements. #1207

Merged
merged 4 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1214,11 +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)
if err == ErrKeyNotFound {
switch {
case err == ErrKeyNotFound:
seq.next = 0
} else if err != nil {
case err != nil:
return err
} else {
default:
var num uint64
if err := item.Value(func(v []byte) error {
num = binary.BigEndian.Uint64(v)
Expand Down
30 changes: 18 additions & 12 deletions levels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)}
}
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 16 additions & 13 deletions table/merge_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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.
Expand Down Expand Up @@ -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,
}
Expand Down
33 changes: 19 additions & 14 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,15 +436,18 @@ 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 {
switch {
case err == io.EOF:
break loop
case err == io.ErrUnexpectedEOF || err == errTruncate:
break loop
case err != nil:
return 0, err
} else if e == nil {
case e == nil:
continue
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down