Example #1
0
// Read implements part of the graphstore.Service interface.
func (s *GraphStore) Read(ctx context.Context, req *spb.ReadRequest, f graphstore.EntryFunc) error {
	s.mu.RLock()
	defer s.mu.RUnlock()
	start := sort.Search(len(s.entries), func(i int) bool {
		comp := compare.VNames(s.entries[i].Source, req.Source)
		return comp != compare.LT && (comp == compare.GT || req.EdgeKind == "*" || s.entries[i].EdgeKind >= req.EdgeKind)
	})
	end := sort.Search(len(s.entries), func(i int) bool {
		comp := compare.VNames(s.entries[i].Source, req.Source)
		return comp == compare.GT || (comp != compare.LT && req.EdgeKind != "*" && s.entries[i].EdgeKind > req.EdgeKind)
	})
	for i := start; i < end; i++ {
		if err := f(s.entries[i]); err == io.EOF {
			return nil
		} else if err != nil {
			return err
		}
	}
	return nil
}
Example #2
0
// Sources returns a channel of Sources derived from a channel of entries in
// GraphStore order.
func Sources(entries <-chan *spb.Entry) <-chan *Source {
	ch := make(chan *Source, 1)

	go func() {
		defer close(ch)

		var es []*spb.Entry
		for entry := range entries {
			if len(es) > 0 && compare.VNames(es[0].Source, entry.Source) != compare.EQ {
				ch <- SourceFromEntries(es)
				es = nil
			}

			es = append(es, entry)
		}
		if len(es) > 0 {
			ch <- SourceFromEntries(es)
		}
	}()

	return ch
}