-
Notifications
You must be signed in to change notification settings - Fork 23
/
store_clickhouse.go
298 lines (274 loc) · 8.25 KB
/
store_clickhouse.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"database/sql"
"fmt"
"net/url"
"time"
_ "github.com/ClickHouse/clickhouse-go"
"github.com/jmoiron/sqlx"
)
var chschema = []string{
`
CREATE TABLE IF NOT EXISTS tuples (
whatever Date DEFAULT '2000-01-01',
query String,
type String,
answer String,
ttl AggregateFunction(anyLast, UInt16),
first AggregateFunction(min, DateTime),
last AggregateFunction(max, DateTime),
count AggregateFunction(sum, UInt64)
) ENGINE = AggregatingMergeTree(whatever, (query, type, answer), 8192);
`,
`
CREATE TABLE IF NOT EXISTS individual (
whatever Date DEFAULT '2000-01-01',
which Enum8('Q'=0, 'A'=1),
value String,
first AggregateFunction(min, DateTime),
last AggregateFunction(max, DateTime),
count AggregateFunction(sum, UInt64)
) ENGINE = AggregatingMergeTree(whatever, (which, value), 8192);
`,
`
CREATE TABLE IF NOT EXISTS filenames (
day Date DEFAULT toDate(ts),
ts DateTime DEFAULT now(),
filename String,
aggregation_time Float64,
total_records UInt64,
skipped_records UInt64,
tuples UInt64,
individual UInt64,
store_time Float64,
inserted UInt64,
updated UInt64
) ENGINE = MergeTree(day, (filename), 8192);
`}
const tuples_temp_stmt = `
CREATE TEMPORARY TABLE tuples_temp (
query String,
type String,
answer String,
ttl String,
first String,
last String,
count UInt64
) ENGINE = Memory`
const individual_temp_stmt = `
CREATE TEMPORARY TABLE individual_temp (
which Enum8('Q'=0, 'A'=1),
value String,
first String,
last String,
count UInt64
) ENGINE = Memory`
type CHStore struct {
conn *sqlx.DB
}
func NewCHStore(uri string) (Store, error) {
_, err := url.Parse(uri)
if err != nil {
return nil, err
}
conn, err := sqlx.Open("clickhouse", uri)
if err != nil {
return nil, err
}
err = conn.Ping()
if err != nil {
return nil, err
}
return &CHStore{
conn: conn,
}, nil
}
func (s *CHStore) Close() error {
return s.Close()
}
func (s *CHStore) Exec(stmt string) error {
_, err := s.conn.Exec(stmt)
return err
}
func (s *CHStore) Init() error {
for _, stmt := range chschema {
err := s.Exec(stmt)
if err != nil {
return err
}
}
return nil
}
func (s *CHStore) Clear() error {
stmts := []string{
"drop table filenames",
"drop table individual",
"drop table tuples",
}
for _, stmt := range stmts {
err := s.Exec(stmt)
if err != nil {
return err
}
}
return nil
}
func (s *CHStore) Begin() error {
return fmt.Errorf("clickhouse doesn't support transactions")
}
func (s *CHStore) Commit() error {
//log.Printf("clickhouse doesn't support transactions")
return nil
}
//DeleteOld Deletes records that haven't been seen in DAYS, returns the total records deleted
func (s *CHStore) DeleteOld(days int64) (int64, error) {
return 0, fmt.Errorf("clickhouse doesn't support delete")
}
func (s *CHStore) Update(ar aggregationResult) (UpdateResult, error) {
var result UpdateResult
var err error
start := time.Now()
s.Exec("DROP TABLE tuples_temp")
s.Exec("DROP TABLE individual_temp")
err = s.Exec(tuples_temp_stmt)
if err != nil {
return result, fmt.Errorf("CHStore.Update failed: %w", err)
}
err = s.Exec(individual_temp_stmt)
if err != nil {
return result, fmt.Errorf("CHStore.Update failed: %w", err)
}
tx, err := s.conn.Begin()
if err != nil {
return result, fmt.Errorf("CHStore.Update failed: %w", err)
}
stmt, err := tx.Prepare(`INSERT INTO tuples_temp
(query, type, answer, ttl, first, last, count)
values (?,?,?,?,?,?,?)`,
)
if err != nil {
return result, fmt.Errorf("CHStore.Update failed: %w", err)
}
// Ok, now let's update stuff
// tuples
for _, q := range ar.Tuples {
//Update the tuples table
query := Reverse(q.query)
_, err := stmt.Exec(query, q.qtype, q.answer, q.ttl, ToTS(q.first), ToTS(q.last), uint64(q.count))
if err != nil {
return result, fmt.Errorf("CHStore.Update failed to run query: %w", err)
}
}
err = tx.Commit()
if err != nil {
return result, fmt.Errorf("CHStore.Update failed: %w", err)
}
err = s.Exec(`INSERT INTO tuples (query, type, answer, ttl, first, last, count) SELECT
query, type, answer,
anyLastState(toUInt16(ttl)),
minState(toDateTime(toFloat64(first))),
maxState(toDateTime(toFloat64(last))),
sumState(count) from tuples_temp group by query, type, answer`,
)
if err != nil {
return result, fmt.Errorf("CHStore.Update failed: %w", err)
}
tx, err = s.conn.Begin()
if err != nil {
return result, fmt.Errorf("CHStore.Update failed: %w", err)
}
// Individuals
stmt, err = tx.Prepare(`INSERT INTO individual_temp
(value, which, first, last, count)
values (?,?,?,?,?)`,
)
if err != nil {
return result, fmt.Errorf("CHStore.Update failed: %w", err)
}
for _, q := range ar.Individual {
//Update the tuples table
value := q.value
if q.which == "Q" {
value = Reverse(value)
}
_, err := stmt.Exec(value, q.which, ToTS(q.first), ToTS(q.last), uint64(q.count))
if err != nil {
return result, fmt.Errorf("CHStore.Update failed to run query: %w", err)
}
}
err = tx.Commit()
if err != nil {
return result, fmt.Errorf("CHStore.Update failed: %w", err)
}
err = s.Exec(`INSERT INTO individual (which, value, first, last, count) SELECT which, value,
minState(toDateTime(toFloat64(first))),
maxState(toDateTime(toFloat64(last))),
sumState(count) from individual_temp group by which, value`)
if err != nil {
return result, fmt.Errorf("CHStore.Update failed: %w", err)
}
result.Updated = uint(ar.TuplesLen + ar.IndividualLen)
result.Duration = time.Since(start)
return result, nil
}
func (s *CHStore) IsLogIndexed(filename string) (bool, error) {
var fn string
err := s.conn.QueryRow("SELECT filename FROM filenames WHERE filename=?", filename).Scan(&fn)
switch {
case err == sql.ErrNoRows:
return false, nil
case err != nil:
return false, err
default:
return true, nil
}
}
func (s *CHStore) SetLogIndexed(filename string, ar aggregationResult, ur UpdateResult) error {
tx, _ := s.conn.Begin()
q := `INSERT INTO filenames (filename,
aggregation_time, total_records, skipped_records, tuples, individual,
store_time, inserted, updated)
VALUES (?,?,?,?,?,?,?,?,?)`
_, err := tx.Exec(q, filename,
ar.Duration.Seconds(), uint64(ar.TotalRecords), uint64(ar.SkippedRecords), len(ar.Tuples), len(ar.Individual),
ur.Duration.Seconds(), uint64(ur.Inserted), uint64(ur.Updated))
if err != nil {
return err
}
return tx.Commit()
}
func (s *CHStore) FindQueryTuples(query string) (tupleResults, error) {
tr := []tupleResult{}
query = Reverse(query)
err := s.conn.Select(&tr, "SELECT * FROM tuples WHERE query = ?", query)
reverseQuery(tr)
return tr, err
}
func (s *CHStore) FindTuples(query string) (tupleResults, error) {
tr := []tupleResult{}
rquery := Reverse(query)
err := s.conn.Select(&tr, "SELECT query, type, answer, anyLastMerge(ttl) as ttl, minMerge(first) as first, maxMerge(last) as last, sumMerge(count) as count from tuples WHERE query = ? OR answer = ? group by query, type, answer ORDER BY query, answer", rquery, query)
reverseQuery(tr)
return tr, err
}
func (s *CHStore) LikeTuples(query string) (tupleResults, error) {
tr := []tupleResult{}
rquery := Reverse(query)
err := s.conn.Select(&tr, "SELECT query, type, answer, anyLastMerge(ttl) as ttl, minMerge(first) as first, maxMerge(last) as last, sumMerge(count) as count from tuples WHERE query like ? OR answer like ? group by query, type, answer ORDER BY query, answer", rquery+"%", query+"%")
reverseQuery(tr)
return tr, err
}
func (s *CHStore) FindIndividual(value string) (individualResults, error) {
rvalue := Reverse(value)
tr := []individualResult{}
err := s.conn.Select(&tr, `SELECT which, value, minMerge(first) as first, maxMerge(last) as last, sumMerge(count) as count from individual WHERE (which='A' AND value = ?) OR (which='Q' AND value = ?) group by which, value ORDER BY value`, value, rvalue)
reverseValue(tr)
return tr, err
}
func (s *CHStore) LikeIndividual(value string) (individualResults, error) {
rvalue := Reverse(value)
tr := []individualResult{}
err := s.conn.Select(&tr, `SELECT which, value, minMerge(first) as first, maxMerge(last) as last, sumMerge(count) as count from individual WHERE (which='A' AND value like ?) OR (which='Q' AND value like ?) group by which, value ORDER BY value`, value+"%", rvalue+"%")
reverseValue(tr)
return tr, err
}