// WriteGraph serializes the graph into the writer where each triple is // marshaled into a separate line. If there is an error writing the // serialization will stop. It returns the number of triples serialized // regardless if it succeeded or failed partially. func WriteGraph(ctx context.Context, w io.Writer, g storage.Graph) (int, error) { var ( wg sync.WaitGroup tErr error wErr error ) cnt, ts := 0, make(chan *triple.Triple) wg.Add(1) go func() { defer wg.Done() tErr = g.Triples(ctx, ts) }() for t := range ts { if wErr != nil { continue } if _, err := io.WriteString(w, fmt.Sprintf("%s\n", t.String())); err != nil { wErr = err continue } cnt++ } wg.Wait() if tErr != nil { return 0, tErr } if wErr != nil { return 0, wErr } return cnt, nil }
// BQLRandomGraphWalking creates the benchmark. func BQLRandomGraphWalking(ctx context.Context, st storage.Store, chanSize int) ([]*runtime.BenchEntry, error) { rgSize := []int{1000, 10000} sizes := []int{10, 1000, 100000} var trplSets [][]*triple.Triple var ids []string var gids []string var gSizes []int gs, err := getGraphGenerators(rgSize) if err != nil { return nil, err } for idx, g := range gs { for _, s := range sizes { ts, err := g.Generate(s) if err != nil { return nil, err } trplSets = append(trplSets, ts) ids = append(ids, fmt.Sprintf("bql rg branch_factor=%04d, size=%07d", rgSize[idx], s)) gids = append(gids, fmt.Sprintf("bql_b%d_s%d", rgSize[idx], s)) gSizes = append(gSizes, s) } } var bes []*runtime.BenchEntry reps := []int{10} for bqlIdx, bqlQuery := range treeGraphWalkingBQL { bql := bqlQuery for i, max := 0, len(ids); i < max; i++ { for idxReps, r := range reps { var g storage.Graph gID := fmt.Sprintf("bql_rg_%d_%s_r%d_i%d", bqlIdx, gids[i], i, idxReps) data := trplSets[i] bes = append(bes, &runtime.BenchEntry{ BatteryID: fmt.Sprintf("Run BQL random graph walking query %d", bqlIdx), ID: fmt.Sprintf("%s, reps=%02d", ids[i], r), Triples: gSizes[i], Reps: r, Setup: func() error { var err error g, err = st.NewGraph(ctx, "?"+gID) if err != nil { return err } return g.AddTriples(ctx, data) }, F: func() error { query := fmt.Sprintf(bql, gID) _, err := run.BQL(ctx, query, st, chanSize) return err }, TearDown: func() error { return st.DeleteGraph(ctx, "?"+gID) }, }) } } } return bes, nil }
// RemoveExistingTreeTriplesBenchmark creates the benchmark. func RemoveExistingTreeTriplesBenchmark(ctx context.Context, st storage.Store, chanSize int) ([]*runtime.BenchEntry, error) { bFactors := []int{2, 200} sizes := []int{10, 1000, 100000} var trplSets [][]*triple.Triple var ids []string var gids []string var gSizes []int gs, err := getTreeGenerators(bFactors) if err != nil { return nil, err } for idx, g := range gs { for _, s := range sizes { ts, err := g.Generate(s) if err != nil { return nil, err } trplSets = append(trplSets, ts) ids = append(ids, fmt.Sprintf("etg branch_factor=%04d, size=%07d", bFactors[idx], s)) gids = append(gids, fmt.Sprintf("b%d_s%d", bFactors[idx], s)) gSizes = append(gSizes, s) } } var bes []*runtime.BenchEntry reps := []int{10} for i, max := 0, len(ids); i < max; i++ { for idxReps, r := range reps { var g storage.Graph gID := fmt.Sprintf("remove_existing_tree_%s_r%d_i%d", gids[i], i, idxReps) data := trplSets[i] bes = append(bes, &runtime.BenchEntry{ BatteryID: "Remove existing triples", ID: fmt.Sprintf("%s, reps=%02d", ids[i], r), Triples: gSizes[i], Reps: r, Setup: func() error { var err error g, err = st.NewGraph(ctx, gID) if err != nil { return err } return g.AddTriples(ctx, data) }, F: func() error { return g.RemoveTriples(ctx, data) }, TearDown: func() error { return st.DeleteGraph(ctx, gID) }, }) } } return bes, nil }
// WriteGraph serializes the graph into the writer where each triple is // marshalled into a separate line. If there is an error writting the // serializatino will stop. It returns the number of triples serialized // regardless if it succeded of it failed partialy. func WriteGraph(w io.Writer, g storage.Graph) (int, error) { cnt := 0 for t := range g.Triples() { _, err := io.WriteString(w, fmt.Sprintf("%s\n", t.String())) if err != nil { return cnt, err } cnt++ } return cnt, nil }
// WriteGraph serializes the graph into the writer where each triple is // marshalled into a separate line. If there is an error writting the // serialization will stop. It returns the number of triples serialized // regardless if it succeded of it failed partialy. func WriteGraph(ctx context.Context, w io.Writer, g storage.Graph) (int, error) { cnt := 0 ts, err := g.Triples(ctx) if err != nil { return 0, err } for t := range ts { if _, err := io.WriteString(w, fmt.Sprintf("%s\n", t.String())); err != nil { return cnt, err } cnt++ } return cnt, nil }
// ReadIntoGraph reads a graph out of the provided reader. The data on the // reader is interpret as text. Each line represents one triple using the // standard serialized format. ReadIntoGraph will stop if fails to Parse // a triple on the stream. The triples read till then would have also been // added to the graph. The int value returns the number of triples added func ReadIntoGraph(g storage.Graph, r io.Reader, b literal.Builder) (int, error) { cnt, scanner := 0, bufio.NewScanner(r) scanner.Split(bufio.ScanLines) for scanner.Scan() { text := strings.TrimSpace(scanner.Text()) if text == "" { continue } t, err := triple.ParseTriple(text, b) if err != nil { return cnt, err } cnt++ g.AddTriples([]*triple.Triple{t}) } return cnt, nil }