From 4e0c932a4f4b5f6f0782a4fa2ac3b5c6a4792724 Mon Sep 17 00:00:00 2001 From: "Matt T. Proud" Date: Thu, 16 May 2013 00:54:18 +0300 Subject: [PATCH] Simplify Encoder's encoding signature. The reality is that if we ever try to encode a Protocol Buffer and it fails, it's likely that such an error is ultimately not a runtime error and should be fixed forthwith. Thusly, we should rename ``Encoder.Encode`` to ``Encoder.MustEncode`` and drop the error return value. --- coding/interface.go | 2 +- coding/protocol_buffer.go | 6 +++--- storage/metric/curator.go | 6 +----- storage/metric/frontier.go | 10 ++-------- storage/metric/stochastic_test.go | 5 +---- storage/metric/tiered.go | 2 +- storage/raw/leveldb/batch.go | 15 +++------------ storage/raw/leveldb/leveldb.go | 21 ++++----------------- 8 files changed, 16 insertions(+), 51 deletions(-) diff --git a/coding/interface.go b/coding/interface.go index 98441d68d7..fc3437624c 100644 --- a/coding/interface.go +++ b/coding/interface.go @@ -14,5 +14,5 @@ package coding type Encoder interface { - Encode() ([]byte, error) + MustEncode() []byte } diff --git a/coding/protocol_buffer.go b/coding/protocol_buffer.go index 19c8bef150..543344fee7 100644 --- a/coding/protocol_buffer.go +++ b/coding/protocol_buffer.go @@ -22,15 +22,15 @@ type ProtocolBuffer struct { message proto.Message } -func (p ProtocolBuffer) Encode() (raw []byte, err error) { - raw, err = proto.Marshal(p.message) +func (p ProtocolBuffer) MustEncode() []byte { + raw, err := proto.Marshal(p.message) // XXX: Adjust legacy users of this to not check for error. if err != nil { panic(err) } - return + return raw } func (p ProtocolBuffer) String() string { diff --git a/storage/metric/curator.go b/storage/metric/curator.go index a75289d1f2..e6ee7c704a 100644 --- a/storage/metric/curator.go +++ b/storage/metric/curator.go @@ -341,11 +341,7 @@ func (w watermarkOperator) Operate(key, _ interface{}) (oErr *storage.OperatorEr FirstTimestamp: seriesFrontier.optimalStartTime(curationState), } - prospectiveKey, err := coding.NewProtocolBuffer(startKey.ToDTO()).Encode() - if err != nil { - // An encoding failure of a key is no reason to stop. - return &storage.OperatorError{error: err, Continuable: true} - } + prospectiveKey := coding.NewProtocolBuffer(startKey.ToDTO()).MustEncode() if !w.sampleIterator.Seek(prospectiveKey) { // LevelDB is picky about the seek ranges. If an iterator was invalidated, // no work may occur, and the iterator cannot be recovered. diff --git a/storage/metric/frontier.go b/storage/metric/frontier.go index 7ea4278933..bdb5f87cdd 100644 --- a/storage/metric/frontier.go +++ b/storage/metric/frontier.go @@ -115,10 +115,7 @@ func newSeriesFrontier(f model.Fingerprint, d diskFrontier, i leveldb.Iterator) Timestamp: upperSeek, } - raw, err := coding.NewProtocolBuffer(key).Encode() - if err != nil { - panic(err) - } + raw := coding.NewProtocolBuffer(key).MustEncode() i.Seek(raw) if i.Key() == nil { @@ -160,10 +157,7 @@ func newSeriesFrontier(f model.Fingerprint, d diskFrontier, i leveldb.Iterator) key.Timestamp = lowerSeek - raw, err = coding.NewProtocolBuffer(key).Encode() - if err != nil { - panic(err) - } + raw = coding.NewProtocolBuffer(key).MustEncode() i.Seek(raw) diff --git a/storage/metric/stochastic_test.go b/storage/metric/stochastic_test.go index b3e8d0d54c..482294a5bc 100644 --- a/storage/metric/stochastic_test.go +++ b/storage/metric/stochastic_test.go @@ -194,10 +194,7 @@ func levelDBGetRangeValues(l *LevelDBMetricPersistence, fp model.Fingerprint, i Timestamp: indexable.EncodeTime(i.OldestInclusive), } - e, err := coding.NewProtocolBuffer(k).Encode() - if err != nil { - return - } + e := coding.NewProtocolBuffer(k).MustEncode() iterator := l.MetricSamples.NewIterator(true) defer iterator.Close() diff --git a/storage/metric/tiered.go b/storage/metric/tiered.go index e1920c02dd..dacfb13c3e 100644 --- a/storage/metric/tiered.go +++ b/storage/metric/tiered.go @@ -488,7 +488,7 @@ func (t *TieredStorage) loadChunkAroundTime(iterator leveldb.Iterator, frontier } // Try seeking to target key. - rawKey, _ := coding.NewProtocolBuffer(targetKey).Encode() + rawKey := coding.NewProtocolBuffer(targetKey).MustEncode() iterator.Seek(rawKey) foundKey, err := extractSampleKey(iterator) diff --git a/storage/raw/leveldb/batch.go b/storage/raw/leveldb/batch.go index acec9eaf6b..6b82af2f0f 100644 --- a/storage/raw/leveldb/batch.go +++ b/storage/raw/leveldb/batch.go @@ -32,24 +32,15 @@ func NewBatch() *batch { } func (b *batch) Drop(key coding.Encoder) { - keyEncoded, err := key.Encode() - if err != nil { - panic(err) - } + keyEncoded := key.MustEncode() b.drops++ b.batch.Delete(keyEncoded) } func (b *batch) Put(key, value coding.Encoder) { - keyEncoded, err := key.Encode() - if err != nil { - panic(err) - } - valueEncoded, err := value.Encode() - if err != nil { - panic(err) - } + keyEncoded := key.MustEncode() + valueEncoded := value.MustEncode() b.puts++ b.batch.Put(keyEncoded, valueEncoded) diff --git a/storage/raw/leveldb/leveldb.go b/storage/raw/leveldb/leveldb.go index bd649733ce..c37e90890b 100644 --- a/storage/raw/leveldb/leveldb.go +++ b/storage/raw/leveldb/leveldb.go @@ -251,10 +251,7 @@ func (l *LevelDBPersistence) Close() { } func (l *LevelDBPersistence) Get(value coding.Encoder) (b []byte, err error) { - key, err := value.Encode() - if err != nil { - return - } + key := value.MustEncode() return l.storage.Get(l.readOptions, key) } @@ -271,26 +268,16 @@ func (l *LevelDBPersistence) Has(value coding.Encoder) (h bool, err error) { } func (l *LevelDBPersistence) Drop(value coding.Encoder) (err error) { - key, err := value.Encode() - if err != nil { - return - } - + key := value.MustEncode() err = l.storage.Delete(l.writeOptions, key) return } func (l *LevelDBPersistence) Put(key, value coding.Encoder) (err error) { - keyEncoded, err := key.Encode() - if err != nil { - return - } + keyEncoded := key.MustEncode() - valueEncoded, err := value.Encode() - if err != nil { - return - } + valueEncoded := value.MustEncode() err = l.storage.Put(l.writeOptions, keyEncoded, valueEncoded)