forked from timescale/tsbs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
batch.go
46 lines (37 loc) · 980 Bytes
/
batch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package victoriametrics
import (
"bytes"
"github.com/benchant/tsbs/pkg/data"
"log"
)
const errNotThreeTuplesFmt = "parse error: line does not have 3 tuples, has %d"
var (
spaceSep = []byte(" ")
commaSep = []byte(",")
newLine = []byte("\n")
)
type batch struct {
buf *bytes.Buffer
rows uint64
metrics uint64
}
func (b *batch) Len() uint {
return uint(b.rows)
}
func (b *batch) Append(item data.LoadedPoint) {
that := item.Data.([]byte)
b.rows++
// Each influx line is format "csv-tags csv-fields timestamp"
if args := bytes.Count(that, spaceSep); args != 2 {
log.Fatalf(errNotThreeTuplesFmt, args+1)
return
}
// seek for fields position in slice
fieldsPos := bytes.Index(that, spaceSep)
// seek for timestamps position in slice
timestampPos := bytes.Index(that[fieldsPos+1:], spaceSep) + fieldsPos
fields := that[fieldsPos+1 : timestampPos]
b.metrics += uint64(bytes.Count(fields, commaSep) + 1)
b.buf.Write(that)
b.buf.Write(newLine)
}