-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.go
More file actions
345 lines (298 loc) · 10.7 KB
/
storage.go
File metadata and controls
345 lines (298 loc) · 10.7 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
package callosum
import (
"database/sql"
"fmt"
"log"
"sync"
_ "github.com/mattn/go-sqlite3" //sqllite DB driver import
)
//UserRow holds the data obtained from fetching a row from the `users` table.
type UserRow struct {
ID int64
ScreenName string
Description string
LastLookedAt string
LatestTweetID int64
LatestFriendID int64
LatestFollowerID int64
Protected int
Processed int
Accepted int
Blob []byte
}
//TweetRow holds the data obtained from fetching a row from the `tweets` table
type TweetRow struct {
TweetID int64
CreatedAt string
Language string
screenName string
tweet []byte
}
//Storage holds a open connection the the sqlite database
type Storage struct {
db *sql.DB
}
type queryArgs struct {
query string
args []interface{}
}
var mutex = &sync.Mutex{}
var chQueryArgs chan *queryArgs
var db *sql.DB
func executeStatements() {
for {
if qa, ok := <-chQueryArgs; ok {
_, err := db.Exec(qa.query, qa.args...)
if err != nil {
log.Fatal(err)
}
}
}
}
//NewStorage creates returns a new Storage object.
//DBName is the name of the sqllite database file where
//all the users and tweets data will be collected. NewStorage
//create the sqlite file, if it is not already present and creates
//the tables. if the database is present, opens a connection.
func NewStorage(DBName string) *Storage {
s := &Storage{}
mutex.Lock()
if db == nil {
s.checkMakeDatabase(DBName)
db = s.db
if chQueryArgs == nil {
chQueryArgs = make(chan *queryArgs, 100)
go executeStatements()
}
s.setupTables()
}
mutex.Unlock()
return s
}
func (s *Storage) setupTables() {
tableName := "users"
s.makeTable(tableName, fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s (
user_id INTEGER PRIMARY KEY,
screen_name TEXT CONSTRAINT uniquescreenname UNIQUE,
description TEXT CONSTRAINT defaultdesc DEFAULT "",
last_looked_at INTEGER CONSTRAINT defaultlastlookedat DEFAULT 0,
latest_tweet_id INTEGER CONSTRAINT defaultlatesttweetid DEFAULT 0,
latest_following_id INTEGER CONSTRAINT defaultlatestfollowingid DEFAULT 0,
latest_follower_id INTEGER CONSTRAINT defaultlatestfollowerid DEFAULT 0,
protected INTEGER CONSTRAINT defaultprotected DEFAULT 0,
processed INTEGER CONSTRAINT defaultprocessed DEFAULT 0,
accepted INTEGER CONSTRAINT defaultaccepted DEFAULT 0,
blob BLOB)`, tableName))
tableName = "tweets"
s.makeTable(tableName, fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s(tweet_id INTEGER PRIMARY KEY,
created_at INTEGER,
langugage TEXT,
user_id INTEGER,
desc TEXT,
blob BLOB
-- FOREIGN KEY(screen_name) REFERENCES users(screen_name)
)`, tableName))
tableName = "screennames"
s.makeTable(tableName, fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s(screen_name TEXT PRIMARY KEY,
processed INTEGER CONSTRAINT defaultprocessed DEFAULT 0)`, tableName))
tableName = "userids"
s.makeTable(tableName, fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s(user_id INTEGER PRIMARY KEY,
processed INTEGER CONSTRAINT defaultprocessed DEFAULT 0)`, tableName))
tableName = "followers"
s.makeTable(tableName, fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s(user_id INTEGER,
follower_id INTEGER,
CONSTRAINT uniquemap UNIQUE (user_id, follower_id))`, tableName))
tableName = "following"
s.makeTable(tableName, fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s(user_id INTEGER,
following_id INTEGER,
CONSTRAINT uniquemap UNIQUE (user_id, following_id))`, tableName))
}
func (s *Storage) checkMakeDatabase(DBName string) *sql.DB {
var db *sql.DB
db, err := sql.Open("sqlite3", DBName+".db") //?cache=shared&mode=rwc")
if err != nil {
log.Fatal(err)
}
db.Exec("PRAGMA journal_mode=WAL;")
s.db = db
return db
}
func (s *Storage) makeTable(tableName, sqlStmt string) {
_, err := s.db.Exec(sqlStmt)
if err != nil {
log.Fatalf("%q: %s\n", err, sqlStmt)
return
}
}
//StoreScreenName inserts the given screenName into the `screenames` table
func (s *Storage) StoreScreenName(screenName string) {
_, err := s.db.Exec("INSERT OR IGNORE INTO screennames (screen_name) VALUES (?)", screenName)
if err != nil {
log.Fatal(err)
}
}
//StoreUser inserts the Twitter user details into the `users` table.
func (s *Storage) StoreUser(userID int64, screenName, description string, protected bool, blob []byte) {
chQueryArgs <- &queryArgs{"INSERT OR IGNORE INTO users (user_id, screen_name, description, protected, blob) VALUES (?, ?, ?, ?, ?)",
[]interface{}{userID, screenName, description, protected, blob}}
}
//StoreTweet inserts the tweet details into the `tweets` table.
func (s *Storage) StoreTweet(tweetID, createdAt, userID int64, language, desc string, blob []byte) {
chQueryArgs <- &queryArgs{"INSERT OR IGNORE INTO tweets (tweet_id, created_at, langugage, user_id, desc, blob) VALUES (?, ?, ?, ?, ?, ?)",
[]interface{}{tweetID, createdAt, language, userID, desc, blob}}
}
func (s *Storage) storeFriendOrFollower(userID, friendOrFollowerID int64, query string) {
chQueryArgs <- &queryArgs{query, []interface{}{userID, friendOrFollowerID}}
}
//StoreFriends stores the mapping between the userID and the IDs of
//users the follow into the `following` table.
func (s *Storage) StoreFriends(userID int64, friendIDs []int64) {
for _, friendID := range friendIDs {
s.storeFriendOrFollower(userID, friendID, "INSERT OR IGNORE INTO following (user_id, following_id) VALUES (?, ?)")
}
}
//StoreFollowers stores the mapping between the userID and the IDs of
//their followes into the `followers` table.
func (s *Storage) StoreFollowers(userID int64, followerIDs []int64) {
for _, followerID := range followerIDs {
s.storeFriendOrFollower(userID, followerID, "INSERT OR IGNORE INTO followers (user_id, follower_id) VALUES (?, ?)")
}
}
func (s *Storage) storeUserID(userID int64) {
chQueryArgs <- &queryArgs{"INSERT OR IGNORE INTO userids (user_id) VALUES (?)", []interface{}{userID}}
}
//StoreUserIDs stores the given userIDs in the `userids` table
func (s *Storage) StoreUserIDs(userIDs []int64) {
for _, userID := range userIDs {
s.storeUserID(userID)
}
}
func (s *Storage) queryScreenNamesOrIDs(query string, results interface{}) {
rows, err := s.db.Query(query)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
switch x := results.(type) {
case *[]string:
var item string
rows.Scan(&item)
*x = append(*x, item)
case *[]int64:
var item int64
rows.Scan(&item)
*x = append(*x, item)
default:
log.Fatal("results type must be *[]string or *[]int64")
}
}
}
//GetScreenNames gets Twitter handles from the `screenames` table that have already been processed
func (s *Storage) GetScreenNames() []string {
var results []string
s.queryScreenNamesOrIDs("SELECT screen_name from screennames where processed=1", &results)
return results
}
//GetUnprocessedScreenNames gets Twitter handles from the `screenames` table that are yet to be processed
func (s *Storage) GetUnprocessedScreenNames() []string {
var results []string
s.queryScreenNamesOrIDs("SELECT screen_name from screennames where processed=0", &results)
return results
}
//GetUserIDs gets user ids from the `userids` table that have already been processed
func (s *Storage) GetUserIDs() []int64 {
var results []int64
s.queryScreenNamesOrIDs("SELECT user_id from userids where processed=1", &results)
return results
}
//GetUnprocessedUserIDs gets user ids from the `userids` table that are yet to be processed
func (s *Storage) GetUnprocessedUserIDs() []int64 {
var results []int64
s.queryScreenNamesOrIDs("SELECT user_id from userids where processed=0", &results)
return results
}
//GetAcceptedUserIDs gets user ids from the `users` table for whom the user filtering
//function has marked them as accepted for further processing
func (s *Storage) GetAcceptedUserIDs() []int64 {
var results []int64
s.queryScreenNamesOrIDs("SELECT user_id from users where accepted=1", &results)
return results
}
//GetUserByScreenNameOrID gets the UserRow for the given screenName or ID
func (s *Storage) GetUserByScreenNameOrID(screenNameOrID interface{}) *UserRow {
var u UserRow
query := `SELECT user_id,
screen_name,
description,
last_looked_at,
latest_tweet_id,
latest_following_id,
latest_follower_id,
protected,
processed,
accepted,
blob
FROM users
WHERE %s=?`
var row *sql.Row
switch x := screenNameOrID.(type) {
case int64:
query = fmt.Sprintf(query, "user_id")
row = s.db.QueryRow(query, x)
case string:
query = fmt.Sprintf(query, "screen_name")
row = s.db.QueryRow(query, x)
}
err := row.Scan(
&u.ID,
&u.ScreenName,
&u.Description,
&u.LastLookedAt,
&u.LatestTweetID,
&u.LatestFriendID,
&u.LatestFollowerID,
&u.Protected,
&u.Processed,
&u.Accepted,
&u.Blob)
switch {
case err == sql.ErrNoRows:
return nil
case err != nil:
log.Fatal(err)
}
return &u
}
//MarkUserLatestTweetsCollected updates the `last_looked_at` timestamp and the `latest_tweet_id` for
//the given user in the `users` table
func (s *Storage) MarkUserLatestTweetsCollected(userID int64, lastLookedAt, latestTweetID int64) {
chQueryArgs <- &queryArgs{"UPDATE users SET last_looked_at=?, latest_tweet_id=? where user_id=?", []interface{}{lastLookedAt, latestTweetID, userID}}
}
//MarkUserLatestFriendsCollected sets the `latest_following_id` to the latest id of the users given userID
//is following
func (s *Storage) MarkUserLatestFriendsCollected(userID, latestFriendID int64) {
chQueryArgs <- &queryArgs{"UPDATE users SET latest_following_id=? where user_id=?", []interface{}{latestFriendID, userID}}
}
//MarkUserLatestFollowersCollected sets the `latest_follower_id` to the latest id of the followers collected
func (s *Storage) MarkUserLatestFollowersCollected(userID, latestFollowerID int64) {
chQueryArgs <- &queryArgs{"UPDATE users SET latest_follower_id=? where user_id=?", []interface{}{latestFollowerID, userID}}
}
//MarkUserProcessed sets the `processed` and the `accepted` flags for the user in the `users` table
func (s *Storage) MarkUserProcessed(ID int64, processed, accepted bool) {
chQueryArgs <- &queryArgs{"UPDATE users SET processed=?, accepted=? where user_id=?", []interface{}{processed, accepted, ID}}
}
//MarkUserIDProcessed sets the `processed` flag for the given user id in the `userids` table
func (s *Storage) MarkUserIDProcessed(ID int64, processed bool) {
chQueryArgs <- &queryArgs{"UPDATE userids SET processed=? where user_id=?", []interface{}{processed, ID}}
}
//MarkScreenNameProcessed sets the `processed` flag for the given screenName in the `screennames` table
func (s *Storage) MarkScreenNameProcessed(screenName string, processed bool) {
chQueryArgs <- &queryArgs{"UPDATE screennames SET processed=? where screen_name=?", []interface{}{processed, screenName}}
}