|
| 1 | +package encode |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/binary" |
| 6 | + "github.com/changsongl/delay-queue/job" |
| 7 | +) |
| 8 | + |
| 9 | +// tag s |
| 10 | +const ( |
| 11 | + TagID uint64 = iota |
| 12 | + TagTopic |
| 13 | + TagDelay |
| 14 | + TagTTR |
| 15 | + TagBody |
| 16 | + TagVersion |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + // TagLength tag length |
| 21 | + TagLength = 1 |
| 22 | + |
| 23 | + // MaxUInt64Length variant max uint64 length |
| 24 | + MaxUInt64Length = 10 |
| 25 | +) |
| 26 | + |
| 27 | +type compress struct { |
| 28 | +} |
| 29 | + |
| 30 | +// NewCompress create a json encoder |
| 31 | +func NewCompress() Encoder { |
| 32 | + return &compress{} |
| 33 | +} |
| 34 | + |
| 35 | +// Encode compress encode |
| 36 | +func (c *compress) Encode(j *job.Job) ([]byte, error) { |
| 37 | + buf := make([]byte, c.bufLength(j)) |
| 38 | + written := 0 |
| 39 | + if !j.Delay.IsEmpty() { |
| 40 | + written += c.PutUInt64(TagDelay, uint64(j.Delay), buf[written:]) |
| 41 | + } |
| 42 | + if !j.TTR.IsEmpty() { |
| 43 | + written += c.PutUInt64(TagTTR, uint64(j.TTR), buf[written:]) |
| 44 | + } |
| 45 | + written += c.PutUInt64(TagVersion, j.Version.UInt64(), buf[written:]) |
| 46 | + |
| 47 | + if !j.ID.IsEmpty() { |
| 48 | + written += c.PutString(TagID, string(j.ID), buf[written:]) |
| 49 | + } |
| 50 | + if !j.Body.IsEmpty() { |
| 51 | + written += c.PutString(TagBody, string(j.Body), buf[written:]) |
| 52 | + } |
| 53 | + if !j.Topic.IsEmpty() { |
| 54 | + written += c.PutString(TagTopic, string(j.Topic), buf[written:]) |
| 55 | + } |
| 56 | + |
| 57 | + return buf[:written], nil |
| 58 | +} |
| 59 | + |
| 60 | +// Decode compress decode |
| 61 | +func (c *compress) Decode(b []byte, j *job.Job) error { |
| 62 | + index := 0 |
| 63 | + for index < len(b) { |
| 64 | + tag, err := binary.ReadUvarint(bytes.NewBuffer(b[index:])) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + index++ |
| 69 | + |
| 70 | + switch tag { |
| 71 | + case TagID: |
| 72 | + id, indexInc := c.ReadString(b[index:]) |
| 73 | + j.ID = job.ID(id) |
| 74 | + index += indexInc |
| 75 | + case TagTopic: |
| 76 | + topic, indexInc := c.ReadString(b[index:]) |
| 77 | + j.Topic = job.Topic(topic) |
| 78 | + index += indexInc |
| 79 | + case TagBody: |
| 80 | + body, indexInc := c.ReadString(b[index:]) |
| 81 | + j.Body = job.Body(body) |
| 82 | + index += indexInc |
| 83 | + case TagTTR: |
| 84 | + ttr, indexInc := c.ReadUint64(b[index:]) |
| 85 | + j.TTR = job.TTR(ttr) |
| 86 | + index += indexInc |
| 87 | + case TagDelay: |
| 88 | + delay, indexInc := c.ReadUint64(b[index:]) |
| 89 | + j.Delay = job.Delay(delay) |
| 90 | + index += indexInc |
| 91 | + case TagVersion: |
| 92 | + ts, indexInc := c.ReadUint64(b[index:]) |
| 93 | + j.SetVersion(int64(ts)) |
| 94 | + index += indexInc |
| 95 | + } |
| 96 | + } |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +func (c *compress) bufLength(j *job.Job) int { |
| 101 | + l := (TagLength+MaxUInt64Length)*5 + len(j.ID) + len(j.Topic) |
| 102 | + if j.Body != "" { |
| 103 | + l += TagLength + MaxUInt64Length + len(j.Topic) |
| 104 | + } |
| 105 | + return l |
| 106 | +} |
| 107 | + |
| 108 | +func (c *compress) ReadUint64(buf []byte) (uint64, int) { |
| 109 | + return binary.Uvarint(buf) |
| 110 | +} |
| 111 | + |
| 112 | +func (c *compress) PutUInt64(tag uint64, num uint64, buf []byte) int { |
| 113 | + written := binary.PutUvarint(buf, tag) |
| 114 | + written += binary.PutUvarint(buf[written:], num) |
| 115 | + return written |
| 116 | +} |
| 117 | + |
| 118 | +func (c *compress) ReadString(buf []byte) (string, int) { |
| 119 | + l, inc := binary.Uvarint(buf) |
| 120 | + end := inc + int(l) |
| 121 | + return string(buf[inc:end]), end |
| 122 | +} |
| 123 | + |
| 124 | +func (c *compress) PutString(tag uint64, str string, buf []byte) int { |
| 125 | + l := len(str) |
| 126 | + written := binary.PutUvarint(buf, tag) |
| 127 | + written += binary.PutUvarint(buf[written:], uint64(l)) |
| 128 | + chs := make([]uint8, 0, l) |
| 129 | + for _, ch := range str { |
| 130 | + chs = append(chs, uint8(ch)) |
| 131 | + } |
| 132 | + |
| 133 | + for _, ch := range []byte(str) { |
| 134 | + buf[written] = ch |
| 135 | + written++ |
| 136 | + } |
| 137 | + |
| 138 | + return written |
| 139 | +} |
0 commit comments