Debian Codegraph @

cmd/influx-tools/internal/format/writer.go

raw symbols SCIP
1 package format
2
3 import (
4 "fmt"
5
6 "github.com/influxdata/influxdb/cmd/influx-tools/internal/storage"
7 "github.com/influxdata/influxdb/models"
8 "github.com/influxdata/influxdb/tsdb"
9 "github.com/influxdata/influxql"
10 )
11
12 var (
13 // Discard is a Writer where all write calls succeed. The source data is also read completely, which can be useful
14 // for testing performance.
15 Discard Writer = &devNull{true}
16
17 // DevNull is a Writer where all write calls succeed, however, no source data is read.
18 DevNull Writer = &devNull{}
19 )
20
21 type Writer interface {
22 NewBucket(start, end int64) (BucketWriter, error)
23 Close() error
24 }
25
26 type BucketWriter interface {
27 Err() error
28 BeginSeries(name, field []byte, typ influxql.DataType, tags models.Tags)
29 EndSeries()
30
31 WriteIntegerCursor(cur tsdb.IntegerBatchCursor)
32 WriteFloatCursor(cur tsdb.FloatBatchCursor)
33 WriteUnsignedCursor(cur tsdb.UnsignedBatchCursor)
34 WriteBooleanCursor(cur tsdb.BooleanBatchCursor)
35 WriteStringCursor(cur tsdb.StringBatchCursor)
36 Close() error
37 }
38
39 // WriteBucket reads data from rs covering the time range [start, end) and streams to w.
40 // The ResultSet must guarantee series+field keys are produced in ascending lexicographical order and values in
41 // ascending time order.
42 func WriteBucket(w Writer, start, end int64, rs *storage.ResultSet) error {
43 bw, err := w.NewBucket(start, end)
44 if err != nil {
45 return err
46 }
47 defer bw.Close()
48
49 for rs.Next() {
50 bw.BeginSeries(rs.Name(), rs.Field(), rs.FieldType(), rs.Tags())
51
52 ci := rs.CursorIterator()
53 for ci.Next() {
54 cur := ci.Cursor()
55 switch c := cur.(type) {
56 case tsdb.IntegerBatchCursor:
57 bw.WriteIntegerCursor(c)
58 case tsdb.FloatBatchCursor:
59 bw.WriteFloatCursor(c)
60 case tsdb.UnsignedBatchCursor:
61 bw.WriteUnsignedCursor(c)
62 case tsdb.BooleanBatchCursor:
63 bw.WriteBooleanCursor(c)
64 case tsdb.StringBatchCursor:
65 bw.WriteStringCursor(c)
66 case nil:
67 // no data for series key + field combination in this shard
68 continue
69 default:
70 panic(fmt.Sprintf("unreachable: %T", c))
71
72 }
73 cur.Close()
74 }
75
76 bw.EndSeries()
77
78 if bw.Err() != nil {
79 return bw.Err()
80 }
81 }
82 return nil
83 }
84
85 type devNull struct {
86 r bool
87 }
88
89 func (w *devNull) NewBucket(start, end int64) (BucketWriter, error) {
90 return w, nil
91 }
92 func (w *devNull) BeginSeries(name, field []byte, typ influxql.DataType, tags models.Tags) {}
93 func (w *devNull) EndSeries() {}
94
95 func (w *devNull) Err() error { return nil }
96 func (w *devNull) Close() error { return nil }
97
98 func (w *devNull) WriteIntegerCursor(cur tsdb.IntegerBatchCursor) {
99 if !w.r {
100 return
101 }
102 for {
103 ts, _ := cur.Next()
104 if len(ts) == 0 {
105 break
106 }
107 }
108 }
109
110 func (w *devNull) WriteFloatCursor(cur tsdb.FloatBatchCursor) {
111 if !w.r {
112 return
113 }
114 for {
115 ts, _ := cur.Next()
116 if len(ts) == 0 {
117 break
118 }
119 }
120 }
121
122 func (w *devNull) WriteUnsignedCursor(cur tsdb.UnsignedBatchCursor) {
123 if !w.r {
124 return
125 }
126 for {
127 ts, _ := cur.Next()
128 if len(ts) == 0 {
129 break
130 }
131 }
132 }
133
134 func (w *devNull) WriteBooleanCursor(cur tsdb.BooleanBatchCursor) {
135 if !w.r {
136 return
137 }
138 for {
139 ts, _ := cur.Next()
140 if len(ts) == 0 {
141 break
142 }
143 }
144 }
145
146 func (w *devNull) WriteStringCursor(cur tsdb.StringBatchCursor) {
147 if !w.r {
148 return
149 }
150 for {
151 ts, _ := cur.Next()
152 if len(ts) == 0 {
153 break
154 }
155 }
156 }