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
|
package svc // import "github.com/getwtxt/getwtxt/svc"
import (
"database/sql"
"net"
"time"
"github.com/getwtxt/registry"
_ "github.com/mattn/go-sqlite3" // for the sqlite3 driver
)
type dbSqlite struct {
db *sql.DB
pullStmt *sql.Stmt
pushStmt *sql.Stmt
}
func initSqlite() *dbSqlite {
confObj.Mu.RLock()
dbpath := confObj.DBPath
confObj.Mu.RUnlock()
lite, err := sql.Open("sqlite3", dbpath)
errFatal("Error opening sqlite3 DB: ", err)
errFatal("", lite.Ping())
_, err = lite.Exec("CREATE TABLE IF NOT EXISTS getwtxt (id INTEGER PRIMARY KEY, urlKey TEXT, isUser BOOL, dataKey TEXT, data BLOB)")
errFatal("Error preparing sqlite3 DB: ", err)
push, err := lite.Prepare("INSERT OR REPLACE INTO getwtxt (urlKey, isUser, dataKey, data) VALUES(?, ?, ?, ?)")
errFatal("", err)
pull, err := lite.Prepare("SELECT * FROM getwtxt")
errFatal("", err)
return &dbSqlite{
db: lite,
pushStmt: push,
pullStmt: pull,
}
}
func (lite *dbSqlite) push() error {
confObj.Mu.Lock()
confObj.LastPush = time.Now()
confObj.Mu.Unlock()
if err := lite.db.Ping(); err != nil {
lite = initSqlite()
}
tx, err := lite.db.Begin()
errLog("", err)
txst := tx.Stmt(lite.pushStmt)
twtxtCache.Mu.RLock()
for i, e := range twtxtCache.Users {
e.Mu.RLock()
_, err = txst.Exec(i, true, "nickname", e.Nick)
errLog("", err)
_, err = txst.Exec(i, true, "rlen", e.RLen)
errLog("", err)
_, err = txst.Exec(i, true, "uip", e.IP)
errLog("", err)
_, err = txst.Exec(i, true, "date", e.Date)
errLog("", err)
for k, v := range e.Status {
_, err = txst.Exec(i, true, k.Format(time.RFC3339), v)
errLog("", err)
}
e.Mu.RUnlock()
}
twtxtCache.Mu.RUnlock()
remoteRegistries.Mu.RLock()
for _, e := range remoteRegistries.List {
_, err = txst.Exec(e, false, "REMOTE REGISTRY", "NULL")
errLog("", err)
}
remoteRegistries.Mu.RUnlock()
err = tx.Commit()
if err != nil {
errLog("", tx.Rollback())
return err
}
return nil
}
func (lite *dbSqlite) pull() {
errLog("Error pinging sqlite DB: ", lite.db.Ping())
rows, err := lite.pullStmt.Query()
errLog("", err)
defer func(rows *sql.Rows) {
errLog("Error while finalizing DB Pull: ", rows.Close())
}(rows)
twtxtCache.Mu.Lock()
for rows.Next() {
var uid int
var urls string
var isUser bool
var dataKey string
var dBlob []byte
errLog("", rows.Scan(&uid, &urls, &isUser, &dataKey, &dBlob))
if !isUser {
remoteRegistries.Mu.Lock()
remoteRegistries.List = append(remoteRegistries.List, urls)
remoteRegistries.Mu.Unlock()
continue
}
user := registry.NewUser()
if _, ok := twtxtCache.Users[urls]; ok {
user = twtxtCache.Users[urls]
}
user.Mu.Lock()
switch dataKey {
case "nickname":
user.Nick = string(dBlob)
case "uip":
user.IP = net.ParseIP(string(dBlob))
case "date":
user.Date = string(dBlob)
case "rlen":
user.RLen = string(dBlob)
default:
thetime, err := time.Parse(time.RFC3339, dataKey)
errLog("While pulling statuses from SQLite: ", err)
user.Status[thetime] = string(dBlob)
}
twtxtCache.Users[urls] = user
user.Mu.Unlock()
}
twtxtCache.Mu.Unlock()
remoteRegistries.Mu.Lock()
remoteRegistries.List = dedupe(remoteRegistries.List)
remoteRegistries.Mu.Unlock()
}
|