-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
62 lines (52 loc) · 1.47 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
package main
import (
"log"
"os"
"time"
bolt "go.etcd.io/bbolt"
)
var PERMANENTLYMUTED = []byte("permanent")
var (
db *bolt.DB
DBUSERS = []byte("users")
DBVALIDATION = []byte("validation") // key=validID val=username
DBFP = []byte("certfp") // key=value: certfp=username
DBSUBFORUMS = []byte("subforums")
DBALLTHREADS = []byte("allthreads")
DBTHREADTOSF = []byte("threadtosubforum") // For looking thread id -> subforum id
DBUSERTHREADS = []byte("userthreads") // for search
DBALLPOSTS = []byte("posts")
DBUSERPOSTS = []byte("userposts") // for search
DBCONSOLELOG = []byte("console") // log console commands
)
func dbCreateBuckets() error {
return db.Update(func(tx *bolt.Tx) error {
for _, b := range [][]byte{DBUSERS, DBVALIDATION, DBFP, DBSUBFORUMS, DBALLTHREADS, DBUSERTHREADS, DBALLPOSTS, DBUSERPOSTS, DBTHREADTOSF, DBCONSOLELOG} {
if _, err := tx.CreateBucketIfNotExists(b); err != nil {
return err
}
}
/*
Create bucket for each subforum id
*/
sf := tx.Bucket(DBSUBFORUMS)
for _, n := range GetAllSubforumIDs() {
if _, err := sf.CreateBucketIfNotExists([]byte(n)); err != nil {
return err
}
}
return nil
})
}
func initDatabase() {
var err error
db, err = bolt.Open(Configuration.Database, 0600, &bolt.Options{Timeout: time.Second})
if err != nil {
log.Println(err.Error())
os.Exit(2)
}
if err := dbCreateBuckets(); err != nil {
log.Println(err.Error())
os.Exit(3)
}
}