|
| 1 | +package serialize |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/binary" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + placeholderText = "AAAAFFEE" |
| 12 | +) |
| 13 | + |
| 14 | +// AkumuliSerializer writes a series of Point elements into RESP encoded |
| 15 | +// buffer. |
| 16 | +type AkumuliSerializer struct { |
| 17 | + book map[string]uint32 |
| 18 | + bookClosed bool |
| 19 | + deferred []byte |
| 20 | + index uint32 |
| 21 | +} |
| 22 | + |
| 23 | +// NewAkumuliSerializer initializes AkumuliSerializer instance. |
| 24 | +func NewAkumuliSerializer() *AkumuliSerializer { |
| 25 | + s := &AkumuliSerializer{} |
| 26 | + s.book = make(map[string]uint32) |
| 27 | + s.deferred = make([]byte, 0, 4096) |
| 28 | + s.bookClosed = false |
| 29 | + return s |
| 30 | +} |
| 31 | + |
| 32 | +// Serialize writes Point data to the given writer, conforming to the |
| 33 | +// AKUMULI RESP protocol. Serializer adds extra data to guide data loader. |
| 34 | +// This function writes output that contains binary and text data in RESP format. |
| 35 | +func (s *AkumuliSerializer) Serialize(p *Point, w io.Writer) (err error) { |
| 36 | + deferPoint := false |
| 37 | + |
| 38 | + buf := make([]byte, 0, 1024) |
| 39 | + // Add cue |
| 40 | + const HeaderLength = 8 |
| 41 | + buf = append(buf, placeholderText...) |
| 42 | + buf = append(buf, "+"...) |
| 43 | + |
| 44 | + // Series name |
| 45 | + for i := 0; i < len(p.fieldKeys); i++ { |
| 46 | + buf = append(buf, p.measurementName...) |
| 47 | + buf = append(buf, '.') |
| 48 | + buf = append(buf, p.fieldKeys[i]...) |
| 49 | + if i+1 < len(p.fieldKeys) { |
| 50 | + buf = append(buf, '|') |
| 51 | + } else { |
| 52 | + buf = append(buf, ' ') |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + for i := 0; i < len(p.tagKeys); i++ { |
| 57 | + buf = append(buf, ' ') |
| 58 | + buf = append(buf, p.tagKeys[i]...) |
| 59 | + buf = append(buf, '=') |
| 60 | + buf = append(buf, p.tagValues[i].(string)...) |
| 61 | + } |
| 62 | + |
| 63 | + series := string(buf[HeaderLength:]) |
| 64 | + if !s.bookClosed { |
| 65 | + // Save point for later |
| 66 | + if id, ok := s.book[series]; ok { |
| 67 | + s.bookClosed = true |
| 68 | + _, err = w.Write(s.deferred) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + buf = buf[:HeaderLength] |
| 73 | + buf = append(buf, fmt.Sprintf(":%d", id)...) |
| 74 | + binary.LittleEndian.PutUint32(buf[:4], id) |
| 75 | + } else { |
| 76 | + // Shortcut |
| 77 | + s.index++ |
| 78 | + tmp := make([]byte, 0, 1024) |
| 79 | + tmp = append(tmp, placeholderText...) |
| 80 | + tmp = append(tmp, "*2\n"...) |
| 81 | + tmp = append(tmp, buf[HeaderLength:]...) |
| 82 | + tmp = append(tmp, '\n') |
| 83 | + tmp = append(tmp, fmt.Sprintf(":%d\n", s.index)...) |
| 84 | + s.book[series] = s.index |
| 85 | + // Update cue |
| 86 | + binary.LittleEndian.PutUint16(tmp[4:6], uint16(len(tmp))) |
| 87 | + binary.LittleEndian.PutUint16(tmp[6:HeaderLength], uint16(0)) |
| 88 | + binary.LittleEndian.PutUint32(tmp[:4], s.index) |
| 89 | + binary.LittleEndian.PutUint32(buf[:4], s.index) |
| 90 | + _, err = w.Write(tmp) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + deferPoint = true |
| 95 | + buf = buf[:HeaderLength] |
| 96 | + buf = append(buf, fmt.Sprintf(":%d", s.index)...) |
| 97 | + } |
| 98 | + } else { |
| 99 | + // Replace the series name with the value from the book |
| 100 | + if id, ok := s.book[series]; ok { |
| 101 | + buf = buf[:HeaderLength] |
| 102 | + buf = append(buf, fmt.Sprintf(":%d", id)...) |
| 103 | + binary.LittleEndian.PutUint16(buf[4:6], uint16(len(buf))) |
| 104 | + binary.LittleEndian.PutUint16(buf[6:HeaderLength], uint16(0)) |
| 105 | + binary.LittleEndian.PutUint32(buf[:4], id) |
| 106 | + } else { |
| 107 | + return errors.New("unexpected series name") |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + buf = append(buf, '\n') |
| 112 | + |
| 113 | + // Timestamp |
| 114 | + buf = append(buf, ':') |
| 115 | + buf = fastFormatAppend(p.timestamp.UTC().UnixNano(), buf) |
| 116 | + buf = append(buf, '\n') |
| 117 | + |
| 118 | + // Values |
| 119 | + buf = append(buf, fmt.Sprintf("*%d\n", len(p.fieldValues))...) |
| 120 | + for i := 0; i < len(p.fieldValues); i++ { |
| 121 | + v := p.fieldValues[i] |
| 122 | + switch v.(type) { |
| 123 | + case int, int64: |
| 124 | + buf = append(buf, ':') |
| 125 | + case float64: |
| 126 | + buf = append(buf, '+') |
| 127 | + } |
| 128 | + buf = fastFormatAppend(v, buf) |
| 129 | + buf = append(buf, '\n') |
| 130 | + } |
| 131 | + |
| 132 | + // Update cue |
| 133 | + binary.LittleEndian.PutUint16(buf[4:6], uint16(len(buf))) |
| 134 | + binary.LittleEndian.PutUint16(buf[6:HeaderLength], uint16(len(p.fieldValues))) |
| 135 | + if deferPoint { |
| 136 | + s.deferred = append(s.deferred, buf...) |
| 137 | + return nil |
| 138 | + } |
| 139 | + _, err = w.Write(buf) |
| 140 | + return err |
| 141 | +} |
0 commit comments