forked from IceFireDB/IceFireDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
177 lines (157 loc) · 3.8 KB
/
main.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
/*
* @Author: gitsrc
* @Date: 2021-03-08 13:09:44
* @LastEditors: gitsrc
* @LastEditTime: 2021-08-20 10:50:01
* @FilePath: /IceFireDB/main.go
*/
package main
import (
"fmt"
"io"
"net/http"
_ "net/http/pprof"
"path/filepath"
"sync/atomic"
badger "github.com/dgraph-io/badger/v3"
lediscfg "github.com/ledisdb/ledisdb/config"
"github.com/ledisdb/ledisdb/ledis"
"github.com/syndtr/goleveldb/leveldb"
"github.com/tidwall/sds"
rafthub "github.com/tidwall/uhaha"
_ "github.com/IceFireDB/IceFireDB/driver/badger"
"github.com/IceFireDB/IceFireDB/driver/hybriddb"
"github.com/IceFireDB/IceFireDB/driver/ipfs"
"github.com/IceFireDB/IceFireDB/driver/orbitdb"
"github.com/IceFireDB/IceFireDB/driver/oss"
"github.com/IceFireDB/IceFireDB/utils"
)
var (
// storageBackend select storage Engine
storageBackend string
// pprof listen
pprofAddr string
// debug
debug bool
)
func main() {
conf.Name = "IceFireDB"
conf.Version = "1.0.0"
conf.GitSHA = BuildVersion
conf.Flag.Custom = true
confInit(&conf)
conf.DataDirReady = func(dir string) {
//os.RemoveAll(filepath.Join(dir, "main.db"))
ldsCfg = lediscfg.NewConfigDefault()
ldsCfg.DataDir = filepath.Join(dir, "main.db")
ldsCfg.Databases = 1
ldsCfg.DBName = storageBackend
var err error
le, err = ledis.Open(ldsCfg)
if err != nil {
panic(err)
}
ldb, err = le.Select(0)
if err != nil {
panic(err)
}
// Obtain the leveldb object and handle it carefully
driver := ldb.GetSDB().GetDriver().GetStorageEngine()
switch v := driver.(type) {
case *leveldb.DB:
db = v
case *badger.DB:
default:
panic(fmt.Errorf("unsupported storage is caused: %T", v))
}
if storageBackend == hybriddb.StorageName {
serverInfo.RegisterExtInfo(ldb.GetSDB().GetDriver().(*hybriddb.DB).Metrics)
}
if storageBackend == ipfs.StorageName {
serverInfo.RegisterExtInfo(ldb.GetSDB().GetDriver().(*ipfs.DB).Metrics)
}
if storageBackend == orbitdb.StorageName {
serverInfo.RegisterExtInfo(ldb.GetSDB().GetDriver().(*orbitdb.DB).Metrics)
}
if storageBackend == oss.StorageName {
//serverInfo.RegisterExtInfo(ldb.GetSDB().GetDriver().(*orbitdb.DB).Metrics)
}
}
if debug {
// pprof for profiling
go func() {
http.ListenAndServe(pprofAddr, nil)
}()
}
conf.Snapshot = snapshot
conf.Restore = restore
conf.ConnOpened = connOpened
conf.ConnClosed = connClosed
conf.CmdRewriteFunc = utils.RedisCmdRewrite
fmt.Printf("start with Storage Engine: %s\n", storageBackend)
rafthub.Main(conf)
}
type snap struct {
s *leveldb.Snapshot
}
func (s *snap) Done(path string) {}
func (s *snap) Persist(wr io.Writer) error {
sw := sds.NewWriter(wr)
iter := s.s.NewIterator(nil, nil)
for ok := iter.First(); ok; ok = iter.Next() {
if err := sw.WriteBytes(iter.Key()); err != nil {
return err
}
if err := sw.WriteBytes(iter.Value()); err != nil {
return err
}
}
iter.Release()
if err := iter.Error(); err != nil {
return err
}
return sw.Flush()
}
func snapshot(data interface{}) (rafthub.Snapshot, error) {
s, err := db.GetSnapshot()
if err != nil {
return nil, err
}
return &snap{s: s}, nil
}
func restore(rd io.Reader) (interface{}, error) {
sr := sds.NewReader(rd)
var batch leveldb.Batch
for {
key, err := sr.ReadBytes()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
value, err := sr.ReadBytes()
if err != nil {
return nil, err
}
batch.Put(key, value)
if batch.Len() == 1000 {
if err := db.Write(&batch, nil); err != nil {
return nil, err
}
batch.Reset()
}
}
if err := db.Write(&batch, nil); err != nil {
return nil, err
}
return nil, nil
}
func connOpened(addr string) (context interface{}, accept bool) {
atomic.AddInt64(&respClientNum, 1)
return nil, true
}
func connClosed(context interface{}, addr string) {
atomic.AddInt64(&respClientNum, -1)
return
}