forked from digisan/event-mgr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.go
More file actions
82 lines (74 loc) · 1.85 KB
/
Copy pathdb.go
File metadata and controls
82 lines (74 loc) · 1.85 KB
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
package eventmgr
import (
"path/filepath"
"sync"
"github.com/dgraph-io/badger/v4"
lk "github.com/digisan/logkit"
)
type DBGrp struct {
sync.Mutex
SpanID *badger.DB // span : event ids
IDEvt *badger.DB // event id : event content
MyID *badger.DB // uname : self own event ids
BookmarkID *badger.DB // uname : bookmarked event ids
IDFlwID *badger.DB // event id : follower event ids
IDPtps *badger.DB // event id : uname participants
}
var (
onceDB sync.Once // do once
DbGrp *DBGrp // global, for keeping single instance
)
func open(dir string) *badger.DB {
opt := badger.DefaultOptions("").WithInMemory(true)
if dir != "" {
opt = badger.DefaultOptions(dir)
opt.Logger = nil
}
db, err := badger.Open(opt)
lk.FailOnErr("%v", err)
return db
}
// init global 'eDB'
func InitDB(dir string) *DBGrp {
if DbGrp == nil {
onceDB.Do(func() {
DbGrp = &DBGrp{
SpanID: open(filepath.Join(dir, "span-ids")),
IDEvt: open(filepath.Join(dir, "id-event")),
MyID: open(filepath.Join(dir, "my-ids")),
BookmarkID: open(filepath.Join(dir, "bookmark-ids")),
IDFlwID: open(filepath.Join(dir, "id-flwids")),
IDPtps: open(filepath.Join(dir, "id-ptps")),
}
})
}
return DbGrp
}
func CloseDB() {
DbGrp.Lock()
defer DbGrp.Unlock()
if DbGrp.SpanID != nil {
lk.FailOnErr("%v", DbGrp.SpanID.Close())
DbGrp.SpanID = nil
}
if DbGrp.IDEvt != nil {
lk.FailOnErr("%v", DbGrp.IDEvt.Close())
DbGrp.IDEvt = nil
}
if DbGrp.MyID != nil {
lk.FailOnErr("%v", DbGrp.MyID.Close())
DbGrp.MyID = nil
}
if DbGrp.BookmarkID != nil {
lk.FailOnErr("%v", DbGrp.BookmarkID.Close())
DbGrp.BookmarkID = nil
}
if DbGrp.IDFlwID != nil {
lk.FailOnErr("%v", DbGrp.IDFlwID.Close())
DbGrp.IDFlwID = nil
}
if DbGrp.IDPtps != nil {
lk.FailOnErr("%v", DbGrp.IDPtps.Close())
DbGrp.IDPtps = nil
}
}