forked from cayleygraph/cayley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.go
157 lines (142 loc) · 3.67 KB
/
repl.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
package command
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/signal"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cayleygraph/cayley/clog"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/internal/repl"
"github.com/cayleygraph/cayley/quad"
"github.com/cayleygraph/cayley/query"
)
const (
keyQueryTimeout = "query.timeout"
)
func getContext() (context.Context, func()) {
ctx, cancel := context.WithCancel(context.Background())
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
go func() {
select {
case <-ch:
case <-ctx.Done():
}
signal.Stop(ch)
cancel()
}()
return ctx, cancel
}
func registerQueryFlags(cmd *cobra.Command) {
langs := query.Languages()
cmd.Flags().Bool("init", false, "initialize the database before using it")
cmd.Flags().String("lang", "gizmo", `query language to use ("`+strings.Join(langs, `", "`)+`")`)
cmd.Flags().DurationP("timeout", "t", 30*time.Second, "elapsed time until an individual query times out")
viper.BindPFlag(keyQueryTimeout, cmd.Flags().Lookup("timeout"))
registerLoadFlags(cmd)
}
func NewReplCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "repl",
Short: "Drop into a REPL of the given query language.",
RunE: func(cmd *cobra.Command, args []string) error {
printBackendInfo()
p := mustSetupProfile(cmd)
defer mustFinishProfile(p)
h, err := openForQueries(cmd)
if err != nil {
return err
}
defer h.Close()
ctx, cancel := getContext()
defer cancel()
timeout := viper.GetDuration("timeout")
lang, _ := cmd.Flags().GetString("lang")
return repl.Repl(ctx, h, lang, timeout)
},
}
registerQueryFlags(cmd)
return cmd
}
func NewQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "query",
Aliases: []string{"qu"},
Short: "Run a query in a specified database and print results.",
RunE: func(cmd *cobra.Command, args []string) error {
var querystr string
if len(args) == 0 {
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("Error occured while reading from stdin : %s.", err)
}
querystr = string(bytes)
} else if len(args) == 1 {
querystr = args[0]
} else {
return fmt.Errorf("Query accepts only one argument, the query string or nothing for reading from stdin.")
}
clog.Infof("Query:\n%s", querystr)
printBackendInfo()
p := mustSetupProfile(cmd)
defer mustFinishProfile(p)
h, err := openForQueries(cmd)
if err != nil {
return err
}
defer h.Close()
ctx, cancel := getContext()
defer cancel()
timeout := viper.GetDuration("timeout")
if timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
lang, _ := cmd.Flags().GetString("lang")
limit, err := cmd.Flags().GetInt("limit")
if err != nil {
return err
}
l := query.GetLanguage(lang)
if l == nil {
return fmt.Errorf("unknown query language: %q", lang)
}
enc := json.NewEncoder(os.Stdout)
sess := l.Session(h)
ch := make(chan query.Result, 100)
go sess.Execute(ctx, querystr, ch, limit)
for i := 0; limit <= 0 || i < limit; i++ {
select {
case <-ctx.Done():
return ctx.Err()
case r, ok := <-ch:
if !ok {
return nil
} else if err = r.Err(); err != nil {
return err
}
obj := r.Result()
switch p := obj.(type) {
case map[string]graph.Value:
m := make(map[string]quad.Value, len(p))
for k, v := range p {
m[k] = h.NameOf(v)
}
obj = m
}
enc.Encode(obj)
}
}
return nil
},
}
registerQueryFlags(cmd)
cmd.Flags().IntP("limit", "n", 100, "limit a number of results")
return cmd
}