-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
182 lines (160 loc) · 4.69 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
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
package main
import (
"context"
"fmt"
"github.com/akrylysov/pogreb"
"github.com/kfsone/gomenacing/pkg/gomschema"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
"log"
"path/filepath"
)
type Database struct {
storePath string
}
func OpenDatabase(path string, dbName string) (*Database, error) {
database := Database{storePath: filepath.Join(path, dbName)}
if _, err := ensureDirectory(database.Path()); err != nil {
return nil, err
}
return &database, nil
}
func (db Database) Close() {
// noop
}
func (db Database) Path() string {
return db.storePath
}
func (db *Database) GetSchema(name string) (schema *Schema, err error) {
path := filepath.Join(db.Path(), name)
store, err := pogreb.Open(path, nil)
if err != nil {
return nil, err
}
schema = &Schema{db, name, store}
return schema, nil
}
// Returns an open handle to the commodity schema
func (db *Database) Commodities() (*Schema, error) {
return db.GetSchema("commodities")
}
// Returns an open handle to the facility schema
func (db *Database) Facilities() (*Schema, error) {
return db.GetSchema("facilities")
}
func (db *Database) Listings() (*Schema, error) {
return db.GetSchema("listings")
}
// Returns an open handle to the system schema
func (db *Database) Systems() (*Schema, error) {
return db.GetSchema("systems")
}
func getSchemaForMessage(db *Database, message proto.Message) (*Schema, error) {
switch v := message.(type) {
case *gomschema.Commodity:
return db.Commodities()
case *gomschema.System:
return db.Systems()
case *gomschema.Facility:
return db.Facilities()
case *gomschema.FacilityListing:
return db.Listings()
default:
return nil, fmt.Errorf("%w: message type: %t", ErrUnknownEntity, v)
}
}
func (db *Database) loadSystems(sdb *SystemDatabase) error {
schema, err := db.Systems()
var loaded int
if err == nil {
sdb.systemIDs = make(map[string]EntityID, schema.Count())
sdb.systemsByID = make(map[EntityID]*System, schema.Count())
temporary := &gomschema.System{}
loader, err := NewTypedDataLoader("gom", temporary, func() error { return sdb.newSystem(temporary) })
if err == nil {
loaded, err = schema.LoadData(loader)
if err == nil {
log.Printf("Loaded %d Systems.", loaded)
}
}
}
return err
}
func (db *Database) loadFacilities(sdb *SystemDatabase) error {
schema, err := db.Facilities()
var loaded int
if err == nil {
sdb.facilitiesByID = make(map[EntityID]*Facility, schema.Count())
temporary := &gomschema.Facility{}
loader, err := NewTypedDataLoader("gom", temporary, func() error { return sdb.newFacility(temporary) })
if err == nil {
loaded, err = schema.LoadData(loader)
if err == nil {
log.Printf("Loaded %d Facilities.", loaded)
}
}
}
return err
}
func (db *Database) loadCommodities(sdb *SystemDatabase) error {
schema, err := db.Commodities()
var loaded int
if err == nil {
sdb.commodityIDs = make(map[string]EntityID, schema.Count())
sdb.commoditiesByID = make(map[EntityID]*Commodity, schema.Count())
temporary := &gomschema.Commodity{}
loader, err := NewTypedDataLoader("gom", temporary, func() error { return sdb.newCommodity(temporary) })
if err == nil {
loaded, err = schema.LoadData(loader)
if err == nil {
log.Printf("Loaded %d Commodities.", loaded)
}
}
}
return err
}
func (db *Database) loadListings(sdb *SystemDatabase) error {
schema, err := db.Listings()
loaded, lineItems := 0, 0
if err == nil {
temporary := &gomschema.FacilityListing{}
loader, err := NewTypedDataLoader("gom", temporary, func() error {
lineItems += len(temporary.Listings)
return sdb.newListings(temporary)
})
if err == nil {
loaded, err = schema.LoadData(loader)
if err == nil {
log.Printf("Loaded %d Listings at %d Facilities.", lineItems, loaded)
}
}
}
return err
}
func (db *Database) LoadDatabase(sdb *SystemDatabase) (err error) {
///TODO: Speed up import-load by making import a part of load.
/// Thus we can load commodities as soon as we've imported them.
/// We could also break up checks into resource channels, so work
/// can be done out of order.
/// Load-from-file -> unmarshal -> conditions -> register.
ctx := context.Background()
eg, ctx := errgroup.WithContext(ctx)
// Systems and facilities need to be loaded synchronously for now.
///TODO: Evaluate making loadFacilities not associate facility->system
/// Make sdb.facilitiesByID be map[EntityID]*Facility
eg.Go(func() error {
if err = db.loadSystems(sdb); err == nil {
err = db.loadFacilities(sdb)
}
return err
})
// We can load the commodity list in parallel
eg.Go(func() error {
return db.loadCommodities(sdb)
})
if err := eg.Wait(); err != nil {
return err
}
// Now import any prices.
return db.loadListings(sdb)
}