-
Notifications
You must be signed in to change notification settings - Fork 0
/
boltdb.go
271 lines (239 loc) · 6.01 KB
/
boltdb.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
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
package boltdb
import (
"fmt"
"strings"
"sync"
"github.com/lunemec/ed-router/pkg/models/dump"
"github.com/pkg/errors"
bolt "go.etcd.io/bbolt"
)
var (
bucketX = []byte("x")
bucketY = []byte("y")
bucketZ = []byte("z")
bucketRoot = []byte("root")
bucketSystems = []byte("systems")
bucketNames = []byte("names")
)
type DB struct {
index *bolt.DB
galaxy *bolt.DB
// once is used to start import goroutines only once.
// used only while importing.
once sync.Once
input chan dump.System
wg sync.WaitGroup
errors []error
}
func Open(indexFile, galaxyFile string, readOnly bool) (*DB, error) {
var (
indexErrMsg = fmt.Sprintf("unable to open index DB: %s", indexFile)
galaxyErrMsg = fmt.Sprintf("unable to open galaxy DB: %s", galaxyFile)
)
var options bolt.Options
if readOnly {
options.ReadOnly = true
}
index, err := bolt.Open(indexFile, 0666, &options)
if err != nil {
return nil, errors.Wrap(err, indexErrMsg)
}
galaxy, err := bolt.Open(galaxyFile, 0666, &options)
if err != nil {
return nil, errors.Wrapf(err, galaxyErrMsg)
}
if !readOnly {
err = prepareIndexDB(index)
if err != nil {
return nil, errors.Wrap(err, indexErrMsg)
}
err = prepareGalaxyDB(galaxy)
if err != nil {
return nil, errors.Wrap(err, galaxyErrMsg)
}
}
return &DB{index: index, galaxy: galaxy, input: make(chan dump.System)}, nil
}
func prepareIndexDB(db *bolt.DB) error {
err := db.Update(func(tx *bolt.Tx) error {
// Create bucket for X coordinates
_, err := tx.CreateBucketIfNotExists(bucketX)
if err != nil {
return errors.Wrap(err, "unable to create X bucket")
}
// Create bucket for Y coordinates
_, err = tx.CreateBucketIfNotExists(bucketY)
if err != nil {
return errors.Wrap(err, "unable to create Y bucket")
}
// Create bucket for Z coordinates
_, err = tx.CreateBucketIfNotExists(bucketZ)
if err != nil {
return errors.Wrap(err, "unable to create Z bucket")
}
_, err = tx.CreateBucketIfNotExists(bucketRoot)
if err != nil {
return errors.Wrap(err, "unable to create root bucket")
}
return nil
})
if err != nil {
return errors.Wrap(err, "error preparing index DB buckets")
}
return nil
}
func prepareGalaxyDB(db *bolt.DB) error {
err := db.Update(func(tx *bolt.Tx) error {
// Main bucket for ID64 -> System JSON
_, err := tx.CreateBucketIfNotExists(bucketSystems)
if err != nil {
return errors.Wrap(err, "unable to create systems bucket")
}
// Bucket to translate name -> id64
_, err = tx.CreateBucketIfNotExists(bucketNames)
if err != nil {
return errors.Wrap(err, "unable to create names bucket")
}
return nil
})
if err != nil {
return errors.Wrap(err, "error preparing galaxy DB buckets")
}
return nil
}
func (db *DB) InsertSystem(s dump.System) error {
db.once.Do(func() { go db.consumer() })
db.input <- s
return nil
}
func (db *DB) StopInsert() error {
close(db.input)
db.wg.Wait()
if len(db.errors) != 0 {
msg := "Errors during import: \n %s"
var errs []string
for i, err := range db.errors {
errs = append(errs, fmt.Sprintf(" [%d] %+v", i, err))
}
return errors.Errorf(msg, strings.Join(errs, "\n"))
}
return nil
}
func (db *DB) Close() error {
errI := db.index.Close()
errG := db.galaxy.Close()
if errI != nil || errG != nil {
return errors.Errorf("error closing database: index: %+v galaxy : %+v", errI, errG)
}
return nil
}
func (db *DB) consumer() {
var (
indexChan = make(chan interface{})
galaxyChan = make(chan interface{})
errChan = make(chan error)
)
go func() {
for err := range errChan {
db.errors = append(db.errors, err)
}
}()
db.wg.Add(1)
go batchWriter(&db.wg, db.index, errChan, indexChan, IndexBatchWriterXYZBuckets)
db.wg.Add(1)
go batchWriter(&db.wg, db.galaxy, errChan, galaxyChan, GalaxyBatchWriter)
for inputSystem := range db.input {
indexChan <- System{
ID64: inputSystem.ID64,
X: inputSystem.Coordinates.X,
Y: inputSystem.Coordinates.Y,
Z: inputSystem.Coordinates.Z,
IsNeutron: NeutronInRange(inputSystem.Bodies),
IsScoopable: ScoopableInRange(inputSystem.Bodies),
}
galaxyChan <- inputSystem
}
close(indexChan)
close(galaxyChan)
db.wg.Wait()
close(errChan)
}
type batchWriterFunc func(db *bolt.DB, batch []interface{}) error
func batchWriter(wg *sync.WaitGroup, db *bolt.DB, errChan chan error, data chan interface{}, writerFunc batchWriterFunc) {
defer wg.Done()
var (
batchSize = 10000
batch []interface{}
)
consumed := 0
for inSystem := range data {
batch = append(batch, inSystem)
consumed++
if consumed == batchSize-1 {
err := writerFunc(db, batch)
if err != nil {
errChan <- err
}
batch = nil
consumed = 0
}
}
if len(batch) > 0 {
err := writerFunc(db, batch)
if err != nil {
errChan <- err
}
}
}
func NeutronInRange(bodies []dump.Body) bool {
// Anything within 1000ls is considered OK.
var maxDistance float64 = 1000
if bodies == nil || len(bodies) == 0 {
return false
}
for _, body := range bodies {
if body.Type != "Star" {
continue
}
// White dwarfs are not checked since they are considered "not worth it".
if body.SubType == "Neutron Star" {
if body.DistanceToArrival <= maxDistance {
return true
}
}
}
return false
}
func ScoopableInRange(bodies []dump.Body) bool {
// Anything within 1000ls is considered OK.
var maxDistance float64 = 1000
if bodies == nil || len(bodies) == 0 {
return false
}
for _, body := range bodies {
if body.Type != "Star" {
continue
}
switch body.SubType {
case
"A (Blue-White super giant) Star",
"A (Blue-White) Star",
"B (Blue-White super giant) Star",
"B (Blue-White) Star",
"F (White super giant) Star",
"F (White) Star",
"G (White-Yellow super giant) Star",
"G (White-Yellow) Star",
"K (Yellow-Orange giant) Star",
"K (Yellow-Orange) Star",
"M (Red dwarf) Star",
"M (Red giant) Star",
"M (Red super giant) Star",
"O (Blue-White) Star":
if body.DistanceToArrival <= maxDistance {
return true
}
}
}
return false
}