-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite3_adapter.go
542 lines (520 loc) · 17 KB
/
sqlite3_adapter.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
Package sqlite3adapter provides an implementation of the
Adapter interface in the sqlset package that works
over a SQLite3 database.
*/
package sqlite3adapter
import (
"bytes"
"context"
"database/sql"
"fmt"
"strings"
"github.com/pbanos/botanic/set/sqlset"
// Import of sqlite3 driver
_ "github.com/mattn/go-sqlite3"
)
const (
discreteValueTableCreateStmt = `CREATE TABLE IF NOT EXISTS discreteValues (
id INTEGER PRIMARY KEY AUTOINCREMENT,
value TEXT UNIQUE NOT NULL)`
// MaxDiscreteValueInsertionsPerStatement is the maximum number
// of discrete values that are allowed to be added with a single
// insert command with the AddDiscreteValues method of the adapter.
// Trying to add more will result in making more insertion commands
MaxDiscreteValueInsertionsPerStatement = 10
// MaxSampleInsertionsPerStatement is the maximum number
// of samples that are allowed to be added with a single
// insert command with the AddSamples method of the adapter.
// Trying to add more will result in making more insertion commands
MaxSampleInsertionsPerStatement = 10
)
type adapter struct {
db *sql.DB
}
/*
New takes a path to an SQLite3 database file and a maxConn integer and returns
an Adapter that works on the file's database or an error if it fails to open as
an sqlite3 database.
If the given maxConn is greater than 0, it will be the maximum concurrent
connections to the database that will be used.
This limit is useful when the OS limits the number of files a process
can open, which is the case for Mac OS X.
*/
func New(path string, maxConn int) (sqlset.Adapter, error) {
db, err := sql.Open("sqlite3", path)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(maxConn)
return &adapter{db}, nil
}
func (a *adapter) ColumnName(featureName string) (string, error) {
if featureName == "id" {
return "", fmt.Errorf(`'%s' is reserved and cannot be used as feature name`, featureName)
}
if strings.ContainsAny(featureName, `"`) {
return "", fmt.Errorf(`feature name '%s' contains invalid character '"'`, featureName)
}
return featureName, nil
}
func (a *adapter) CreateDiscreteValuesTable(ctx context.Context) error {
createStmt, err := a.db.PrepareContext(ctx, discreteValueTableCreateStmt)
if err != nil {
return fmt.Errorf("preparing discreteValues creation statement: %v", err)
}
defer createStmt.Close()
_, err = createStmt.ExecContext(ctx)
if err != nil {
return fmt.Errorf("running discreteValues creation statement: %v", err)
}
return nil
}
func (a *adapter) CreateSampleTable(ctx context.Context, discreteFeatureColumns, continuousFeatureColumns []string) error {
var createStmtBuf bytes.Buffer
_, err := a.db.ExecContext(ctx, "PRAGMA foreign_keys=ON")
if err != nil {
return err
}
createStmtBuf.WriteString("CREATE TABLE IF NOT EXISTS samples(")
for _, c := range discreteFeatureColumns {
createStmtBuf.WriteString(fmt.Sprintf(`"%s" INTEGER NULL REFERENCES discreteValues(id), `, c))
}
for _, c := range continuousFeatureColumns {
createStmtBuf.WriteString(fmt.Sprintf(`"%s" REAL NULL, `, c))
}
createStmtBuf.WriteString(`"id" INTEGER PRIMARY KEY AUTOINCREMENT)`)
createStmt, err := a.db.PrepareContext(ctx, createStmtBuf.String())
if err != nil {
return fmt.Errorf("preparing samples creation statement: %v", err)
}
defer createStmt.Close()
_, err = createStmt.ExecContext(ctx)
if err != nil {
return fmt.Errorf("ensuring samples table exists: %v", err)
}
return nil
}
func (a *adapter) AddDiscreteValues(ctx context.Context, values []string) (int, error) {
var (
chunkStart = 0
chunkEnd = MaxDiscreteValueInsertionsPerStatement
insertStmtBuffer bytes.Buffer
)
if len(values) == 0 {
return 0, nil
}
insertStmtStart := "INSERT INTO discreteValues (value) VALUES (?)"
if len(values) > MaxDiscreteValueInsertionsPerStatement {
insertStmtBuffer.WriteString(insertStmtStart)
for i := 1; i < MaxDiscreteValueInsertionsPerStatement; i++ {
insertStmtBuffer.WriteString(", (?)")
}
insertStmt, err := a.db.PrepareContext(ctx, insertStmtBuffer.String())
if err != nil {
return 0, fmt.Errorf("preparing insert command for %d values: %v", MaxDiscreteValueInsertionsPerStatement, err)
}
for c := 0; c < len(values)/MaxDiscreteValueInsertionsPerStatement; c++ {
iv := make([]interface{}, 0, MaxDiscreteValueInsertionsPerStatement)
for _, v := range values[chunkStart:chunkEnd] {
iv = append(iv, v)
}
_, err = insertStmt.ExecContext(ctx, iv...)
if err != nil {
return chunkStart, fmt.Errorf("inserting the %dth %d values: %v", c+1, MaxDiscreteValueInsertionsPerStatement, err)
}
chunkStart += MaxDiscreteValueInsertionsPerStatement
chunkEnd += MaxDiscreteValueInsertionsPerStatement
}
err = insertStmt.Close()
if err != nil {
return chunkStart, fmt.Errorf("closing insert command for %d values: %v", MaxDiscreteValueInsertionsPerStatement, err)
}
}
chunkEnd = len(values)
lastValues := values[chunkStart:chunkEnd]
if len(lastValues) > 0 {
insertStmtBuffer = bytes.Buffer{}
insertStmtBuffer.WriteString(insertStmtStart)
for i := 1; i < len(lastValues); i++ {
insertStmtBuffer.WriteString(", (?)")
}
insertStmt, err := a.db.PrepareContext(ctx, insertStmtBuffer.String())
if err != nil {
return chunkStart, fmt.Errorf("preparing insert command for %d values: %v", len(lastValues), err)
}
ilv := make([]interface{}, 0, len(lastValues))
for _, v := range lastValues {
ilv = append(ilv, v)
}
_, err = insertStmt.ExecContext(ctx, ilv...)
if err != nil {
return chunkStart, fmt.Errorf("inserting the last %d values: %v", len(lastValues), err)
}
err = insertStmt.Close()
if err != nil {
return chunkEnd, fmt.Errorf("closing insert command for %d values: %v", len(lastValues), err)
}
}
return chunkEnd, nil
}
func (a *adapter) ListDiscreteValues(ctx context.Context) (map[int]string, error) {
rows, err := a.db.QueryContext(ctx, `SELECT id, value FROM discreteValues`)
if err != nil {
return nil, err
}
result := make(map[int]string)
for rows.Next() {
var id int
var value string
err = rows.Scan(&id, &value)
if err != nil {
return nil, err
}
result[id] = value
}
err = rows.Err()
if err != nil {
return nil, err
}
err = rows.Close()
return result, err
}
func (a *adapter) AddSamples(ctx context.Context, rawSamples []map[string]interface{}, discreteFeatureColumns, continuousFeatureColumns []string) (int, error) {
var (
chunkStart = 0
chunkEnd = MaxSampleInsertionsPerStatement
insertStmtBuffer bytes.Buffer
insertStmtStartBuffer bytes.Buffer
)
if len(rawSamples) == 0 {
return 0, nil
}
if len(discreteFeatureColumns)+len(continuousFeatureColumns) == 0 {
return 0, fmt.Errorf("no features to store")
}
insertStmtStartBuffer.WriteString(`INSERT INTO samples ("`)
insertStmtStartBuffer.WriteString(strings.Join(discreteFeatureColumns, `", "`))
if len(discreteFeatureColumns) > 0 && len(continuousFeatureColumns) > 0 {
insertStmtStartBuffer.WriteString(`", "`)
}
insertStmtStartBuffer.WriteString(strings.Join(continuousFeatureColumns, `", "`))
insertStmtStartBuffer.WriteString(`") VALUES (?`)
for i := 1; i < len(discreteFeatureColumns)+len(continuousFeatureColumns); i++ {
insertStmtStartBuffer.WriteString(", ?")
}
insertStmtStartBuffer.WriteString(`)`)
insertStmtStart := insertStmtStartBuffer.String()
if len(rawSamples) > MaxSampleInsertionsPerStatement {
insertStmtBuffer.WriteString(insertStmtStart)
for i := 1; i < MaxSampleInsertionsPerStatement; i++ {
insertStmtBuffer.WriteString(", (?")
for j := 1; j < len(discreteFeatureColumns)+len(continuousFeatureColumns); j++ {
insertStmtStartBuffer.WriteString(", ?")
}
insertStmtStartBuffer.WriteString(`)`)
}
insertStmt, err := a.db.PrepareContext(ctx, insertStmtBuffer.String())
if err != nil {
return 0, fmt.Errorf("preparing insert command for %d samples: %v", MaxSampleInsertionsPerStatement, err)
}
for c := 0; c < len(rawSamples)/MaxSampleInsertionsPerStatement; c++ {
irs := make([]interface{}, 0, MaxSampleInsertionsPerStatement*(len(discreteFeatureColumns)+len(continuousFeatureColumns)))
for _, rs := range rawSamples[chunkStart:chunkEnd] {
for _, f := range discreteFeatureColumns {
irs = append(irs, rs[f])
}
for _, f := range continuousFeatureColumns {
irs = append(irs, rs[f])
}
}
_, err = insertStmt.ExecContext(ctx, irs...)
if err != nil {
return chunkStart, fmt.Errorf("inserting the %dth %d samples: %v", c+1, MaxSampleInsertionsPerStatement, err)
}
chunkStart += MaxSampleInsertionsPerStatement
chunkEnd += MaxSampleInsertionsPerStatement
}
err = insertStmt.Close()
if err != nil {
return chunkStart, fmt.Errorf("closing insert command for %d samples: %v", MaxSampleInsertionsPerStatement, err)
}
}
chunkEnd = len(rawSamples)
lastRawSamples := rawSamples[chunkStart:chunkEnd]
if len(lastRawSamples) > 0 {
insertStmtBuffer = bytes.Buffer{}
insertStmtBuffer.WriteString(insertStmtStart)
for i := 1; i < len(lastRawSamples); i++ {
insertStmtBuffer.WriteString(", (?")
for j := 1; j < len(discreteFeatureColumns)+len(continuousFeatureColumns); j++ {
insertStmtStartBuffer.WriteString(", ?")
}
insertStmtStartBuffer.WriteString(`)`)
}
insertStmt, err := a.db.PrepareContext(ctx, insertStmtBuffer.String())
if err != nil {
return chunkStart, fmt.Errorf("preparing insert command for %d values: %v", len(lastRawSamples), err)
}
ilrs := make([]interface{}, 0, len(lastRawSamples)*(len(discreteFeatureColumns)+len(continuousFeatureColumns)))
for _, rs := range rawSamples[chunkStart:chunkEnd] {
for _, f := range discreteFeatureColumns {
ilrs = append(ilrs, rs[f])
}
for _, f := range continuousFeatureColumns {
ilrs = append(ilrs, rs[f])
}
}
_, err = insertStmt.ExecContext(ctx, ilrs...)
if err != nil {
return chunkStart, fmt.Errorf("inserting the last %d values: %v", len(lastRawSamples), err)
}
err = insertStmt.Close()
if err != nil {
return chunkEnd, fmt.Errorf("closing insert command for %d values: %v", len(lastRawSamples), err)
}
}
return chunkEnd, nil
}
func (a *adapter) ListSamples(ctx context.Context, criteria []*sqlset.FeatureCriterion, discreteFeatureColumns, continuousFeatureColumns []string) ([]map[string]interface{}, error) {
var result []map[string]interface{}
err := a.IterateOnSamples(
ctx,
criteria,
discreteFeatureColumns,
continuousFeatureColumns,
func(_ int, rawSample map[string]interface{}) (bool, error) {
result = append(result, rawSample)
return true, nil
})
if err != nil {
return nil, err
}
return result, nil
}
func (a *adapter) IterateOnSamples(ctx context.Context, criteria []*sqlset.FeatureCriterion, discreteFeatureColumns, continuousFeatureColumns []string, lambda func(int, map[string]interface{}) (bool, error)) error {
var queryBuffer bytes.Buffer
var whereValues []interface{}
queryBuffer.WriteString(`SELECT "`)
queryBuffer.WriteString(strings.Join(discreteFeatureColumns, `", "`))
if len(discreteFeatureColumns) > 0 && len(continuousFeatureColumns) > 0 {
queryBuffer.WriteString(`", "`)
}
queryBuffer.WriteString(strings.Join(continuousFeatureColumns, `", "`))
queryBuffer.WriteString(`" FROM samples`)
if len(criteria) > 0 {
var whereClause string
whereClause, whereValues = buildWhereClause(criteria)
queryBuffer.WriteString(whereClause)
}
rows, err := a.db.QueryContext(ctx, queryBuffer.String(), whereValues...)
if err != nil {
return err
}
for j := 0; rows.Next(); j++ {
rawSample := make(map[string]interface{})
discreteValues := make([]sql.NullInt64, len(discreteFeatureColumns))
continuousValues := make([]sql.NullFloat64, len(continuousFeatureColumns))
values := make([]interface{}, 0, len(discreteFeatureColumns)+len(continuousFeatureColumns))
for i := range discreteValues {
values = append(values, &discreteValues[i])
}
for i := range continuousValues {
values = append(values, &continuousValues[i])
}
err = rows.Scan(values...)
if err != nil {
return err
}
for i, c := range discreteFeatureColumns {
if discreteValues[i].Valid {
rawSample[c] = int(discreteValues[i].Int64)
}
}
for i, c := range continuousFeatureColumns {
if continuousValues[i].Valid {
rawSample[c] = continuousValues[i].Float64
}
}
ok, err := lambda(j, rawSample)
if err != nil {
return err
}
if !ok {
break
}
}
err = rows.Err()
if err != nil {
return err
}
err = rows.Close()
return err
}
func (a *adapter) CountSamples(ctx context.Context, criteria []*sqlset.FeatureCriterion) (int, error) {
var queryBuffer bytes.Buffer
var whereValues []interface{}
queryBuffer.WriteString(`SELECT COUNT(*) FROM samples`)
if len(criteria) > 0 {
var whereClause string
whereClause, whereValues = buildWhereClause(criteria)
queryBuffer.WriteString(whereClause)
}
rows, err := a.db.QueryContext(ctx, queryBuffer.String(), whereValues...)
if err != nil {
return 0, err
}
if !rows.Next() {
return 0, rows.Err()
}
var count int
err = rows.Scan(&count)
if err != nil {
return 0, err
}
err = rows.Close()
return count, err
}
func (a *adapter) ListSampleDiscreteFeatureValues(ctx context.Context, fc string, criteria []*sqlset.FeatureCriterion) ([]int, error) {
var queryBuffer bytes.Buffer
var whereValues []interface{}
queryBuffer.WriteString(fmt.Sprintf(`SELECT DISTINCT "%s" FROM samples`, fc))
if len(criteria) > 0 {
var whereClause string
whereClause, whereValues = buildWhereClause(criteria)
queryBuffer.WriteString(whereClause)
}
rows, err := a.db.QueryContext(ctx, queryBuffer.String(), whereValues...)
if err != nil {
return nil, err
}
var result []int
for rows.Next() {
var value sql.NullInt64
err = rows.Scan(&value)
if err != nil {
return nil, err
}
if value.Valid {
result = append(result, int(value.Int64))
}
}
err = rows.Err()
if err != nil {
return nil, err
}
err = rows.Close()
return result, err
}
func (a *adapter) ListSampleContinuousFeatureValues(ctx context.Context, fc string, criteria []*sqlset.FeatureCriterion) ([]float64, error) {
var queryBuffer bytes.Buffer
var whereValues []interface{}
queryBuffer.WriteString(fmt.Sprintf(`SELECT DISTINCT "%s" FROM samples`, fc))
if len(criteria) > 0 {
var whereClause string
whereClause, whereValues = buildWhereClause(criteria)
queryBuffer.WriteString(whereClause)
}
rows, err := a.db.QueryContext(ctx, queryBuffer.String(), whereValues...)
if err != nil {
return nil, err
}
var result []float64
for rows.Next() {
var value sql.NullFloat64
err = rows.Scan(&value)
if err != nil {
return nil, err
}
if value.Valid {
result = append(result, value.Float64)
}
}
err = rows.Err()
if err != nil {
return nil, err
}
err = rows.Close()
return result, err
}
func (a *adapter) CountSampleDiscreteFeatureValues(ctx context.Context, fc string, criteria []*sqlset.FeatureCriterion) (map[int]int, error) {
var queryBuffer bytes.Buffer
var whereValues []interface{}
queryBuffer.WriteString(fmt.Sprintf(`SELECT "%s", COUNT("%s") FROM samples`, fc, fc))
if len(criteria) > 0 {
var whereClause string
whereClause, whereValues = buildWhereClause(criteria)
queryBuffer.WriteString(whereClause)
}
queryBuffer.WriteString(fmt.Sprintf(` GROUP BY "%s"`, fc))
rows, err := a.db.QueryContext(ctx, queryBuffer.String(), whereValues...)
if err != nil {
return nil, err
}
result := make(map[int]int)
for rows.Next() {
var value sql.NullInt64
var count int
err = rows.Scan(&value, &count)
if err != nil {
return nil, err
}
if value.Valid {
result[int(value.Int64)] = count
}
}
err = rows.Err()
if err != nil {
return nil, err
}
err = rows.Close()
return result, err
}
func (a *adapter) CountSampleContinuousFeatureValues(ctx context.Context, fc string, criteria []*sqlset.FeatureCriterion) (map[float64]int, error) {
var queryBuffer bytes.Buffer
var whereValues []interface{}
queryBuffer.WriteString(fmt.Sprintf(`SELECT "%s", COUNT("%s") FROM samples`, fc, fc))
if len(criteria) > 0 {
var whereClause string
whereClause, whereValues = buildWhereClause(criteria)
queryBuffer.WriteString(whereClause)
}
queryBuffer.WriteString(fmt.Sprintf(` GROUP BY "%s"`, fc))
rows, err := a.db.QueryContext(ctx, queryBuffer.String(), whereValues...)
if err != nil {
return nil, err
}
result := make(map[float64]int)
for rows.Next() {
var value sql.NullFloat64
var count int
err = rows.Scan(&value, &count)
if err != nil {
return nil, err
}
if value.Valid {
result[value.Float64] = count
}
}
err = rows.Err()
if err != nil {
return nil, err
}
err = rows.Close()
return result, err
}
func buildWhereClause(criteria []*sqlset.FeatureCriterion) (string, []interface{}) {
if len(criteria) == 0 {
return "", nil
}
var buf bytes.Buffer
values := make([]interface{}, 0, len(criteria))
buf.WriteString(" WHERE ")
buf.WriteString(fmt.Sprintf(`"%s" %s ?`, criteria[0].FeatureColumn, criteria[0].Operator))
values = append(values, criteria[0].Value)
for i := 1; i < len(criteria); i++ {
buf.WriteString(fmt.Sprintf(`AND "%s" %s ?`, criteria[i].FeatureColumn, criteria[i].Operator))
values = append(values, criteria[i].Value)
}
return buf.String(), values
}