/
model.go
358 lines (294 loc) · 9.88 KB
/
model.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
/*
* Copyright 2019 ObjectBox Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package modelinfo
import (
"fmt"
"math/rand"
"os"
"strings"
)
// Id identifies a model element locally (e.g. property inside an entity)
type Id = uint32
// Uid identifies an element globally (i.e. is unique across the whole model)
type Uid = uint64
const (
// ModelVersion specifies current version of the model JSON file generated
ModelVersion = 5
// modelVersion supported by this parser & generator
minModelVersion = 4
maxModelVersion = ModelVersion
)
// ModelInfo is a serialization interface for the model JSON file
type ModelInfo struct {
// NOTE don't change order of these json exported properties because it will change users' model.json files
Note1 string `json:"_note1"`
Note2 string `json:"_note2"`
Note3 string `json:"_note3"`
Entities []*Entity `json:"entities"`
LastEntityId IdUid `json:"lastEntityId"`
LastIndexId IdUid `json:"lastIndexId"`
LastRelationId IdUid `json:"lastRelationId"`
ModelVersion int `json:"modelVersion"`
MinimumParserVersion int `json:"modelVersionParserMinimum"`
RetiredEntityUids []Uid `json:"retiredEntityUids"`
RetiredIndexUids []Uid `json:"retiredIndexUids"`
RetiredPropertyUids []Uid `json:"retiredPropertyUids"`
RetiredRelationUids []Uid `json:"retiredRelationUids"`
Version int `json:"version"` // user specified version
file *os.File // file handle, locked while the model is open
Rand *rand.Rand `json:"-"` // seeded random number generator
// Model Template
Package string `json:"-"`
}
var defaultModel = ModelInfo{
Note1: "KEEP THIS FILE! Check it into a version control system (VCS) like git.",
Note2: "ObjectBox manages crucial IDs for your object model. See docs for details.",
Note3: "If you have VCS merge conflicts, you must resolve them according to ObjectBox docs.",
Entities: make([]*Entity, 0),
RetiredEntityUids: make([]Uid, 0),
RetiredIndexUids: make([]Uid, 0),
RetiredPropertyUids: make([]Uid, 0),
RetiredRelationUids: make([]Uid, 0),
ModelVersion: maxModelVersion,
MinimumParserVersion: maxModelVersion,
Version: 1,
}
func createModelInfo() *ModelInfo {
var model = defaultModel
return &model
}
func (model *ModelInfo) fillMissing() {
// just replace comments with the latest ones
model.Note1 = defaultModel.Note1
model.Note2 = defaultModel.Note2
model.Note3 = defaultModel.Note3
}
// Validate performs initial validation of loaded data so that it doesn't have to be checked in each function
func (model *ModelInfo) Validate() (err error) {
if model.ModelVersion < minModelVersion {
return fmt.Errorf("the loaded model is too old - version %d while the minimum supported is %d - "+
" consider upgrading with an older generator or manually.", model.ModelVersion, minModelVersion)
}
if model.ModelVersion > maxModelVersion {
if model.MinimumParserVersion == 0 || model.MinimumParserVersion > ModelVersion {
return fmt.Errorf("the loaded model has been created with a newer generator version %d "+
" while the maximimum supported version is %d. Please upgrade your toolchain/generator",
model.ModelVersion, maxModelVersion)
}
}
if model.Entities == nil {
return fmt.Errorf("entities are not defined or not an array")
}
for _, entity := range model.Entities {
if entity.model == nil {
entity.model = model
} else if entity.model != model {
return fmt.Errorf("entity %s %s has incorrect parent model reference", entity.Name, entity.Id)
}
err = entity.Validate()
if err != nil {
return fmt.Errorf("entity %s %s is invalid: %s", entity.Name, entity.Id, err)
}
}
if len(model.Entities) > 0 {
if err = model.LastEntityId.Validate(); err != nil {
return fmt.Errorf("lastEntityId: %s", err)
}
var lastId = model.LastEntityId.getIdSafe()
var lastUid = model.LastEntityId.getUidSafe()
var found = false
for _, entity := range model.Entities {
if lastId == entity.Id.getIdSafe() {
if lastUid != entity.Id.getUidSafe() {
return fmt.Errorf("lastEntityId %s doesn't match entity %s %s",
model.LastEntityId, entity.Name, entity.Id)
}
found = true
} else if lastId < entity.Id.getIdSafe() {
return fmt.Errorf("lastEntityId %s is lower than entity %s %s",
model.LastEntityId, entity.Name, entity.Id)
}
}
if !found && !searchSliceUid(model.RetiredEntityUids, lastUid) {
return fmt.Errorf("lastEntityId %s doesn't match any entity", model.LastEntityId)
}
}
if len(model.LastIndexId) > 0 {
if err = model.LastIndexId.Validate(); err != nil {
return fmt.Errorf("lastIndexId: %s", err)
}
}
if len(model.LastRelationId) > 0 || model.hasRelations() {
if err = model.LastRelationId.Validate(); err != nil {
return fmt.Errorf("lastRelationId: %s", err)
}
// find the last relation ID among entities' relations
var lastId = model.LastRelationId.getIdSafe()
var lastUid = model.LastRelationId.getUidSafe()
var found = false
for _, entity := range model.Entities {
for _, relation := range entity.Relations {
if relation.entity == nil {
relation.entity = entity
} else if relation.entity != entity {
return fmt.Errorf("relation %s %s has incorrect parent entity reference",
relation.Name, relation.Id)
}
if lastId == relation.Id.getIdSafe() {
if lastUid != relation.Id.getUidSafe() {
return fmt.Errorf("lastRelationId %s doesn't match relation %s %s",
model.LastRelationId, relation.Name, relation.Id)
}
found = true
} else if lastId < relation.Id.getIdSafe() {
return fmt.Errorf("lastRelationId %s is lower than relation %s %s",
model.LastRelationId, relation.Name, relation.Id)
}
}
}
if !found && !searchSliceUid(model.RetiredRelationUids, lastUid) {
return fmt.Errorf("lastRelationId %s doesn't match any relation", model.LastRelationId)
}
}
if model.RetiredEntityUids == nil {
return fmt.Errorf("retiredEntityUids are not defined or not an array")
}
if model.RetiredIndexUids == nil {
return fmt.Errorf("retiredIndexUids are not defined or not an array")
}
if model.RetiredPropertyUids == nil {
return fmt.Errorf("retiredPropertyUids are not defined or not an array")
}
return nil
}
func (model *ModelInfo) hasRelations() bool {
for _, entity := range model.Entities {
if len(entity.Relations) > 0 {
return true
}
}
return false
}
// FindEntityByUid finds entity by Uid
func (model *ModelInfo) FindEntityByUid(uid Uid) (*Entity, error) {
for _, entity := range model.Entities {
entityUid, _ := entity.Id.GetUid()
if entityUid == uid {
return entity, nil
}
}
return nil, fmt.Errorf("entity with uid %d was not found", uid)
}
// FindEntityByName finds entity by name
func (model *ModelInfo) FindEntityByName(name string) (*Entity, error) {
for _, entity := range model.Entities {
if strings.ToLower(entity.Name) == strings.ToLower(name) {
return entity, nil
}
}
return nil, fmt.Errorf("entity named '%s' was not found", name)
}
// CreateEntity creates an entity
func (model *ModelInfo) CreateEntity(name string) (*Entity, error) {
var id Id = 1
if len(model.Entities) > 0 {
id = model.LastEntityId.getIdSafe() + 1
}
uniqueUid, err := model.GenerateUid()
if err != nil {
return nil, err
}
var entity = CreateEntity(model, id, uniqueUid)
entity.Name = name
model.Entities = append(model.Entities, entity)
model.LastEntityId = entity.Id
return entity, nil
}
// GenerateUid generates a unique UID
func (model *ModelInfo) GenerateUid() (result Uid, err error) {
result = 0
for i := 0; i < 1000; i++ {
t := Uid(model.Rand.Int63())
if !model.containsUid(t) {
result = t
break
}
}
if result == 0 {
err = fmt.Errorf("internal error = could not generate a unique UID")
}
return result, err
}
func (model *ModelInfo) createIndexId() (IdUid, error) {
var id Id = 1
if len(model.LastIndexId) > 0 {
id = model.LastIndexId.getIdSafe() + 1
}
uniqueUid, err := model.GenerateUid()
if err != nil {
return "", err
}
model.LastIndexId = CreateIdUid(id, uniqueUid)
return model.LastIndexId, nil
}
func (model *ModelInfo) createRelationId() (IdUid, error) {
var id Id = 1
if len(model.LastRelationId) > 0 {
id = model.LastRelationId.getIdSafe() + 1
}
uniqueUid, err := model.GenerateUid()
if err != nil {
return "", err
}
model.LastRelationId = CreateIdUid(id, uniqueUid)
return model.LastRelationId, nil
}
// recursively checks whether given UID is present in the model
func (model *ModelInfo) containsUid(searched Uid) bool {
if model.LastEntityId.getUidSafe() == searched {
return true
}
if model.LastIndexId.getUidSafe() == searched {
return true
}
if model.LastRelationId.getUidSafe() == searched {
return true
}
if searchSliceUid(model.RetiredEntityUids, searched) {
return true
}
if searchSliceUid(model.RetiredIndexUids, searched) {
return true
}
if searchSliceUid(model.RetiredPropertyUids, searched) {
return true
}
for _, entity := range model.Entities {
if entity.containsUid(searched) {
return true
}
}
return false
}
// the passed slices are not too large so let's just do linear search
func searchSliceUid(slice []Uid, searched Uid) bool {
for _, i := range slice {
if i == searched {
return true
}
}
return false
}