Skip to content

Commit da8aa9e

Browse files
author
roman.atachiants@careem.com
committedAug 1, 2021
sort by default
1 parent 462771a commit da8aa9e

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed
 

‎sorted/codecs.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,26 @@ func (c *uintSliceCodec) DecodeTo(d *binary.Decoder, rv reflect.Value) (err erro
135135

136136
// ------------------------------------------------------------------------------
137137

138-
type timestampCodec struct{}
138+
// TimestampCodec returns a timestamp codec.
139+
func TimestampCodec(sort bool) binary.Codec {
140+
return &timestampCodec{
141+
sort: sort,
142+
}
143+
}
144+
145+
type timestampCodec struct {
146+
sort bool // Whether codec needs to sort or not
147+
}
139148

140149
// EncodeTo encodes a value into the encoder.
141-
func (timestampCodec) EncodeTo(e *binary.Encoder, rv reflect.Value) (err error) {
150+
func (c timestampCodec) EncodeTo(e *binary.Encoder, rv reflect.Value) (err error) {
142151
data := rv.Interface().(Timestamps)
152+
if c.sort {
153+
sort.Sort(Uint64s(data))
154+
}
155+
143156
temp := make([]byte, 10)
144157
buffer := make([]byte, 0, 2*len(data)) // ~1-2 bytes per timestamp
145-
146158
prev := uint64(0)
147159
for _, curr := range data {
148160
diff := curr - prev

‎sorted/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,5 @@ type Timestamps []uint64
100100

101101
// GetBinaryCodec retrieves a custom binary codec.
102102
func (ts *Timestamps) GetBinaryCodec() binary.Codec {
103-
return timestampCodec{}
103+
return TimestampCodec(true)
104104
}

0 commit comments

Comments
 (0)
Please sign in to comment.