Skip to content

Commit afeb0af

Browse files
committed
Cleanup
1 parent fc42f30 commit afeb0af

9 files changed

+423
-23
lines changed

edge_collection.go

+8
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,12 @@ type EdgeCollection interface {
3131

3232
// CreateEdge creates a new edge in this edge collection.
3333
CreateEdge(ctx context.Context, from, to DocumentID, document interface{}) (DocumentMeta, error)
34+
35+
// Remove the edge collection from the graph.
36+
Remove(ctx context.Context) error
37+
38+
// Replace creates an edge collection in the graph.
39+
// from: contains the names of one or more vertex collections that can contain source vertices.
40+
// to: contains the names of one or more edge collections that can contain target vertices.
41+
Replace(ctx context.Context, from, to []string) error
3442
}

edge_collection_impl.go

+44-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type edgeCollection struct {
4949
}
5050

5151
// relPath creates the relative path to this edge collection (`_db/<db-name>/_api/gharial/<graph-name>/edge/<collection-name>`)
52-
func (c *edgeCollection) relPath(apiName string) string {
52+
func (c *edgeCollection) relPath() string {
5353
escapedName := pathEscape(c.name)
5454
return path.Join(c.g.relPath(), "edge", escapedName)
5555
}
@@ -63,3 +63,46 @@ func (c *edgeCollection) Name() string {
6363
func (c *edgeCollection) CreateEdge(ctx context.Context, from, to DocumentID, document interface{}) (DocumentMeta, error) {
6464
return DocumentMeta{}, nil
6565
}
66+
67+
// Remove the edge collection from the graph.
68+
func (c *edgeCollection) Remove(ctx context.Context) error {
69+
req, err := c.conn.NewRequest("DELETE", c.relPath())
70+
if err != nil {
71+
return WithStack(err)
72+
}
73+
resp, err := c.conn.Do(ctx, req)
74+
if err != nil {
75+
return WithStack(err)
76+
}
77+
if err := resp.CheckStatus(201, 202); err != nil {
78+
return WithStack(err)
79+
}
80+
return nil
81+
}
82+
83+
// Replace creates an edge collection in the graph.
84+
// collection: The name of the edge collection to be used.
85+
// from: contains the names of one or more vertex collections that can contain source vertices.
86+
// to: contains the names of one or more edge collections that can contain target vertices.
87+
func (c *edgeCollection) Replace(ctx context.Context, from, to []string) error {
88+
req, err := c.conn.NewRequest("PUT", c.relPath())
89+
if err != nil {
90+
return WithStack(err)
91+
}
92+
input := EdgeDefinition{
93+
Collection: c.name,
94+
From: from,
95+
To: to,
96+
}
97+
if _, err := req.SetBody(input); err != nil {
98+
return WithStack(err)
99+
}
100+
resp, err := c.conn.Do(ctx, req)
101+
if err != nil {
102+
return WithStack(err)
103+
}
104+
if err := resp.CheckStatus(201, 202); err != nil {
105+
return WithStack(err)
106+
}
107+
return nil
108+
}

graph.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ type Graph interface {
3333
// If the graph does not exist, a NotFoundError is returned.
3434
Remove(ctx context.Context) error
3535

36-
// CreateEdgeCollection creates an edge collection in the graph.
37-
CreateEdgeCollection(ctx context.Context, definition EdgeDefinition) (EdgeCollection, error)
36+
// EdgeCollection functions
37+
GraphEdgeCollections
3838
}

graph_edge_collections.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2017 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package driver
24+
25+
import "context"
26+
27+
// GraphEdgeCollections provides access to all edge collections of a single graph in a database.
28+
type GraphEdgeCollections interface {
29+
// EdgeCollection opens a connection to an existing edge-collection within the graph.
30+
// If no edge-collection with given name exists, an NotFoundError is returned.
31+
EdgeCollection(ctx context.Context, name string) (EdgeCollection, error)
32+
33+
// EdgeCollectionExists returns true if an edge-collection with given name exists within the graph.
34+
EdgeCollectionExists(ctx context.Context, name string) (bool, error)
35+
36+
// EdgeCollections returns all edge collections of this graph
37+
EdgeCollections(ctx context.Context) ([]EdgeCollection, error)
38+
39+
// CreateEdgeCollection creates an edge collection in the graph.
40+
// collection: The name of the edge collection to be used.
41+
// from: contains the names of one or more vertex collections that can contain source vertices.
42+
// to: contains the names of one or more edge collections that can contain target vertices.
43+
CreateEdgeCollection(ctx context.Context, collection string, from, to []string) (EdgeCollection, error)
44+
}

graph_edge_collections_impl.go

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2017 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package driver
24+
25+
import (
26+
"context"
27+
"path"
28+
)
29+
30+
type listEdgeCollectionResponse struct {
31+
Collections []string `json:"collections,omitempty"`
32+
}
33+
34+
// EdgeCollection opens a connection to an existing edge-collection within the graph.
35+
// If no edge-collection with given name exists, an NotFoundError is returned.
36+
func (g *graph) EdgeCollection(ctx context.Context, name string) (EdgeCollection, error) {
37+
req, err := g.conn.NewRequest("GET", path.Join(g.relPath(), "edge"))
38+
if err != nil {
39+
return nil, WithStack(err)
40+
}
41+
resp, err := g.conn.Do(ctx, req)
42+
if err != nil {
43+
return nil, WithStack(err)
44+
}
45+
if err := resp.CheckStatus(200); err != nil {
46+
return nil, WithStack(err)
47+
}
48+
var data listEdgeCollectionResponse
49+
if err := resp.ParseBody("", &data); err != nil {
50+
return nil, WithStack(err)
51+
}
52+
for _, n := range data.Collections {
53+
if n == name {
54+
ec, err := newEdgeCollection(name, g)
55+
if err != nil {
56+
return nil, WithStack(err)
57+
}
58+
return ec, nil
59+
}
60+
}
61+
return nil, WithStack(newArangoError(404, 0, "not found"))
62+
}
63+
64+
// EdgeCollectionExists returns true if an edge-collection with given name exists within the graph.
65+
func (g *graph) EdgeCollectionExists(ctx context.Context, name string) (bool, error) {
66+
req, err := g.conn.NewRequest("GET", path.Join(g.relPath(), "edge"))
67+
if err != nil {
68+
return false, WithStack(err)
69+
}
70+
resp, err := g.conn.Do(ctx, req)
71+
if err != nil {
72+
return false, WithStack(err)
73+
}
74+
if err := resp.CheckStatus(200); err != nil {
75+
return false, WithStack(err)
76+
}
77+
var data listEdgeCollectionResponse
78+
if err := resp.ParseBody("", &data); err != nil {
79+
return false, WithStack(err)
80+
}
81+
for _, n := range data.Collections {
82+
if n == name {
83+
return true, nil
84+
}
85+
}
86+
return false, nil
87+
}
88+
89+
// EdgeCollections returns all edge collections of this graph
90+
func (g *graph) EdgeCollections(ctx context.Context) ([]EdgeCollection, error) {
91+
req, err := g.conn.NewRequest("GET", path.Join(g.relPath(), "edge"))
92+
if err != nil {
93+
return nil, WithStack(err)
94+
}
95+
resp, err := g.conn.Do(ctx, req)
96+
if err != nil {
97+
return nil, WithStack(err)
98+
}
99+
if err := resp.CheckStatus(200); err != nil {
100+
return nil, WithStack(err)
101+
}
102+
var data listEdgeCollectionResponse
103+
if err := resp.ParseBody("", &data); err != nil {
104+
return nil, WithStack(err)
105+
}
106+
result := make([]EdgeCollection, 0, len(data.Collections))
107+
for _, name := range data.Collections {
108+
ec, err := newEdgeCollection(name, g)
109+
if err != nil {
110+
return nil, WithStack(err)
111+
}
112+
result = append(result, ec)
113+
}
114+
return result, nil
115+
}
116+
117+
// collection: The name of the edge collection to be used.
118+
// from: contains the names of one or more vertex collections that can contain source vertices.
119+
// to: contains the names of one or more edge collections that can contain target vertices.
120+
func (g *graph) CreateEdgeCollection(ctx context.Context, collection string, from, to []string) (EdgeCollection, error) {
121+
req, err := g.conn.NewRequest("POST", path.Join(g.relPath(), "edge"))
122+
if err != nil {
123+
return nil, WithStack(err)
124+
}
125+
input := EdgeDefinition{
126+
Collection: collection,
127+
From: from,
128+
To: to,
129+
}
130+
if _, err := req.SetBody(input); err != nil {
131+
return nil, WithStack(err)
132+
}
133+
resp, err := g.conn.Do(ctx, req)
134+
if err != nil {
135+
return nil, WithStack(err)
136+
}
137+
if err := resp.CheckStatus(201, 202); err != nil {
138+
return nil, WithStack(err)
139+
}
140+
ec, err := newEdgeCollection(collection, g)
141+
if err != nil {
142+
return nil, WithStack(err)
143+
}
144+
return ec, nil
145+
}

graph_impl.go

-20
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,3 @@ func (g *graph) Remove(ctx context.Context) error {
7575
}
7676
return nil
7777
}
78-
79-
// CreateEdgeCollection creates an edge collection in the graph.
80-
func (g *graph) CreateEdgeCollection(ctx context.Context, definition EdgeDefinition) (EdgeCollection, error) {
81-
req, err := g.conn.NewRequest("POST", path.Join(g.relPath(), "edge"))
82-
if err != nil {
83-
return nil, WithStack(err)
84-
}
85-
resp, err := g.conn.Do(ctx, req)
86-
if err != nil {
87-
return nil, WithStack(err)
88-
}
89-
if err := resp.CheckStatus(201, 202); err != nil {
90-
return nil, WithStack(err)
91-
}
92-
ec, err := newEdgeCollection(definition.Collection, g)
93-
if err != nil {
94-
return nil, WithStack(err)
95-
}
96-
return ec, nil
97-
}

test/collection_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ func ensureCollection(ctx context.Context, db driver.Database, name string, opti
4545
return c
4646
}
4747

48+
// assertCollection is a helper to check if a collection exists and fail if it does not.
49+
func assertCollection(ctx context.Context, db driver.Database, name string, t *testing.T) driver.Collection {
50+
c, err := db.Collection(ctx, name)
51+
if driver.IsNotFound(err) {
52+
t.Fatalf("Collection '%s': does not exist", name)
53+
} else if err != nil {
54+
t.Fatalf("Failed to open collection '%s': %s", name, describe(err))
55+
}
56+
return c
57+
}
58+
4859
// TestCreateCollection creates a collection and then checks that it exists.
4960
func TestCreateCollection(t *testing.T) {
5061
c := createClientFromEnv(t, true)

0 commit comments

Comments
 (0)