forked from vesoft-inc/nebula-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
388 lines (351 loc) · 9.53 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/vesoft-inc/nebula-console/cli"
"github.com/vesoft-inc/nebula-console/printer"
nebula "github.com/vesoft-inc/nebula-go"
)
// Console side commands
const (
Unknown = -1
Quit = 0
SetCsv = 1
UnsetCsv = 2
PlayData = 3
Sleep = 4
SetDot = 5
UnsetDot = 6
)
var dataSetPrinter = printer.NewDataSetPrinter()
var planDescPrinter = printer.NewPlanDescPrinter()
var datasets = map[string]string{
"nba": "./data/nba.ngql",
}
func welcome(interactive bool) {
defer dataSetPrinter.UnsetOutCsv()
defer planDescPrinter.UnsetOutDot()
if !interactive {
return
}
fmt.Println()
fmt.Printf("Welcome to Nebula Graph!\n")
fmt.Println()
}
func bye(username string, interactive bool) {
fmt.Println()
fmt.Printf("Bye %s!\n", username)
fmt.Println(time.Now().In(time.Local).Format(time.RFC1123))
fmt.Println()
}
func printConsoleResp(msg string) {
fmt.Println(msg)
fmt.Println()
fmt.Println(time.Now().In(time.Local).Format(time.RFC1123))
fmt.Println()
}
func playData(data string) (string, error) {
path, exist := datasets[data]
if !exist {
return "", fmt.Errorf("dataset %s, not existed", data)
}
fd, err := os.Open(path)
if err != nil {
return "", err
}
c := cli.NewnCli(fd, false, "", func() { fd.Close() })
c.PlayingData(true)
defer c.PlayingData(false)
fmt.Printf("Start loading dataset %s...\n", data)
childSession, err := pool.GetSession(*username, *password)
if err != nil {
log.Panicf("Fail to create a new session from connection pool, %s", err.Error())
}
defer childSession.Release()
err = loop(childSession, c)
if err != nil {
return "", err
}
respErr := c.GetRespError()
if respErr != "" {
return "", fmt.Errorf(respErr)
}
return c.GetSpace(), nil
}
// Console side cmd will not be sent to server
func isConsoleCmd(cmd string) (isLocal bool, localCmd int, args []string) {
// Currently, command "exit" and "quit" can also exit the console
if cmd == "exit" || cmd == "quit" {
isLocal = true
localCmd = Quit
return
}
plain := strings.TrimSpace(strings.ToLower(cmd))
if len(plain) < 1 || plain[0] != ':' {
return
}
isLocal = true
words := strings.Fields(plain[1:])
switch len(words) {
case 1:
if words[0] == "exit" || words[0] == "quit" {
localCmd = Quit
} else {
localCmd = Unknown
}
case 2:
if words[0] == "unset" && words[1] == "csv" {
localCmd = UnsetCsv
} else if words[0] == "unset" && words[1] == "dot" {
localCmd = UnsetDot
} else if words[0] == "sleep" {
localCmd = Sleep
args = []string{words[1]}
} else if words[0] == "play" {
localCmd = PlayData
args = []string{words[1]}
} else {
localCmd = Unknown
}
case 3:
if words[0] == "set" && words[1] == "csv" {
localCmd = SetCsv
args = []string{words[2]}
} else if words[0] == "set" && words[1] == "dot" {
localCmd = SetDot
args = []string{words[2]}
} else {
localCmd = Unknown
}
default:
localCmd = Unknown
}
return
}
func executeConsoleCmd(cmd int, args []string) (newSpace string) {
switch cmd {
case SetCsv:
dataSetPrinter.SetOutCsv(args[0])
case UnsetCsv:
dataSetPrinter.UnsetOutCsv()
case SetDot:
planDescPrinter.SetOutDot(args[0])
case UnsetDot:
planDescPrinter.UnsetOutDot()
case PlayData:
var err error
newSpace, err = playData(args[0])
if err != nil {
printConsoleResp("Error: load dataset failed, " + err.Error())
} else {
printConsoleResp("Load dataset succeeded!")
}
case Sleep:
i, err := strconv.Atoi(args[0])
if err != nil {
printConsoleResp("Error: invalid integer, " + err.Error())
}
time.Sleep(time.Duration(i) * time.Second)
default:
printConsoleResp("Error: this local command not exists!")
}
return newSpace
}
func printResultSet(res *nebula.ResultSet, duration time.Duration) {
if !res.IsSucceed() && !res.IsPartialSucceed() {
fmt.Printf("[ERROR (%d)]: %s", res.GetErrorCode(), res.GetErrorMsg())
fmt.Println()
fmt.Println()
return
}
// Show table
if res.IsSetData() {
dataSetPrinter.PrintDataSet(res)
numRows := res.GetRowSize()
if numRows > 0 {
fmt.Printf("Got %d rows (time spent %d/%d us)\n", numRows, res.GetLatency(), duration/1000)
} else {
fmt.Printf("Empty set (time spent %d/%d us)\n", res.GetLatency(), duration/1000)
}
} else {
fmt.Printf("Execution succeeded (time spent %d/%d us)\n", res.GetLatency(), duration/1000)
}
if res.IsPartialSucceed() {
fmt.Println()
fmt.Printf("[WARNING]: Got partial result.")
}
if res.IsSetComment() {
fmt.Println()
fmt.Printf("[WARNING]: %s", res.GetComment())
}
if res.IsSetPlanDesc() {
fmt.Println()
fmt.Printf("Execution Plan (optimize time %d us)\n", res.GetPlanDesc().GetOptimizeTimeInUs())
fmt.Println()
planDescPrinter.PrintPlanDesc(res)
}
fmt.Println()
}
// Loop the request util fatal or timeout
// We treat one line as one query
// Add line break yourself as `SHOW \<CR>HOSTS`
func loop(session *nebula.Session, c cli.Cli) error {
for {
line, exit, err := c.ReadLine()
if err != nil {
return err
}
if exit { // Ctrl+D
fmt.Println()
return nil
}
if len(line) == 0 {
continue
}
// Console side command
if isLocal, cmd, args := isConsoleCmd(line); isLocal {
if cmd == Quit {
return nil
}
newSpace := executeConsoleCmd(cmd, args)
if newSpace != "" {
c.SetSpace(newSpace)
session.Execute(fmt.Sprintf("USE %s", newSpace))
if err != nil {
return err
}
}
continue
}
// Server side command
start := time.Now()
res, err := session.Execute(line)
if err != nil {
return err
}
if !res.IsSucceed() && !res.IsPartialSucceed() {
c.SetRespError(fmt.Sprintf("[ERROR (%d)]: %s", res.GetErrorCode(), res.GetErrorMsg()))
if c.IsPlayingData() {
break
}
}
duration := time.Since(start)
if c.Output() {
printResultSet(res, duration)
fmt.Println(time.Now().In(time.Local).Format(time.RFC1123))
fmt.Println()
}
c.SetSpace(res.GetSpaceName())
}
return nil
}
// Nebula Console version related
var (
gitCommit string
buildDate string
)
var (
address *string = flag.String("addr", "127.0.0.1", "The Nebula Graph IP/HOST address")
port *int = flag.Int("P", -1, "The Nebula Graph Port")
username *string = flag.String("u", "", "The Nebula Graph login user name")
password *string = flag.String("p", "", "The Nebula Graph login password")
timeout *int = flag.Int("t", 0, "The Nebula Graph client connection timeout in seconds, 0 means never timeout")
script *string = flag.String("e", "", "The nGQL directly")
file *string = flag.String("f", "", "The nGQL script file name")
version *bool = flag.Bool("v", false, "The Nebula Console version")
)
func init() {
flag.StringVar(address, "address", "127.0.0.1", "The Nebula Graph IP/HOST address")
flag.IntVar(port, "port", -1, "The Nebula Graph Port")
flag.StringVar(username, "user", "", "The Nebula Graph login user name")
flag.StringVar(password, "password", "", "The Nebula Graph login password")
flag.IntVar(timeout, "timeout", 0, "The Nebula Graph client connection timeout in seconds, 0 means never timeout")
flag.StringVar(script, "eval", "", "The nGQL directly")
flag.StringVar(file, "file", "", "The nGQL script file name")
flag.BoolVar(version, "version", false, "The Nebula Console version")
}
func validateFlags() {
if *port == -1 {
log.Panicf("Error: argument port is missed!")
}
if len(*username) == 0 {
log.Panicf("Error: username is empty!")
}
if len(*password) == 0 {
log.Panicf("Error: password is empty!")
}
}
var pool *nebula.ConnectionPool
func main() {
flag.Parse()
if flag.NFlag() == 1 && *version {
fmt.Printf("nebula-console version Git: %s, Build Time: %s\n", gitCommit, buildDate)
return
}
// Check if flags are valid
validateFlags()
interactive := *script == "" && *file == ""
historyHome := os.Getenv("HOME")
if historyHome == "" {
ex, err := os.Executable()
if err != nil {
log.Panicf("Get executable failed: %s", err.Error())
}
historyHome = filepath.Dir(ex) // Set to executable folder
}
hostAddress := nebula.HostAddress{Host: *address, Port: *port}
hostList := []nebula.HostAddress{hostAddress}
poolConfig := nebula.PoolConfig{
TimeOut: time.Duration(*timeout) * time.Millisecond,
IdleTime: 0 * time.Millisecond,
MaxConnPoolSize: 2,
MinConnPoolSize: 0,
}
var err error
pool, err = nebula.NewConnectionPool(hostList, poolConfig, nebula.DefaultLogger{})
if err != nil {
log.Panicf(fmt.Sprintf("Fail to initialize the connection pool, host: %s, port: %d, %s", *address, *port, err.Error()))
}
defer pool.Close()
session, err := pool.GetSession(*username, *password)
if err != nil {
log.Panicf("Fail to create a new session from connection pool, %s", err.Error())
}
defer session.Release()
welcome(interactive)
defer bye(*username, interactive)
var c cli.Cli = nil
// Loop the request
if interactive {
historyFile := path.Join(historyHome, ".nebula_history")
c = cli.NewiCli(historyFile, *username)
} else if *script != "" {
c = cli.NewnCli(strings.NewReader(*script), true, *username, nil)
} else if *file != "" {
fd, err := os.Open(*file)
if err != nil {
log.Panicf("Open file %s failed, %s", *file, err.Error())
}
c = cli.NewnCli(fd, true, *username, func() { fd.Close() })
}
if c == nil {
return
}
defer c.Close()
err = loop(session, c)
if err != nil {
log.Panicf("Loop error, %s", err.Error())
}
}