/
class.go
234 lines (159 loc) · 3.81 KB
/
class.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
package graph
import (
"fmt"
"sync"
//
"github.com/golangdaddy/tarantula/router/common"
"github.com/golangdaddy/tarantula/web/validation"
)
type Class struct {
// root schema
DB *Database `json:"-"`
// deterministic id
Uid int64
// class name
Name string
// primary keys
PKs []Constraint
// foreign keys
FKs map[string]Constraint
// class schema
Properties Properties
PropertyMap map[string]*Property
// gf node
// Node *gf.Node
// gf node
// InstanceNode *gf.Node
// is internal class
IsUser bool
IsAnchor bool
Internal bool
sync.RWMutex
}
func (db *Database) newClass(name string) *Class {
return &Class{
db,
0,
name,
[]Constraint{},
map[string]Constraint{},
Properties{},
map[string]*Property{},
// db.Root.Add(name, "$class"),
// nil,
false,
false,
false,
sync.RWMutex{},
}
}
// adds a class marked as internal
func (db *Database) addInternalClass(name string) *Class {
class := db.newClass(name)
class.Internal = true
return class
}
// adds a custom class to the schema
func (db *Database) AddClass(name string) *Class {
class := db.ClassNameIndex(name)
if class != nil {
e := fmt.Sprintf("CLASS %s ALREADY EXISTS IN SCHEMA!", name)
panic(e)
}
class = db.newClass(name)
db.Classes.Add(class)
return class
}
func (class *Class) Table() string {
return class.DB.Name + "." + class.Name
}
func (class *Class) NewVertex(x string, data map[string]interface{}) *Vertex {
if data == nil {
data = map[string]interface{}{}
}
vertex := &Vertex{
class.DB,
class,
0,
x,
data,
sync.RWMutex{},
}
return vertex
}
func (class *Class) HasProperty(key string) (bool, *Property) {
class.RLock()
p := class.PropertyMap[key]
class.RUnlock()
return (p != nil), p
}
func (class *Class) NewProperty(key string, value *validation.Config) *Property {
return &Property{class.DB, 0, key, value, 0, map[Constraint]bool{}, sync.RWMutex{}}
}
func (class *Class) Column(i int) (bool, *Property) {
p := class.Properties[i]
return (p != nil), p
}
func (class *Class) InsertColumns() Properties {
props := Properties{&Property{}}
props = append(props, class.Properties[1:]...)
return props
}
func (class *Class) Validation() *common.Payload {
schema := common.Payload{}
for _, property := range class.Properties {
schema[property.Key] = property.Value
}
return &schema
}
func (class *Class) AddProperty(key string, value *validation.Config, options ...interface{}) {
var propExists bool
class.RLock()
for _, prop := range class.Properties {
if key == prop.Key {
propExists = true
}
}
class.RUnlock()
if propExists {
e := fmt.Sprintf("PROPERTY WITH KEY %s ALREADY EXISTS IN CLASS %s!", key, class.Name)
class.DB.Log.Panic(e)
}
// property = Null, Key, Value, Max
property := class.NewProperty(key, value)
// apply options
property.Options(options)
class.Lock()
class.Properties = append(class.Properties, property)
class.Unlock()
if class.Internal { property.SQL_Datatype() }
}
func (class *Class) Link(predicate string, classes ...*Class) {
if len(classes) == 0 {
class.DB.Log.Panic("THIS METHOD REQUIRES AT LEAST 1 CLASS ARGUMENT")
}
for _, counterparty := range classes {
relationship := &Relationship{
In: class,
Out: counterparty,
Predicate: predicate,
}
// create predicate struct and create predicate table
class.DB.AddPredicate(predicate)
// register relationship
class.DB.AddRelationship(relationship)
}
}
func (db *Database) ClassNameIndex(name string) *Class {
db.Classes.RLock()
class := db.Classes.NameIndex[name]
db.Classes.RUnlock()
return class
}
func (db *Database) ClassUidIndex(uid int64) *Class {
db.Classes.RLock()
class := db.Classes.UidIndex[uid]
db.Classes.RUnlock()
//if class == nil { panic("CLASS NOT FOUND: "+fmt.Sprintf("%v", uid)) }
return class
}