summary refs log tree commit diff stats
path: root/cache.go
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-21 16:04:10 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-21 16:04:10 -0400
commitb29e1c163f3cc88037f301c1cc1d71c7707ee64f (patch)
treeb06e51c3e27a8fb70f3801a27a957e5f7cae4bcb /cache.go
parent137e237b17b770e9697e7367c1ee01f4f3bb5717 (diff)
downloadgetwtxt-b29e1c163f3cc88037f301c1cc1d71c7707ee64f.tar.gz
review of db functions
Diffstat (limited to 'cache.go')
-rw-r--r--cache.go90
1 files changed, 62 insertions, 28 deletions
diff --git a/cache.go b/cache.go
index 253de98..be3d7d3 100644
--- a/cache.go
+++ b/cache.go
@@ -10,18 +10,20 @@ import (
 	"github.com/syndtr/goleveldb/leveldb"
 )
 
-// checks if it's time to refresh the cache or not
+// Checks whether it's time to refresh
+// the cache.
 func checkCacheTime() bool {
 	return time.Since(confObj.lastCache) > confObj.cacheInterval
 }
 
-// checks if it's time to push the cache to the database
+// Checks whether it's time to push
+// the cache to the database
 func checkDBtime() bool {
 	return time.Since(confObj.lastPush) > confObj.dbInterval
 }
 
