-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_database.go
More file actions
355 lines (324 loc) · 6.86 KB
/
Copy pathsql_database.go
File metadata and controls
355 lines (324 loc) · 6.86 KB
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
package main
import (
"database/sql"
"os"
_ "github.com/mattn/go-sqlite3"
)
type SQLDataBase struct {
db_path string
db *sql.DB
}
func NewSQLDataBase(db_path string) (*SQLDataBase, error) {
// Create database
db, err := sql.Open("sqlite3", db_path)
if err != nil {
return nil, err
}
// Create tables
_, err = db.Exec(`
PRAGMA foreign_keys=ON;
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS words(
id INTEGER PRIMARY KEY,
word STRING UNIQUE
);
CREATE TABLE IF NOT EXISTS documents(
id INTEGER PRIMARY KEY,
title STRING,
url STRING UNIQUE,
wordcount INTEGER
);
CREATE TABLE IF NOT EXISTS frequencies(
id INTEGER PRIMARY KEY,
word_id INTEGER,
doc_id INTEGER,
freq INTEGER,
FOREIGN KEY(word_id) REFERENCES words(id),
FOREIGN KEY(doc_id) REFERENCES documents(id),
UNIQUE(word_id, doc_id)
);
COMMIT;
`)
if err != nil {
return nil, err
}
return &SQLDataBase{db_path, db}, nil
}
func (sdb *SQLDataBase) GetAllDocs() (map[string]struct{}, error) {
// Create freq map and get rows
m := make(map[string]struct{})
rows, err := sdb.db.Query(`SELECT url FROM documents`)
if err != nil {
return nil, err
}
defer rows.Close()
// Loop through rows to make map
for rows.Next() {
var url string
err = rows.Scan(&url)
if err != nil {
return nil, err
}
m[url] = struct{}{}
}
return m, nil
}
func (sdb *SQLDataBase) GetAllFreq(doc string) (map[string]uint, error) {
// Start transaction
tx, err := sdb.db.Begin()
if err != nil {
return nil, err
}
defer tx.Commit()
// Get doc id
doc_id, err := sdb.docToId(tx, doc)
if err == sql.ErrNoRows {
return make(map[string]uint), nil
}
if err != nil {
return nil, err
}
// Create freq map and get rows
m := make(map[string]uint)
rows, err := tx.Query(`SELECT word_id, freq FROM frequencies WHERE doc_id = ?`, doc_id)
if err == sql.ErrNoRows {
return make(map[string]uint), nil
}
if err != nil {
return nil, err
}
defer rows.Close()
// Loop through rows to make map
for rows.Next() {
var word_id, freq int
err = rows.Scan(&word_id, &freq)
if err != nil {
return nil, err
}
word, err := sdb.idToWord(tx, word_id)
if err != nil {
return nil, err
}
m[word] = uint(freq)
}
return m, nil
}
func (sdb *SQLDataBase) GetDocsWithWord(word string) (map[string]struct {
title string
word_count uint
freq uint
}, error) {
// Start transaction
tx, err := sdb.db.Begin()
if err != nil {
return nil, err
}
defer tx.Commit()
// Get word id
word_id, err := sdb.wordToId(tx, word)
if err == sql.ErrNoRows {
return make(map[string]struct {
title string
word_count uint
freq uint
}), nil
}
if err != nil {
return nil, err
}
// Create freq map and get rows
m := make(map[string]struct {
title string
word_count uint
freq uint
})
rows, err := tx.Query(`SELECT doc_id, freq FROM frequencies WHERE word_id = ?`, word_id)
if err != nil {
return nil, err
}
defer rows.Close()
// Loop through rows to make map
for rows.Next() {
var doc_id, freq int
err = rows.Scan(&doc_id, &freq)
if err != nil {
return nil, err
}
doc, err := sdb.idToDoc(tx, doc_id)
if err != nil {
return nil, err
}
m[doc.url] = struct {
title string
word_count uint
freq uint
}{
doc.title,
doc.word_count,
uint(freq),
}
}
return m, nil
}
func (sdb *SQLDataBase) AddDocWithWords(title string, url string, wordcount uint, words map[string]uint) error {
// Start transaction
tx, err := sdb.db.Begin()
if err != nil {
return err
}
defer tx.Commit()
// Add doc
_, err = tx.Exec(`INSERT INTO documents(title, url, wordcount) VALUES(?, ?, ?)`, title, url, wordcount)
if err != nil {
return err
}
// Get doc id
doc_id, err := sdb.docToId(tx, url)
if err != nil {
return err
}
// Add all words
for word, freq := range words {
// Get word id and add word to table if new
word_id, err := sdb.wordToId(tx, word)
if err == sql.ErrNoRows {
_, err = tx.Exec(`INSERT INTO words(word) VALUES(?)`, word)
if err != nil {
return err
}
word_id, err = sdb.wordToId(tx, word)
}
if err != nil {
return err
}
// Add freq
_, err = tx.Exec(`INSERT OR REPLACE INTO frequencies(doc_id, word_id, freq) VALUES(?, ?, ?)`, doc_id, word_id, freq)
if err != nil {
return err
}
}
return nil
}
func (sdb *SQLDataBase) ContainsDoc(doc string) (bool, error) {
ids, err := sdb.db.Query(`SELECT id FROM documents WHERE url = ?`, doc)
if err != nil {
return false, err
}
defer ids.Close()
if ids.Next() {
return true, nil
} else {
return false, nil
}
}
func (sdb *SQLDataBase) Size() (uint, error) {
var count int
row := sdb.db.QueryRow(`SELECT COUNT(*) FROM documents`)
err := row.Scan(&count)
if err != nil {
return 0, err
}
return uint(count), nil
}
// Delete the database
func (sdb *SQLDataBase) DeleteDataBase() {
sdb.Close()
os.Remove(sdb.db_path)
}
// Close closes the database connection
func (sdb *SQLDataBase) Close() error {
if sdb.db != nil {
return sdb.db.Close()
}
return nil
}
// -------- Internal Methods -------- //
func (sdb *SQLDataBase) docToId(tx *sql.Tx, doc string) (int, error) {
ids, err := tx.Query(`SELECT id FROM documents WHERE url = ?`, doc)
if err != nil {
return 0, err
}
defer ids.Close()
var id int
if ids.Next() {
err = ids.Scan(&id)
if err != nil {
return 0, err
}
} else {
return 0, sql.ErrNoRows
}
return id, nil
}
func (sdb *SQLDataBase) idToDoc(tx *sql.Tx, id int) (struct {
title string
url string
word_count uint
}, error) {
docs, err := tx.Query(`SELECT title, url, wordcount FROM documents WHERE id = ?`, id)
if err != nil {
return struct {
title string
url string
word_count uint
}{}, err
}
defer docs.Close()
var url, title string
var word_count int
if docs.Next() {
err = docs.Scan(&title, &url, &word_count)
if err != nil {
return struct {
title string
url string
word_count uint
}{}, err
}
} else {
return struct {
title string
url string
word_count uint
}{}, sql.ErrNoRows
}
return struct {
title string
url string
word_count uint
}{title, url, uint(word_count)}, nil
}
func (sdb *SQLDataBase) wordToId(tx *sql.Tx, word string) (int, error) {
ids, err := tx.Query(`SELECT id FROM words WHERE word = ?`, word)
if err != nil {
return 0, err
}
defer ids.Close()
var id int
if ids.Next() {
err = ids.Scan(&id)
if err != nil {
return 0, err
}
} else {
return 0, sql.ErrNoRows
}
return id, nil
}
func (sdb *SQLDataBase) idToWord(tx *sql.Tx, id int) (string, error) {
words, err := tx.Query(`SELECT word FROM words WHERE id = ?`, id)
if err != nil {
return "", err
}
defer words.Close()
var word string
if words.Next() {
err = words.Scan(&word)
if err != nil {
return "", err
}
} else {
return "", sql.ErrNoRows
}
return word, nil
}