-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
202 lines (168 loc) · 3.98 KB
/
database.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
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"os"
"sync"
"time"
)
type logrecord struct {
Count int
Title string
}
// Logdatabase - main database structure
// database is a map of maps
// realm -> uri -> logrecord
type Logdatabase struct {
lock *sync.Mutex
db map[string]map[string]logrecord
dirty bool
sweeper *time.Ticker
filename string
rollingLog RollingLog
}
// MakeLogDatabase Create a new logdatabase
func MakeLogDatabase() *Logdatabase {
r := Logdatabase{lock: &sync.Mutex{},
db: make(map[string]map[string]logrecord),
dirty: false,
rollingLog: MakeRollingLog(time.Hour * 24),
}
r.sweeper = time.NewTicker(10 * time.Second)
go func() {
handledError := false
for {
<-r.sweeper.C
err := r.DumpDatabaseToFile("")
if err != nil {
if handledError == false {
log.Printf("Error when saving database %s", err.Error())
handledError = true
}
} else {
handledError = true
}
}
}()
return &r
}
// Checks that the map contains the URI
func (ldb *Logdatabase) containsURI(uri string, realm string) bool {
ldb.lock.Lock()
defer ldb.lock.Unlock()
realmMap, ok := ldb.db[realm]
if ok {
_, ok = realmMap[uri]
}
return ok
}
// Updates the database
func (ldb *Logdatabase) updateURI(uri string, realm string) logrecord {
ldb.lock.Lock()
defer ldb.lock.Unlock()
realmMap, exists := ldb.db[realm]
if !exists {
return logrecord{Title: "", Count: 0}
}
i, exists := realmMap[uri]
if !exists {
// special case handling for the "hit" realm
if realm == "hit" {
info := ValidateURL("https://sheep.horse" + uri)
if info.err != nil {
return logrecord{Title: "", Count: 0}
}
i.Title = info.title
} else {
return logrecord{Title: "", Count: 0}
}
}
if realm == "hit" {
ldb.rollingLog.AddEntry(uri)
}
i.Count = i.Count + 1
realmMap[uri] = i
ldb.dirty = true
return i
}
func (ldb *Logdatabase) getURINoLock(uri string, realm string) (logrecord, bool) {
var i logrecord
realmdb, ok := ldb.db[realm]
if !ok {
return i, ok
}
i, ok = realmdb[uri]
return i, ok
}
// Gets a value from the database
func (ldb *Logdatabase) getURI(uri string, realm string) (logrecord, bool) {
ldb.lock.Lock()
defer ldb.lock.Unlock()
return ldb.getURINoLock(uri, realm)
}
func (ldb *Logdatabase) marshalDatabase() ([]byte, error) {
result, err := json.MarshalIndent(ldb.db, "", " ")
return result, err
}
// DumpDatabaseRealm - copies part of the database to an array
func (ldb *Logdatabase) DumpDatabaseRealm(realm string) ([]byte, error) {
ldb.lock.Lock()
defer ldb.lock.Unlock()
realmdb, found := ldb.db[realm]
if !found {
return nil, errors.New("Realm not found")
}
result, err := json.MarshalIndent(realmdb, "", " ")
return result, err
}
// DumpDatabaseToFile - dumps the database to a file
func (ldb *Logdatabase) DumpDatabaseToFile(filename string) error {
ldb.lock.Lock()
defer ldb.lock.Unlock()
if filename == "" {
filename = ldb.filename
}
if ldb.dirty == false {
return nil
}
contents, err := ldb.marshalDatabase()
if err != nil {
return err
}
err = ioutil.WriteFile(filename, contents, 0666)
if err == nil {
ldb.dirty = false
}
return err
}
// LoadDatabase - loads from a file
func (ldb *Logdatabase) LoadDatabase(filename string) {
file, e := ioutil.ReadFile(filename)
if e != nil {
log.Fatalf("Could not open database file: " + filename)
os.Exit(1)
}
var data map[string]map[string]logrecord
json.Unmarshal(file, &data)
ldb.lock.Lock()
defer ldb.lock.Unlock()
ldb.db = data
ldb.dirty = false
ldb.filename = filename
}
// DumpRollingLog - json representation of the rolling log
func (ldb *Logdatabase) DumpRollingLog() ([]byte, error) {
ldb.lock.Lock()
defer ldb.lock.Unlock()
rollingLogCounts := ldb.rollingLog.GetAllCounts()
data := make(map[string]logrecord)
for k, v := range rollingLogCounts {
r, _ := ldb.getURINoLock(k, "hit")
r.Count = v
data[k] = r
}
result, err := json.MarshalIndent(data, "", " ")
return result, err
}