From 3f42cb5e4af4037e2007e135219bb873ba8f5167 Mon Sep 17 00:00:00 2001 From: Xavi Ramirez Date: Tue, 18 Jul 2017 19:52:26 +0000 Subject: [PATCH] Created IsLessThan method to SequencePair to make code more readable --- batchconsumer/batcher/message_batcher.go | 25 +++++++++++++++++++----- batchconsumer/writer.go | 5 +---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/batchconsumer/batcher/message_batcher.go b/batchconsumer/batcher/message_batcher.go index 244dbc3..b57a6e5 100644 --- a/batchconsumer/batcher/message_batcher.go +++ b/batchconsumer/batcher/message_batcher.go @@ -13,6 +13,25 @@ type SequencePair struct { SubSequence int } +func (s SequencePair) IsLessThan(pair SequencePair) bool { + if s.Sequence == nil { + return false + } + if pair.Sequence == nil { + return true + } + + cmp := s.Sequence.Cmp(pair.Sequence) + if cmp == -1 { + return true + } + if cmp == 1 { + return false + } + + return s.SubSequence < pair.SubSequence +} + // Sync is used to allow a writer to syncronize with the batcher. // The writer declares how to write messages (via its `SendBatch` method), while the batcher // keeps track of messages written @@ -110,11 +129,7 @@ func (b *batcher) updateSequenceNumbers(pair SequencePair) { b.mux.Lock() defer b.mux.Unlock() - isSmaller := b.smallestSeq.Sequence == nil || - pair.Sequence.Cmp(b.smallestSeq.Sequence) == -1 || - (pair.Sequence.Cmp(b.smallestSeq.Sequence) == 0 && - pair.SubSequence < b.smallestSeq.SubSequence) - if isSmaller { + if pair.IsLessThan(b.smallestSeq) { b.smallestSeq = SequencePair{pair.Sequence, pair.SubSequence} } } diff --git a/batchconsumer/writer.go b/batchconsumer/writer.go index 95bb7da..a703880 100644 --- a/batchconsumer/writer.go +++ b/batchconsumer/writer.go @@ -207,10 +207,7 @@ func (b *batchedWriter) CheckPointBatch(tag string) { continue } - isSmaller := smallest.Sequence == nil || // smallest.Sequence means batch just flushed - pair.Sequence.Cmp(smallest.Sequence) == -1 || - (pair.Sequence.Cmp(smallest.Sequence) == 0 && pair.SubSequence < smallest.SubSequence) - if isSmaller { + if pair.IsLessThan(smallest) { smallest = pair } }