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.
This commit is contained in:
Matt T. Proud 2013-05-16 00:54:18 +03:00
parent 516101f015
commit 4e0c932a4f
8 changed files with 16 additions and 51 deletions

View file

@ -14,5 +14,5 @@
package coding
type Encoder interface {
Encode() ([]byte, error)
MustEncode() []byte
}

View file

@ -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 {

View file

@ -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.

View file

@ -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)

View file

@ -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()

View file

@ -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)

View file

@ -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)

View file

@ -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)