|
| 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 | +} |
0 commit comments