This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
/
config.go
278 lines (231 loc) · 7.36 KB
/
config.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
// Copyright (c) 2020 MindStand Technologies, Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package gogm
import (
"errors"
"fmt"
"github.com/cornelk/hashmap"
"github.com/neo4j/neo4j-go-driver/neo4j"
"github.com/sirupsen/logrus"
"reflect"
"strconv"
"strings"
)
var neoVersion float64
var log logrus.FieldLogger
func init() {
makeDefaultLogger()
}
func makeDefaultLogger() {
_log := logrus.New()
log = _log.WithField("error", "not initialized")
}
func getIsV4() bool {
return true
}
// Config Defined GoGM config
type Config struct {
// Host is the neo4j host
Host string `yaml:"host" json:"host" mapstructure:"host"`
// Port is the neo4j port
Port int `yaml:"port" json:"port" mapstructure:"port"`
// IsCluster specifies whether GoGM is connecting to a casual cluster or not
IsCluster bool `yaml:"is_cluster" json:"is_cluster" mapstructure:"is_cluster"`
// Username is the GoGM username
Username string `yaml:"username" json:"username" mapstructure:"username"`
// Password is the GoGM password
Password string `yaml:"password" json:"password" mapstructure:"password"`
// PoolSize is the size of the connection pool for GoGM
PoolSize int `yaml:"pool_size" json:"pool_size" mapstructure:"pool_size"`
Realm string `yaml:"realm" json:"realm" mapstructure:"realm"`
Encrypted bool `yaml:"encrypted" json:"encrypted" mapstructure:"encrypted"`
// Index Strategy defines the index strategy for GoGM
IndexStrategy IndexStrategy `yaml:"index_strategy" json:"index_strategy" mapstructure:"index_strategy"`
TargetDbs []string `yaml:"target_dbs" json:"target_dbs" mapstructure:"target_dbs"`
Logger logrus.FieldLogger `yaml:"-" json:"-" mapstructure:"-"`
// if logger is not nil log level will be ignored
LogLevel string `json:"log_level" yaml:"log_level" mapstructure:"log_level"`
}
// ConnectionString builds the neo4j bolt/bolt+routing connection string
func (c *Config) ConnectionString() string {
var protocol string
if c.IsCluster {
protocol = "bolt+routing"
} else {
protocol = "bolt"
}
// In case of special characters in password string
//password := url.QueryEscape(c.Password)
return fmt.Sprintf("%s://%s:%v", protocol, c.Host, c.Port)
}
// Index Strategy typedefs int to define different index approaches
type IndexStrategy int
const (
// ASSERT_INDEX ensures that all indices are set and sets them if they are not there
ASSERT_INDEX IndexStrategy = 0
// VALIDATE_INDEX ensures that all indices are set
VALIDATE_INDEX IndexStrategy = 1
// IGNORE_INDEX skips the index step of setup
IGNORE_INDEX IndexStrategy = 2
)
//holds mapped types
var mappedTypes = &hashmap.HashMap{}
//thread pool
var driver neo4j.Driver
//relationship + label
var mappedRelations = &relationConfigs{}
func makeRelMapKey(start, edge, direction, rel string) string {
return fmt.Sprintf("%s-%s-%v-%s", start, edge, direction, rel)
}
var isSetup = false
// Init sets up gogm. Takes in config object and variadic slice of gogm nodes to map.
// Note: Must pass pointers to nodes!
func Init(conf *Config, mapTypes ...interface{}) error {
return setupInit(false, conf, mapTypes...)
}
// Resets GoGM configuration
func Reset() {
mappedTypes = &hashmap.HashMap{}
mappedRelations = &relationConfigs{}
isSetup = false
}
var internalConfig *Config
// internal setup logic for gogm
func setupInit(isTest bool, conf *Config, mapTypes ...interface{}) error {
if isSetup && !isTest {
return errors.New("gogm has already been initialized")
} else if isTest && isSetup {
mappedRelations = &relationConfigs{}
}
if !isTest {
if conf == nil {
return errors.New("config can not be nil")
}
}
if conf != nil {
if conf.Logger != nil {
log = conf.Logger
} else {
_log := logrus.New()
// set info if nothing has been set
if conf.LogLevel == "" {
conf.LogLevel = "INFO"
}
lvl, err := logrus.ParseLevel(conf.LogLevel)
if err != nil {
return err
}
_log.SetLevel(lvl)
log = _log
}
if conf.TargetDbs == nil || len(conf.TargetDbs) == 0 {
conf.TargetDbs = []string{"neo4j"}
}
internalConfig = conf
} else {
internalConfig = &Config{
TargetDbs: []string{"neo4j"},
}
}
log.Debug("mapping types")
for _, t := range mapTypes {
name := reflect.TypeOf(t).Elem().Name()
dc, err := getStructDecoratorConfig(t, mappedRelations)
if err != nil {
return err
}
log.Debugf("mapped type %s", name)
mappedTypes.Set(name, *dc)
}
log.Debug("validating edges")
if err := mappedRelations.Validate(); err != nil {
log.WithError(err).Error("failed to validate edges")
return err
}
if !isTest {
log.Debug("opening connection to neo4j")
// todo tls support
config := func(neoConf *neo4j.Config) {
neoConf.Encrypted = conf.Encrypted
neoConf.MaxConnectionPoolSize = conf.PoolSize
}
var err error
driver, err = neo4j.NewDriver(conf.ConnectionString(), neo4j.BasicAuth(conf.Username, conf.Password, conf.Realm), config)
if err != nil {
return err
}
// get neoversion
sess, err := driver.Session(neo4j.AccessModeRead)
if err != nil {
return err
}
res, err := sess.Run("return 1", nil)
if err != nil {
return err
} else if err = res.Err(); err != nil {
return err
}
sum, err := res.Summary()
if err != nil {
return err
}
// grab version
version := strings.Split(strings.Replace(strings.ToLower(sum.Server().Version()), "neo4j/", "", -1), ".")
neoVersion, err = strconv.ParseFloat(version[0], 64)
if err != nil {
return err
}
}
log.Debug("starting index verification step")
if !isTest {
var err error
if conf.IndexStrategy == ASSERT_INDEX {
log.Debug("chose ASSERT_INDEX strategy")
log.Debug("dropping all known indexes")
err = dropAllIndexesAndConstraints()
if err != nil {
return err
}
log.Debug("creating all mapped indexes")
err = createAllIndexesAndConstraints(mappedTypes)
if err != nil {
return err
}
log.Debug("verifying all indexes")
err = verifyAllIndexesAndConstraints(mappedTypes)
if err != nil {
return err
}
} else if conf.IndexStrategy == VALIDATE_INDEX {
log.Debug("chose VALIDATE_INDEX strategy")
log.Debug("verifying all indexes")
err = verifyAllIndexesAndConstraints(mappedTypes)
if err != nil {
return err
}
} else if conf.IndexStrategy == IGNORE_INDEX {
log.Debug("ignoring indexes")
} else {
return errors.New("unknown index strategy")
}
}
log.Debug("setup complete")
isSetup = true
return nil
}