-
Notifications
You must be signed in to change notification settings - Fork 55
/
schema.go
158 lines (145 loc) · 4.08 KB
/
schema.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
// Copyright (c) 2012-2013 Jason McVetta. This is Free Software, released under
// the terms of the GPL v3. See http://www.gnu.org/copyleft/gpl.html for details.
// Resist intellectual serfdom - the ownership of ideas is akin to slavery.
package neoism
import (
"errors"
)
type indexRequest struct {
PropertyKeys []string `json:"property_keys"`
}
// An Index improves the speed of looking up nodes in the database.
type Index struct {
db *Database
Label string
PropertyKeys []string `json:"property_keys"`
}
// Drop removes the index.
func (idx *Index) Drop() error {
uri := join(idx.db.Url, "schema/index", idx.Label, idx.PropertyKeys[0])
ne := NeoError{}
resp, err := idx.db.Session.Delete(uri, nil, nil, &ne)
if err != nil {
return err
}
if resp.Status() == 404 {
return NotFound
}
if resp.Status() != 204 {
return ne
}
return nil
}
// CreateIndex starts a background job in the database that will create and
// populate the new index of a specified property on nodes of a given label.
func (db *Database) CreateIndex(label, property string) (*Index, error) {
uri := join(db.Url, "schema/index", label)
payload := indexRequest{[]string{property}}
result := Index{db: db}
ne := NeoError{}
resp, err := db.Session.Post(uri, payload, &result, &ne)
if err != nil {
return nil, err
}
switch resp.Status() {
case 200:
return &result, nil // Success
case 405:
return nil, NotAllowed
}
return nil, ne
}
// Indexes lists indexes for a label. If a blank string is given as the label,
// returns all indexes.
func (db *Database) Indexes(label string) ([]*Index, error) {
uri := join(db.Url, "schema/index", label)
result := []*Index{}
ne := NeoError{}
resp, err := db.Session.Get(uri, nil, &result, &ne)
if err != nil {
return result, err
}
if resp.Status() == 404 {
return result, NotFound
}
if resp.Status() != 200 {
return result, ne
}
for _, idx := range result {
idx.db = db
}
return result, nil
}
type uniqueConstraintRequest struct {
PropertyKeys []string `json:"property_keys"`
}
// A UniqueConstraint makes sure that your database will never contain more
// than one node with a specific label and one property value.
type UniqueConstraint struct {
db *Database
Label string `json:"label"`
Type string `json:"type"`
PropertyKeys []string `json:"property_keys"`
}
// CreateUniqueConstraint create a unique constraint on a property on nodes
// with a specific label.
func (db *Database) CreateUniqueConstraint(label, property string) (*UniqueConstraint, error) {
uri := join(db.Url, "schema/constraint", label, "uniqueness")
payload := uniqueConstraintRequest{[]string{property}}
result := UniqueConstraint{db: db}
ne := NeoError{}
resp, err := db.Session.Post(uri, payload, &result, &ne)
if err != nil {
return nil, err
}
switch resp.Status() {
case 200:
return &result, nil // Success
case 405:
return nil, NotAllowed
case 409:
return nil, NotAllowed // Constraint is viloated by existing data
}
return nil, ne
}
// UniqueConstraints get a specific unique constraint for a label and a property.
// If a blank string is given as the property, return all unique constraint for
// the label.
func (db *Database) UniqueConstraints(label, property string) ([]*UniqueConstraint, error) {
if label == "" {
return nil, errors.New("Label cannot be empty")
}
uri := join(db.Url, "schema/constraint", label, "uniqueness", property)
result := []*UniqueConstraint{}
ne := NeoError{}
resp, err := db.Session.Get(uri, nil, &result, &ne)
if err != nil {
return result, err
}
switch resp.Status() {
case 200:
for _, cstr := range result {
cstr.db = db
}
return result, nil // Success
case 404:
return nil, NotFound
}
return nil, ne
}
// Drop removes the unique constraint.
func (cstr *UniqueConstraint) Drop() error {
uri := join(cstr.db.Url, "schema/constraint", cstr.Label, "uniqueness", cstr.PropertyKeys[0])
ne := NeoError{}
resp, err := cstr.db.Session.Delete(uri, nil, nil, &ne)
if err != nil {
return err
}
switch resp.Status() {
case 204:
return nil
case 404:
return NotFound
}
return ne
}