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
|
package main
import (
"log"
"net"
"strings"
"time"
"github.com/getwtxt/registry"
"github.com/syndtr/goleveldb/leveldb"
)
func checkDBtime() bool {
confObj.Mu.RLock()
answer := time.Since(confObj.LastPush) > confObj.DBInterval
confObj.Mu.RUnlock()
return answer
}
// Pushes the registry's cache data to a local
// database for safe keeping.
func pushDatabase() error {
db := <-dbChan
dbChan <- db
return db.push()
}
func pullDatabase() {
db := <-dbChan
dbChan <- db
db.pull()
}
func (lvl dbLevel) push() error {
twtxtCache.Mu.RLock()
var dbBasket = &leveldb.Batch{}
for k, v := range twtxtCache.Users {
dbBasket.Put([]byte(k+"*Nick"), []byte(v.Nick))
dbBasket.Put([]byte(k+"*URL"), []byte(v.URL))
dbBasket.Put([]byte(k+"*IP"), []byte(v.IP.String()))
dbBasket.Put([]byte(k+"*Date"), []byte(v.Date))
for i, e := range v.Status {
rfc := i.Format(time.RFC3339)
dbBasket.Put([]byte(k+"*Status*"+rfc), []byte(e))
}
}
twtxtCache.Mu.RUnlock()
remoteRegistries.Mu.RLock()
for k, v := range remoteRegistries.List {
dbBasket.Put([]byte("remote*"+string(k)), []byte(v))
}
remoteRegistries.Mu.RUnlock()
if err := lvl.db.Write(dbBasket, nil); err != nil {
return err
}
confObj.Mu.Lock()
confObj.LastPush = time.Now()
confObj.Mu.Unlock()
return nil
}
func (lvl dbLevel) pull() {
iter := lvl.db.NewIterator(nil, nil)
for iter.Next() {
key := string(iter.Key())
val := string(iter.Value())
split := strings.Split(key, "*")
urls := split[0]
field := split[1]
if urls == "remote" {
remoteRegistries.Mu.Lock()
remoteRegistries.List = append(remoteRegistries.List, val)
remoteRegistries.Mu.Unlock()
continue
}
data := registry.NewUser()
twtxtCache.Mu.RLock()
if _, ok := twtxtCache.Users[urls]; ok {
data = twtxtCache.Users[urls]
}
twtxtCache.Mu.RUnlock()
switch field {
case "IP":
data.IP = net.ParseIP(val)
case "Nick":
data.Nick = val
case "URL":
data.URL = val
case "Date":
data.Date = val
case "Status":
thetime, err := time.Parse(time.RFC3339, split[2])
if err != nil {
log.Printf("%v\n", err.Error())
}
data.Status[thetime] = val
}
twtxtCache.Mu.Lock()
twtxtCache.Users[urls] = data
twtxtCache.Mu.Unlock()
}
remoteRegistries.Mu.Lock()
remoteRegistries.List = dedupe(remoteRegistries.List)
remoteRegistries.Mu.Unlock()
iter.Release()
err := iter.Error()
if err != nil {
log.Printf("Error while pulling DB into registry cache: %v\n", err.Error())
}
}
func (lite dbSqlite) push() error {
return nil
}
func (lite dbSqlite) pull() {
}
|