|
| 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 | +} |
0 commit comments