-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
399 lines (361 loc) · 10.3 KB
/
storage.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package storage
import (
"database/sql"
"errors"
"fmt"
"log"
"os"
"reflect"
"github.com/cayleygraph/cayley"
"github.com/cayleygraph/cayley/graph"
_ "github.com/cayleygraph/cayley/graph/bolt"
_ "github.com/cayleygraph/cayley/graph/sql"
"github.com/cayleygraph/cayley/quad" // Need for Cayley to connect to database
"github.com/cayleygraph/cayley/schema"
"github.com/edfungus/conduction/messenger"
"github.com/sirupsen/logrus"
)
// Logger logs but can be replaced
var Logger = logrus.New()
type Storage interface {
SaveFlow(flow Flow) (Key, error)
GetFlowByKey(key Key) (Flow, error)
SavePath(path messenger.Path) (Key, error)
GetPathByKey(key Key) (messenger.Path, error)
GetKeyOfPath(path messenger.Path) (Key, error)
ChainNextFlowToPath(flowKey Key, pathKey Key) error
GetNextFlows(key Key) ([]Flow, []Key, error)
}
const (
databaseURL string = "postgresql://%s@%s:%d/%s?sslmode=disable"
)
var (
ErrFlowCannotBeRetrieved error = fmt.Errorf("Could not retrieve Flow from storage")
ErrPathCannotBeRetrieved error = fmt.Errorf("Could not retrieve Path from storage")
ErrResolvingKey error = fmt.Errorf("Error resolving key in database")
)
type flowDTO struct {
ID quad.IRI `quad:"@id"`
Name string `quad:"name"`
Description string `quad:"description"`
Path quad.IRI `quad:"path"`
}
// NewFlowDTO returns a new flowDTO
func NewFlowDTO(id quad.IRI, name string, description string, path quad.IRI) flowDTO {
return flowDTO{
ID: id,
Name: name,
Description: description,
Path: path,
}
}
type pathDTO struct {
ID quad.IRI `quad:"@id"`
Route string `quad:"route"`
Type string `quad:"type"`
Flows []quad.IRI `quad:"triggers,optional"`
}
func NewPathDTO(id quad.IRI, route string, pathType string, flows []quad.IRI) pathDTO {
return pathDTO{
ID: id,
Route: route,
Type: pathType,
Flows: flows,
}
}
type GraphStorageConfig struct {
Host string
Port int
User string
DatabaseName string
DatabaseType string
}
// GetDatabaseConnectionPath returns a constructed URL from the database details in config
func (gsc GraphStorageConfig) GetDatabaseConnectionPath() string {
return fmt.Sprintf(databaseURL, gsc.User, gsc.Host, gsc.Port, gsc.DatabaseName)
}
type GraphStorage struct {
store *cayley.Handle
}
// NewGraphStorage returns a new Storage that uses Cayley and CockroachDB
func NewGraphStorage(config GraphStorageConfig) (*GraphStorage, error) {
err := setupDatabaseForGraphStore(config)
if err != nil {
return nil, err
}
store, err := cayley.NewGraph("sql", config.GetDatabaseConnectionPath(), graph.Options{"flavor": config.DatabaseType})
if err != nil {
return nil, err
}
return &GraphStorage{
store: store,
}, nil
}
// NewGraphStorageBolt allows graph to be stored in a file instead sql above. This is to dodge the issue with inserting a struct of optional or empty array in to the graph (postgres issue: https://github.com/cayleygraph/cayley/issues/563)
func NewGraphStorageBolt(filepath string) (*GraphStorage, error) {
tmpfile, err := os.Open(filepath)
if err != nil {
log.Fatal(err)
}
graph.InitQuadStore("bolt", tmpfile.Name(), nil)
store, err := cayley.NewGraph("bolt", tmpfile.Name(), nil)
if err != nil {
return nil, err
}
return &GraphStorage{
store: store,
}, nil
}
// SaveFlow adds a new Flow to the graph. If the Path does not exist, it will be added, else it will be made
func (gs *GraphStorage) SaveFlow(flow Flow) (Key, error) {
pathKey, err := gs.SavePath(*flow.Path)
if err != nil {
return Key{}, err
}
flowKey := NewRandomKey()
flowDTO := NewFlowDTO(flowKey.QuadIRI(), flow.Name, flow.Description, pathKey.QuadIRI())
err = gs.writeToGraph(flowDTO)
if err != nil {
return Key{}, err
}
return flowKey, nil
}
// GetFlowByKey returns a Flow of the sepcified uuid from the graph
func (gs *GraphStorage) GetFlowByKey(key Key) (Flow, error) {
var flowDTO flowDTO
err := schema.LoadTo(nil, gs.store, &flowDTO, key.QuadValue())
if err != nil {
return Flow{}, ErrFlowCannotBeRetrieved
}
pathKey, err := NewKeyFromQuadIRI(flowDTO.Path)
if err != nil {
return Flow{}, err
}
path, err := gs.GetPathByKey(pathKey)
if err != nil {
return Flow{}, err
}
return Flow{
Name: flowDTO.Name,
Description: flowDTO.Description,
Path: &messenger.Path{
Route: path.Route,
Type: path.Type,
},
}, nil
}
// SavePath adds path to graph if new, else it will return the id of the existing path. Path are unique based on route and type combined
func (gs *GraphStorage) SavePath(path messenger.Path) (Key, error) {
pathExists, err := gs.doesPathExists(path)
if err != nil {
return Key{}, err
}
if pathExists {
return gs.GetKeyOfPath(path)
}
pathKey := NewRandomKey()
pathDTO := NewPathDTO(pathKey.QuadIRI(), path.Route, path.Type, nil)
err = gs.writeToGraph(pathDTO)
if err != nil {
return Key{}, err
}
return pathKey, nil
}
// GetKeyOfPath returns the Key of a given Path if it exists
func (gs *GraphStorage) GetKeyOfPath(path messenger.Path) (Key, error) {
pathDTOList, err := gs.getPathDTOsByRouteAndType(path.Route, path.Type)
if err != nil {
return Key{}, err
}
switch length := len(pathDTOList); length {
case 0:
return Key{}, errors.New("Path was not found in graph store")
case 1:
pathKey, err := NewKeyFromQuadIRI(pathDTOList[0].ID)
if err != nil {
return Key{}, err
}
return pathKey, nil
default:
return Key{}, errors.New("There are multiple matching Paths. They are expected to be unique")
}
}
// GetPathByKey returns Path based on uuid.
func (gs *GraphStorage) GetPathByKey(key Key) (messenger.Path, error) {
var pathDTO pathDTO
err := schema.LoadTo(nil, gs.store, &pathDTO, key.QuadValue())
if err != nil {
return messenger.Path{}, ErrPathCannotBeRetrieved
}
return messenger.Path{
Route: pathDTO.Route,
Type: pathDTO.Type,
}, nil
}
// ChainNextFlowToPath connects Flows to be triggered by a Path
func (gs *GraphStorage) ChainNextFlowToPath(flowKey Key, pathKey Key) error {
_, err := gs.GetFlowByKey(flowKey)
if err != nil {
return err
}
_, err = gs.GetPathByKey(pathKey)
if err != nil {
return err
}
err = gs.linkKeyToTriggerKey(pathKey, flowKey)
if err != nil {
return err
}
return nil
}
// GetNextFlows returns a list of Flows that are triggers by the Flow
func (gs *GraphStorage) GetNextFlows(key Key) ([]Flow, []Key, error) {
flowKeyList, err := gs.getKeysTriggeredByKey(key)
if err != nil {
return nil, nil, err
}
var flowList []Flow
for _, v := range flowKeyList {
flow, err := gs.GetFlowByKey(v)
if err != nil {
return nil, nil, err
}
flowList = append(flowList, flow)
}
return flowList, flowKeyList, nil
}
func (gs *GraphStorage) doesPathExists(path messenger.Path) (bool, error) {
pathDTOList, err := gs.getPathDTOsByRouteAndType(path.Route, path.Type)
if err != nil {
return false, nil
}
if len(pathDTOList) > 0 {
return true, nil
}
return false, nil
}
func (gs *GraphStorage) getPathDTOsByRouteAndType(pathRoute string, pathType string) ([]pathDTO, error) {
p := cayley.StartPath(gs.store, quad.StringToValue(pathType)).In(quad.IRI("type")).Has(quad.IRI("route"), quad.StringToValue(pathRoute))
var pathDTOs []pathDTO
err := schema.LoadIteratorTo(nil, gs.store, reflect.ValueOf(&pathDTOs), p.BuildIterator())
return pathDTOs, err
}
func (gs *GraphStorage) linkKeyToTriggerKey(originKey Key, destinationKey Key) error {
return gs.store.AddQuad(quad.Make(originKey.QuadValue(), quad.IRI("triggers"), destinationKey.QuadValue(), nil))
}
func (gs *GraphStorage) getKeysTriggeredByKey(key Key) ([]Key, error) {
p := cayley.StartPath(gs.store, key.QuadValue()).Out(quad.IRI("triggers"))
keyQuadList, err := p.Iterate(nil).AllValues(gs.store)
if err != nil {
return nil, ErrResolvingKey
}
keys, err := convertQuadValueListToKeyList(keyQuadList)
if err != nil {
return nil, err
}
return keys, nil
}
func convertQuadValueListToKeyList(quads []quad.Value) ([]Key, error) {
var keys []Key
for _, v := range quads {
key, err := NewKeyFromQuadValue(v)
if err != nil {
return nil, err
}
keys = append(keys, key)
}
return keys, nil
}
func (gs *GraphStorage) writeToGraph(dto interface{}) error {
writer := graph.NewWriter(gs.store)
_, err := schema.WriteAsQuads(writer, dto)
if err != nil {
return err
}
err = writer.Close()
if err != nil {
return err
}
return nil
}
// Close ends graph database session. Currently, it will delete temporary database file if used
func (gs *GraphStorage) Close() {
gs.store.Close()
}
func setupDatabaseForGraphStore(config GraphStorageConfig) error {
err := createSQLDatabaseIfNotExist(config)
if err != nil {
return err
}
err = initGraphStoreIfNotInitialized(config)
if err != nil {
return err
}
return nil
}
func createSQLDatabaseIfNotExist(config GraphStorageConfig) error {
databaseExist, err := doesDatabaseExist(config)
if err != nil {
return err
}
if !databaseExist {
err := createDatabase(config)
if err != nil {
return err
}
}
return nil
}
func initGraphStoreIfNotInitialized(config GraphStorageConfig) error {
databaseInitialized, err := isDatabaseInitializedForGraphStore(config)
if err != nil {
return err
}
if !databaseInitialized {
err := graph.InitQuadStore("sql", config.GetDatabaseConnectionPath(), graph.Options{"flavor": config.DatabaseType})
if err != nil {
return err
}
}
return nil
}
func doesDatabaseExist(config GraphStorageConfig) (bool, error) {
db, err := connectToSQLDatabase(config)
if err != nil {
return false, err
}
defer db.Close()
_, err = db.Exec(fmt.Sprintf("SET DATABASE = %s", config.DatabaseName))
if err != nil {
return false, nil
}
return true, nil
}
func createDatabase(config GraphStorageConfig) error {
db, err := connectToSQLDatabase(config)
if err != nil {
return err
}
defer db.Close()
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", config.DatabaseName))
return err
}
func isDatabaseInitializedForGraphStore(config GraphStorageConfig) (bool, error) {
db, err := connectToSQLDatabase(config)
if err != nil {
return false, err
}
defer db.Close()
_, err = db.Exec(fmt.Sprintf("SET DATABASE = %s; SELECT * FROM quads", config.DatabaseName))
if err != nil {
return false, nil
}
return true, nil
}
func connectToSQLDatabase(config GraphStorageConfig) (*sql.DB, error) {
db, err := sql.Open("postgres", config.GetDatabaseConnectionPath())
if err != nil {
return nil, err
}
return db, nil
}