Repository: kythe/kythe
Pull request #5451 (2022-11-23 02:51:03)

fix(api): only count xrefs cut by CorpusPathFilters


Patch:
diff --git a/kythe/go/serving/xrefs/xrefs.go b/kythe/go/serving/xrefs/xrefs.go
index d4a665a6e..156dcaf05 100644
--- a/kythe/go/serving/xrefs/xrefs.go
+++ b/kythe/go/serving/xrefs/xrefs.go
@@ -695,21 +695,6 @@ func (t *Table) CrossReferences(ctx context.Context, req *xpb.CrossReferencesReq
 	}
 	stats.reply = reply
 
-	addFiltered := func(kind string, incomplete bool, total int64) {
-		switch {
-		case xrefs.IsDefKind(xpb.CrossReferencesRequest_ALL_DEFINITIONS, kind, incomplete):
-			reply.Filtered.Definitions += total
-		case xrefs.IsDeclKind(xpb.CrossReferencesRequest_ALL_DECLARATIONS, kind, incomplete):
-			reply.Filtered.Declarations += total
-		case xrefs.IsCallerKind(xpb.CrossReferencesRequest_OVERRIDE_CALLERS, kind):
-			reply.Filtered.Callers += total
-		case xrefs.IsRelatedNodeKind(nil, kind):
-			reply.Filtered.RelatedNodesByRelation[kind] += total
-		default:
-			reply.Filtered.References += total
-		}
-	}
-
 	buildConfigs := stringset.New(req.BuildConfig...)
 	patterns := xrefs.ConvertFilters(req.Filter)
 	stats.nodeConverter = nodeConverter{patterns}
@@ -818,24 +803,26 @@ func (t *Table) CrossReferences(ctx context.Context, req *xpb.CrossReferencesReq
 		for _, grp := range cr.Group {
 			// Filter anchor groups based on requested build configs
 			if len(buildConfigs) != 0 && !buildConfigs.Contains(grp.BuildConfig) && !xrefs.IsRelatedNodeKind(relatedKinds, grp.Kind) {
-				addFiltered(grp.Kind, cr.Incomplete, int64(len(grp.Anchor)+len(grp.Caller)+len(grp.RelatedNode)))
 				continue
 			}
 
-			filter.FilterGroup(grp)
+			filtered := filter.FilterGroup(grp)
 			switch {
 			case xrefs.IsDefKind(req.DefinitionKind, grp.Kind, cr.Incomplete):
 				reply.Total.Definitions += int64(len(grp.Anchor))
+				reply.Filtered.Definitions += int64(filtered)
 				if wantMoreCrossRefs {
 					stats.addAnchors(&crs.Definition, grp)
 				}
 			case xrefs.IsDeclKind(req.DeclarationKind, grp.Kind, cr.Incomplete):
 				reply.Total.Declarations += int64(len(grp.Anchor))
+				reply.Filtered.Declarations += int64(filtered)
 				if wantMoreCrossRefs {
 					stats.addAnchors(&crs.Declaration, grp)
 				}
 			case xrefs.IsRefKind(req.ReferenceKind, grp.Kind):
 				reply.Total.References += int64(len(grp.Anchor))
+				reply.Filtered.References += int64(filtered)
 				if wantMoreCrossRefs {
 					stats.addAnchors(&crs.Reference, grp)
 				}
@@ -849,24 +836,23 @@ func (t *Table) CrossReferences(ctx context.Context, req *xpb.CrossReferencesReq
 
 				if len(req.Filter) > 0 && xrefs.IsRelatedNodeKind(relatedKinds, grp.Kind) {
 					reply.Total.RelatedNodesByRelation[grp.Kind] += int64(len(grp.RelatedNode))
+					reply.Filtered.RelatedNodesByRelation[grp.Kind] += int64(filtered)
 					if wantMoreCrossRefs {
 						stats.addRelatedNodes(crs, grp)
 					}
 				}
 			case xrefs.IsCallerKind(req.CallerKind, grp.Kind):
 				reply.Total.Callers += int64(len(grp.Caller))
+				reply.Filtered.Callers += int64(filtered)
 				if wantMoreCrossRefs {
 					stats.addCallers(crs, grp)
 				}
-			default:
-				addFiltered(grp.Kind, cr.Incomplete, int64(len(grp.Anchor)+len(grp.Caller)+len(grp.RelatedNode)))
 			}
 		}
 
 		for _, idx := range cr.PageIndex {
 			// Filter anchor pages based on requested build configs
 			if len(buildConfigs) != 0 && !buildConfigs.Contains(idx.BuildConfig) && !xrefs.IsRelatedNodeKind(relatedKinds, idx.Kind) {
-				addFiltered(idx.Kind, cr.Incomplete, int64(idx.Count))
 				continue
 			}
 
@@ -946,8 +932,6 @@ func (t *Table) CrossReferences(ctx context.Context, req *xpb.CrossReferencesReq
 					reply.Filtered.Callers += int64(filtered)
 					stats.addCallers(crs, p.Group)
 				}
-			default:
-				addFiltered(idx.Kind, cr.Incomplete, int64(idx.Count))
 			}
 		}
 
diff --git a/kythe/proto/xref.proto b/kythe/proto/xref.proto
index 66838b85e..bd0fda327 100644
--- a/kythe/proto/xref.proto
+++ b/kythe/proto/xref.proto
@@ -560,12 +560,14 @@ message CrossReferencesReply {
 
     map<string, int64> related_nodes_by_relation = 6;
   }
-  // Total number of cross-references on all pages matching requested filters.
-  // This may be an approximation if allowed by the CrossReferencesRequest.
+  // Total number of cross-references on all pages matching requested kinds,
+  // build configs, and filters. This may be an approximation if allowed by the
+  // CrossReferencesRequest.
   Total total = 5;
 
-  // Total number of cross-references removed based on requested filters.
-  // This may be an approximation if allowed by the CrossReferencesRequest.
+  // Total number of cross-references removed based on the request's
+  // corpus_path_filters. This may be an approximation if allowed by the
+  // CrossReferencesRequest.
   Total filtered = 12;
 
   // A map from ticket to cross-references for each ticket specified in the


Test patch:
diff --git a/kythe/go/serving/xrefs/xrefs_test.go b/kythe/go/serving/xrefs/xrefs_test.go
index cdb445b6b..2b9d83692 100644
--- a/kythe/go/serving/xrefs/xrefs_test.go
+++ b/kythe/go/serving/xrefs/xrefs_test.go
@@ -1289,6 +1289,9 @@ func TestCrossReferences(t *testing.T) {
 	}, reply.Total); err != nil {
 		t.Error(err)
 	}
