-
Notifications
You must be signed in to change notification settings - Fork 55
/
node_index.go
118 lines (107 loc) · 2.93 KB
/
node_index.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
// 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 (
"net/url"
"strconv"
)
// A LegacyNodeIndex is a searchable index for nodes.
type LegacyNodeIndex struct {
index
}
// CreateLegacyNodeIndex creates a new node index with optional type and provider.
func (db *Database) CreateLegacyNodeIndex(name, idxType, provider string) (*LegacyNodeIndex, error) {
idx, err := db.createIndex(db.HrefNodeIndex, name, idxType, provider)
if err != nil {
return nil, err
}
return &LegacyNodeIndex{*idx}, nil
}
// LegacyNodeIndexes returns all node indexes.
func (db *Database) LegacyNodeIndexes() ([]*LegacyNodeIndex, error) {
indexes, err := db.indexes(db.HrefNodeIndex)
if err != nil {
return nil, err
}
nis := make([]*LegacyNodeIndex, len(indexes))
for i, idx := range indexes {
nis[i] = &LegacyNodeIndex{*idx}
}
return nis, nil
}
// LegacyNodeIndex returns the named relationship index.
func (db *Database) LegacyNodeIndex(name string) (*LegacyNodeIndex, error) {
idx, err := db.index(db.HrefNodeIndex, name)
if err != nil {
return nil, err
}
ni := LegacyNodeIndex{*idx}
return &ni, nil
}
// Add indexes a node with a key/value pair.
func (nix *LegacyNodeIndex) Add(n *Node, key string, value interface{}) error {
return nix.add(n.entity, key, value)
}
// Remove deletes all entries with a given node, key and value from the index.
// If value or both key and value are the blank string, they are ignored.
func (nix *LegacyNodeIndex) Remove(n *Node, key, value string) error {
id := strconv.Itoa(n.Id())
return nix.remove(n.entity, id, key, value)
}
// Find locates Nodes in the index by exact key/value match.
func (idx *LegacyNodeIndex) Find(key, value string) (map[int]*Node, error) {
nm := make(map[int]*Node)
rawurl, err := idx.uri()
if err != nil {
return nm, err
}
rawurl = join(rawurl, key, value)
u, err := url.ParseRequestURI(rawurl)
if err != nil {
return nm, err
}
result := []Node{}
ne := NeoError{}
resp, err := idx.db.Session.Get(u.String(), nil, &result, &ne)
if err != nil {
return nm, err
}
if resp.Status() != 200 {
return nm, ne
}
for _, n := range result {
n.Db = idx.db
nm[n.Id()] = &n
}
return nm, nil
}
// Query finds nodes with a query.
func (idx *index) Query(query string) (map[int]*Node, error) {
nm := make(map[int]*Node)
rawurl, err := idx.uri()
if err != nil {
return nm, err
}
v := make(url.Values)
v.Add("query", query)
rawurl += "?" + v.Encode()
u, err := url.ParseRequestURI(rawurl)
if err != nil {
return nm, err
}
result := []Node{}
ne := NeoError{}
resp, err := idx.db.Session.Get(u.String(), nil, &result, &ne)
if err != nil {
return nm, err
}
if resp.Status() != 200 {
return nm, ne
}
for _, n := range result {
n.Db = idx.db
nm[n.Id()] = &n
}
return nm, nil
}