Skip to content

Commit

Permalink
Add export command to the command line and REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
xllora committed Mar 6, 2016
1 parent dd0ca72 commit 3daddbd
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 7 deletions.
2 changes: 2 additions & 0 deletions tools/vcli/bw/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/google/badwolf/storage"
"github.com/google/badwolf/tools/vcli/bw/assert"
"github.com/google/badwolf/tools/vcli/bw/command"
"github.com/google/badwolf/tools/vcli/bw/export"
"github.com/google/badwolf/tools/vcli/bw/load"
"github.com/google/badwolf/tools/vcli/bw/repl"
"github.com/google/badwolf/tools/vcli/bw/run"
Expand Down Expand Up @@ -99,6 +100,7 @@ func InitializeDriver(driverName string, drivers map[string]StoreGenerator) (sto
func InitializeCommands(driver storage.Store, chanSize, bulkTripleOpSize, builderSize int) []*command.Command {
return []*command.Command{
assert.New(driver, literal.DefaultBuilder(), chanSize),
export.New(driver),
load.New(driver, bulkTripleOpSize, builderSize),
run.New(driver, chanSize),
repl.New(driver, chanSize, bulkTripleOpSize, builderSize),
Expand Down
98 changes: 98 additions & 0 deletions tools/vcli/bw/export/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package export contains the command allowing to dump all triples of a graphs
// into a file.
package export

import (
"fmt"
"os"
"strings"
"sync"

"golang.org/x/net/context"

"github.com/google/badwolf/storage"
"github.com/google/badwolf/tools/vcli/bw/command"
"github.com/google/badwolf/triple"
)

// New creates the help command.
func New(store storage.Store) *command.Command {
cmd := &command.Command{
UsageLine: "export <graph_names_separated_by_commas> <file_path>",
Short: "export triples in bulk from graphs into a file.",
Long: `Export all the triples in the provided graphs into the provided
text file.`,
}
cmd.Run = func(ctx context.Context, args []string) int {
return Eval(ctx, cmd.UsageLine+"\n\n"+cmd.Long, args, store)
}
return cmd
}

// Eval loads the triples in the file against as indicated by the command.
func Eval(ctx context.Context, usage string, args []string, store storage.Store) int {
if len(args) <= 3 {
fmt.Fprintf(os.Stderr, "[ERROR] Missing required file path and/or graph names.\n\n%s", usage)
return 2
}
graphs, path := strings.Split(args[len(args)-2], ","), args[len(args)-1]
f, err := os.Create(path)
if err != nil {
fmt.Fprintf(os.Stderr, "[ERROR] Failed to open target file %q with error %v.\n\n", path, err)
return 2
}
defer f.Close()
var sgs []storage.Graph
for _, gr := range graphs {
g, err := store.Graph(ctx, gr)
if err != nil {
fmt.Fprintf(os.Stderr, "[ERROR] Failed to retrieve graph %q with error %v.\n\n", gr, err)
return 2
}
sgs = append(sgs, g)
}

cnt := 0
var errs []error
var mu sync.Mutex
chn := make(chan *triple.Triple, 1000)
for _, vg := range sgs {
go func(g storage.Graph) {
err := g.Triples(ctx, chn)
mu.Lock()
errs = append(errs, err)
mu.Unlock()
}(vg)
}

for t := range chn {
if _, err := f.WriteString(t.String() + "\n"); err != nil {
fmt.Fprintf(os.Stderr, "[ERROR] Failed to write triple %s to file %q, %v.\n\n", t.String(), path, err)
return 2
}
cnt++
}
for _, err := range errs {
if err != nil {
fmt.Fprintf(os.Stderr, "[ERROR] Failed to retrieve triples with error %v.\n\n", err)
return 2
}
}

fmt.Printf("Successfully written %d triples to file %q.\nTriples exported from graphs:\n\t- %s\n", cnt, path, strings.Join(graphs, "\n\t- "))
return 0
}
22 changes: 15 additions & 7 deletions tools/vcli/bw/repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/google/badwolf/bql/version"
"github.com/google/badwolf/storage"
"github.com/google/badwolf/tools/vcli/bw/command"
"github.com/google/badwolf/tools/vcli/bw/export"
"github.com/google/badwolf/tools/vcli/bw/io"
"github.com/google/badwolf/tools/vcli/bw/load"
)
Expand Down Expand Up @@ -94,13 +95,10 @@ func REPL(driver storage.Store, input *os.File, rl readLiner, chanSize, bulkSize
fmt.Print(prompt)
continue
}
if strings.HasPrefix(l, "run") {
path, cmds, err := runBQLFromFile(ctx, driver, chanSize, l)
if err != nil {
fmt.Printf("[ERROR] %s\n\n", err)
} else {
fmt.Printf("Loaded %q and run %d BQL commands successfully\n\n", path, cmds)
}
if strings.HasPrefix(l, "export") {
args := strings.Split("bw "+l, " ")
usage := "Wrong syntax\n\n\tload <graph_names_separated_by_commas> <file_path>\n"
export.Eval(ctx, usage, args, driver)
fmt.Print(prompt)
continue
}
Expand All @@ -111,6 +109,16 @@ func REPL(driver storage.Store, input *os.File, rl readLiner, chanSize, bulkSize
fmt.Print(prompt)
continue
}
if strings.HasPrefix(l, "run") {
path, cmds, err := runBQLFromFile(ctx, driver, chanSize, l)
if err != nil {
fmt.Printf("[ERROR] %s\n\n", err)
} else {
fmt.Printf("Loaded %q and run %d BQL commands successfully\n\n", path, cmds)
}
fmt.Print(prompt)
continue
}
table, err := runBQL(ctx, l, driver, chanSize)
if err != nil {
fmt.Printf("[ERROR] %s\n\n", err)
Expand Down

0 comments on commit 3daddbd

Please sign in to comment.