+	if err := testutil.DeepEqual(&xpb.CrossReferencesReply_Total{}, reply.Filtered); err != nil {
+		t.Error(err)
+	}
 
 	xr := reply.CrossReferences[ticket]
 	if xr == nil {
@@ -1758,9 +1761,7 @@ func TestCrossReferences_BuildConfigRefs(t *testing.T) {
 	}, reply.Total); err != nil {
 		t.Error(err)
 	}
-	if err := testutil.DeepEqual(&xpb.CrossReferencesReply_Total{
-		References: 2,
-	}, reply.Filtered); err != nil {
+	if err := testutil.DeepEqual(&xpb.CrossReferencesReply_Total{}, reply.Filtered); err != nil {
 		t.Error(err)
 	}
 
@@ -1869,9 +1870,6 @@ func TestCrossReferencesMarkedSource(t *testing.T) {
 	}, reply.Total); err != nil {
 		t.Error(err)
 	}
-	if err := testutil.DeepEqual(&xpb.CrossReferencesReply_Total{}, reply.Filtered); err != nil {
-		t.Error(err)
-	}
 
 	xr := reply.CrossReferences[ticket]
 	if xr == nil {
@@ -1938,11 +1936,6 @@ func TestCrossReferencesMerge(t *testing.T) {
 	}, reply.Total); err != nil {
 		t.Error(err)
 	}
-	if err := testutil.DeepEqual(&xpb.CrossReferencesReply_Total{
-		Callers: 2,
-	}, reply.Filtered); err != nil {
-		t.Error(err)
-	}
 
 	xr := reply.CrossReferences[ticket]
 	if xr == nil {
@@ -2051,11 +2044,6 @@ func TestCrossReferencesIndirection(t *testing.T) {
 		}, reply.Total); err != nil {
 			t.Error(err)
 		}
-		if err := testutil.DeepEqual(&xpb.CrossReferencesReply_Total{
-			Definitions: 1,
-		}, reply.Filtered); err != nil {
-			t.Error(err)
-		}
 
 		xr := reply.CrossReferences[ticket]
 		if xr == nil {
@@ -2125,11 +2113,6 @@ func TestCrossReferencesIndirection(t *testing.T) {
 		}, reply.Total); err != nil {
 			t.Error(err)
 		}
-		if err := testutil.DeepEqual(&xpb.CrossReferencesReply_Total{
-			Definitions: 1,
-		}, reply.Filtered); err != nil {
-			t.Error(err)
-		}
 
 		xr := reply.CrossReferences[ticket]
 		if xr == nil {
@@ -2182,9 +2165,6 @@ func TestCrossReferencesIndirection(t *testing.T) {
 		}, reply.Total); err != nil {
 			t.Error(err)
 		}
-		if err := testutil.DeepEqual(&xpb.CrossReferencesReply_Total{}, reply.Filtered); err != nil {
-			t.Error(err)
-		}
 
 		xr := reply.CrossReferences[ticket]
 		if xr == nil {
@@ -2263,11 +2243,6 @@ func TestCrossReferencesIndirection(t *testing.T) {
 		}, reply.Total); err != nil {
 			t.Error(err)
 		}
-		if err := testutil.DeepEqual(&xpb.CrossReferencesReply_Total{
-			Definitions: 1,
-		}, reply.Filtered); err != nil {
-			t.Error(err)
-		}
 
 		xr := reply.CrossReferences[ticket]
 		if xr == nil {
@@ -2317,11 +2292,6 @@ func TestCrossReferencesDirectCallers(t *testing.T) {
 	}, reply.Total); err != nil {
 		t.Error(err)
 	}
-	if err := testutil.DeepEqual(&xpb.CrossReferencesReply_Total{
-		Callers: 2,
-	}, reply.Filtered); err != nil {
-		t.Error(err)
-	}
 
 	xr := reply.CrossReferences[ticket]
 	if xr == nil {
@@ -2388,9 +2358,6 @@ func TestCrossReferencesOverrideCallers(t *testing.T) {
 	}, reply.Total); err != nil {
 		t.Error(err)
 	}
-	if err := testutil.DeepEqual(&xpb.CrossReferencesReply_Total{}, reply.Filtered); err != nil {
-		t.Error(err)
-	}
 
 	xr := reply.CrossReferences[ticket]
 	if xr == nil {