/
db.go
299 lines (267 loc) · 6.43 KB
/
db.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
package main
import (
"crypto/sha1"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"strings"
"sync"
"github.com/boltdb/bolt"
"github.com/cheggaaa/pb"
)
func getQuotesFromIndex(index string) []Quote {
index = cleanString(index)
db, err := bolt.Open("quotations.db", 0600, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
indexMap := make(map[int]bool)
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("index"))
v := b.Get([]byte(index))
if v != nil {
json.Unmarshal(v, &indexMap)
}
return nil
})
quotes := make([]Quote, len(indexMap))
var quote Quote
i := 0
for id := range indexMap {
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("data"))
v := b.Get(itob(id))
json.Unmarshal(v, "e)
quotes[i] = quote
i++
return nil
})
}
if len(quotes) == 0 {
log.Println("Could not find in index, going to iterate through everything")
indexMap = make(map[int]bool)
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("data"))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
json.Unmarshal(v, "e)
if strings.Contains(strings.ToLower(quote.Text), index) ||
strings.Contains(strings.ToLower(quote.Name), index) {
quotes = append(quotes, quote)
indexMap[quote.ID] = true
}
}
return nil
})
if len(indexMap) > 0 {
// Insert into the index, now that something is found
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("index"))
buf, err := json.Marshal(indexMap)
if err != nil {
return err
}
b.Put([]byte(index), buf)
return nil
})
}
}
log.Printf("Found %d quotes for '%s'", len(quotes), index)
return quotes
}
var randomQuotePool = struct {
sync.RWMutex
q []Quote
}{q: make([]Quote, 0)}
func getRandomQuotes(num int) []Quote {
if num < 1 {
num = 1
} else if num > 50 {
num = 50
}
var quotes []Quote
randomQuotePool.RLock()
if len(randomQuotePool.q) > num {
quotes = randomQuotePool.q[0:num]
} else {
quotes = generateRandomQuotes(num)
}
randomQuotePool.RUnlock()
go cacheRandomQuotes(num)
return quotes
}
func cacheRandomQuotes(num int) {
randomQuotePool.Lock()
if len(randomQuotePool.q) == 0 {
randomQuotePool.q = generateRandomQuotes(2000)
} else {
randomQuotePool.q = randomQuotePool.q[num:]
randomQuotePool.q = append(randomQuotePool.q, generateRandomQuotes(num)...)
}
randomQuotePool.Unlock()
}
func generateRandomQuotes(num int) []Quote {
quotes := make([]Quote, num)
db, err := bolt.Open("quotations.db", 0600, nil)
if err != nil {
log.Printf("Error: %v\n", err)
db.Close()
return quotes
}
defer db.Close()
var quote Quote
maxNumber := 0
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("data"))
c := b.Cursor()
for k, v := c.Last(); k != nil; k, v = c.Prev() {
json.Unmarshal(v, "e)
maxNumber = quote.ID
return nil
}
return nil
})
for i := range quotes {
randomID := rand.Intn(maxNumber)
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("data"))
c := b.Cursor()
_, v := c.Seek(itob(randomID))
json.Unmarshal(v, "e)
quotes[i] = quote
return nil
})
}
return quotes
}
func dumpDatabase() {
fmt.Println("Dumping database...")
db, err := bolt.Open("quotations.db", 0600, nil)
if err != nil {
log.Printf("Error: %v\n", err)
db.Close()
}
defer db.Close()
var quote Quote
maxNumber := 0
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("data"))
c := b.Cursor()
for k, v := c.Last(); k != nil; k, v = c.Prev() {
json.Unmarshal(v, "e)
maxNumber = quote.ID
return nil
}
return nil
})
quotes := make([]Quote, maxNumber)
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("data"))
c := b.Cursor()
i := 0
for k, v := c.First(); k != nil; k, v = c.Next() {
json.Unmarshal(v, "e)
quotes[i] = quote
i++
}
return nil
})
bJson, _ := json.MarshalIndent(quotes, "", " ")
ioutil.WriteFile("quotations.json", bJson, 0644)
}
func buildDatabase() {
fmt.Println("Building database...")
var quotes []Quote
bJson, _ := ioutil.ReadFile("quotations.json")
json.Unmarshal(bJson, "es)
db, err := bolt.Open("quotations.db", 0600, nil)
if err != nil {
log.Printf("Error: %v\n", err)
db.Close()
return
}
defer db.Close()
fmt.Println("Inserting data")
indexing := make(map[string]map[int]bool)
bar := pb.StartNew(len(quotes))
alreadyFinished := make(map[string]bool)
db.Update(func(tx *bolt.Tx) error {
b, _ := tx.CreateBucketIfNotExists([]byte("data"))
for _, quote := range quotes {
bar.Increment()
// Skip quotes that are too long
if len(quote.Text) > 300 {
continue
}
// Skip quotes that have already been added
h := sha1.New()
h.Write([]byte(cleanString(quote.Text)))
sha1_hash := hex.EncodeToString(h.Sum(nil))
if _, ok := alreadyFinished[sha1_hash]; ok {
continue
}
alreadyFinished[sha1_hash] = true
// Add this quote!
id, _ := b.NextSequence()
quote.ID = int(id)
quote.Name = strings.TrimSpace(quote.Name)
quote.Text = strings.TrimSpace(quote.Text)
buf, err := json.Marshal(quote)
if err != nil {
return err
}
b.Put(itob(quote.ID), buf)
// Do the indexing of the words
for _, word := range strings.Fields(quote.Text) {
word = cleanString(word)
// Only index if its not a stop word
if !isStopWord(word) {
if _, ok := indexing[word]; !ok {
indexing[word] = make(map[int]bool)
}
if _, ok := indexing[word][quote.ID]; !ok {
indexing[word][quote.ID] = true
}
}
}
// Do the indexing of the authors
word := cleanString(quote.Name)
// Only index if its not a stop word
if _, ok := indexing[word]; !ok {
indexing[word] = make(map[int]bool)
}
if _, ok := indexing[word][quote.ID]; !ok {
indexing[word][quote.ID] = true
}
// continue looping through quotes!
}
return nil
})
bar.FinishPrint("Finished inserting data")
fmt.Println("Indexing data")
bar = pb.StartNew(len(indexing))
db.Update(func(tx *bolt.Tx) error {
b, _ := tx.CreateBucketIfNotExists([]byte("index"))
for word := range indexing {
bar.Increment()
buf, err := json.Marshal(indexing[word])
if err != nil {
return err
}
b.Put([]byte(word), buf)
}
return nil
})
bar.FinishPrint("Finished indexing data")
}
// itob returns an 8-byte big endian representation of v.
func itob(v int) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(v))
return b
}