-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.go
62 lines (52 loc) · 1.21 KB
/
delete.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
package main
import (
"context"
"math/rand"
"os"
"time"
"github.com/nbd-wtf/go-nostr"
)
func deleteOldStuffRoutine() {
for {
entries, err := os.ReadDir(s.DatabaseDir)
if err != nil {
log.Error().Err(err).Msg("failed to list databases")
continue
}
entry := entries[rand.Intn(len(entries))]
country := entry.Name()
if !entry.IsDir() && len(country) != 2 {
continue
}
db := getDatabaseForCountry(country)
log.Debug().Str("country", country).Msg("deleting old stuff")
prevMaxLimit := db.MaxLimit
db.MaxLimit = 2500
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
_ = cancel
ch, err := db.QueryEvents(ctx, nostr.Filter{Kinds: []int{1}, Limit: 1500})
if err != nil {
log.Error().Err(err).Str("country", country).Msg("error querying")
db.MaxLimit = prevMaxLimit
continue
}
// we will keep the first 500
count := 0
for range ch {
// skip the first 500
count++
if count == 500 {
break
}
}
count = 0
for evt := range ch {
// now we delete all of these
db.DeleteEvent(ctx, evt)
count++
}
log.Debug().Str("country", country).Int("events", count).Msg("deleted")
db.MaxLimit = prevMaxLimit
time.Sleep(time.Hour * 7)
}
}