本文整理汇总了Golang中kythe/io/kythe/go/util/stringset.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Construct
func (tbl *testTable) Construct(t *testing.T) xrefs.Service {
p := make(testProtoTable)
tickets := stringset.New()
for _, n := range tbl.Nodes {
tickets.Add(n.Ticket)
}
for _, es := range tbl.EdgeSets {
tickets.Remove(es.Source.Ticket)
testutil.FatalOnErrT(t, "Error writing edge set: %v", p.Put(ctx, EdgeSetKey(mustFix(t, es.Source.Ticket)), es))
}
// Fill in EdgeSets for zero-degree nodes
for ticket := range tickets {
es := &srvpb.PagedEdgeSet{
Source: getNode(ticket),
}
testutil.FatalOnErrT(t, "Error writing edge set: %v", p.Put(ctx, EdgeSetKey(mustFix(t, es.Source.Ticket)), es))
}
for _, ep := range tbl.EdgePages {
testutil.FatalOnErrT(t, "Error writing edge page: %v", p.Put(ctx, EdgePageKey(ep.PageKey), ep))
}
for _, d := range tbl.Decorations {
testutil.FatalOnErrT(t, "Error writing file decorations: %v", p.Put(ctx, DecorationsKey(mustFix(t, d.File.Ticket)), d))
}
for _, cr := range tbl.RefSets {
testutil.FatalOnErrT(t, "Error writing cross-references: %v", p.Put(ctx, CrossReferencesKey(mustFix(t, cr.SourceTicket)), cr))
}
for _, crp := range tbl.RefPages {
testutil.FatalOnErrT(t, "Error writing cross-references: %v", p.Put(ctx, CrossReferencesPageKey(crp.PageKey), crp))
}
return NewCombinedTable(table.ProtoBatchParallel{p})
}
示例2: edgeSet
func edgeSet(kinds []string, pes *srvpb.PagedEdgeSet, pages []*srvpb.EdgePage) *xpb.EdgeSet {
set := stringset.New(kinds...)
es := &xpb.EdgeSet{
SourceTicket: pes.EdgeSet.SourceTicket,
}
for _, g := range pes.EdgeSet.Group {
if set.Contains(g.Kind) || len(set) == 0 {
es.Group = append(es.Group, &xpb.EdgeSet_Group{
Kind: g.Kind,
TargetTicket: g.TargetTicket,
})
}
}
for _, ep := range pages {
g := ep.EdgesGroup
if set.Contains(g.Kind) || len(set) == 0 {
es.Group = append(es.Group, &xpb.EdgeSet_Group{
Kind: g.Kind,
TargetTicket: g.TargetTicket,
})
}
}
return es
}
示例3: SourceFromEntries
// SourceFromEntries returns a new Source from the given a set of entries with
// the same source VName.
func SourceFromEntries(entries []*spb.Entry) *Source {
if len(entries) == 0 {
return nil
}
src := &Source{
Ticket: kytheuri.ToString(entries[0].Source),
Facts: make(map[string][]byte),
Edges: make(map[string][]string),
}
edgeTargets := make(map[string]stringset.Set)
for _, e := range entries {
if graphstore.IsEdge(e) {
tgts, ok := edgeTargets[e.EdgeKind]
if !ok {
tgts = stringset.New()
edgeTargets[e.EdgeKind] = tgts
}
tgts.Add(kytheuri.ToString(e.Target))
} else {
src.Facts[e.FactName] = e.FactValue
}
}
for kind, targets := range edgeTargets {
src.Edges[kind] = targets.Slice()
sort.Strings(src.Edges[kind])
}
return src
}
示例4: Decorations
// Decorations implements part of the xrefs.Interface.
func (d *DB) Decorations(ctx context.Context, req *xpb.DecorationsRequest) (*xpb.DecorationsReply, error) {
if d.selectText == nil {
var err error
d.selectText, err = d.Prepare("SELECT text, text_encoding FROM Nodes WHERE ticket = $1;")
if err != nil {
return nil, fmt.Errorf("error preparing selectText statement: %v", err)
}
}
// TODO(schroederc): dirty buffers
// TODO(schroederc): span locations
fileTicket, err := kytheuri.Fix(req.Location.Ticket)
if err != nil {
return nil, fmt.Errorf("invalid location ticket: %v", err)
}
req.Location.Ticket = fileTicket
decor := &xpb.DecorationsReply{Location: req.Location}
r := d.selectText.QueryRow(fileTicket)
var text []byte
var textEncoding sql.NullString
if err := r.Scan(&text, &textEncoding); err != nil {
return nil, err
}
norm := xrefs.NewNormalizer(text)
if req.SourceText {
decor.SourceText = text
decor.Encoding = textEncoding.String
}
if req.References {
var err error
decor.Reference, err = d.scanReferences(fileTicket, norm)
if err != nil {
return nil, err
}
if len(req.Filter) > 0 && len(decor.Reference) > 0 {
nodeTickets := stringset.New()
for _, r := range decor.Reference {
nodeTickets.Add(r.TargetTicket)
}
nodes, err := d.Nodes(ctx, &xpb.NodesRequest{
Ticket: nodeTickets.Slice(),
Filter: req.Filter,
})
if err != nil {
return nil, fmt.Errorf("error filtering nodes:%v", err)
}
decor.Nodes = nodes.Nodes
}
}
return decor, nil
}
示例5: main
func main() {
flag.Parse()
if len(flag.Args()) == 0 {
flagutil.UsageError("not given any files")
}
xs := xrefs.WebClient(*remoteAPI)
for _, file := range flag.Args() {
ticket := (&kytheuri.URI{Corpus: *corpus, Path: file}).String()
decor, err := xs.Decorations(ctx, &xpb.DecorationsRequest{
Location: &xpb.Location{Ticket: ticket},
SourceText: true,
References: true,
})
if err != nil {
log.Fatalf("Failed to get decorations for file %q", file)
}
nodes := xrefs.NodesMap(decor.Nodes)
emitted := stringset.New()
for _, r := range decor.Reference {
if r.Kind != schema.DefinesBindingEdge || emitted.Contains(r.TargetTicket) {
continue
}
ident := string(nodes[r.TargetTicket][identifierFact])
if ident == "" {
continue
}
offset, err := strconv.Atoi(string(nodes[r.SourceTicket][schema.AnchorStartFact]))
if err != nil {
log.Printf("Invalid start offset for anchor %q", r.SourceTicket)
continue
}
fields, err := getTagFields(xs, r.TargetTicket)
if err != nil {
log.Printf("Failed to get tagfields for %q: %v", r.TargetTicket, err)
}
fmt.Printf("%s\t%s\t%d;\"\t%s\n",
ident, file, offsetLine(decor.SourceText, offset), strings.Join(fields, "\t"))
emitted.Add(r.TargetTicket)
}
}
}
示例6: Edges
// Edges implements part of the xrefs Service interface.
func (t *tableImpl) Edges(ctx context.Context, req *xpb.EdgesRequest) (*xpb.EdgesReply, error) {
tickets, err := xrefs.FixTickets(req.Ticket)
if err != nil {
return nil, err
}
allowedKinds := stringset.New(req.Kind...)
return t.edges(ctx, edgesRequest{
Tickets: tickets,
Filters: req.Filter,
Kinds: func(kind string) bool {
return len(allowedKinds) == 0 || allowedKinds.Contains(kind)
},
PageSize: int(req.PageSize),
PageToken: req.PageToken,
})
}
示例7: displayTargets
func displayTargets(edges []*xpb.EdgeSet) error {
targets := stringset.New()
for _, es := range edges {
for _, g := range es.Group {
targets.Add(g.TargetTicket...)
}
}
if *displayJSON {
return json.NewEncoder(out).Encode(targets.Slice())
}
for target := range targets {
if _, err := fmt.Fprintln(out, target); err != nil {
return err
}
}
return nil
}
示例8: writeEdges
func writeEdges(ctx context.Context, t table.Proto, edges <-chan *spb.Entry, maxEdgePageSize int) error {
defer drainEntries(edges) // ensure channel is drained on errors
temp, err := tempTable("edge.groups")
if err != nil {
return fmt.Errorf("failed to create temporary table: %v", err)
}
edgeGroups := &table.KVProto{temp}
defer func() {
if err := edgeGroups.Close(ctx); err != nil {
log.Println("Error closing edge groups table: %v", err)
}
}()
log.Println("Writing temporary edges table")
var (
src *spb.VName
kind string
targets stringset.Set
)
for e := range edges {
if src != nil && (!compare.VNamesEqual(e.Source, src) || kind != e.EdgeKind) {
if err := writeWithReverses(ctx, edgeGroups, kytheuri.ToString(src), kind, targets.Slice()); err != nil {
return err
}
src = nil
}
if src == nil {
src = e.Source
kind = e.EdgeKind
targets = stringset.New()
}
targets.Add(kytheuri.ToString(e.Target))
}
if src != nil {
if err := writeWithReverses(ctx, edgeGroups, kytheuri.ToString(src), kind, targets.Slice()); err != nil {
return err
}
}
return writeEdgePages(ctx, t, edgeGroups, maxEdgePageSize)
}
示例9: edgeSet
func edgeSet(kinds []string, pes *srvpb.PagedEdgeSet, pages []*srvpb.EdgePage) *xpb.EdgeSet {
set := stringset.New(kinds...)
es := &xpb.EdgeSet{Groups: make(map[string]*xpb.EdgeSet_Group, len(pes.Group))}
for _, g := range pes.Group {
if set.Contains(g.Kind) || len(set) == 0 {
es.Groups[g.Kind] = &xpb.EdgeSet_Group{
Edge: e2e(g.Edge),
}
}
}
for _, ep := range pages {
g := ep.EdgesGroup
if set.Contains(g.Kind) || len(set) == 0 {
es.Groups[g.Kind] = &xpb.EdgeSet_Group{
Edge: e2e(g.Edge),
}
}
}
return es
}
示例10: Edges
// Edges implements part of the xrefs.Service interface.
func (db *DB) Edges(req *xpb.EdgesRequest) (*xpb.EdgesReply, error) {
if req.PageSize != 0 || req.PageToken != "" {
return nil, errors.New("edge pages unimplemented for SQL DB")
}
reply := &xpb.EdgesReply{}
allowedKinds := stringset.New(req.Kind...)
nodeTickets := stringset.New()
for _, t := range req.Ticket {
uri, err := kytheuri.Parse(t)
if err != nil {
return nil, err
}
edges := make(map[string]stringset.Set)
var (
target kytheuri.URI
kind string
)
rows, err := db.edgesStmt.Query(uri.Signature, uri.Corpus, uri.Root, uri.Path, uri.Language)
if err != nil {
rows.Close()
return nil, fmt.Errorf("forward edges query error: %v", err)
}
for rows.Next() {
if err := rows.Scan(&target.Signature, &target.Corpus, &target.Root, &target.Path, &target.Language, &kind); err != nil {
rows.Close()
return nil, fmt.Errorf("forward edges scan error: %v", err)
} else if len(allowedKinds) != 0 && !allowedKinds.Contains(kind) {
continue
}
targets, ok := edges[kind]
if !ok {
targets = stringset.New()
edges[kind] = targets
}
ticket := target.String()
targets.Add(ticket)
nodeTickets.Add(ticket)
}
if err := rows.Close(); err != nil {
return nil, err
}
rows, err = db.revEdgesStmt.Query(uri.Signature, uri.Corpus, uri.Root, uri.Path, uri.Language)
if err != nil {
rows.Close()
return nil, fmt.Errorf("reverse edges query error: %v", err)
}
for rows.Next() {
if err := rows.Scan(&target.Signature, &target.Corpus, &target.Root, &target.Path, &target.Language, &kind); err != nil {
rows.Close()
return nil, fmt.Errorf("reverse edges scan error: %v", err)
}
kind = schema.MirrorEdge(kind)
if len(allowedKinds) != 0 && !allowedKinds.Contains(kind) {
continue
}
targets, ok := edges[kind]
if !ok {
targets = stringset.New()
edges[kind] = targets
}
ticket := target.String()
targets.Add(ticket)
nodeTickets.Add(ticket)
}
if err := rows.Close(); err != nil {
return nil, err
}
var g []*xpb.EdgeSet_Group
for kind, targets := range edges {
g = append(g, &xpb.EdgeSet_Group{
Kind: kind,
TargetTicket: targets.Slice(),
})
}
if len(g) != 0 {
reply.EdgeSet = append(reply.EdgeSet, &xpb.EdgeSet{
SourceTicket: t,
Group: g,
})
}
}
nodesReply, err := db.Nodes(&xpb.NodesRequest{
Ticket: nodeTickets.Slice(),
Filter: req.Filter,
})
if err != nil {
//.........这里部分代码省略.........
示例11: CrossReferences
// CrossReferences implements part of the xrefs Service interface.
func (g *GraphStoreService) CrossReferences(ctx context.Context, req *xpb.CrossReferencesRequest) (*xpb.CrossReferencesReply, error) {
if len(req.Ticket) == 0 {
return nil, errors.New("no cross-references requested")
}
requestedPageSize := int(req.PageSize)
if requestedPageSize == 0 {
requestedPageSize = defaultXRefPageSize
}
eReply, err := g.Edges(ctx, &xpb.EdgesRequest{
Ticket: req.Ticket,
PageSize: int32(requestedPageSize),
PageToken: req.PageToken,
})
if err != nil {
return nil, fmt.Errorf("error getting edges for cross-references: %v", err)
}
reply := &xpb.CrossReferencesReply{
CrossReferences: make(map[string]*xpb.CrossReferencesReply_CrossReferenceSet),
NextPageToken: eReply.NextPageToken,
}
var allRelatedNodes stringset.Set
if len(req.Filter) > 0 {
reply.Nodes = make(map[string]*xpb.NodeInfo)
allRelatedNodes = stringset.New()
}
// Cache parent files across all anchors
files := make(map[string]*fileNode)
var totalXRefs int
for {
for source, es := range eReply.EdgeSets {
xr, ok := reply.CrossReferences[source]
if !ok {
xr = &xpb.CrossReferencesReply_CrossReferenceSet{Ticket: source}
}
var count int
for kind, grp := range es.Groups {
switch {
// TODO(schroeder): handle declarations
case xrefs.IsDefKind(req.DefinitionKind, kind, false):
anchors, err := completeAnchors(ctx, g, req.AnchorText, files, kind, edgeTickets(grp.Edge))
if err != nil {
return nil, fmt.Errorf("error resolving definition anchors: %v", err)
}
count += len(anchors)
xr.Definition = append(xr.Definition, anchors...)
case xrefs.IsRefKind(req.ReferenceKind, kind):
anchors, err := completeAnchors(ctx, g, req.AnchorText, files, kind, edgeTickets(grp.Edge))
if err != nil {
return nil, fmt.Errorf("error resolving reference anchors: %v", err)
}
count += len(anchors)
xr.Reference = append(xr.Reference, anchors...)
case xrefs.IsDocKind(req.DocumentationKind, kind):
anchors, err := completeAnchors(ctx, g, req.AnchorText, files, kind, edgeTickets(grp.Edge))
if err != nil {
return nil, fmt.Errorf("error resolving documentation anchors: %v", err)
}
count += len(anchors)
xr.Documentation = append(xr.Documentation, anchors...)
case allRelatedNodes != nil && !schema.IsAnchorEdge(kind):
count += len(grp.Edge)
for _, edge := range grp.Edge {
xr.RelatedNode = append(xr.RelatedNode, &xpb.CrossReferencesReply_RelatedNode{
Ticket: edge.TargetTicket,
RelationKind: kind,
Ordinal: edge.Ordinal,
})
allRelatedNodes.Add(edge.TargetTicket)
}
}
}
if count > 0 {
reply.CrossReferences[xr.Ticket] = xr
totalXRefs += count
}
}
if reply.NextPageToken == "" || totalXRefs > 0 {
break
}
// We need to return at least 1 xref, if there are any
log.Println("Extra CrossReferences Edges call: ", reply.NextPageToken)
eReply, err = g.Edges(ctx, &xpb.EdgesRequest{
Ticket: req.Ticket,
PageSize: int32(requestedPageSize),
PageToken: reply.NextPageToken,
})
if err != nil {
return nil, fmt.Errorf("error getting edges for cross-references: %v", err)
}
//.........这里部分代码省略.........
示例12: Edges
// Edges implements part of the Service interface.
func (g *GraphStoreService) Edges(ctx context.Context, req *xpb.EdgesRequest) (*xpb.EdgesReply, error) {
if len(req.Ticket) == 0 {
return nil, errors.New("no tickets specified")
} else if req.PageToken != "" {
return nil, errors.New("UNIMPLEMENTED: page_token")
}
patterns := xrefs.ConvertFilters(req.Filter)
allowedKinds := stringset.New(req.Kind...)
targetSet := stringset.New()
reply := new(xpb.EdgesReply)
for _, ticket := range req.Ticket {
vname, err := kytheuri.ToVName(ticket)
if err != nil {
return nil, fmt.Errorf("invalid ticket %q: %v", ticket, err)
}
var (
// EdgeKind -> StringSet<TargetTicket>
filteredEdges = make(map[string]stringset.Set)
filteredFacts []*xpb.Fact
)
if err := g.gs.Read(ctx, &spb.ReadRequest{
Source: vname,
EdgeKind: "*",
}, func(entry *spb.Entry) error {
edgeKind := entry.EdgeKind
if edgeKind == "" {
// node fact
if len(patterns) > 0 && xrefs.MatchesAny(entry.FactName, patterns) {
filteredFacts = append(filteredFacts, entryToFact(entry))
}
} else {
// edge
if len(req.Kind) == 0 || allowedKinds.Contains(edgeKind) {
targets := filteredEdges[edgeKind]
if targets == nil {
targets = stringset.New()
filteredEdges[edgeKind] = targets
}
targets.Add(kytheuri.ToString(entry.Target))
}
}
return nil
}); err != nil {
return nil, fmt.Errorf("failed to retrieve entries for ticket %q", ticket)
}
// Only add a EdgeSet if there are targets for the requested edge kinds.
if len(filteredEdges) > 0 {
var groups []*xpb.EdgeSet_Group
for edgeKind, targets := range filteredEdges {
g := &xpb.EdgeSet_Group{Kind: edgeKind}
for target := range targets {
g.TargetTicket = append(g.TargetTicket, target)
targetSet.Add(target)
}
groups = append(groups, g)
}
reply.EdgeSet = append(reply.EdgeSet, &xpb.EdgeSet{
SourceTicket: ticket,
Group: groups,
})
// In addition, only add a NodeInfo if the filters have resulting facts.
if len(filteredFacts) > 0 {
reply.Node = append(reply.Node, &xpb.NodeInfo{
Ticket: ticket,
Fact: filteredFacts,
})
}
}
}
// Only request Nodes when there are fact filters given.
if len(req.Filter) > 0 {
// Ensure reply.Node is a unique set by removing already requested nodes from targetSet
for _, n := range reply.Node {
targetSet.Remove(n.Ticket)
}
// Batch request all leftover target nodes
nodesReply, err := g.Nodes(ctx, &xpb.NodesRequest{
Ticket: targetSet.Slice(),
Filter: req.Filter,
})
if err != nil {
return nil, fmt.Errorf("failure getting target nodes: %v", err)
}
reply.Node = append(reply.Node, nodesReply.Node...)
}
return reply, nil
}
示例13: CrossReferences
//.........这里部分代码省略.........
if wantMoreCrossRefs && !stats.skipPage(idx) {
p, err := t.crossReferencesPage(ctx, idx.PageKey)
if err != nil {
return nil, fmt.Errorf("internal error: error retrieving cross-references page: %v", idx.PageKey)
}
stats.addAnchors(&crs.Reference, p.Group.Anchor, req.AnchorText)
}
}
}
if len(crs.Declaration) > 0 || len(crs.Definition) > 0 || len(crs.Reference) > 0 || len(crs.Documentation) > 0 || len(crs.Caller) > 0 {
reply.CrossReferences[crs.Ticket] = crs
}
}
if pageToken+stats.total != sumTotalCrossRefs(reply.Total) && stats.total != 0 {
nextToken = &ipb.PageToken{Index: int32(pageToken + stats.total)}
}
if len(req.Filter) > 0 {
er, err := t.edges(ctx, edgesRequest{
Tickets: tickets,
Filters: req.Filter,
Kinds: func(kind string) bool { return !schema.IsAnchorEdge(kind) },
PageToken: edgesPageToken,
TotalOnly: (stats.max <= stats.total),
PageSize: stats.max - stats.total,
})
if err != nil {
return nil, fmt.Errorf("error getting related nodes: %v", err)
}
reply.Total.RelatedNodesByRelation = er.TotalEdgesByKind
for ticket, es := range er.EdgeSets {
nodes := stringset.New()
crs, ok := reply.CrossReferences[ticket]
if !ok {
crs = &xpb.CrossReferencesReply_CrossReferenceSet{
Ticket: ticket,
}
}
for kind, g := range es.Groups {
for _, edge := range g.Edge {
nodes.Add(edge.TargetTicket)
crs.RelatedNode = append(crs.RelatedNode, &xpb.CrossReferencesReply_RelatedNode{
RelationKind: kind,
Ticket: edge.TargetTicket,
Ordinal: edge.Ordinal,
})
}
}
if len(nodes) > 0 {
for ticket, n := range er.Nodes {
if nodes.Contains(ticket) {
reply.Nodes[ticket] = n
}
}
}
if !ok && len(crs.RelatedNode) > 0 {
reply.CrossReferences[ticket] = crs
}
}
if er.NextPageToken != "" {
nextToken = &ipb.PageToken{SecondaryToken: er.NextPageToken}
}
示例14: edges
func (t *tableImpl) edges(ctx context.Context, req edgesRequest) (*xpb.EdgesReply, error) {
stats := filterStats{
max: int(req.PageSize),
}
if req.TotalOnly {
stats.max = 0
} else if stats.max < 0 {
return nil, fmt.Errorf("invalid page_size: %d", req.PageSize)
} else if stats.max == 0 {
stats.max = defaultPageSize
} else if stats.max > maxPageSize {
stats.max = maxPageSize
}
if req.PageToken != "" {
rec, err := base64.StdEncoding.DecodeString(req.PageToken)
if err != nil {
return nil, fmt.Errorf("invalid page_token: %q", req.PageToken)
}
var t ipb.PageToken
if err := proto.Unmarshal(rec, &t); err != nil || t.Index < 0 {
return nil, fmt.Errorf("invalid page_token: %q", req.PageToken)
}
stats.skip = int(t.Index)
}
pageToken := stats.skip
nodeTickets := stringset.New()
rs, err := t.pagedEdgeSets(ctx, req.Tickets)
if err != nil {
return nil, err
}
defer func() {
// drain channel in case of errors or early return
for _ = range rs {
}
}()
patterns := xrefs.ConvertFilters(req.Filters)
reply := &xpb.EdgesReply{
EdgeSets: make(map[string]*xpb.EdgeSet),
Nodes: make(map[string]*xpb.NodeInfo),
TotalEdgesByKind: make(map[string]int64),
}
for r := range rs {
if r.Err == table.ErrNoSuchKey {
continue
} else if r.Err != nil {
return nil, r.Err
}
pes := r.PagedEdgeSet
countEdgeKinds(pes, req.Kinds, reply.TotalEdgesByKind)
// Don't scan the EdgeSet_Groups if we're already at the specified page_size.
if stats.total == stats.max {
continue
}
groups := make(map[string]*xpb.EdgeSet_Group)
for _, grp := range pes.Group {
if req.Kinds == nil || req.Kinds(grp.Kind) {
ng, ns := stats.filter(grp)
if ng != nil {
for _, n := range ns {
if len(patterns) > 0 && !nodeTickets.Contains(n.Ticket) {
nodeTickets.Add(n.Ticket)
reply.Nodes[n.Ticket] = nodeToInfo(patterns, n)
}
}
groups[grp.Kind] = ng
if stats.total == stats.max {
break
}
}
}
}
// TODO(schroederc): ensure that pes.EdgeSet.Groups and pes.PageIndexes of
// the same kind are grouped together in the EdgesReply
if stats.total != stats.max {
for _, idx := range pes.PageIndex {
if req.Kinds == nil || req.Kinds(idx.EdgeKind) {
if stats.skipPage(idx) {
log.Printf("Skipping EdgePage: %s", idx.PageKey)
continue
}
log.Printf("Retrieving EdgePage: %s", idx.PageKey)
ep, err := t.edgePage(ctx, idx.PageKey)
if err == table.ErrNoSuchKey {
return nil, fmt.Errorf("internal error: missing edge page: %q", idx.PageKey)
} else if err != nil {
return nil, fmt.Errorf("edge page lookup error (page key: %q): %v", idx.PageKey, err)
}
ng, ns := stats.filter(ep.EdgesGroup)
//.........这里部分代码省略.........
示例15: Decorations
// Decorations implements part of the xrefs.Service interface.
func (db *DB) Decorations(req *xpb.DecorationsRequest) (*xpb.DecorationsReply, error) {
if req.GetLocation() == nil {
return nil, errors.New("missing location")
} else if req.Location.Kind != xpb.Location_FILE {
return nil, fmt.Errorf("%s location kind unimplemented", req.Location.Kind)
} else if len(req.DirtyBuffer) != 0 {
return nil, errors.New("dirty buffers unimplemented")
}
fileTicket := req.Location.Ticket
edgesReply, err := db.Edges(&xpb.EdgesRequest{
Ticket: []string{fileTicket},
Kind: []string{revChildOfEdge},
Filter: []string{schema.NodeKindFact, schema.TextFact, schema.TextEncodingFact},
})
if err != nil {
return nil, err
}
reply := &xpb.DecorationsReply{
Location: &xpb.Location{
Ticket: fileTicket,
},
}
nodes := xrefs.NodesMap(edgesReply.Node)
if req.SourceText {
if nodes[fileTicket] == nil {
nodesReply, err := db.Nodes(&xpb.NodesRequest{Ticket: []string{fileTicket}})
if err != nil {
return nil, err
}
nodes = xrefs.NodesMap(nodesReply.Node)
}
reply.SourceText = nodes[fileTicket][schema.TextFact]
reply.Encoding = string(nodes[fileTicket][schema.TextEncodingFact])
}
nodeTickets := stringset.New()
if req.References {
// Traverse the following chain of edges:
// file --%/kythe/edge/childof-> []anchor --forwardEdgeKind-> []target
edges := xrefs.EdgesMap(edgesReply.EdgeSet)
for _, anchor := range edges[fileTicket][revChildOfEdge] {
if string(nodes[anchor][schema.NodeKindFact]) != schema.AnchorKind {
continue
}
uri, err := kytheuri.Parse(anchor)
if err != nil {
return nil, fmt.Errorf("invalid anchor ticket found %q: %v", anchor, err)
}
rows, err := db.anchorEdgesStmt.Query(schema.ChildOfEdge, uri.Signature, uri.Corpus, uri.Root, uri.Path, uri.Language)
if err != nil {
return nil, fmt.Errorf("anchor %q edge query error: %v", anchor, err)
}
for rows.Next() {
var target kytheuri.URI
var kind string
if err := rows.Scan(&target.Signature, &target.Corpus, &target.Root, &target.Path, &target.Language, &kind); err != nil {
return nil, fmt.Errorf("anchor %q edge scan error: %v", anchor, err)
}
ticket := target.String()
reply.Reference = append(reply.Reference, &xpb.DecorationsReply_Reference{
SourceTicket: anchor,
Kind: kind,
TargetTicket: ticket,
})
nodeTickets.Add(anchor, ticket)
}
}
}
nodesReply, err := db.Nodes(&xpb.NodesRequest{Ticket: nodeTickets.Slice()})
if err != nil {
return nil, err
}
reply.Node = nodesReply.Node
return reply, nil
}