-
Notifications
You must be signed in to change notification settings - Fork 23
/
store_sqlite.go
169 lines (155 loc) · 4.09 KB
/
store_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
package main
import (
"time"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
)
const schema = `
CREATE TABLE IF NOT EXISTS tuples (
query character varying,
type character varying,
answer character varying,
count integer,
ttl integer,
first REAL,
last REAL,
PRIMARY KEY (query, type, answer)
) ;
CREATE INDEX IF NOT EXISTS tuples_query ON tuples(query);
CREATE INDEX IF NOT EXISTS tuples_answer ON tuples(answer);
CREATE INDEX IF NOT EXISTS tuples_first ON tuples(first);
CREATE INDEX IF NOT EXISTS tuples_last ON tuples(last);
CREATE TABLE IF NOT EXISTS individual (
which char(1),
value character varying,
count integer,
first REAL,
last REAL,
PRIMARY KEY (which, value)
);
CREATE INDEX IF NOT EXISTS individual_first ON individual(first);
CREATE INDEX IF NOT EXISTS individual_last ON individual(last);
CREATE TABLE IF NOT EXISTS filenames (
filename character varying PRIMARY KEY UNIQUE NOT NULL,
time REAL DEFAULT (datetime('now', 'localtime')),
aggregation_time real,
total_records int,
skipped_records int,
tuples int,
individual int,
store_time real,
inserted int,
updated int
);
PRAGMA case_sensitive_like=ON;
-- PRAGMA journal_mode=WAL;
-- PRAGMA synchronous=off;
PRAGMA temp_store = MEMORY;
PRAGMA cache_size = 5000;
`
type SQLiteStore struct {
conn *sqlx.DB
*SQLCommonStore
}
func NewSQLiteStore(uri string) (Store, error) {
conn, err := sqlx.Open("sqlite3", uri)
if err != nil {
return nil, err
}
common := &SQLCommonStore{conn: conn}
return &SQLiteStore{conn: conn, SQLCommonStore: common}, nil
}
func (s *SQLiteStore) Close() error {
return s.Close()
}
func (s *SQLiteStore) Init() error {
_, err := s.conn.Exec(schema)
return err
}
func (s *SQLiteStore) Update(ar aggregationResult) (UpdateResult, error) {
var result UpdateResult
start := time.Now()
tx, err := s.BeginTx()
if err != nil {
return result, err
}
//Setup the 4 different prepared statements
update_tuples, err := tx.Prepare(`UPDATE tuples SET
count=count+$1,
ttl=$2,
first=min(datetime($3, 'unixepoch'), first),
last =max(datetime($4, 'unixepoch'), last)
WHERE query=$5 AND type=$6 AND answer=$7`)
if err != nil {
return result, err
}
defer update_tuples.Close()
insert_tuples, err := tx.Prepare(`INSERT INTO tuples (query, type, answer, ttl, count, first, last)
VALUES ($1, $2, $3, $4, $5, datetime($6, 'unixepoch'), datetime($7,'unixepoch'))`)
if err != nil {
return result, err
}
defer insert_tuples.Close()
update_individual, err := tx.Prepare(`UPDATE individual SET
count=count+$1,
first=min(datetime($2, 'unixepoch'), first),
last =max(datetime($3, 'unixepoch'), last)
WHERE value=$4 AND which=$5`)
if err != nil {
return result, err
}
defer update_individual.Close()
insert_individual, err := tx.Prepare(`INSERT INTO individual (value, which, count, first, last)
VALUES ($1, $2, $3, datetime($4, 'unixepoch'), datetime($5,'unixepoch'))`)
if err != nil {
return result, err
}
defer insert_individual.Close()
// Ok, now let's update stuff
for _, q := range ar.Tuples {
//Update the tuples table
query := Reverse(q.query)
res, err := update_tuples.Exec(q.count, q.ttl, ToTS(q.first), ToTS(q.last), query, q.qtype, q.answer)
if err != nil {
return result, err
}
rows, err := res.RowsAffected()
if err != nil {
return result, err
}
if rows == 0 {
_, err := insert_tuples.Exec(query, q.qtype, q.answer, q.ttl, q.count, ToTS(q.first), ToTS(q.last))
if err != nil {
return result, err
}
result.Inserted++
} else {
result.Updated++
}
}
for _, q := range ar.Individual {
value := q.value
if q.which == "Q" {
value = Reverse(value)
}
res, err := update_individual.Exec(q.count, ToTS(q.first), ToTS(q.last), value, q.which)
if err != nil {
return result, err
}
rows, err := res.RowsAffected()
if err != nil {
return result, err
}
if rows == 0 {
_, err := insert_individual.Exec(value, q.which, q.count, ToTS(q.first), ToTS(q.last))
if err != nil {
return result, err
}
result.Inserted++
} else {
result.Updated++
}
}
result.Duration = time.Since(start)
return result, s.Commit()
}