forked from Restream/reindexer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arango.go
199 lines (164 loc) · 4.06 KB
/
arango.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
package repo
import (
"log"
"math/rand"
driver "github.com/arangodb/go-driver"
"github.com/arangodb/go-driver/http"
)
type ArangoDBRepo struct {
db driver.Database
client driver.Client
col driver.Collection
}
func (repo *ArangoDBRepo) Init() bool {
conn, err := http.NewConnection(http.ConnectionConfig{
Endpoints: []string{"http://localhost:8529"},
})
if err != nil {
panic(err)
}
c, err := driver.NewClient(driver.ClientConfig{
Connection: conn,
Authentication: driver.BasicAuthentication("root", ""),
})
if err != nil {
panic(err)
}
c.CreateDatabase(nil, "test", nil)
db, err := c.Database(nil, "test")
if err != nil {
panic(0)
}
repo.db = db
repo.client = c
repo.col, _ = repo.db.Collection(nil, "items")
return true
}
func (repo *ArangoDBRepo) Seed(itemsInDataSet int) bool {
col, err := repo.db.Collection(nil, "items")
if err == nil {
col.Remove(nil)
}
col, err = repo.db.CreateCollection(nil, "items", nil)
if err != nil {
panic(err)
}
col.EnsureHashIndex(nil, []string{"id"}, &driver.EnsureHashIndexOptions{Unique: true})
col.EnsureHashIndex(nil, []string{"name"}, &driver.EnsureHashIndexOptions{Unique: false})
col.EnsureHashIndex(nil, []string{"year"}, &driver.EnsureHashIndexOptions{Unique: false})
col.EnsureFullTextIndex(nil, []string{"description"}, &driver.EnsureFullTextIndexOptions{})
log.Printf("Seeding data to arango")
for i := 0; i < itemsInDataSet; i++ {
_, err := col.CreateDocument(nil, newItem(i))
if err != nil {
panic(0)
}
}
repo.col = col
return true
}
func (repo *ArangoDBRepo) QueryFullText(textQuery func() string, N int, limit int) (ret []*Item) {
ret = make([]*Item, 0, limit)
for i := 0; i < N; i++ {
cursor, err := repo.db.Query(nil, "FOR d IN FULLTEXT(items, 'description', @text,@limit ) RETURN d", map[string]interface{}{
"text": textQuery(),
"limit": limit,
})
if err != nil {
panic(err)
}
defer cursor.Close()
for cursor.HasMore() {
it := &Item{}
cursor.ReadDocument(nil, it)
ret = append(ret, it)
}
}
return ret
}
func (repo *ArangoDBRepo) QueryByID(N int, onlyQuery bool) (it *Item) {
for i := 0; i < N; i++ {
cursor, err := repo.db.Query(nil, "FOR d IN items FILTER d.id == @id RETURN d", map[string]interface{}{
"id": rand.Int() % itemsInDataSet,
})
if err != nil {
panic(err)
}
defer cursor.Close()
it = &Item{}
if !onlyQuery {
cursor.ReadDocument(nil, it)
}
}
return it
}
func (repo *ArangoDBRepo) Query2Cond(N int, onlyQuery bool, limit int) (ret []*Item) {
if !onlyQuery {
ret = make([]*Item, 0, limit)
}
for i := 0; i < N; i++ {
cursor, err := repo.db.Query(nil, "FOR d IN items FILTER d.name == @name && d.year > @year LIMIT @limit RETURN d", map[string]interface{}{
"name": randString(),
"year": 2010,
"limit": limit,
})
if err != nil {
panic(err)
}
defer cursor.Close()
if !onlyQuery {
for cursor.HasMore() {
it := &Item{}
cursor.ReadDocument(nil, it)
ret = append(ret, it)
}
}
}
return ret
}
func (repo *ArangoDBRepo) Query1Cond(N int, onlyQuery bool, limit int) (ret []*Item) {
if !onlyQuery {
ret = make([]*Item, 0, limit)
}
for i := 0; i < N; i++ {
cursor, err := repo.db.Query(nil, "FOR d IN items FILTER d.year == @year LIMIT @limit RETURN d", map[string]interface{}{
"year": 2010,
"limit": limit,
})
if err != nil {
panic(err)
}
defer cursor.Close()
if !onlyQuery {
for cursor.HasMore() {
it := &Item{}
cursor.ReadDocument(nil, it)
ret = append(ret, it)
}
}
}
return ret
}
func (repo *ArangoDBRepo) Update(N int) {
for i := 0; i < N; i++ {
id := rand.Int() % itemsInDataSet
cursor, err := repo.db.Query(nil, "FOR d IN items FILTER d.id == @id RETURN d", map[string]interface{}{"id": id})
if err != nil {
panic(err)
}
defer cursor.Close()
it := &Item{}
meta, err := cursor.ReadDocument(nil, it)
if err != nil {
panic(err)
}
item := newItem(id)
_, err = repo.col.ReplaceDocument(nil, meta.Key, item)
if err != nil {
panic(err)
}
}
}
func init() {
registerRepo("arango", &ArangoDBRepo{})
}