Skip to content

Commit 3daddbd

Browse files
committed
Add export command to the command line and REPL
1 parent dd0ca72 commit 3daddbd

3 files changed

Lines changed: 115 additions & 7 deletions

File tree

tools/vcli/bw/common/common.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/google/badwolf/storage"
2929
"github.com/google/badwolf/tools/vcli/bw/assert"
3030
"github.com/google/badwolf/tools/vcli/bw/command"
31+
"github.com/google/badwolf/tools/vcli/bw/export"
3132
"github.com/google/badwolf/tools/vcli/bw/load"
3233
"github.com/google/badwolf/tools/vcli/bw/repl"
3334
"github.com/google/badwolf/tools/vcli/bw/run"
@@ -99,6 +100,7 @@ func InitializeDriver(driverName string, drivers map[string]StoreGenerator) (sto
99100
func InitializeCommands(driver storage.Store, chanSize, bulkTripleOpSize, builderSize int) []*command.Command {
100101
return []*command.Command{
101102
assert.New(driver, literal.DefaultBuilder(), chanSize),
103+
export.New(driver),
102104
load.New(driver, bulkTripleOpSize, builderSize),
103105
run.New(driver, chanSize),
104106
repl.New(driver, chanSize, bulkTripleOpSize, builderSize),

tools/vcli/bw/export/export.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2016 Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package export contains the command allowing to dump all triples of a graphs
16+
// into a file.
17+
package export
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"strings"
23+
"sync"
24+
25+
"golang.org/x/net/context"
26+
27+
"github.com/google/badwolf/storage"
28+
"github.com/google/badwolf/tools/vcli/bw/command"
29+
"github.com/google/badwolf/triple"
30+
)
31+
32+
// New creates the help command.
33+
func New(store storage.Store) *command.Command {
34+
cmd := &command.Command{
35+
UsageLine: "export <graph_names_separated_by_commas> <file_path>",
36+
Short: "export triples in bulk from graphs into a file.",
37+
Long: `Export all the triples in the provided graphs into the provided
38+
text file.`,
39+
}
40+
cmd.Run = func(ctx context.Context, args []string) int {
41+
return Eval(ctx, cmd.UsageLine+"\n\n"+cmd.Long, args, store)
42+
}
43+
return cmd
44+
}
45+
46+
// Eval loads the triples in the file against as indicated by the command.
47+
func Eval(ctx context.Context, usage string, args []string, store storage.Store) int {
48+
if len(args) <= 3 {
49+
fmt.Fprintf(os.Stderr, "[ERROR] Missing required file path and/or graph names.\n\n%s", usage)
50+
return 2
51+
}
52+
graphs, path := strings.Split(args[len(args)-2], ","), args[len(args)-1]
53+
f, err := os.Create(path)
54+
if err != nil {
55+
fmt.Fprintf(os.Stderr, "[ERROR] Failed to open target file %q with error %v.\n\n", path, err)
56+
return 2
57+
}
58+
defer f.Close()
59+
var sgs []storage.Graph
60+
for _, gr := range graphs {
61+
g, err := store.Graph(ctx, gr)
62+
if err != nil {
63+
fmt.Fprintf(os.Stderr, "[ERROR] Failed to retrieve graph %q with error %v.\n\n", gr, err)
64+
return 2
65+
}
66+
sgs = append(sgs, g)
67+
}
68+
69+
cnt := 0
70+
var errs []error
71+
var mu sync.Mutex
72+
chn := make(chan *triple.Triple, 1000)
73+
for _, vg := range sgs {
74+
go func(g storage.Graph) {
75+
err := g.Triples(ctx, chn)
76+
mu.Lock()
77+
errs = append(errs, err)
78+
mu.Unlock()
79+
}(vg)
80+
}
81+
82+
for t := range chn {
83+
if _, err := f.WriteString(t.String() + "\n"); err != nil {
84+
fmt.Fprintf(os.Stderr, "[ERROR] Failed to write triple %s to file %q, %v.\n\n", t.String(), path, err)
85+
return 2
86+
}
87+
cnt++
88+
}
89+
for _, err := range errs {
90+
if err != nil {
91+
fmt.Fprintf(os.Stderr, "[ERROR] Failed to retrieve triples with error %v.\n\n", err)
92+
return 2
93+
}
94+
}
95+
96+
fmt.Printf("Successfully written %d triples to file %q.\nTriples exported from graphs:\n\t- %s\n", cnt, path, strings.Join(graphs, "\n\t- "))
97+
return 0
98+
}

tools/vcli/bw/repl/repl.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/google/badwolf/bql/version"
3333
"github.com/google/badwolf/storage"
3434
"github.com/google/badwolf/tools/vcli/bw/command"
35+
"github.com/google/badwolf/tools/vcli/bw/export"
3536
"github.com/google/badwolf/tools/vcli/bw/io"
3637
"github.com/google/badwolf/tools/vcli/bw/load"
3738
)
@@ -94,13 +95,10 @@ func REPL(driver storage.Store, input *os.File, rl readLiner, chanSize, bulkSize
9495
fmt.Print(prompt)
9596
continue
9697
}
97-
if strings.HasPrefix(l, "run") {
98-
path, cmds, err := runBQLFromFile(ctx, driver, chanSize, l)
99-
if err != nil {
100-
fmt.Printf("[ERROR] %s\n\n", err)
101-
} else {
102-
fmt.Printf("Loaded %q and run %d BQL commands successfully\n\n", path, cmds)
103-
}
98+
if strings.HasPrefix(l, "export") {
99+
args := strings.Split("bw "+l, " ")
100+
usage := "Wrong syntax\n\n\tload <graph_names_separated_by_commas> <file_path>\n"
101+
export.Eval(ctx, usage, args, driver)
104102
fmt.Print(prompt)
105103
continue
106104
}
@@ -111,6 +109,16 @@ func REPL(driver storage.Store, input *os.File, rl readLiner, chanSize, bulkSize
111109
fmt.Print(prompt)
112110
continue
113111
}
112+
if strings.HasPrefix(l, "run") {
113+
path, cmds, err := runBQLFromFile(ctx, driver, chanSize, l)
114+
if err != nil {
115+
fmt.Printf("[ERROR] %s\n\n", err)
116+
} else {
117+
fmt.Printf("Loaded %q and run %d BQL commands successfully\n\n", path, cmds)
118+
}
119+
fmt.Print(prompt)
120+
continue
121+
}
114122
table, err := runBQL(ctx, l, driver, chanSize)
115123
if err != nil {
116124
fmt.Printf("[ERROR] %s\n\n", err)

0 commit comments

Comments
 (0)