Esempio n. 1
0
File: io.go Progetto: google/badwolf
// 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
}
Esempio n. 2
0
// 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
}
Esempio n. 3
0
// 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
}