-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsqlite.go
362 lines (301 loc) · 6.18 KB
/
sqlite.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package tracing
import (
"database/sql"
"fmt"
"os"
// Need to use SQLite connections.
_ "github.com/mattn/go-sqlite3"
"github.com/rs/xid"
"github.com/tebeka/atexit"
)
// SQLiteTraceWriter is a writer that writes trace data to a SQLite database.
type SQLiteTraceWriter struct {
*sql.DB
statement *sql.Stmt
dbName string
tasksToWriteToDB []Task
batchSize int
}
// NewSQLiteTraceWriter creates a new SQLiteWriter.
func NewSQLiteTraceWriter(path string) *SQLiteTraceWriter {
w := &SQLiteTraceWriter{
dbName: path,
batchSize: 100000,
}
atexit.Register(func() { w.Flush() })
return w
}
// Init establishes a connection to the database.
func (t *SQLiteTraceWriter) Init() {
t.createDatabase()
t.prepareStatement()
}
// Write writes a task to the database.
func (t *SQLiteTraceWriter) Write(task Task) {
t.tasksToWriteToDB = append(t.tasksToWriteToDB, task)
if len(t.tasksToWriteToDB) >= t.batchSize {
t.Flush()
}
}
// Flush writes all the buffered tasks to the database.
func (t *SQLiteTraceWriter) Flush() {
if len(t.tasksToWriteToDB) == 0 {
return
}
t.mustExecute("BEGIN TRANSACTION")
defer t.mustExecute("COMMIT TRANSACTION")
for _, task := range t.tasksToWriteToDB {
// fmt.Println("inserting: ", task.ID)
_, err := t.statement.Exec(
task.ID,
task.ParentID,
task.Kind,
task.What,
task.Where,
task.StartTime,
task.EndTime,
)
if err != nil {
fmt.Println(task)
panic(err)
}
}
t.tasksToWriteToDB = nil
}
func (t *SQLiteTraceWriter) createDatabase() {
if t.dbName == "" {
t.dbName = "akita_trace_" + xid.New().String()
}
filename := t.dbName + ".sqlite3"
_, err := os.Stat(filename)
if err == nil {
panic(fmt.Errorf("file %s already exists", filename))
}
fmt.Fprintf(os.Stderr, "Trace is Collected in Database: %s\n", filename)
db, err := sql.Open("sqlite3", filename)
if err != nil {
panic(err)
}
t.DB = db
t.createTable()
}
func (t *SQLiteTraceWriter) createTable() {
t.mustExecute(`
create table trace
(
task_id varchar(200) null unique,
parent_id varchar(200) null,
kind varchar(100) null,
what varchar(100) null,
location varchar(100) null,
start_time float null,
end_time float null
);
`)
t.mustExecute(`
create index trace_end_time_index
on trace (end_time);
`)
t.mustExecute(`
create index trace_task_id_uindex
on trace (task_id);
`)
t.mustExecute(`
create index trace_kind_index
on trace (kind);
`)
t.mustExecute(`
create index trace_start_time_index
on trace (start_time);
`)
t.mustExecute(`
create index trace_what_index
on trace (what);
`)
t.mustExecute(`
create index trace_location_index
on trace (location);
`)
t.mustExecute(`
create index trace_parent_id_index
on trace (parent_id);
`)
}
func (t *SQLiteTraceWriter) prepareStatement() {
sqlStr := `INSERT INTO trace VALUES (?, ?, ?, ?, ?, ?, ?)`
stmt, err := t.Prepare(sqlStr)
if err != nil {
panic(err)
}
t.statement = stmt
}
func (t *SQLiteTraceWriter) mustExecute(query string) sql.Result {
res, err := t.Exec(query)
if err != nil {
fmt.Printf("Failed to execute: %s\n", query)
panic(err)
}
return res
}
// SQLiteTraceReader is a reader that reads trace data from a SQLite database.
type SQLiteTraceReader struct {
*sql.DB
filename string
}
// NewSQLiteTraceReader creates a new SQLiteTraceReader.
func NewSQLiteTraceReader(filename string) *SQLiteTraceReader {
r := &SQLiteTraceReader{
filename: filename,
}
return r
}
// Init establishes a connection to the database.
func (r *SQLiteTraceReader) Init() {
db, err := sql.Open("sqlite3", r.filename)
if err != nil {
panic(err)
}
r.DB = db
}
// ListComponents returns a list of components in the trace.
func (r *SQLiteTraceReader) ListComponents() []string {
var components []string
rows, err := r.Query("SELECT DISTINCT location FROM trace")
if err != nil {
panic(err)
}
defer func() {
err := rows.Close()
if err != nil {
panic(err)
}
}()
for rows.Next() {
var component string
err := rows.Scan(&component)
if err != nil {
panic(err)
}
components = append(components, component)
}
return components
}
// ListTasks returns a list of tasks in the trace according to the given query.
func (r *SQLiteTraceReader) ListTasks(query TaskQuery) []Task {
sqlStr := r.prepareTaskQueryStr(query)
rows, err := r.Query(sqlStr)
if err != nil {
panic(err)
}
tasks := []Task{}
for rows.Next() {
t := Task{}
pt := Task{}
if query.EnableParentTask {
t.ParentTask = &pt
err := rows.Scan(
&t.ID,
&t.ParentID,
&t.Kind,
&t.What,
&t.Where,
&t.StartTime,
&t.EndTime,
&pt.ID,
&pt.ParentID,
&pt.Kind,
&pt.What,
&pt.Where,
&pt.StartTime,
&pt.EndTime,
)
if err != nil {
panic(err)
}
} else {
err := rows.Scan(
&t.ID,
&t.ParentID,
&t.Kind,
&t.What,
&t.Where,
&t.StartTime,
&t.EndTime,
)
if err != nil {
panic(err)
}
}
tasks = append(tasks, t)
}
return tasks
}
func (r *SQLiteTraceReader) prepareTaskQueryStr(query TaskQuery) string {
sqlStr := `
SELECT
t.task_id,
t.parent_id,
t.kind,
t.what,
t.location,
t.start_time,
t.end_time
`
if query.EnableParentTask {
sqlStr += `,
pt.task_id,
pt.parent_id,
pt.kind,
pt.what,
pt.location,
pt.start_time,
pt.end_time
`
}
sqlStr += `
FROM trace t
`
if query.EnableParentTask {
sqlStr += `
LEFT JOIN trace pt
ON t.parent_id = pt.task_id
`
}
sqlStr = r.addQueryConditionsToQueryStr(sqlStr, query)
return sqlStr
}
func (*SQLiteTraceReader) addQueryConditionsToQueryStr(
sqlStr string,
query TaskQuery,
) string {
sqlStr += `
WHERE 1=1
`
if query.ID != "" {
sqlStr += `
AND t.task_id = '` + query.ID + `'
`
}
if query.ParentID != "" {
sqlStr += `
AND t.parent_id = '` + query.ParentID + `'
`
}
if query.Kind != "" {
sqlStr += `
AND t.kind = '` + query.Kind + `'
`
}
if query.Where != "" {
sqlStr += `
AND t.location = '` + query.Where + `'
`
}
if query.EnableTimeRange {
sqlStr += fmt.Sprintf(
"AND t.end_time > %.15f AND t.start_time < %.15f",
query.StartTime,
query.EndTime)
}
return sqlStr
}