points.go 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490
  1. // Package models implements basic objects used throughout the TICK stack.
  2. package models // import "github.com/influxdata/platform/models"
  3. import (
  4. "bytes"
  5. "encoding/binary"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "math"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "unicode"
  15. "unicode/utf8"
  16. "github.com/influxdata/platform/pkg/escape"
  17. )
  18. type escapeSet struct {
  19. k [1]byte
  20. esc [2]byte
  21. }
  22. var (
  23. measurementEscapeCodes = [...]escapeSet{
  24. {k: [1]byte{','}, esc: [2]byte{'\\', ','}},
  25. {k: [1]byte{' '}, esc: [2]byte{'\\', ' '}},
  26. }
  27. tagEscapeCodes = [...]escapeSet{
  28. {k: [1]byte{','}, esc: [2]byte{'\\', ','}},
  29. {k: [1]byte{' '}, esc: [2]byte{'\\', ' '}},
  30. {k: [1]byte{'='}, esc: [2]byte{'\\', '='}},
  31. }
  32. // ErrPointMustHaveAField is returned when operating on a point that does not have any fields.
  33. ErrPointMustHaveAField = errors.New("point without fields is unsupported")
  34. // ErrInvalidNumber is returned when a number is expected but not provided.
  35. ErrInvalidNumber = errors.New("invalid number")
  36. // ErrInvalidPoint is returned when a point cannot be parsed correctly.
  37. ErrInvalidPoint = errors.New("point is invalid")
  38. )
  39. const (
  40. // MaxKeyLength is the largest allowed size of the combined measurement and tag keys.
  41. MaxKeyLength = 65535
  42. )
  43. // enableUint64Support will enable uint64 support if set to true.
  44. var enableUint64Support = false
  45. // EnableUintSupport manually enables uint support for the point parser.
  46. // This function will be removed in the future and only exists for unit tests during the
  47. // transition.
  48. func EnableUintSupport() {
  49. enableUint64Support = true
  50. }
  51. // Point defines the values that will be written to the database.
  52. type Point interface {
  53. // Name return the measurement name for the point.
  54. Name() []byte
  55. // SetName updates the measurement name for the point.
  56. SetName(string)
  57. // Tags returns the tag set for the point.
  58. Tags() Tags
  59. // ForEachTag iterates over each tag invoking fn. If fn return false, iteration stops.
  60. ForEachTag(fn func(k, v []byte) bool)
  61. // AddTag adds or replaces a tag value for a point.
  62. AddTag(key, value string)
  63. // SetTags replaces the tags for the point.
  64. SetTags(tags Tags)
  65. // HasTag returns true if the tag exists for the point.
  66. HasTag(tag []byte) bool
  67. // Fields returns the fields for the point.
  68. Fields() (Fields, error)
  69. // Time return the timestamp for the point.
  70. Time() time.Time
  71. // SetTime updates the timestamp for the point.
  72. SetTime(t time.Time)
  73. // UnixNano returns the timestamp of the point as nanoseconds since Unix epoch.
  74. UnixNano() int64
  75. // HashID returns a non-cryptographic checksum of the point's key.
  76. HashID() uint64
  77. // Key returns the key (measurement joined with tags) of the point.
  78. Key() []byte
  79. // String returns a string representation of the point. If there is a
  80. // timestamp associated with the point then it will be specified with the default
  81. // precision of nanoseconds.
  82. String() string
  83. // MarshalBinary returns a binary representation of the point.
  84. MarshalBinary() ([]byte, error)
  85. // PrecisionString returns a string representation of the point. If there
  86. // is a timestamp associated with the point then it will be specified in the
  87. // given unit.
  88. PrecisionString(precision string) string
  89. // RoundedString returns a string representation of the point. If there
  90. // is a timestamp associated with the point, then it will be rounded to the
  91. // given duration.
  92. RoundedString(d time.Duration) string
  93. // Split will attempt to return multiple points with the same timestamp whose
  94. // string representations are no longer than size. Points with a single field or
  95. // a point without a timestamp may exceed the requested size.
  96. Split(size int) []Point
  97. // Round will round the timestamp of the point to the given duration.
  98. Round(d time.Duration)
  99. // StringSize returns the length of the string that would be returned by String().
  100. StringSize() int
  101. // AppendString appends the result of String() to the provided buffer and returns
  102. // the result, potentially reducing string allocations.
  103. AppendString(buf []byte) []byte
  104. // FieldIterator retuns a FieldIterator that can be used to traverse the
  105. // fields of a point without constructing the in-memory map.
  106. FieldIterator() FieldIterator
  107. }
  108. // FieldType represents the type of a field.
  109. type FieldType int
  110. const (
  111. // Integer indicates the field's type is integer.
  112. Integer FieldType = iota
  113. // Float indicates the field's type is float.
  114. Float
  115. // Boolean indicates the field's type is boolean.
  116. Boolean
  117. // String indicates the field's type is string.
  118. String
  119. // Empty is used to indicate that there is no field.
  120. Empty
  121. // Unsigned indicates the field's type is an unsigned integer.
  122. Unsigned
  123. )
  124. func (t FieldType) String() string {
  125. switch t {
  126. case Integer:
  127. return "Integer"
  128. case Float:
  129. return "Float"
  130. case Boolean:
  131. return "Boolean"
  132. case String:
  133. return "String"
  134. case Empty:
  135. return "Empty"
  136. default:
  137. return "<unknown>"
  138. }
  139. }
  140. // FieldIterator provides a low-allocation interface to iterate through a point's fields.
  141. type FieldIterator interface {
  142. // Next indicates whether there any fields remaining.
  143. Next() bool
  144. // FieldKey returns the key of the current field.
  145. FieldKey() []byte
  146. // Type returns the FieldType of the current field.
  147. Type() FieldType
  148. // StringValue returns the string value of the current field.
  149. StringValue() string
  150. // IntegerValue returns the integer value of the current field.
  151. IntegerValue() (int64, error)
  152. // UnsignedValue returns the unsigned value of the current field.
  153. UnsignedValue() (uint64, error)
  154. // BooleanValue returns the boolean value of the current field.
  155. BooleanValue() (bool, error)
  156. // FloatValue returns the float value of the current field.
  157. FloatValue() (float64, error)
  158. // Reset resets the iterator to its initial state.
  159. Reset()
  160. }
  161. // Points represents a sortable list of points by timestamp.
  162. type Points []Point
  163. // Len implements sort.Interface.
  164. func (a Points) Len() int { return len(a) }
  165. // Less implements sort.Interface.
  166. func (a Points) Less(i, j int) bool { return a[i].Time().Before(a[j].Time()) }
  167. // Swap implements sort.Interface.
  168. func (a Points) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  169. // point is the default implementation of Point.
  170. type point struct {
  171. time time.Time
  172. // text encoding of measurement and tags
  173. // key must always be stored sorted by tags, if the original line was not sorted,
  174. // we need to resort it
  175. key []byte
  176. // text encoding of field data
  177. fields []byte
  178. // text encoding of timestamp
  179. ts []byte
  180. // cached version of parsed fields from data
  181. cachedFields map[string]interface{}
  182. // cached version of parsed name from key
  183. cachedName string
  184. // cached version of parsed tags
  185. cachedTags Tags
  186. it fieldIterator
  187. }
  188. // type assertions
  189. var (
  190. _ Point = (*point)(nil)
  191. _ FieldIterator = (*point)(nil)
  192. )
  193. const (
  194. // the number of characters for the largest possible int64 (9223372036854775807)
  195. maxInt64Digits = 19
  196. // the number of characters for the smallest possible int64 (-9223372036854775808)
  197. minInt64Digits = 20
  198. // the number of characters for the largest possible uint64 (18446744073709551615)
  199. maxUint64Digits = 20
  200. // the number of characters required for the largest float64 before a range check
  201. // would occur during parsing
  202. maxFloat64Digits = 25
  203. // the number of characters required for smallest float64 before a range check occur
  204. // would occur during parsing
  205. minFloat64Digits = 27
  206. )
  207. // ParsePoints returns a slice of Points from a text representation of a point
  208. // with each point separated by newlines. If any points fail to parse, a non-nil error
  209. // will be returned in addition to the points that parsed successfully.
  210. func ParsePoints(buf []byte) ([]Point, error) {
  211. return ParsePointsWithPrecision(buf, time.Now().UTC(), "n")
  212. }
  213. // ParsePointsString is identical to ParsePoints but accepts a string.
  214. func ParsePointsString(buf string) ([]Point, error) {
  215. return ParsePoints([]byte(buf))
  216. }
  217. // ParseKey returns the measurement name and tags from a point.
  218. //
  219. // NOTE: to minimize heap allocations, the returned Tags will refer to subslices of buf.
  220. // This can have the unintended effect preventing buf from being garbage collected.
  221. func ParseKey(buf []byte) (string, Tags) {
  222. name, tags := ParseKeyBytes(buf)
  223. return string(name), tags
  224. }
  225. func ParseKeyBytes(buf []byte) ([]byte, Tags) {
  226. return ParseKeyBytesWithTags(buf, nil)
  227. }
  228. func ParseKeyBytesWithTags(buf []byte, tags Tags) ([]byte, Tags) {
  229. // Ignore the error because scanMeasurement returns "missing fields" which we ignore
  230. // when just parsing a key
  231. state, i, _ := scanMeasurement(buf, 0)
  232. var name []byte
  233. if state == tagKeyState {
  234. tags = parseTags(buf, tags)
  235. // scanMeasurement returns the location of the comma if there are tags, strip that off
  236. name = buf[:i-1]
  237. } else {
  238. name = buf[:i]
  239. }
  240. return UnescapeMeasurement(name), tags
  241. }
  242. func ParseTags(buf []byte) Tags {
  243. return parseTags(buf, nil)
  244. }
  245. func ParseName(buf []byte) []byte {
  246. // Ignore the error because scanMeasurement returns "missing fields" which we ignore
  247. // when just parsing a key
  248. state, i, _ := scanMeasurement(buf, 0)
  249. var name []byte
  250. if state == tagKeyState {
  251. name = buf[:i-1]
  252. } else {
  253. name = buf[:i]
  254. }
  255. return UnescapeMeasurement(name)
  256. }
  257. // ValidPrecision checks if the precision is known.
  258. func ValidPrecision(precision string) bool {
  259. switch precision {
  260. case "n", "ns", "u", "us", "ms", "s":
  261. return true
  262. default:
  263. return false
  264. }
  265. }
  266. // ParsePointsWithPrecision is similar to ParsePoints, but allows the
  267. // caller to provide a precision for time.
  268. //
  269. // NOTE: to minimize heap allocations, the returned Points will refer to subslices of buf.
  270. // This can have the unintended effect preventing buf from being garbage collected.
  271. func ParsePointsWithPrecision(buf []byte, defaultTime time.Time, precision string) ([]Point, error) {
  272. points := make([]Point, 0, bytes.Count(buf, []byte{'\n'})+1)
  273. var (
  274. pos int
  275. block []byte
  276. failed []string
  277. )
  278. for pos < len(buf) {
  279. pos, block = scanLine(buf, pos)
  280. pos++
  281. if len(block) == 0 {
  282. continue
  283. }
  284. // lines which start with '#' are comments
  285. start := skipWhitespace(block, 0)
  286. // If line is all whitespace, just skip it
  287. if start >= len(block) {
  288. continue
  289. }
  290. if block[start] == '#' {
  291. continue
  292. }
  293. // strip the newline if one is present
  294. if block[len(block)-1] == '\n' {
  295. block = block[:len(block)-1]
  296. }
  297. pt, err := parsePoint(block[start:], defaultTime, precision)
  298. if err != nil {
  299. failed = append(failed, fmt.Sprintf("unable to parse '%s': %v", string(block[start:]), err))
  300. } else {
  301. points = append(points, pt)
  302. }
  303. }
  304. if len(failed) > 0 {
  305. return points, fmt.Errorf("%s", strings.Join(failed, "\n"))
  306. }
  307. return points, nil
  308. }
  309. func parsePoint(buf []byte, defaultTime time.Time, precision string) (Point, error) {
  310. // scan the first block which is measurement[,tag1=value1,tag2=value=2...]
  311. pos, key, err := scanKey(buf, 0)
  312. if err != nil {
  313. return nil, err
  314. }
  315. // measurement name is required
  316. if len(key) == 0 {
  317. return nil, fmt.Errorf("missing measurement")
  318. }
  319. if len(key) > MaxKeyLength {
  320. return nil, fmt.Errorf("max key length exceeded: %v > %v", len(key), MaxKeyLength)
  321. }
  322. // scan the second block is which is field1=value1[,field2=value2,...]
  323. pos, fields, err := scanFields(buf, pos)
  324. if err != nil {
  325. return nil, err
  326. }
  327. // at least one field is required
  328. if len(fields) == 0 {
  329. return nil, fmt.Errorf("missing fields")
  330. }
  331. var maxKeyErr error
  332. err = walkFields(fields, func(k, v []byte) bool {
  333. if sz := seriesKeySize(key, k); sz > MaxKeyLength {
  334. maxKeyErr = fmt.Errorf("max key length exceeded: %v > %v", sz, MaxKeyLength)
  335. return false
  336. }
  337. return true
  338. })
  339. if err != nil {
  340. return nil, err
  341. }
  342. if maxKeyErr != nil {
  343. return nil, maxKeyErr
  344. }
  345. // scan the last block which is an optional integer timestamp
  346. pos, ts, err := scanTime(buf, pos)
  347. if err != nil {
  348. return nil, err
  349. }
  350. pt := &point{
  351. key: key,
  352. fields: fields,
  353. ts: ts,
  354. }
  355. if len(ts) == 0 {
  356. pt.time = defaultTime
  357. pt.SetPrecision(precision)
  358. } else {
  359. ts, err := parseIntBytes(ts, 10, 64)
  360. if err != nil {
  361. return nil, err
  362. }
  363. pt.time, err = SafeCalcTime(ts, precision)
  364. if err != nil {
  365. return nil, err
  366. }
  367. // Determine if there are illegal non-whitespace characters after the
  368. // timestamp block.
  369. for pos < len(buf) {
  370. if buf[pos] != ' ' {
  371. return nil, ErrInvalidPoint
  372. }
  373. pos++
  374. }
  375. }
  376. return pt, nil
  377. }
  378. // GetPrecisionMultiplier will return a multiplier for the precision specified.
  379. func GetPrecisionMultiplier(precision string) int64 {
  380. d := time.Nanosecond
  381. switch precision {
  382. case "u":
  383. d = time.Microsecond
  384. case "ms":
  385. d = time.Millisecond
  386. case "s":
  387. d = time.Second
  388. case "m":
  389. d = time.Minute
  390. case "h":
  391. d = time.Hour
  392. }
  393. return int64(d)
  394. }
  395. // scanKey scans buf starting at i for the measurement and tag portion of the point.
  396. // It returns the ending position and the byte slice of key within buf. If there
  397. // are tags, they will be sorted if they are not already.
  398. func scanKey(buf []byte, i int) (int, []byte, error) {
  399. start := skipWhitespace(buf, i)
  400. i = start
  401. // Determines whether the tags are sort, assume they are
  402. sorted := true
  403. // indices holds the indexes within buf of the start of each tag. For example,
  404. // a buf of 'cpu,host=a,region=b,zone=c' would have indices slice of [4,11,20]
  405. // which indicates that the first tag starts at buf[4], seconds at buf[11], and
  406. // last at buf[20]
  407. indices := make([]int, 100)
  408. // tracks how many commas we've seen so we know how many values are indices.
  409. // Since indices is an arbitrarily large slice,
  410. // we need to know how many values in the buffer are in use.
  411. commas := 0
  412. // First scan the Point's measurement.
  413. state, i, err := scanMeasurement(buf, i)
  414. if err != nil {
  415. return i, buf[start:i], err
  416. }
  417. // Optionally scan tags if needed.
  418. if state == tagKeyState {
  419. i, commas, indices, err = scanTags(buf, i, indices)
  420. if err != nil {
  421. return i, buf[start:i], err
  422. }
  423. }
  424. // Now we know where the key region is within buf, and the location of tags, we
  425. // need to determine if duplicate tags exist and if the tags are sorted. This iterates
  426. // over the list comparing each tag in the sequence with each other.
  427. for j := 0; j < commas-1; j++ {
  428. // get the left and right tags
  429. _, left := scanTo(buf[indices[j]:indices[j+1]-1], 0, '=')
  430. _, right := scanTo(buf[indices[j+1]:indices[j+2]-1], 0, '=')
  431. // If left is greater than right, the tags are not sorted. We do not have to
  432. // continue because the short path no longer works.
  433. // If the tags are equal, then there are duplicate tags, and we should abort.
  434. // If the tags are not sorted, this pass may not find duplicate tags and we
  435. // need to do a more exhaustive search later.
  436. if cmp := bytes.Compare(left, right); cmp > 0 {
  437. sorted = false
  438. break
  439. } else if cmp == 0 {
  440. return i, buf[start:i], fmt.Errorf("duplicate tags")
  441. }
  442. }
  443. // If the tags are not sorted, then sort them. This sort is inline and
  444. // uses the tag indices we created earlier. The actual buffer is not sorted, the
  445. // indices are using the buffer for value comparison. After the indices are sorted,
  446. // the buffer is reconstructed from the sorted indices.
  447. if !sorted && commas > 0 {
  448. // Get the measurement name for later
  449. measurement := buf[start : indices[0]-1]
  450. // Sort the indices
  451. indices := indices[:commas]
  452. insertionSort(0, commas, buf, indices)
  453. // Create a new key using the measurement and sorted indices
  454. b := make([]byte, len(buf[start:i]))
  455. pos := copy(b, measurement)
  456. for _, i := range indices {
  457. b[pos] = ','
  458. pos++
  459. _, v := scanToSpaceOr(buf, i, ',')
  460. pos += copy(b[pos:], v)
  461. }
  462. // Check again for duplicate tags now that the tags are sorted.
  463. for j := 0; j < commas-1; j++ {
  464. // get the left and right tags
  465. _, left := scanTo(buf[indices[j]:], 0, '=')
  466. _, right := scanTo(buf[indices[j+1]:], 0, '=')
  467. // If the tags are equal, then there are duplicate tags, and we should abort.
  468. // If the tags are not sorted, this pass may not find duplicate tags and we
  469. // need to do a more exhaustive search later.
  470. if bytes.Equal(left, right) {
  471. return i, b, fmt.Errorf("duplicate tags")
  472. }
  473. }
  474. return i, b, nil
  475. }
  476. return i, buf[start:i], nil
  477. }
  478. // The following constants allow us to specify which state to move to
  479. // next, when scanning sections of a Point.
  480. const (
  481. tagKeyState = iota
  482. tagValueState
  483. fieldsState
  484. )
  485. // scanMeasurement examines the measurement part of a Point, returning
  486. // the next state to move to, and the current location in the buffer.
  487. func scanMeasurement(buf []byte, i int) (int, int, error) {
  488. // Check first byte of measurement, anything except a comma is fine.
  489. // It can't be a space, since whitespace is stripped prior to this
  490. // function call.
  491. if i >= len(buf) || buf[i] == ',' {
  492. return -1, i, fmt.Errorf("missing measurement")
  493. }
  494. for {
  495. i++
  496. if i >= len(buf) {
  497. // cpu
  498. return -1, i, fmt.Errorf("missing fields")
  499. }
  500. if buf[i-1] == '\\' {
  501. // Skip character (it's escaped).
  502. continue
  503. }
  504. // Unescaped comma; move onto scanning the tags.
  505. if buf[i] == ',' {
  506. return tagKeyState, i + 1, nil
  507. }
  508. // Unescaped space; move onto scanning the fields.
  509. if buf[i] == ' ' {
  510. // cpu value=1.0
  511. return fieldsState, i, nil
  512. }
  513. }
  514. }
  515. // scanTags examines all the tags in a Point, keeping track of and
  516. // returning the updated indices slice, number of commas and location
  517. // in buf where to start examining the Point fields.
  518. func scanTags(buf []byte, i int, indices []int) (int, int, []int, error) {
  519. var (
  520. err error
  521. commas int
  522. state = tagKeyState
  523. )
  524. for {
  525. switch state {
  526. case tagKeyState:
  527. // Grow our indices slice if we have too many tags.
  528. if commas >= len(indices) {
  529. newIndics := make([]int, cap(indices)*2)
  530. copy(newIndics, indices)
  531. indices = newIndics
  532. }
  533. indices[commas] = i
  534. commas++
  535. i, err = scanTagsKey(buf, i)
  536. state = tagValueState // tag value always follows a tag key
  537. case tagValueState:
  538. state, i, err = scanTagsValue(buf, i)
  539. case fieldsState:
  540. indices[commas] = i + 1
  541. return i, commas, indices, nil
  542. }
  543. if err != nil {
  544. return i, commas, indices, err
  545. }
  546. }
  547. }
  548. // scanTagsKey scans each character in a tag key.
  549. func scanTagsKey(buf []byte, i int) (int, error) {
  550. // First character of the key.
  551. if i >= len(buf) || buf[i] == ' ' || buf[i] == ',' || buf[i] == '=' {
  552. // cpu,{'', ' ', ',', '='}
  553. return i, fmt.Errorf("missing tag key")
  554. }
  555. // Examine each character in the tag key until we hit an unescaped
  556. // equals (the tag value), or we hit an error (i.e., unescaped
  557. // space or comma).
  558. for {
  559. i++
  560. // Either we reached the end of the buffer or we hit an
  561. // unescaped comma or space.
  562. if i >= len(buf) ||
  563. ((buf[i] == ' ' || buf[i] == ',') && buf[i-1] != '\\') {
  564. // cpu,tag{'', ' ', ','}
  565. return i, fmt.Errorf("missing tag value")
  566. }
  567. if buf[i] == '=' && buf[i-1] != '\\' {
  568. // cpu,tag=
  569. return i + 1, nil
  570. }
  571. }
  572. }
  573. // scanTagsValue scans each character in a tag value.
  574. func scanTagsValue(buf []byte, i int) (int, int, error) {
  575. // Tag value cannot be empty.
  576. if i >= len(buf) || buf[i] == ',' || buf[i] == ' ' {
  577. // cpu,tag={',', ' '}
  578. return -1, i, fmt.Errorf("missing tag value")
  579. }
  580. // Examine each character in the tag value until we hit an unescaped
  581. // comma (move onto next tag key), an unescaped space (move onto
  582. // fields), or we error out.
  583. for {
  584. i++
  585. if i >= len(buf) {
  586. // cpu,tag=value
  587. return -1, i, fmt.Errorf("missing fields")
  588. }
  589. // An unescaped equals sign is an invalid tag value.
  590. if buf[i] == '=' && buf[i-1] != '\\' {
  591. // cpu,tag={'=', 'fo=o'}
  592. return -1, i, fmt.Errorf("invalid tag format")
  593. }
  594. if buf[i] == ',' && buf[i-1] != '\\' {
  595. // cpu,tag=foo,
  596. return tagKeyState, i + 1, nil
  597. }
  598. // cpu,tag=foo value=1.0
  599. // cpu, tag=foo\= value=1.0
  600. if buf[i] == ' ' && buf[i-1] != '\\' {
  601. return fieldsState, i, nil
  602. }
  603. }
  604. }
  605. func insertionSort(l, r int, buf []byte, indices []int) {
  606. for i := l + 1; i < r; i++ {
  607. for j := i; j > l && less(buf, indices, j, j-1); j-- {
  608. indices[j], indices[j-1] = indices[j-1], indices[j]
  609. }
  610. }
  611. }
  612. func less(buf []byte, indices []int, i, j int) bool {
  613. // This grabs the tag names for i & j, it ignores the values
  614. _, a := scanTo(buf, indices[i], '=')
  615. _, b := scanTo(buf, indices[j], '=')
  616. return bytes.Compare(a, b) < 0
  617. }
  618. // scanFields scans buf, starting at i for the fields section of a point. It returns
  619. // the ending position and the byte slice of the fields within buf.
  620. func scanFields(buf []byte, i int) (int, []byte, error) {
  621. start := skipWhitespace(buf, i)
  622. i = start
  623. quoted := false
  624. // tracks how many '=' we've seen
  625. equals := 0
  626. // tracks how many commas we've seen
  627. commas := 0
  628. for {
  629. // reached the end of buf?
  630. if i >= len(buf) {
  631. break
  632. }
  633. // escaped characters?
  634. if buf[i] == '\\' && i+1 < len(buf) {
  635. i += 2
  636. continue
  637. }
  638. // If the value is quoted, scan until we get to the end quote
  639. // Only quote values in the field value since quotes are not significant
  640. // in the field key
  641. if buf[i] == '"' && equals > commas {
  642. quoted = !quoted
  643. i++
  644. continue
  645. }
  646. // If we see an =, ensure that there is at least on char before and after it
  647. if buf[i] == '=' && !quoted {
  648. equals++
  649. // check for "... =123" but allow "a\ =123"
  650. if buf[i-1] == ' ' && buf[i-2] != '\\' {
  651. return i, buf[start:i], fmt.Errorf("missing field key")
  652. }
  653. // check for "...a=123,=456" but allow "a=123,a\,=456"
  654. if buf[i-1] == ',' && buf[i-2] != '\\' {
  655. return i, buf[start:i], fmt.Errorf("missing field key")
  656. }
  657. // check for "... value="
  658. if i+1 >= len(buf) {
  659. return i, buf[start:i], fmt.Errorf("missing field value")
  660. }
  661. // check for "... value=,value2=..."
  662. if buf[i+1] == ',' || buf[i+1] == ' ' {
  663. return i, buf[start:i], fmt.Errorf("missing field value")
  664. }
  665. if isNumeric(buf[i+1]) || buf[i+1] == '-' || buf[i+1] == 'N' || buf[i+1] == 'n' {
  666. var err error
  667. i, err = scanNumber(buf, i+1)
  668. if err != nil {
  669. return i, buf[start:i], err
  670. }
  671. continue
  672. }
  673. // If next byte is not a double-quote, the value must be a boolean
  674. if buf[i+1] != '"' {
  675. var err error
  676. i, _, err = scanBoolean(buf, i+1)
  677. if err != nil {
  678. return i, buf[start:i], err
  679. }
  680. continue
  681. }
  682. }
  683. if buf[i] == ',' && !quoted {
  684. commas++
  685. }
  686. // reached end of block?
  687. if buf[i] == ' ' && !quoted {
  688. break
  689. }
  690. i++
  691. }
  692. if quoted {
  693. return i, buf[start:i], fmt.Errorf("unbalanced quotes")
  694. }
  695. // check that all field sections had key and values (e.g. prevent "a=1,b"
  696. if equals == 0 || commas != equals-1 {
  697. return i, buf[start:i], fmt.Errorf("invalid field format")
  698. }
  699. return i, buf[start:i], nil
  700. }
  701. // scanTime scans buf, starting at i for the time section of a point. It
  702. // returns the ending position and the byte slice of the timestamp within buf
  703. // and and error if the timestamp is not in the correct numeric format.
  704. func scanTime(buf []byte, i int) (int, []byte, error) {
  705. start := skipWhitespace(buf, i)
  706. i = start
  707. for {
  708. // reached the end of buf?
  709. if i >= len(buf) {
  710. break
  711. }
  712. // Reached end of block or trailing whitespace?
  713. if buf[i] == '\n' || buf[i] == ' ' {
  714. break
  715. }
  716. // Handle negative timestamps
  717. if i == start && buf[i] == '-' {
  718. i++
  719. continue
  720. }
  721. // Timestamps should be integers, make sure they are so we don't need
  722. // to actually parse the timestamp until needed.
  723. if buf[i] < '0' || buf[i] > '9' {
  724. return i, buf[start:i], fmt.Errorf("bad timestamp")
  725. }
  726. i++
  727. }
  728. return i, buf[start:i], nil
  729. }
  730. func isNumeric(b byte) bool {
  731. return (b >= '0' && b <= '9') || b == '.'
  732. }
  733. // scanNumber returns the end position within buf, start at i after
  734. // scanning over buf for an integer, or float. It returns an
  735. // error if a invalid number is scanned.
  736. func scanNumber(buf []byte, i int) (int, error) {
  737. start := i
  738. var isInt, isUnsigned bool
  739. // Is negative number?
  740. if i < len(buf) && buf[i] == '-' {
  741. i++
  742. // There must be more characters now, as just '-' is illegal.
  743. if i == len(buf) {
  744. return i, ErrInvalidNumber
  745. }
  746. }
  747. // how many decimal points we've see
  748. decimal := false
  749. // indicates the number is float in scientific notation
  750. scientific := false
  751. for {
  752. if i >= len(buf) {
  753. break
  754. }
  755. if buf[i] == ',' || buf[i] == ' ' {
  756. break
  757. }
  758. if buf[i] == 'i' && i > start && !(isInt || isUnsigned) {
  759. isInt = true
  760. i++
  761. continue
  762. } else if buf[i] == 'u' && i > start && !(isInt || isUnsigned) {
  763. isUnsigned = true
  764. i++
  765. continue
  766. }
  767. if buf[i] == '.' {
  768. // Can't have more than 1 decimal (e.g. 1.1.1 should fail)
  769. if decimal {
  770. return i, ErrInvalidNumber
  771. }
  772. decimal = true
  773. }
  774. // `e` is valid for floats but not as the first char
  775. if i > start && (buf[i] == 'e' || buf[i] == 'E') {
  776. scientific = true
  777. i++
  778. continue
  779. }
  780. // + and - are only valid at this point if they follow an e (scientific notation)
  781. if (buf[i] == '+' || buf[i] == '-') && (buf[i-1] == 'e' || buf[i-1] == 'E') {
  782. i++
  783. continue
  784. }
  785. // NaN is an unsupported value
  786. if i+2 < len(buf) && (buf[i] == 'N' || buf[i] == 'n') {
  787. return i, ErrInvalidNumber
  788. }
  789. if !isNumeric(buf[i]) {
  790. return i, ErrInvalidNumber
  791. }
  792. i++
  793. }
  794. if (isInt || isUnsigned) && (decimal || scientific) {
  795. return i, ErrInvalidNumber
  796. }
  797. numericDigits := i - start
  798. if isInt {
  799. numericDigits--
  800. }
  801. if decimal {
  802. numericDigits--
  803. }
  804. if buf[start] == '-' {
  805. numericDigits--
  806. }
  807. if numericDigits == 0 {
  808. return i, ErrInvalidNumber
  809. }
  810. // It's more common that numbers will be within min/max range for their type but we need to prevent
  811. // out or range numbers from being parsed successfully. This uses some simple heuristics to decide
  812. // if we should parse the number to the actual type. It does not do it all the time because it incurs
  813. // extra allocations and we end up converting the type again when writing points to disk.
  814. if isInt {
  815. // Make sure the last char is an 'i' for integers (e.g. 9i10 is not valid)
  816. if buf[i-1] != 'i' {
  817. return i, ErrInvalidNumber
  818. }
  819. // Parse the int to check bounds the number of digits could be larger than the max range
  820. // We subtract 1 from the index to remove the `i` from our tests
  821. if len(buf[start:i-1]) >= maxInt64Digits || len(buf[start:i-1]) >= minInt64Digits {
  822. if _, err := parseIntBytes(buf[start:i-1], 10, 64); err != nil {
  823. return i, fmt.Errorf("unable to parse integer %s: %s", buf[start:i-1], err)
  824. }
  825. }
  826. } else if isUnsigned {
  827. // Return an error if uint64 support has not been enabled.
  828. if !enableUint64Support {
  829. return i, ErrInvalidNumber
  830. }
  831. // Make sure the last char is a 'u' for unsigned
  832. if buf[i-1] != 'u' {
  833. return i, ErrInvalidNumber
  834. }
  835. // Make sure the first char is not a '-' for unsigned
  836. if buf[start] == '-' {
  837. return i, ErrInvalidNumber
  838. }
  839. // Parse the uint to check bounds the number of digits could be larger than the max range
  840. // We subtract 1 from the index to remove the `u` from our tests
  841. if len(buf[start:i-1]) >= maxUint64Digits {
  842. if _, err := parseUintBytes(buf[start:i-1], 10, 64); err != nil {
  843. return i, fmt.Errorf("unable to parse unsigned %s: %s", buf[start:i-1], err)
  844. }
  845. }
  846. } else {
  847. // Parse the float to check bounds if it's scientific or the number of digits could be larger than the max range
  848. if scientific || len(buf[start:i]) >= maxFloat64Digits || len(buf[start:i]) >= minFloat64Digits {
  849. if _, err := parseFloatBytes(buf[start:i], 10); err != nil {
  850. return i, fmt.Errorf("invalid float")
  851. }
  852. }
  853. }
  854. return i, nil
  855. }
  856. // scanBoolean returns the end position within buf, start at i after
  857. // scanning over buf for boolean. Valid values for a boolean are
  858. // t, T, true, TRUE, f, F, false, FALSE. It returns an error if a invalid boolean
  859. // is scanned.
  860. func scanBoolean(buf []byte, i int) (int, []byte, error) {
  861. start := i
  862. if i < len(buf) && (buf[i] != 't' && buf[i] != 'f' && buf[i] != 'T' && buf[i] != 'F') {
  863. return i, buf[start:i], fmt.Errorf("invalid boolean")
  864. }
  865. i++
  866. for {
  867. if i >= len(buf) {
  868. break
  869. }
  870. if buf[i] == ',' || buf[i] == ' ' {
  871. break
  872. }
  873. i++
  874. }
  875. // Single char bool (t, T, f, F) is ok
  876. if i-start == 1 {
  877. return i, buf[start:i], nil
  878. }
  879. // length must be 4 for true or TRUE
  880. if (buf[start] == 't' || buf[start] == 'T') && i-start != 4 {
  881. return i, buf[start:i], fmt.Errorf("invalid boolean")
  882. }
  883. // length must be 5 for false or FALSE
  884. if (buf[start] == 'f' || buf[start] == 'F') && i-start != 5 {
  885. return i, buf[start:i], fmt.Errorf("invalid boolean")
  886. }
  887. // Otherwise
  888. valid := false
  889. switch buf[start] {
  890. case 't':
  891. valid = bytes.Equal(buf[start:i], []byte("true"))
  892. case 'f':
  893. valid = bytes.Equal(buf[start:i], []byte("false"))
  894. case 'T':
  895. valid = bytes.Equal(buf[start:i], []byte("TRUE")) || bytes.Equal(buf[start:i], []byte("True"))
  896. case 'F':
  897. valid = bytes.Equal(buf[start:i], []byte("FALSE")) || bytes.Equal(buf[start:i], []byte("False"))
  898. }
  899. if !valid {
  900. return i, buf[start:i], fmt.Errorf("invalid boolean")
  901. }
  902. return i, buf[start:i], nil
  903. }
  904. // skipWhitespace returns the end position within buf, starting at i after
  905. // scanning over spaces in tags.
  906. func skipWhitespace(buf []byte, i int) int {
  907. for i < len(buf) {
  908. if buf[i] != ' ' && buf[i] != '\t' && buf[i] != 0 {
  909. break
  910. }
  911. i++
  912. }
  913. return i
  914. }
  915. // scanLine returns the end position in buf and the next line found within
  916. // buf.
  917. func scanLine(buf []byte, i int) (int, []byte) {
  918. start := i
  919. quoted := false
  920. fields := false
  921. // tracks how many '=' and commas we've seen
  922. // this duplicates some of the functionality in scanFields
  923. equals := 0
  924. commas := 0
  925. for {
  926. // reached the end of buf?
  927. if i >= len(buf) {
  928. break
  929. }
  930. // skip past escaped characters
  931. if buf[i] == '\\' && i+2 < len(buf) {
  932. i += 2
  933. continue
  934. }
  935. if buf[i] == ' ' {
  936. fields = true
  937. }
  938. // If we see a double quote, makes sure it is not escaped
  939. if fields {
  940. if !quoted && buf[i] == '=' {
  941. i++
  942. equals++
  943. continue
  944. } else if !quoted && buf[i] == ',' {
  945. i++
  946. commas++
  947. continue
  948. } else if buf[i] == '"' && equals > commas {
  949. i++
  950. quoted = !quoted
  951. continue
  952. }
  953. }
  954. if buf[i] == '\n' && !quoted {
  955. break
  956. }
  957. i++
  958. }
  959. return i, buf[start:i]
  960. }
  961. // scanTo returns the end position in buf and the next consecutive block
  962. // of bytes, starting from i and ending with stop byte, where stop byte
  963. // has not been escaped.
  964. //
  965. // If there are leading spaces, they are skipped.
  966. func scanTo(buf []byte, i int, stop byte) (int, []byte) {
  967. start := i
  968. for {
  969. // reached the end of buf?
  970. if i >= len(buf) {
  971. break
  972. }
  973. // Reached unescaped stop value?
  974. if buf[i] == stop && (i == 0 || buf[i-1] != '\\') {
  975. break
  976. }
  977. i++
  978. }
  979. return i, buf[start:i]
  980. }
  981. // scanTo returns the end position in buf and the next consecutive block
  982. // of bytes, starting from i and ending with stop byte. If there are leading
  983. // spaces, they are skipped.
  984. func scanToSpaceOr(buf []byte, i int, stop byte) (int, []byte) {
  985. start := i
  986. if buf[i] == stop || buf[i] == ' ' {
  987. return i, buf[start:i]
  988. }
  989. for {
  990. i++
  991. if buf[i-1] == '\\' {
  992. continue
  993. }
  994. // reached the end of buf?
  995. if i >= len(buf) {
  996. return i, buf[start:i]
  997. }
  998. // reached end of block?
  999. if buf[i] == stop || buf[i] == ' ' {
  1000. return i, buf[start:i]
  1001. }
  1002. }
  1003. }
  1004. func scanTagValue(buf []byte, i int) (int, []byte) {
  1005. start := i
  1006. for {
  1007. if i >= len(buf) {
  1008. break
  1009. }
  1010. if buf[i] == ',' && buf[i-1] != '\\' {
  1011. break
  1012. }
  1013. i++
  1014. }
  1015. if i > len(buf) {
  1016. return i, nil
  1017. }
  1018. return i, buf[start:i]
  1019. }
  1020. func scanFieldValue(buf []byte, i int) (int, []byte) {
  1021. start := i
  1022. quoted := false
  1023. for i < len(buf) {
  1024. // Only escape char for a field value is a double-quote and backslash
  1025. if buf[i] == '\\' && i+1 < len(buf) && (buf[i+1] == '"' || buf[i+1] == '\\') {
  1026. i += 2
  1027. continue
  1028. }
  1029. // Quoted value? (e.g. string)
  1030. if buf[i] == '"' {
  1031. i++
  1032. quoted = !quoted
  1033. continue
  1034. }
  1035. if buf[i] == ',' && !quoted {
  1036. break
  1037. }
  1038. i++
  1039. }
  1040. return i, buf[start:i]
  1041. }
  1042. func EscapeMeasurement(in []byte) []byte {
  1043. for _, c := range measurementEscapeCodes {
  1044. if bytes.IndexByte(in, c.k[0]) != -1 {
  1045. in = bytes.Replace(in, c.k[:], c.esc[:], -1)
  1046. }
  1047. }
  1048. return in
  1049. }
  1050. func UnescapeMeasurement(in []byte) []byte {
  1051. if bytes.IndexByte(in, '\\') == -1 {
  1052. return in
  1053. }
  1054. for i := range measurementEscapeCodes {
  1055. c := &measurementEscapeCodes[i]
  1056. if bytes.IndexByte(in, c.k[0]) != -1 {
  1057. in = bytes.Replace(in, c.esc[:], c.k[:], -1)
  1058. }
  1059. }
  1060. return in
  1061. }
  1062. func escapeTag(in []byte) []byte {
  1063. for i := range tagEscapeCodes {
  1064. c := &tagEscapeCodes[i]
  1065. if bytes.IndexByte(in, c.k[0]) != -1 {
  1066. in = bytes.Replace(in, c.k[:], c.esc[:], -1)
  1067. }
  1068. }
  1069. return in
  1070. }
  1071. func unescapeTag(in []byte) []byte {
  1072. if bytes.IndexByte(in, '\\') == -1 {
  1073. return in
  1074. }
  1075. for i := range tagEscapeCodes {
  1076. c := &tagEscapeCodes[i]
  1077. if bytes.IndexByte(in, c.k[0]) != -1 {
  1078. in = bytes.Replace(in, c.esc[:], c.k[:], -1)
  1079. }
  1080. }
  1081. return in
  1082. }
  1083. // escapeStringFieldReplacer replaces double quotes and backslashes
  1084. // with the same character preceded by a backslash.
  1085. // As of Go 1.7 this benchmarked better in allocations and CPU time
  1086. // compared to iterating through a string byte-by-byte and appending to a new byte slice,
  1087. // calling strings.Replace twice, and better than (*Regex).ReplaceAllString.
  1088. var escapeStringFieldReplacer = strings.NewReplacer(`"`, `\"`, `\`, `\\`)
  1089. // EscapeStringField returns a copy of in with any double quotes or
  1090. // backslashes with escaped values.
  1091. func EscapeStringField(in string) string {
  1092. return escapeStringFieldReplacer.Replace(in)
  1093. }
  1094. // unescapeStringField returns a copy of in with any escaped double-quotes
  1095. // or backslashes unescaped.
  1096. func unescapeStringField(in string) string {
  1097. if strings.IndexByte(in, '\\') == -1 {
  1098. return in
  1099. }
  1100. var out []byte
  1101. i := 0
  1102. for {
  1103. if i >= len(in) {
  1104. break
  1105. }
  1106. // unescape backslashes
  1107. if in[i] == '\\' && i+1 < len(in) && in[i+1] == '\\' {
  1108. out = append(out, '\\')
  1109. i += 2
  1110. continue
  1111. }
  1112. // unescape double-quotes
  1113. if in[i] == '\\' && i+1 < len(in) && in[i+1] == '"' {
  1114. out = append(out, '"')
  1115. i += 2
  1116. continue
  1117. }
  1118. out = append(out, in[i])
  1119. i++
  1120. }
  1121. return string(out)
  1122. }
  1123. // NewPoint returns a new point with the given measurement name, tags, fields and timestamp. If
  1124. // an unsupported field value (NaN, or +/-Inf) or out of range time is passed, this function
  1125. // returns an error.
  1126. func NewPoint(name string, tags Tags, fields Fields, t time.Time) (Point, error) {
  1127. key, err := pointKey(name, tags, fields, t)
  1128. if err != nil {
  1129. return nil, err
  1130. }
  1131. return &point{
  1132. key: key,
  1133. time: t,
  1134. fields: fields.MarshalBinary(),
  1135. }, nil
  1136. }
  1137. // pointKey checks some basic requirements for valid points, and returns the
  1138. // key, along with an possible error.
  1139. func pointKey(measurement string, tags Tags, fields Fields, t time.Time) ([]byte, error) {
  1140. if len(fields) == 0 {
  1141. return nil, ErrPointMustHaveAField
  1142. }
  1143. if !t.IsZero() {
  1144. if err := CheckTime(t); err != nil {
  1145. return nil, err
  1146. }
  1147. }
  1148. for key, value := range fields {
  1149. switch value := value.(type) {
  1150. case float64:
  1151. // Ensure the caller validates and handles invalid field values
  1152. if math.IsInf(value, 0) {
  1153. return nil, fmt.Errorf("+/-Inf is an unsupported value for field %s", key)
  1154. }
  1155. if math.IsNaN(value) {
  1156. return nil, fmt.Errorf("NaN is an unsupported value for field %s", key)
  1157. }
  1158. case float32:
  1159. // Ensure the caller validates and handles invalid field values
  1160. if math.IsInf(float64(value), 0) {
  1161. return nil, fmt.Errorf("+/-Inf is an unsupported value for field %s", key)
  1162. }
  1163. if math.IsNaN(float64(value)) {
  1164. return nil, fmt.Errorf("NaN is an unsupported value for field %s", key)
  1165. }
  1166. }
  1167. if len(key) == 0 {
  1168. return nil, fmt.Errorf("all fields must have non-empty names")
  1169. }
  1170. }
  1171. key := MakeKey([]byte(measurement), tags)
  1172. for field := range fields {
  1173. sz := seriesKeySize(key, []byte(field))
  1174. if sz > MaxKeyLength {
  1175. return nil, fmt.Errorf("max key length exceeded: %v > %v", sz, MaxKeyLength)
  1176. }
  1177. }
  1178. return key, nil
  1179. }
  1180. func seriesKeySize(key, field []byte) int {
  1181. // 4 is the length of the tsm1.fieldKeySeparator constant. It's inlined here to avoid a circular
  1182. // dependency.
  1183. return len(key) + 4 + len(field)
  1184. }
  1185. // NewPointFromBytes returns a new Point from a marshalled Point.
  1186. func NewPointFromBytes(b []byte) (Point, error) {
  1187. p := &point{}
  1188. if err := p.UnmarshalBinary(b); err != nil {
  1189. return nil, err
  1190. }
  1191. // This does some basic validation to ensure there are fields and they
  1192. // can be unmarshalled as well.
  1193. iter := p.FieldIterator()
  1194. var hasField bool
  1195. for iter.Next() {
  1196. if len(iter.FieldKey()) == 0 {
  1197. continue
  1198. }
  1199. hasField = true
  1200. switch iter.Type() {
  1201. case Float:
  1202. _, err := iter.FloatValue()
  1203. if err != nil {
  1204. return nil, fmt.Errorf("unable to unmarshal field %s: %s", string(iter.FieldKey()), err)
  1205. }
  1206. case Integer:
  1207. _, err := iter.IntegerValue()
  1208. if err != nil {
  1209. return nil, fmt.Errorf("unable to unmarshal field %s: %s", string(iter.FieldKey()), err)
  1210. }
  1211. case Unsigned:
  1212. _, err := iter.UnsignedValue()
  1213. if err != nil {
  1214. return nil, fmt.Errorf("unable to unmarshal field %s: %s", string(iter.FieldKey()), err)
  1215. }
  1216. case String:
  1217. // Skip since this won't return an error
  1218. case Boolean:
  1219. _, err := iter.BooleanValue()
  1220. if err != nil {
  1221. return nil, fmt.Errorf("unable to unmarshal field %s: %s", string(iter.FieldKey()), err)
  1222. }
  1223. }
  1224. }
  1225. if !hasField {
  1226. return nil, ErrPointMustHaveAField
  1227. }
  1228. return p, nil
  1229. }
  1230. // MustNewPoint returns a new point with the given measurement name, tags, fields and timestamp. If
  1231. // an unsupported field value (NaN) is passed, this function panics.
  1232. func MustNewPoint(name string, tags Tags, fields Fields, time time.Time) Point {
  1233. pt, err := NewPoint(name, tags, fields, time)
  1234. if err != nil {
  1235. panic(err.Error())
  1236. }
  1237. return pt
  1238. }
  1239. // Key returns the key (measurement joined with tags) of the point.
  1240. func (p *point) Key() []byte {
  1241. return p.key
  1242. }
  1243. func (p *point) name() []byte {
  1244. _, name := scanTo(p.key, 0, ',')
  1245. return name
  1246. }
  1247. func (p *point) Name() []byte {
  1248. return escape.Unescape(p.name())
  1249. }
  1250. // SetName updates the measurement name for the point.
  1251. func (p *point) SetName(name string) {
  1252. p.cachedName = ""
  1253. p.key = MakeKey([]byte(name), p.Tags())
  1254. }
  1255. // Time return the timestamp for the point.
  1256. func (p *point) Time() time.Time {
  1257. return p.time
  1258. }
  1259. // SetTime updates the timestamp for the point.
  1260. func (p *point) SetTime(t time.Time) {
  1261. p.time = t
  1262. }
  1263. // Round will round the timestamp of the point to the given duration.
  1264. func (p *point) Round(d time.Duration) {
  1265. p.time = p.time.Round(d)
  1266. }
  1267. // Tags returns the tag set for the point.
  1268. func (p *point) Tags() Tags {
  1269. if p.cachedTags != nil {
  1270. return p.cachedTags
  1271. }
  1272. p.cachedTags = parseTags(p.key, nil)
  1273. return p.cachedTags
  1274. }
  1275. func (p *point) ForEachTag(fn func(k, v []byte) bool) {
  1276. walkTags(p.key, fn)
  1277. }
  1278. func (p *point) HasTag(tag []byte) bool {
  1279. if len(p.key) == 0 {
  1280. return false
  1281. }
  1282. var exists bool
  1283. walkTags(p.key, func(key, value []byte) bool {
  1284. if bytes.Equal(tag, key) {
  1285. exists = true
  1286. return false
  1287. }
  1288. return true
  1289. })
  1290. return exists
  1291. }
  1292. func walkTags(buf []byte, fn func(key, value []byte) bool) {
  1293. if len(buf) == 0 {
  1294. return
  1295. }
  1296. pos, name := scanTo(buf, 0, ',')
  1297. // it's an empty key, so there are no tags
  1298. if len(name) == 0 {
  1299. return
  1300. }
  1301. hasEscape := bytes.IndexByte(buf, '\\') != -1
  1302. i := pos + 1
  1303. var key, value []byte
  1304. for {
  1305. if i >= len(buf) {
  1306. break
  1307. }
  1308. i, key = scanTo(buf, i, '=')
  1309. i, value = scanTagValue(buf, i+1)
  1310. if len(value) == 0 {
  1311. continue
  1312. }
  1313. if hasEscape {
  1314. if !fn(unescapeTag(key), unescapeTag(value)) {
  1315. return
  1316. }
  1317. } else {
  1318. if !fn(key, value) {
  1319. return
  1320. }
  1321. }
  1322. i++
  1323. }
  1324. }
  1325. // walkFields walks each field key and value via fn. If fn returns false, the iteration
  1326. // is stopped. The values are the raw byte slices and not the converted types.
  1327. func walkFields(buf []byte, fn func(key, value []byte) bool) error {
  1328. var i int
  1329. var key, val []byte
  1330. for len(buf) > 0 {
  1331. i, key = scanTo(buf, 0, '=')
  1332. if i > len(buf)-2 {
  1333. return fmt.Errorf("invalid value: field-key=%s", key)
  1334. }
  1335. buf = buf[i+1:]
  1336. i, val = scanFieldValue(buf, 0)
  1337. buf = buf[i:]
  1338. if !fn(key, val) {
  1339. break
  1340. }
  1341. // slice off comma
  1342. if len(buf) > 0 {
  1343. buf = buf[1:]
  1344. }
  1345. }
  1346. return nil
  1347. }
  1348. // parseTags parses buf into the provided destination tags, returning destination
  1349. // Tags, which may have a different length and capacity.
  1350. func parseTags(buf []byte, dst Tags) Tags {
  1351. if len(buf) == 0 {
  1352. return nil
  1353. }
  1354. n := bytes.Count(buf, []byte(","))
  1355. if cap(dst) < n {
  1356. dst = make(Tags, n)
  1357. } else {
  1358. dst = dst[:n]
  1359. }
  1360. // Ensure existing behaviour when point has no tags and nil slice passed in.
  1361. if dst == nil {
  1362. dst = Tags{}
  1363. }
  1364. // Series keys can contain escaped commas, therefore the number of commas
  1365. // in a series key only gives an estimation of the upper bound on the number
  1366. // of tags.
  1367. var i int
  1368. walkTags(buf, func(key, value []byte) bool {
  1369. dst[i].Key, dst[i].Value = key, value
  1370. i++
  1371. return true
  1372. })
  1373. return dst[:i]
  1374. }
  1375. // MakeKey creates a key for a set of tags.
  1376. func MakeKey(name []byte, tags Tags) []byte {
  1377. return AppendMakeKey(nil, name, tags)
  1378. }
  1379. // AppendMakeKey appends the key derived from name and tags to dst and returns the extended buffer.
  1380. func AppendMakeKey(dst []byte, name []byte, tags Tags) []byte {
  1381. // unescape the name and then re-escape it to avoid double escaping.
  1382. // The key should always be stored in escaped form.
  1383. dst = append(dst, EscapeMeasurement(UnescapeMeasurement(name))...)
  1384. dst = tags.AppendHashKey(dst)
  1385. return dst
  1386. }
  1387. // SetTags replaces the tags for the point.
  1388. func (p *point) SetTags(tags Tags) {
  1389. p.key = MakeKey(p.Name(), tags)
  1390. p.cachedTags = tags
  1391. }
  1392. // AddTag adds or replaces a tag value for a point.
  1393. func (p *point) AddTag(key, value string) {
  1394. tags := p.Tags()
  1395. tags = append(tags, Tag{Key: []byte(key), Value: []byte(value)})
  1396. sort.Sort(tags)
  1397. p.cachedTags = tags
  1398. p.key = MakeKey(p.Name(), tags)
  1399. }
  1400. // Fields returns the fields for the point.
  1401. func (p *point) Fields() (Fields, error) {
  1402. if p.cachedFields != nil {
  1403. return p.cachedFields, nil
  1404. }
  1405. cf, err := p.unmarshalBinary()
  1406. if err != nil {
  1407. return nil, err
  1408. }
  1409. p.cachedFields = cf
  1410. return p.cachedFields, nil
  1411. }
  1412. // SetPrecision will round a time to the specified precision.
  1413. func (p *point) SetPrecision(precision string) {
  1414. switch precision {
  1415. case "n":
  1416. case "u":
  1417. p.SetTime(p.Time().Truncate(time.Microsecond))
  1418. case "ms":
  1419. p.SetTime(p.Time().Truncate(time.Millisecond))
  1420. case "s":
  1421. p.SetTime(p.Time().Truncate(time.Second))
  1422. case "m":
  1423. p.SetTime(p.Time().Truncate(time.Minute))
  1424. case "h":
  1425. p.SetTime(p.Time().Truncate(time.Hour))
  1426. }
  1427. }
  1428. // String returns the string representation of the point.
  1429. func (p *point) String() string {
  1430. if p.Time().IsZero() {
  1431. return string(p.Key()) + " " + string(p.fields)
  1432. }
  1433. return string(p.Key()) + " " + string(p.fields) + " " + strconv.FormatInt(p.UnixNano(), 10)
  1434. }
  1435. // AppendString appends the string representation of the point to buf.
  1436. func (p *point) AppendString(buf []byte) []byte {
  1437. buf = append(buf, p.key...)
  1438. buf = append(buf, ' ')
  1439. buf = append(buf, p.fields...)
  1440. if !p.time.IsZero() {
  1441. buf = append(buf, ' ')
  1442. buf = strconv.AppendInt(buf, p.UnixNano(), 10)
  1443. }
  1444. return buf
  1445. }
  1446. // StringSize returns the length of the string that would be returned by String().
  1447. func (p *point) StringSize() int {
  1448. size := len(p.key) + len(p.fields) + 1
  1449. if !p.time.IsZero() {
  1450. digits := 1 // even "0" has one digit
  1451. t := p.UnixNano()
  1452. if t < 0 {
  1453. // account for negative sign, then negate
  1454. digits++
  1455. t = -t
  1456. }
  1457. for t > 9 { // already accounted for one digit
  1458. digits++
  1459. t /= 10
  1460. }
  1461. size += digits + 1 // digits and a space
  1462. }
  1463. return size
  1464. }
  1465. // MarshalBinary returns a binary representation of the point.
  1466. func (p *point) MarshalBinary() ([]byte, error) {
  1467. if len(p.fields) == 0 {
  1468. return nil, ErrPointMustHaveAField
  1469. }
  1470. tb, err := p.time.MarshalBinary()
  1471. if err != nil {
  1472. return nil, err
  1473. }
  1474. b := make([]byte, 8+len(p.key)+len(p.fields)+len(tb))
  1475. i := 0
  1476. binary.BigEndian.PutUint32(b[i:], uint32(len(p.key)))
  1477. i += 4
  1478. i += copy(b[i:], p.key)
  1479. binary.BigEndian.PutUint32(b[i:i+4], uint32(len(p.fields)))
  1480. i += 4
  1481. i += copy(b[i:], p.fields)
  1482. copy(b[i:], tb)
  1483. return b, nil
  1484. }
  1485. // UnmarshalBinary decodes a binary representation of the point into a point struct.
  1486. func (p *point) UnmarshalBinary(b []byte) error {
  1487. var n int
  1488. // Read key length.
  1489. if len(b) < 4 {
  1490. return io.ErrShortBuffer
  1491. }
  1492. n, b = int(binary.BigEndian.Uint32(b[:4])), b[4:]
  1493. // Read key.
  1494. if len(b) < n {
  1495. return io.ErrShortBuffer
  1496. }
  1497. p.key, b = b[:n], b[n:]
  1498. // Read fields length.
  1499. if len(b) < 4 {
  1500. return io.ErrShortBuffer
  1501. }
  1502. n, b = int(binary.BigEndian.Uint32(b[:4])), b[4:]
  1503. // Read fields.
  1504. if len(b) < n {
  1505. return io.ErrShortBuffer
  1506. }
  1507. p.fields, b = b[:n], b[n:]
  1508. // Read timestamp.
  1509. return p.time.UnmarshalBinary(b)
  1510. }
  1511. // PrecisionString returns a string representation of the point. If there
  1512. // is a timestamp associated with the point then it will be specified in the
  1513. // given unit.
  1514. func (p *point) PrecisionString(precision string) string {
  1515. if p.Time().IsZero() {
  1516. return fmt.Sprintf("%s %s", p.Key(), string(p.fields))
  1517. }
  1518. return fmt.Sprintf("%s %s %d", p.Key(), string(p.fields),
  1519. p.UnixNano()/GetPrecisionMultiplier(precision))
  1520. }
  1521. // RoundedString returns a string representation of the point. If there
  1522. // is a timestamp associated with the point, then it will be rounded to the
  1523. // given duration.
  1524. func (p *point) RoundedString(d time.Duration) string {
  1525. if p.Time().IsZero() {
  1526. return fmt.Sprintf("%s %s", p.Key(), string(p.fields))
  1527. }
  1528. return fmt.Sprintf("%s %s %d", p.Key(), string(p.fields),
  1529. p.time.Round(d).UnixNano())
  1530. }
  1531. func (p *point) unmarshalBinary() (Fields, error) {
  1532. iter := p.FieldIterator()
  1533. fields := make(Fields, 8)
  1534. for iter.Next() {
  1535. if len(iter.FieldKey()) == 0 {
  1536. continue
  1537. }
  1538. switch iter.Type() {
  1539. case Float:
  1540. v, err := iter.FloatValue()
  1541. if err != nil {
  1542. return nil, fmt.Errorf("unable to unmarshal field %s: %s", string(iter.FieldKey()), err)
  1543. }
  1544. fields[string(iter.FieldKey())] = v
  1545. case Integer:
  1546. v, err := iter.IntegerValue()
  1547. if err != nil {
  1548. return nil, fmt.Errorf("unable to unmarshal field %s: %s", string(iter.FieldKey()), err)
  1549. }
  1550. fields[string(iter.FieldKey())] = v
  1551. case Unsigned:
  1552. v, err := iter.UnsignedValue()
  1553. if err != nil {
  1554. return nil, fmt.Errorf("unable to unmarshal field %s: %s", string(iter.FieldKey()), err)
  1555. }
  1556. fields[string(iter.FieldKey())] = v
  1557. case String:
  1558. fields[string(iter.FieldKey())] = iter.StringValue()
  1559. case Boolean:
  1560. v, err := iter.BooleanValue()
  1561. if err != nil {
  1562. return nil, fmt.Errorf("unable to unmarshal field %s: %s", string(iter.FieldKey()), err)
  1563. }
  1564. fields[string(iter.FieldKey())] = v
  1565. }
  1566. }
  1567. return fields, nil
  1568. }
  1569. // HashID returns a non-cryptographic checksum of the point's key.
  1570. func (p *point) HashID() uint64 {
  1571. h := NewInlineFNV64a()
  1572. h.Write(p.key)
  1573. sum := h.Sum64()
  1574. return sum
  1575. }
  1576. // UnixNano returns the timestamp of the point as nanoseconds since Unix epoch.
  1577. func (p *point) UnixNano() int64 {
  1578. return p.Time().UnixNano()
  1579. }
  1580. // Split will attempt to return multiple points with the same timestamp whose
  1581. // string representations are no longer than size. Points with a single field or
  1582. // a point without a timestamp may exceed the requested size.
  1583. func (p *point) Split(size int) []Point {
  1584. if p.time.IsZero() || p.StringSize() <= size {
  1585. return []Point{p}
  1586. }
  1587. // key string, timestamp string, spaces
  1588. size -= len(p.key) + len(strconv.FormatInt(p.time.UnixNano(), 10)) + 2
  1589. var points []Point
  1590. var start, cur int
  1591. for cur < len(p.fields) {
  1592. end, _ := scanTo(p.fields, cur, '=')
  1593. end, _ = scanFieldValue(p.fields, end+1)
  1594. if cur > start && end-start > size {
  1595. points = append(points, &point{
  1596. key: p.key,
  1597. time: p.time,
  1598. fields: p.fields[start : cur-1],
  1599. })
  1600. start = cur
  1601. }
  1602. cur = end + 1
  1603. }
  1604. points = append(points, &point{
  1605. key: p.key,
  1606. time: p.time,
  1607. fields: p.fields[start:],
  1608. })
  1609. return points
  1610. }
  1611. // Tag represents a single key/value tag pair.
  1612. type Tag struct {
  1613. Key []byte
  1614. Value []byte
  1615. }
  1616. // NewTag returns a new Tag.
  1617. func NewTag(key, value []byte) Tag {
  1618. return Tag{
  1619. Key: key,
  1620. Value: value,
  1621. }
  1622. }
  1623. // Size returns the size of the key and value.
  1624. func (t Tag) Size() int { return len(t.Key) + len(t.Value) }
  1625. // Clone returns a shallow copy of Tag.
  1626. //
  1627. // Tags associated with a Point created by ParsePointsWithPrecision will hold references to the byte slice that was parsed.
  1628. // Use Clone to create a Tag with new byte slices that do not refer to the argument to ParsePointsWithPrecision.
  1629. func (t Tag) Clone() Tag {
  1630. other := Tag{
  1631. Key: make([]byte, len(t.Key)),
  1632. Value: make([]byte, len(t.Value)),
  1633. }
  1634. copy(other.Key, t.Key)
  1635. copy(other.Value, t.Value)
  1636. return other
  1637. }
  1638. // String returns the string reprsentation of the tag.
  1639. func (t *Tag) String() string {
  1640. var buf bytes.Buffer
  1641. buf.WriteByte('{')
  1642. buf.WriteString(string(t.Key))
  1643. buf.WriteByte(' ')
  1644. buf.WriteString(string(t.Value))
  1645. buf.WriteByte('}')
  1646. return buf.String()
  1647. }
  1648. // Tags represents a sorted list of tags.
  1649. type Tags []Tag
  1650. // NewTags returns a new Tags from a map.
  1651. func NewTags(m map[string]string) Tags {
  1652. if len(m) == 0 {
  1653. return nil
  1654. }
  1655. a := make(Tags, 0, len(m))
  1656. for k, v := range m {
  1657. a = append(a, NewTag([]byte(k), []byte(v)))
  1658. }
  1659. sort.Sort(a)
  1660. return a
  1661. }
  1662. // Keys returns the list of keys for a tag set.
  1663. func (a Tags) Keys() []string {
  1664. if len(a) == 0 {
  1665. return nil
  1666. }
  1667. keys := make([]string, len(a))
  1668. for i, tag := range a {
  1669. keys[i] = string(tag.Key)
  1670. }
  1671. return keys
  1672. }
  1673. // Values returns the list of values for a tag set.
  1674. func (a Tags) Values() []string {
  1675. if len(a) == 0 {
  1676. return nil
  1677. }
  1678. values := make([]string, len(a))
  1679. for i, tag := range a {
  1680. values[i] = string(tag.Value)
  1681. }
  1682. return values
  1683. }
  1684. // String returns the string representation of the tags.
  1685. func (a Tags) String() string {
  1686. var buf bytes.Buffer
  1687. buf.WriteByte('[')
  1688. for i := range a {
  1689. buf.WriteString(a[i].String())
  1690. if i < len(a)-1 {
  1691. buf.WriteByte(' ')
  1692. }
  1693. }
  1694. buf.WriteByte(']')
  1695. return buf.String()
  1696. }
  1697. // Size returns the number of bytes needed to store all tags. Note, this is
  1698. // the number of bytes needed to store all keys and values and does not account
  1699. // for data structures or delimiters for example.
  1700. func (a Tags) Size() int {
  1701. var total int
  1702. for i := range a {
  1703. total += a[i].Size()
  1704. }
  1705. return total
  1706. }
  1707. // Clone returns a copy of the slice where the elements are a result of calling `Clone` on the original elements
  1708. //
  1709. // Tags associated with a Point created by ParsePointsWithPrecision will hold references to the byte slice that was parsed.
  1710. // Use Clone to create Tags with new byte slices that do not refer to the argument to ParsePointsWithPrecision.
  1711. func (a Tags) Clone() Tags {
  1712. if len(a) == 0 {
  1713. return nil
  1714. }
  1715. others := make(Tags, len(a))
  1716. for i := range a {
  1717. others[i] = a[i].Clone()
  1718. }
  1719. return others
  1720. }
  1721. func (a Tags) Len() int { return len(a) }
  1722. func (a Tags) Less(i, j int) bool { return bytes.Compare(a[i].Key, a[j].Key) == -1 }
  1723. func (a Tags) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  1724. // Equal returns true if a equals other.
  1725. func (a Tags) Equal(other Tags) bool {
  1726. if len(a) != len(other) {
  1727. return false
  1728. }
  1729. for i := range a {
  1730. if !bytes.Equal(a[i].Key, other[i].Key) || !bytes.Equal(a[i].Value, other[i].Value) {
  1731. return false
  1732. }
  1733. }
  1734. return true
  1735. }
  1736. // CompareTags returns -1 if a < b, 1 if a > b, and 0 if a == b.
  1737. func CompareTags(a, b Tags) int {
  1738. // Compare each key & value until a mismatch.
  1739. for i := 0; i < len(a) && i < len(b); i++ {
  1740. if cmp := bytes.Compare(a[i].Key, b[i].Key); cmp != 0 {
  1741. return cmp
  1742. }
  1743. if cmp := bytes.Compare(a[i].Value, b[i].Value); cmp != 0 {
  1744. return cmp
  1745. }
  1746. }
  1747. // If all tags are equal up to this point then return shorter tagset.
  1748. if len(a) < len(b) {
  1749. return -1
  1750. } else if len(a) > len(b) {
  1751. return 1
  1752. }
  1753. // All tags are equal.
  1754. return 0
  1755. }
  1756. // Get returns the value for a key.
  1757. func (a Tags) Get(key []byte) []byte {
  1758. // OPTIMIZE: Use sort.Search if tagset is large.
  1759. for _, t := range a {
  1760. if bytes.Equal(t.Key, key) {
  1761. return t.Value
  1762. }
  1763. }
  1764. return nil
  1765. }
  1766. // GetString returns the string value for a string key.
  1767. func (a Tags) GetString(key string) string {
  1768. return string(a.Get([]byte(key)))
  1769. }
  1770. // Set sets the value for a key.
  1771. func (a *Tags) Set(key, value []byte) {
  1772. for i, t := range *a {
  1773. if bytes.Equal(t.Key, key) {
  1774. (*a)[i].Value = value
  1775. return
  1776. }
  1777. }
  1778. *a = append(*a, Tag{Key: key, Value: value})
  1779. sort.Sort(*a)
  1780. }
  1781. // SetString sets the string value for a string key.
  1782. func (a *Tags) SetString(key, value string) {
  1783. a.Set([]byte(key), []byte(value))
  1784. }
  1785. // Delete removes a tag by key.
  1786. func (a *Tags) Delete(key []byte) {
  1787. for i, t := range *a {
  1788. if bytes.Equal(t.Key, key) {
  1789. copy((*a)[i:], (*a)[i+1:])
  1790. (*a)[len(*a)-1] = Tag{}
  1791. *a = (*a)[:len(*a)-1]
  1792. return
  1793. }
  1794. }
  1795. }
  1796. // Map returns a map representation of the tags.
  1797. func (a Tags) Map() map[string]string {
  1798. m := make(map[string]string, len(a))
  1799. for _, t := range a {
  1800. m[string(t.Key)] = string(t.Value)
  1801. }
  1802. return m
  1803. }
  1804. // Merge merges the tags combining the two. If both define a tag with the
  1805. // same key, the merged value overwrites the old value.
  1806. // A new map is returned.
  1807. func (a Tags) Merge(other map[string]string) Tags {
  1808. merged := make(map[string]string, len(a)+len(other))
  1809. for _, t := range a {
  1810. merged[string(t.Key)] = string(t.Value)
  1811. }
  1812. for k, v := range other {
  1813. merged[k] = v
  1814. }
  1815. return NewTags(merged)
  1816. }
  1817. // HashKey hashes all of a tag's keys.
  1818. func (a Tags) HashKey() []byte {
  1819. return a.AppendHashKey(nil)
  1820. }
  1821. func (a Tags) needsEscape() bool {
  1822. for i := range a {
  1823. t := &a[i]
  1824. for j := range tagEscapeCodes {
  1825. c := &tagEscapeCodes[j]
  1826. if bytes.IndexByte(t.Key, c.k[0]) != -1 || bytes.IndexByte(t.Value, c.k[0]) != -1 {
  1827. return true
  1828. }
  1829. }
  1830. }
  1831. return false
  1832. }
  1833. // AppendHashKey appends the result of hashing all of a tag's keys and values to dst and returns the extended buffer.
  1834. func (a Tags) AppendHashKey(dst []byte) []byte {
  1835. // Empty maps marshal to empty bytes.
  1836. if len(a) == 0 {
  1837. return dst
  1838. }
  1839. // Type invariant: Tags are sorted
  1840. sz := 0
  1841. var escaped Tags
  1842. if a.needsEscape() {
  1843. var tmp [20]Tag
  1844. if len(a) < len(tmp) {
  1845. escaped = tmp[:len(a)]
  1846. } else {
  1847. escaped = make(Tags, len(a))
  1848. }
  1849. for i := range a {
  1850. t := &a[i]
  1851. nt := &escaped[i]
  1852. nt.Key = escapeTag(t.Key)
  1853. nt.Value = escapeTag(t.Value)
  1854. sz += len(nt.Key) + len(nt.Value)
  1855. }
  1856. } else {
  1857. sz = a.Size()
  1858. escaped = a
  1859. }
  1860. sz += len(escaped) + (len(escaped) * 2) // separators
  1861. // Generate marshaled bytes.
  1862. if cap(dst)-len(dst) < sz {
  1863. nd := make([]byte, len(dst), len(dst)+sz)
  1864. copy(nd, dst)
  1865. dst = nd
  1866. }
  1867. buf := dst[len(dst) : len(dst)+sz]
  1868. idx := 0
  1869. for i := range escaped {
  1870. k := &escaped[i]
  1871. if len(k.Value) == 0 {
  1872. continue
  1873. }
  1874. buf[idx] = ','
  1875. idx++
  1876. copy(buf[idx:], k.Key)
  1877. idx += len(k.Key)
  1878. buf[idx] = '='
  1879. idx++
  1880. copy(buf[idx:], k.Value)
  1881. idx += len(k.Value)
  1882. }
  1883. return dst[:len(dst)+idx]
  1884. }
  1885. // CopyTags returns a shallow copy of tags.
  1886. func CopyTags(a Tags) Tags {
  1887. other := make(Tags, len(a))
  1888. copy(other, a)
  1889. return other
  1890. }
  1891. // DeepCopyTags returns a deep copy of tags.
  1892. func DeepCopyTags(a Tags) Tags {
  1893. // Calculate size of keys/values in bytes.
  1894. var n int
  1895. for _, t := range a {
  1896. n += len(t.Key) + len(t.Value)
  1897. }
  1898. // Build single allocation for all key/values.
  1899. buf := make([]byte, n)
  1900. // Copy tags to new set.
  1901. other := make(Tags, len(a))
  1902. for i, t := range a {
  1903. copy(buf, t.Key)
  1904. other[i].Key, buf = buf[:len(t.Key)], buf[len(t.Key):]
  1905. copy(buf, t.Value)
  1906. other[i].Value, buf = buf[:len(t.Value)], buf[len(t.Value):]
  1907. }
  1908. return other
  1909. }
  1910. // Fields represents a mapping between a Point's field names and their
  1911. // values.
  1912. type Fields map[string]interface{}
  1913. // FieldIterator retuns a FieldIterator that can be used to traverse the
  1914. // fields of a point without constructing the in-memory map.
  1915. func (p *point) FieldIterator() FieldIterator {
  1916. p.Reset()
  1917. return p
  1918. }
  1919. type fieldIterator struct {
  1920. start, end int
  1921. key, keybuf []byte
  1922. valueBuf []byte
  1923. fieldType FieldType
  1924. }
  1925. // Next indicates whether there any fields remaining.
  1926. func (p *point) Next() bool {
  1927. p.it.start = p.it.end
  1928. if p.it.start >= len(p.fields) {
  1929. return false
  1930. }
  1931. p.it.end, p.it.key = scanTo(p.fields, p.it.start, '=')
  1932. if escape.IsEscaped(p.it.key) {
  1933. p.it.keybuf = escape.AppendUnescaped(p.it.keybuf[:0], p.it.key)
  1934. p.it.key = p.it.keybuf
  1935. }
  1936. p.it.end, p.it.valueBuf = scanFieldValue(p.fields, p.it.end+1)
  1937. p.it.end++
  1938. if len(p.it.valueBuf) == 0 {
  1939. p.it.fieldType = Empty
  1940. return true
  1941. }
  1942. c := p.it.valueBuf[0]
  1943. if c == '"' {
  1944. p.it.fieldType = String
  1945. return true
  1946. }
  1947. if strings.IndexByte(`0123456789-.nNiIu`, c) >= 0 {
  1948. if p.it.valueBuf[len(p.it.valueBuf)-1] == 'i' {
  1949. p.it.fieldType = Integer
  1950. p.it.valueBuf = p.it.valueBuf[:len(p.it.valueBuf)-1]
  1951. } else if p.it.valueBuf[len(p.it.valueBuf)-1] == 'u' {
  1952. p.it.fieldType = Unsigned
  1953. p.it.valueBuf = p.it.valueBuf[:len(p.it.valueBuf)-1]
  1954. } else {
  1955. p.it.fieldType = Float
  1956. }
  1957. return true
  1958. }
  1959. // to keep the same behavior that currently exists, default to boolean
  1960. p.it.fieldType = Boolean
  1961. return true
  1962. }
  1963. // FieldKey returns the key of the current field.
  1964. func (p *point) FieldKey() []byte {
  1965. return p.it.key
  1966. }
  1967. // Type returns the FieldType of the current field.
  1968. func (p *point) Type() FieldType {
  1969. return p.it.fieldType
  1970. }
  1971. // StringValue returns the string value of the current field.
  1972. func (p *point) StringValue() string {
  1973. return unescapeStringField(string(p.it.valueBuf[1 : len(p.it.valueBuf)-1]))
  1974. }
  1975. // IntegerValue returns the integer value of the current field.
  1976. func (p *point) IntegerValue() (int64, error) {
  1977. n, err := parseIntBytes(p.it.valueBuf, 10, 64)
  1978. if err != nil {
  1979. return 0, fmt.Errorf("unable to parse integer value %q: %v", p.it.valueBuf, err)
  1980. }
  1981. return n, nil
  1982. }
  1983. // UnsignedValue returns the unsigned value of the current field.
  1984. func (p *point) UnsignedValue() (uint64, error) {
  1985. n, err := parseUintBytes(p.it.valueBuf, 10, 64)
  1986. if err != nil {
  1987. return 0, fmt.Errorf("unable to parse unsigned value %q: %v", p.it.valueBuf, err)
  1988. }
  1989. return n, nil
  1990. }
  1991. // BooleanValue returns the boolean value of the current field.
  1992. func (p *point) BooleanValue() (bool, error) {
  1993. b, err := parseBoolBytes(p.it.valueBuf)
  1994. if err != nil {
  1995. return false, fmt.Errorf("unable to parse bool value %q: %v", p.it.valueBuf, err)
  1996. }
  1997. return b, nil
  1998. }
  1999. // FloatValue returns the float value of the current field.
  2000. func (p *point) FloatValue() (float64, error) {
  2001. f, err := parseFloatBytes(p.it.valueBuf, 64)
  2002. if err != nil {
  2003. return 0, fmt.Errorf("unable to parse floating point value %q: %v", p.it.valueBuf, err)
  2004. }
  2005. return f, nil
  2006. }
  2007. // Reset resets the iterator to its initial state.
  2008. func (p *point) Reset() {
  2009. p.it.fieldType = Empty
  2010. p.it.key = nil
  2011. p.it.valueBuf = nil
  2012. p.it.start = 0
  2013. p.it.end = 0
  2014. }
  2015. // MarshalBinary encodes all the fields to their proper type and returns the binary
  2016. // represenation
  2017. // NOTE: uint64 is specifically not supported due to potential overflow when we decode
  2018. // again later to an int64
  2019. // NOTE2: uint is accepted, and may be 64 bits, and is for some reason accepted...
  2020. func (p Fields) MarshalBinary() []byte {
  2021. var b []byte
  2022. keys := make([]string, 0, len(p))
  2023. for k := range p {
  2024. keys = append(keys, k)
  2025. }
  2026. // Not really necessary, can probably be removed.
  2027. sort.Strings(keys)
  2028. for i, k := range keys {
  2029. if i > 0 {
  2030. b = append(b, ',')
  2031. }
  2032. b = appendField(b, k, p[k])
  2033. }
  2034. return b
  2035. }
  2036. func appendField(b []byte, k string, v interface{}) []byte {
  2037. b = append(b, []byte(escape.String(k))...)
  2038. b = append(b, '=')
  2039. // check popular types first
  2040. switch v := v.(type) {
  2041. case float64:
  2042. b = strconv.AppendFloat(b, v, 'f', -1, 64)
  2043. case int64:
  2044. b = strconv.AppendInt(b, v, 10)
  2045. b = append(b, 'i')
  2046. case string:
  2047. b = append(b, '"')
  2048. b = append(b, []byte(EscapeStringField(v))...)
  2049. b = append(b, '"')
  2050. case bool:
  2051. b = strconv.AppendBool(b, v)
  2052. case int32:
  2053. b = strconv.AppendInt(b, int64(v), 10)
  2054. b = append(b, 'i')
  2055. case int16:
  2056. b = strconv.AppendInt(b, int64(v), 10)
  2057. b = append(b, 'i')
  2058. case int8:
  2059. b = strconv.AppendInt(b, int64(v), 10)
  2060. b = append(b, 'i')
  2061. case int:
  2062. b = strconv.AppendInt(b, int64(v), 10)
  2063. b = append(b, 'i')
  2064. case uint64:
  2065. b = strconv.AppendUint(b, v, 10)
  2066. b = append(b, 'u')
  2067. case uint32:
  2068. b = strconv.AppendInt(b, int64(v), 10)
  2069. b = append(b, 'i')
  2070. case uint16:
  2071. b = strconv.AppendInt(b, int64(v), 10)
  2072. b = append(b, 'i')
  2073. case uint8:
  2074. b = strconv.AppendInt(b, int64(v), 10)
  2075. b = append(b, 'i')
  2076. case uint:
  2077. // TODO: 'uint' should be converted to writing as an unsigned integer,
  2078. // but we cannot since that would break backwards compatibility.
  2079. b = strconv.AppendInt(b, int64(v), 10)
  2080. b = append(b, 'i')
  2081. case float32:
  2082. b = strconv.AppendFloat(b, float64(v), 'f', -1, 32)
  2083. case []byte:
  2084. b = append(b, v...)
  2085. case nil:
  2086. // skip
  2087. default:
  2088. // Can't determine the type, so convert to string
  2089. b = append(b, '"')
  2090. b = append(b, []byte(EscapeStringField(fmt.Sprintf("%v", v)))...)
  2091. b = append(b, '"')
  2092. }
  2093. return b
  2094. }
  2095. // ValidKeyToken returns true if the token used for measurement, tag key, or tag
  2096. // value is a valid unicode string and only contains printable, non-replacement characters.
  2097. func ValidKeyToken(s string) bool {
  2098. if !utf8.ValidString(s) {
  2099. return false
  2100. }
  2101. for _, r := range s {
  2102. if !unicode.IsPrint(r) || r == unicode.ReplacementChar {
  2103. return false
  2104. }
  2105. }
  2106. return true
  2107. }
  2108. // ValidKeyTokens returns true if the measurement name and all tags are valid.
  2109. func ValidKeyTokens(name string, tags Tags) bool {
  2110. if !ValidKeyToken(name) {
  2111. return false
  2112. }
  2113. for _, tag := range tags {
  2114. if !ValidKeyToken(string(tag.Key)) || !ValidKeyToken(string(tag.Value)) {
  2115. return false
  2116. }
  2117. }
  2118. return true
  2119. }