// getGraphFromStore returns a Graph. Will create it if it does not exist. func getGraphFromStore(ctx context.Context, st storage.Store, id string) (storage.Graph, error) { g, err := st.Graph(ctx, id) if err == nil { return g, nil } return st.NewGraph(ctx, id) }
// newQueryPlan returns a new query plan ready to be excecuted. func newQueryPlan(store storage.Store, stm *semantic.Statement) (*queryPlan, error) { bs := []string{} for _, b := range stm.Bindings() { bs = append(bs, b) } t, err := table.New([]string{}) if err != nil { return nil, err } var gs []storage.Graph for _, g := range stm.Graphs() { ng, err := store.Graph(g) if err != nil { return nil, err } gs = append(gs, ng) } return &queryPlan{ stm: stm, store: store, bndgs: bs, grfs: gs, grfsNames: stm.Graphs(), cls: stm.SortedGraphPatternClauses(), tbl: t, }, nil }
func update(stm *semantic.Statement, store storage.Store, f updater) error { var ( mu sync.Mutex wg sync.WaitGroup errs []string ) appendError := func(err error) { mu.Lock() defer mu.Unlock() errs = append(errs, err.Error()) } for _, graphBinding := range stm.Graphs() { wg.Add(1) go func(graph string) { defer wg.Done() g, err := store.Graph(graph) if err != nil { appendError(err) return } err = f(g, stm.Data()) if err != nil { appendError(err) } }(graphBinding) } wg.Wait() if len(errs) > 0 { return errors.New(strings.Join(errs, "; ")) } return nil }
// 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, bulkSize int) 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, bulkSize) 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 }
func flush(ctx context.Context, graphs []string, store storage.Store) error { defer func() { workingTrpls = nil }() if len(workingTrpls) > 0 { for _, graph := range graphs { g, err := store.Graph(ctx, graph) if err != nil { return err } if err := g.AddTriples(ctx, workingTrpls); err != nil { return err } } } return nil }