-// launched by init as a goroutine to constantly watch
-// for the update interval to pass
+// Launched by init as a goroutine to constantly watch
+// for the update interval to pass.
 func cacheAndPush() {
 	for {
 		if checkCacheTime() {
@@ -35,9 +37,11 @@ func cacheAndPush() {
 	}
 }
 
-// refreshes the cache
+// Refreshes the cache.
 func refreshCache() {
 
+	// Iterate over the registry and
+	// update each individual user.
 	for k := range twtxtCache.Reg {
 		err := twtxtCache.UpdateUser(k)
 		if err != nil {
@@ -46,6 +50,9 @@ func refreshCache() {
 		}
 	}
 
+	// Re-scrape all the remote registries
+	// to see if they have any new users
+	// to add locally.
 	for _, v := range remoteRegistries.List {
 		err := twtxtCache.ScrapeRemoteRegistry(v)
 		if err != nil {
@@ -57,40 +64,46 @@ func refreshCache() {
 	confObj.mu.Unlock()
 }
 
-// pushes the registry's cache data to a local
-// database for safe keeping
+// Pushes the registry's cache data to a local
+// database for safe keeping.
 func pushDatabase() error {
+	// Acquire the database from the aether.
+	// goleveldb is concurrency-safe, so we
+	// can immediately push it back into the
+	// channel for other functions to use.
 	db := <-dbChan
-	twtxtCache.Mu.RLock()
+	dbChan <- db
 
-	// create a batch write job so it can
+	// Create a batch write job so it can
 	// be done at one time rather than
-	// per value
+	// per entry.
+	twtxtCache.Mu.RLock()
 	var dbBasket *leveldb.Batch
 	for k, v := range twtxtCache.Reg {
-		dbBasket.Put([]byte(k+".Nick"), []byte(v.Nick))
-		dbBasket.Put([]byte(k+".URL"), []byte(v.URL))
-		dbBasket.Put([]byte(k+".IP"), []byte(v.IP))
-		dbBasket.Put([]byte(k+".Date"), []byte(v.Date))
+		dbBasket.Put([]byte(k+"*Nick"), []byte(v.Nick))
+		dbBasket.Put([]byte(k+"*URL"), []byte(v.URL))
+		dbBasket.Put([]byte(k+"*IP"), []byte(v.IP))
+		dbBasket.Put([]byte(k+"*Date"), []byte(v.Date))
 		for i, e := range v.Status {
-			dbBasket.Put([]byte(k+".Status."+i.String()), []byte(e))
+			rfc := i.Format(time.RFC3339)
+			dbBasket.Put([]byte(k+"*Status*"+rfc), []byte(e))
 		}
 	}
+	twtxtCache.Mu.RUnlock()
 
-	// save our list of remote registries to scrape
+	// Save our list of remote registries to scrape.
 	for k, v := range remoteRegistries.List {
-		dbBasket.Put([]byte("remote."+string(k)), []byte(v))
+		dbBasket.Put([]byte("remote*"+string(k)), []byte(v))
 	}
 
-	// execute the batch job
+	// Execute the batch job.
 	if err := db.Write(dbBasket, nil); err != nil {
 		return err
 	}
 
-	twtxtCache.Mu.RUnlock()
-	dbChan <- db
-
-	// update the last push time
+	// Update the last push time for
+	// our timer/watch function to
+	// reference.
 	confObj.mu.Lock()
 	confObj.lastPush = time.Now()
 	confObj.mu.Unlock()
@@ -98,29 +111,42 @@ func pushDatabase() error {
 	return nil
 }
 
-// pulls registry data from the DB on startup
+// Pulls registry data from the DB on startup.
+// Iterates over the database one entry at a time.
 func pullDatabase() {
+	// Acquire the database from the aether.
+	// goleveldb is concurrency-safe, so we
+	// can immediately push it back into the
+	// channel for other functions to use.
 	db := <-dbChan
+	dbChan <- db
 
 	iter := db.NewIterator(nil, nil)
 
+	// Read the database key-by-key
 	for iter.Next() {
 		key := iter.Key()
 		val := iter.Value()
 
-		split := strings.Split(string(key), ".")
+		split := strings.Split(string(key), "*")
 		urls := string(split[0])
 		field := string(split[1])
-		data := registry.NewUserData()
 
+		// Start with an empty Data struct. If
+		// there's already one in the cache, pull
+		// it and use it instead.
+		data := registry.NewUserData()
 		twtxtCache.Mu.RLock()
 		if _, ok := twtxtCache.Reg[urls]; ok {
 			data = twtxtCache.Reg[urls]
 		}
 		twtxtCache.Mu.RUnlock()
-
 		ref := reflect.ValueOf(data).Elem()
 
+		// Use reflection to find the right field
+		// in the Data struct. Once found, assign
+		// the value and break so the DB iteration
+		// can continue.
 		if field != "Status" && urls != "remote" {
 			for i := 0; i < ref.NumField(); i++ {
 
@@ -132,6 +158,9 @@ func pullDatabase() {
 			}
 		} else if field == "Status" && urls != "remote" {
 
+			// If we're looking at a Status entry in the DB,
+			// parse the time then add it to the TimeMap under
+			// data.Status
 			thetime, err := time.Parse("RFC3339", split[2])
 			if err != nil {
 				log.Printf("%v\n", err)
@@ -139,11 +168,18 @@ func pullDatabase() {
 			data.Status[thetime] = string(val)
 
 		} else {
+			// The third and final possibility is
+			// if we've come across an entry for
+			// a remote twtxt registry to scrape.
+			// If so, add it to our list.
 			remoteRegistries.Mu.Lock()
 			remoteRegistries.List = append(remoteRegistries.List, string(val))
 			remoteRegistries.Mu.Unlock()
+			continue
 		}
 
+		// Push the data struct (back) into
+		// the cache.
 		twtxtCache.Mu.Lock()
 		twtxtCache.Reg[urls] = data
 		twtxtCache.Mu.Unlock()
@@ -154,6 +190,4 @@ func pullDatabase() {
 	if err != nil {
 		log.Printf("Error while pulling DB into registry cache: %v\n", err)
 	}
-
-	dbChan <- db
 }
ik Agaram <vc@akkartik.com> 2020-09-13 00:41:09 -0700 committer Kartik Agaram <vc@akkartik.com> 2020-09-13 00:41:09 -0700 6776 - new app: a programming environment' href='/akkartik/mu/commit/apps/tile/surface.mu?h=hlt&id=40d40b83decac3d4f9a3da2dc222d19d1ab704f1'>40d40b83 ^
a3f77915 ^
40d40b83 ^
a3f77915 ^
40d40b83 ^



a3f77915 ^
40d40b83 ^

a3f77915 ^
40d40b83 ^
a3f77915 ^
40d40b83 ^



a3f77915 ^
40d40b83 ^

a3f77915 ^
40d40b83 ^
a3f77915 ^
40d40b83 ^



a3f77915 ^
40d40b83 ^


a3f77915 ^
40d40b83 ^


a3f77915 ^
40d40b83 ^

74f1512f ^
40d40b83 ^

74f1512f ^
40d40b83 ^



a3f77915 ^
40d40b83 ^

a3f77915 ^
40d40b83 ^


a3f77915 ^
40d40b83 ^

74f1512f ^
40d40b83 ^

74f1512f ^
40d40b83 ^



a3f77915 ^
40d40b83 ^









74f1512f ^
40d40b83 ^

74f1512f ^
40d40b83 ^










































































































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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412