-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
202 lines (180 loc) · 4.46 KB
/
main.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
200
201
202
package mongoload
import (
"fmt"
"io"
"log"
"strings"
"github.com/bmeg/arachne/aql"
"github.com/bmeg/arachne/mongo"
"github.com/bmeg/arachne/util"
"github.com/bmeg/arachne/util/rpc"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"github.com/spf13/cobra"
)
var host = "localhost"
var database = "arachnedb"
var graph string
var vertexFile string
var edgeFile string
var batchSize = 15
var maxRetries = 3
func found(set []string, val string) bool {
for _, i := range set {
if i == val {
return true
}
}
return false
}
// MaxRetries is the number of times driver will reconnect on connection failure
// TODO, move to per instance config, rather then global
var MaxRetries = 3
func isNetError(e error) bool {
if e == io.EOF {
return true
}
if b, ok := e.(*mgo.BulkError); ok {
for _, c := range b.Cases() {
if c.Err == io.EOF {
return true
}
if strings.Contains(c.Err.Error(), "connection") {
return true
}
}
}
return false
}
// Cmd is the declaration of the command line
var Cmd = &cobra.Command{
Use: "mongoload <graph>",
Short: "Directly load data into mongodb",
Long: ``,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if vertexFile == "" && edgeFile == "" {
return fmt.Errorf("no edge or vertex files were provided")
}
graph = args[0]
conn, err := aql.Connect(rpc.ConfigWithDefaults(host), true)
if err != nil {
return err
}
graphs, err := conn.ListGraphs()
if err != nil {
return err
}
found := false
for g := range graphs {
if graph == g {
found = true
}
}
if !found {
log.Println("Creating graph:", graph)
err := conn.AddGraph(graph)
if err != nil {
return err
}
}
log.Println("Loading data into graph:", graph)
session, err := mgo.Dial(host)
if err != nil {
return err
}
vertexCo := session.DB(database).C(fmt.Sprintf("%s_vertices", graph))
edgeCo := session.DB(database).C(fmt.Sprintf("%s_edges", graph))
if vertexFile != "" {
log.Printf("Loading %s", vertexFile)
count := 0
docChan := make(chan []map[string]interface{}, 100)
docBatch := make([]map[string]interface{}, 0, batchSize)
go func() {
defer close(docChan)
for v := range util.StreamVerticesFromFile(vertexFile) {
data := mongo.PackVertex(v)
docBatch = append(docBatch, data)
if len(docBatch) > batchSize {
docChan <- docBatch
docBatch = make([]map[string]interface{}, 0, batchSize)
}
count++
if count%1000 == 0 {
log.Printf("Loaded %d vertices", count)
}
}
if len(docBatch) > 0 {
docChan <- docBatch
}
}()
for batch := range docChan {
for i := 0; i < maxRetries; i++ {
bulk := vertexCo.Bulk()
for _, data := range batch {
bulk.Upsert(bson.M{"_id": data["_id"]}, data)
}
_, err = bulk.Run()
if err == nil || !isNetError(err) {
i = maxRetries
} else {
log.Printf("Refreshing Connection")
session.Refresh()
}
}
}
log.Printf("Loaded %d vertices", count)
}
if edgeFile != "" {
log.Printf("Loading %s", edgeFile)
count := 0
docChan := make(chan []map[string]interface{}, 100)
docBatch := make([]map[string]interface{}, 0, batchSize)
go func() {
defer close(docChan)
for e := range util.StreamEdgesFromFile(edgeFile) {
data := mongo.PackEdge(e)
if data["_id"] == "" {
data["_id"] = bson.NewObjectId().Hex()
}
docBatch = append(docBatch, data)
if len(docBatch) > batchSize {
docChan <- docBatch
docBatch = make([]map[string]interface{}, 0, batchSize)
}
count++
if count%1000 == 0 {
log.Printf("Loaded %d edges", count)
}
}
if len(docBatch) > 0 {
docChan <- docBatch
}
}()
for batch := range docChan {
for i := 0; i < maxRetries; i++ {
bulk := edgeCo.Bulk()
for _, data := range batch {
bulk.Upsert(bson.M{"_id": data["_id"]}, data)
}
_, err = bulk.Run()
if err == nil || !isNetError(err) {
i = maxRetries
} else {
log.Printf("Refreshing Connection")
session.Refresh()
}
}
}
log.Printf("Loaded %d edges", count)
}
return nil
},
}
func init() {
flags := Cmd.Flags()
flags.StringVar(&host, "host", host, "mongo server url")
flags.StringVar(&database, "database", database, "database name in mongo to store graph")
flags.StringVar(&vertexFile, "vertex", "", "vertex file")
flags.StringVar(&edgeFile, "edge", "", "edge file")
}