map_codec.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright 2017, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. package tag
  16. import (
  17. "encoding/binary"
  18. "fmt"
  19. )
  20. // KeyType defines the types of keys allowed. Currently only keyTypeString is
  21. // supported.
  22. type keyType byte
  23. const (
  24. keyTypeString keyType = iota
  25. keyTypeInt64
  26. keyTypeTrue
  27. keyTypeFalse
  28. tagsVersionID = byte(0)
  29. )
  30. type encoderGRPC struct {
  31. buf []byte
  32. writeIdx, readIdx int
  33. }
  34. // writeKeyString writes the fieldID '0' followed by the key string and value
  35. // string.
  36. func (eg *encoderGRPC) writeTagString(k, v string) {
  37. eg.writeByte(byte(keyTypeString))
  38. eg.writeStringWithVarintLen(k)
  39. eg.writeStringWithVarintLen(v)
  40. }
  41. func (eg *encoderGRPC) writeTagUint64(k string, i uint64) {
  42. eg.writeByte(byte(keyTypeInt64))
  43. eg.writeStringWithVarintLen(k)
  44. eg.writeUint64(i)
  45. }
  46. func (eg *encoderGRPC) writeTagTrue(k string) {
  47. eg.writeByte(byte(keyTypeTrue))
  48. eg.writeStringWithVarintLen(k)
  49. }
  50. func (eg *encoderGRPC) writeTagFalse(k string) {
  51. eg.writeByte(byte(keyTypeFalse))
  52. eg.writeStringWithVarintLen(k)
  53. }
  54. func (eg *encoderGRPC) writeBytesWithVarintLen(bytes []byte) {
  55. length := len(bytes)
  56. eg.growIfRequired(binary.MaxVarintLen64 + length)
  57. eg.writeIdx += binary.PutUvarint(eg.buf[eg.writeIdx:], uint64(length))
  58. copy(eg.buf[eg.writeIdx:], bytes)
  59. eg.writeIdx += length
  60. }
  61. func (eg *encoderGRPC) writeStringWithVarintLen(s string) {
  62. length := len(s)
  63. eg.growIfRequired(binary.MaxVarintLen64 + length)
  64. eg.writeIdx += binary.PutUvarint(eg.buf[eg.writeIdx:], uint64(length))
  65. copy(eg.buf[eg.writeIdx:], s)
  66. eg.writeIdx += length
  67. }
  68. func (eg *encoderGRPC) writeByte(v byte) {
  69. eg.growIfRequired(1)
  70. eg.buf[eg.writeIdx] = v
  71. eg.writeIdx++
  72. }
  73. func (eg *encoderGRPC) writeUint32(i uint32) {
  74. eg.growIfRequired(4)
  75. binary.LittleEndian.PutUint32(eg.buf[eg.writeIdx:], i)
  76. eg.writeIdx += 4
  77. }
  78. func (eg *encoderGRPC) writeUint64(i uint64) {
  79. eg.growIfRequired(8)
  80. binary.LittleEndian.PutUint64(eg.buf[eg.writeIdx:], i)
  81. eg.writeIdx += 8
  82. }
  83. func (eg *encoderGRPC) readByte() byte {
  84. b := eg.buf[eg.readIdx]
  85. eg.readIdx++
  86. return b
  87. }
  88. func (eg *encoderGRPC) readUint32() uint32 {
  89. i := binary.LittleEndian.Uint32(eg.buf[eg.readIdx:])
  90. eg.readIdx += 4
  91. return i
  92. }
  93. func (eg *encoderGRPC) readUint64() uint64 {
  94. i := binary.LittleEndian.Uint64(eg.buf[eg.readIdx:])
  95. eg.readIdx += 8
  96. return i
  97. }
  98. func (eg *encoderGRPC) readBytesWithVarintLen() ([]byte, error) {
  99. if eg.readEnded() {
  100. return nil, fmt.Errorf("unexpected end while readBytesWithVarintLen '%x' starting at idx '%v'", eg.buf, eg.readIdx)
  101. }
  102. length, valueStart := binary.Uvarint(eg.buf[eg.readIdx:])
  103. if valueStart <= 0 {
  104. return nil, fmt.Errorf("unexpected end while readBytesWithVarintLen '%x' starting at idx '%v'", eg.buf, eg.readIdx)
  105. }
  106. valueStart += eg.readIdx
  107. valueEnd := valueStart + int(length)
  108. if valueEnd > len(eg.buf) {
  109. return nil, fmt.Errorf("malformed encoding: length:%v, upper:%v, maxLength:%v", length, valueEnd, len(eg.buf))
  110. }
  111. eg.readIdx = valueEnd
  112. return eg.buf[valueStart:valueEnd], nil
  113. }
  114. func (eg *encoderGRPC) readStringWithVarintLen() (string, error) {
  115. bytes, err := eg.readBytesWithVarintLen()
  116. if err != nil {
  117. return "", err
  118. }
  119. return string(bytes), nil
  120. }
  121. func (eg *encoderGRPC) growIfRequired(expected int) {
  122. if len(eg.buf)-eg.writeIdx < expected {
  123. tmp := make([]byte, 2*(len(eg.buf)+1)+expected)
  124. copy(tmp, eg.buf)
  125. eg.buf = tmp
  126. }
  127. }
  128. func (eg *encoderGRPC) readEnded() bool {
  129. return eg.readIdx >= len(eg.buf)
  130. }
  131. func (eg *encoderGRPC) bytes() []byte {
  132. return eg.buf[:eg.writeIdx]
  133. }
  134. // Encode encodes the tag map into a []byte. It is useful to propagate
  135. // the tag maps on wire in binary format.
  136. func Encode(m *Map) []byte {
  137. if m == nil {
  138. return nil
  139. }
  140. eg := &encoderGRPC{
  141. buf: make([]byte, len(m.m)),
  142. }
  143. eg.writeByte(byte(tagsVersionID))
  144. for k, v := range m.m {
  145. if v.m.ttl.ttl == valueTTLUnlimitedPropagation {
  146. eg.writeByte(byte(keyTypeString))
  147. eg.writeStringWithVarintLen(k.name)
  148. eg.writeBytesWithVarintLen([]byte(v.value))
  149. }
  150. }
  151. return eg.bytes()
  152. }
  153. // Decode decodes the given []byte into a tag map.
  154. func Decode(bytes []byte) (*Map, error) {
  155. ts := newMap()
  156. err := DecodeEach(bytes, ts.upsert)
  157. if err != nil {
  158. // no partial failures
  159. return nil, err
  160. }
  161. return ts, nil
  162. }
  163. // DecodeEach decodes the given serialized tag map, calling handler for each
  164. // tag key and value decoded.
  165. func DecodeEach(bytes []byte, fn func(key Key, val string, md metadatas)) error {
  166. eg := &encoderGRPC{
  167. buf: bytes,
  168. }
  169. if len(eg.buf) == 0 {
  170. return nil
  171. }
  172. version := eg.readByte()
  173. if version > tagsVersionID {
  174. return fmt.Errorf("cannot decode: unsupported version: %q; supports only up to: %q", version, tagsVersionID)
  175. }
  176. for !eg.readEnded() {
  177. typ := keyType(eg.readByte())
  178. if typ != keyTypeString {
  179. return fmt.Errorf("cannot decode: invalid key type: %q", typ)
  180. }
  181. k, err := eg.readBytesWithVarintLen()
  182. if err != nil {
  183. return err
  184. }
  185. v, err := eg.readBytesWithVarintLen()
  186. if err != nil {
  187. return err
  188. }
  189. key, err := NewKey(string(k))
  190. if err != nil {
  191. return err
  192. }
  193. val := string(v)
  194. if !checkValue(val) {
  195. return errInvalidValue
  196. }
  197. fn(key, val, createMetadatas(WithTTL(TTLUnlimitedPropagation)))
  198. if err != nil {
  199. return err
  200. }
  201. }
  202. return nil
  203. }