1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.server.appsearch.external.localstorage.stats;
17 
18 import android.annotation.IntDef;
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.app.appsearch.AppSearchResult;
22 import android.app.appsearch.AppSearchSchema.StringPropertyConfig.JoinableValueType;
23 import android.app.appsearch.SearchSpec;
24 import android.app.appsearch.annotation.CanIgnoreReturnValue;
25 
26 import com.android.internal.util.Preconditions;
27 
28 import java.lang.annotation.Retention;
29 import java.lang.annotation.RetentionPolicy;
30 import java.util.Objects;
31 
32 /**
33  * Class holds detailed stats for {@link android.app.appsearch.AppSearchSession#search(String,
34  * SearchSpec)}
35  *
36  * @hide
37  */
38 public final class SearchStats {
39     /** Types of Visibility scopes available for search. */
40     @IntDef(
41             value = {
42                 // Searches apps' own documents.
43                 VISIBILITY_SCOPE_LOCAL,
44                 // Searches the global documents. Including platform surfaceable and 3p-access.
45                 VISIBILITY_SCOPE_GLOBAL,
46                 VISIBILITY_SCOPE_UNKNOWN,
47                 // TODO(b/173532925) Add THIRD_PARTY_ACCESS once we can distinguish platform
48                 //  surfaceable from 3p access(right both of them are categorized as
49                 //  VISIBILITY_SCOPE_GLOBAL)
50             })
51     @Retention(RetentionPolicy.SOURCE)
52     public @interface VisibilityScope {}
53 
54     // Searches apps' own documents.
55     public static final int VISIBILITY_SCOPE_LOCAL = 1;
56     // Searches the global documents. Including platform surfaceable and 3p-access.
57     public static final int VISIBILITY_SCOPE_GLOBAL = 2;
58     public static final int VISIBILITY_SCOPE_UNKNOWN = 3;
59 
60     // TODO(b/173532925): Add a field searchType to indicate where the search is used(normal
61     //  query vs in removeByQuery vs during migration)
62 
63     @NonNull private final String mPackageName;
64     @Nullable private final String mDatabase;
65 
66     /**
67      * The status code returned by {@link AppSearchResult#getResultCode()} for the call or internal
68      * state.
69      */
70     @AppSearchResult.ResultCode private final int mStatusCode;
71 
72     private final int mTotalLatencyMillis;
73 
74     /** Time used to rewrite the search spec. */
75     private final int mRewriteSearchSpecLatencyMillis;
76 
77     /** Time used to rewrite the search results. */
78     private final int mRewriteSearchResultLatencyMillis;
79 
80     /** Time passed while waiting to acquire the lock during Java function calls. */
81     private final int mJavaLockAcquisitionLatencyMillis;
82 
83     /**
84      * Time spent on ACL checking. This is the time spent filtering namespaces based on package
85      * permissions and Android permission access.
86      */
87     private final int mAclCheckLatencyMillis;
88 
89     /** Defines the scope the query is searching over. */
90     @VisibilityScope private final int mVisibilityScope;
91 
92     /** Overall time used for the native function call. */
93     private final int mNativeLatencyMillis;
94 
95     /** Number of terms in the query string. */
96     private final int mNativeNumTerms;
97 
98     /** Length of the query string. */
99     private final int mNativeQueryLength;
100 
101     /** Number of namespaces filtered. */
102     private final int mNativeNumNamespacesFiltered;
103 
104     /** Number of schema types filtered. */
105     private final int mNativeNumSchemaTypesFiltered;
106 
107     /** The requested number of results in one page. */
108     private final int mNativeRequestedPageSize;
109 
110     /** The actual number of results returned in the current page. */
111     private final int mNativeNumResultsReturnedCurrentPage;
112 
113     /**
114      * Whether the function call is querying the first page. If it's not, Icing will fetch the
115      * results from cache so that some steps may be skipped.
116      */
117     private final boolean mNativeIsFirstPage;
118 
119     /**
120      * Time used to parse the query, including 2 parts: tokenizing and transforming tokens into an
121      * iterator tree.
122      */
123     private final int mNativeParseQueryLatencyMillis;
124 
125     /** Strategy of scoring and ranking. */
126     @SearchSpec.RankingStrategy private final int mNativeRankingStrategy;
127 
128     /** Number of documents scored. */
129     private final int mNativeNumDocumentsScored;
130 
131     /** Time used to score the raw results. */
132     private final int mNativeScoringLatencyMillis;
133 
134     /** Time used to rank the scored results. */
135     private final int mNativeRankingLatencyMillis;
136 
137     /**
138      * Time used to fetch the document protos. Note that it includes the time to snippet if {@link
139      * SearchStats#mNativeNumResultsWithSnippets} is greater than 0.
140      */
141     private final int mNativeDocumentRetrievingLatencyMillis;
142 
143     /** How many snippets are calculated. */
144     private final int mNativeNumResultsWithSnippets;
145 
146     /** Time passed while waiting to acquire the lock during native function calls. */
147     private final int mNativeLockAcquisitionLatencyMillis;
148 
149     /** Time used to send data across the JNI boundary from java to native side. */
150     private final int mJavaToNativeJniLatencyMillis;
151 
152     /** Time used to send data across the JNI boundary from native to java side. */
153     private final int mNativeToJavaJniLatencyMillis;
154 
155     /** The type of join performed. Zero if no join is performed */
156     @JoinableValueType private final int mJoinType;
157 
158     /** The total number of joined documents in the current page. */
159     private final int mNativeNumJoinedResultsCurrentPage;
160 
161     /** Time taken to join documents together. */
162     private final int mNativeJoinLatencyMillis;
163 
164     @Nullable private final String mSearchSourceLogTag;
165 
SearchStats(@onNull Builder builder)166     SearchStats(@NonNull Builder builder) {
167         Objects.requireNonNull(builder);
168         mPackageName = builder.mPackageName;
169         mDatabase = builder.mDatabase;
170         mStatusCode = builder.mStatusCode;
171         mTotalLatencyMillis = builder.mTotalLatencyMillis;
172         mRewriteSearchSpecLatencyMillis = builder.mRewriteSearchSpecLatencyMillis;
173         mRewriteSearchResultLatencyMillis = builder.mRewriteSearchResultLatencyMillis;
174         mJavaLockAcquisitionLatencyMillis = builder.mJavaLockAcquisitionLatencyMillis;
175         mAclCheckLatencyMillis = builder.mAclCheckLatencyMillis;
176         mVisibilityScope = builder.mVisibilityScope;
177         mNativeLatencyMillis = builder.mNativeLatencyMillis;
178         mNativeNumTerms = builder.mNativeNumTerms;
179         mNativeQueryLength = builder.mNativeQueryLength;
180         mNativeNumNamespacesFiltered = builder.mNativeNumNamespacesFiltered;
181         mNativeNumSchemaTypesFiltered = builder.mNativeNumSchemaTypesFiltered;
182         mNativeRequestedPageSize = builder.mNativeRequestedPageSize;
183         mNativeNumResultsReturnedCurrentPage = builder.mNativeNumResultsReturnedCurrentPage;
184         mNativeIsFirstPage = builder.mNativeIsFirstPage;
185         mNativeParseQueryLatencyMillis = builder.mNativeParseQueryLatencyMillis;
186         mNativeRankingStrategy = builder.mNativeRankingStrategy;
187         mNativeNumDocumentsScored = builder.mNativeNumDocumentsScored;
188         mNativeScoringLatencyMillis = builder.mNativeScoringLatencyMillis;
189         mNativeRankingLatencyMillis = builder.mNativeRankingLatencyMillis;
190         mNativeNumResultsWithSnippets = builder.mNativeNumResultsWithSnippets;
191         mNativeDocumentRetrievingLatencyMillis = builder.mNativeDocumentRetrievingLatencyMillis;
192         mNativeLockAcquisitionLatencyMillis = builder.mNativeLockAcquisitionLatencyMillis;
193         mJavaToNativeJniLatencyMillis = builder.mJavaToNativeJniLatencyMillis;
194         mNativeToJavaJniLatencyMillis = builder.mNativeToJavaJniLatencyMillis;
195         mJoinType = builder.mJoinType;
196         mNativeNumJoinedResultsCurrentPage = builder.mNativeNumJoinedResultsCurrentPage;
197         mNativeJoinLatencyMillis = builder.mNativeJoinLatencyMillis;
198         mSearchSourceLogTag = builder.mSearchSourceLogTag;
199     }
200 
201     /** Returns the package name of the session. */
202     @NonNull
getPackageName()203     public String getPackageName() {
204         return mPackageName;
205     }
206 
207     /**
208      * Returns the database name of the session.
209      *
210      * @return database name used by the session. {@code null} if and only if it is a global
211      *     search(visibilityScope is {@link SearchStats#VISIBILITY_SCOPE_GLOBAL}).
212      */
213     @Nullable
getDatabase()214     public String getDatabase() {
215         return mDatabase;
216     }
217 
218     /** Returns status of the search. */
219     @AppSearchResult.ResultCode
getStatusCode()220     public int getStatusCode() {
221         return mStatusCode;
222     }
223 
224     /** Returns the total latency of the search. */
getTotalLatencyMillis()225     public int getTotalLatencyMillis() {
226         return mTotalLatencyMillis;
227     }
228 
229     /** Returns how much time spent on rewriting the {@link SearchSpec}. */
getRewriteSearchSpecLatencyMillis()230     public int getRewriteSearchSpecLatencyMillis() {
231         return mRewriteSearchSpecLatencyMillis;
232     }
233 
234     /** Returns how much time spent on rewriting the {@link android.app.appsearch.SearchResult}. */
getRewriteSearchResultLatencyMillis()235     public int getRewriteSearchResultLatencyMillis() {
236         return mRewriteSearchResultLatencyMillis;
237     }
238 
239     /** Returns time passed while waiting to acquire the lock during Java function calls */
getJavaLockAcquisitionLatencyMillis()240     public int getJavaLockAcquisitionLatencyMillis() {
241         return mJavaLockAcquisitionLatencyMillis;
242     }
243 
244     /**
245      * Returns time spent on ACL checking, which is the time spent filtering namespaces based on
246      * package permissions and Android permission access.
247      */
getAclCheckLatencyMillis()248     public int getAclCheckLatencyMillis() {
249         return mAclCheckLatencyMillis;
250     }
251 
252     /** Returns the visibility scope of the search. */
253     @VisibilityScope
getVisibilityScope()254     public int getVisibilityScope() {
255         return mVisibilityScope;
256     }
257 
258     /** Returns how much time spent on the native calls. */
getNativeLatencyMillis()259     public int getNativeLatencyMillis() {
260         return mNativeLatencyMillis;
261     }
262 
263     /** Returns number of terms in the search string. */
getTermCount()264     public int getTermCount() {
265         return mNativeNumTerms;
266     }
267 
268     /** Returns the length of the search string. */
getQueryLength()269     public int getQueryLength() {
270         return mNativeQueryLength;
271     }
272 
273     /** Returns number of namespaces filtered. */
getFilteredNamespaceCount()274     public int getFilteredNamespaceCount() {
275         return mNativeNumNamespacesFiltered;
276     }
277 
278     /** Returns number of schema types filtered. */
getFilteredSchemaTypeCount()279     public int getFilteredSchemaTypeCount() {
280         return mNativeNumSchemaTypesFiltered;
281     }
282 
283     /** Returns the requested number of results in one page. */
getRequestedPageSize()284     public int getRequestedPageSize() {
285         return mNativeRequestedPageSize;
286     }
287 
288     /** Returns the actual number of results returned in the current page. */
getCurrentPageReturnedResultCount()289     public int getCurrentPageReturnedResultCount() {
290         return mNativeNumResultsReturnedCurrentPage;
291     }
292 
293     // TODO(b/185184738) Make it an integer to show how many pages having been returned.
294     /** Returns whether the function call is querying the first page. */
isFirstPage()295     public boolean isFirstPage() {
296         return mNativeIsFirstPage;
297     }
298 
299     /**
300      * Returns time used to parse the query, including 2 parts: tokenizing and transforming tokens
301      * into an iterator tree.
302      */
getParseQueryLatencyMillis()303     public int getParseQueryLatencyMillis() {
304         return mNativeParseQueryLatencyMillis;
305     }
306 
307     /** Returns strategy of scoring and ranking. */
308     @SearchSpec.RankingStrategy
getRankingStrategy()309     public int getRankingStrategy() {
310         return mNativeRankingStrategy;
311     }
312 
313     /** Returns number of documents scored. */
getScoredDocumentCount()314     public int getScoredDocumentCount() {
315         return mNativeNumDocumentsScored;
316     }
317 
318     /** Returns time used to score the raw results. */
getScoringLatencyMillis()319     public int getScoringLatencyMillis() {
320         return mNativeScoringLatencyMillis;
321     }
322 
323     /** Returns time used to rank the scored results. */
getRankingLatencyMillis()324     public int getRankingLatencyMillis() {
325         return mNativeRankingLatencyMillis;
326     }
327 
328     /**
329      * Returns time used to fetch the document protos. Note that it includes the time to snippet if
330      * {@link SearchStats#mNativeNumResultsWithSnippets} is not zero.
331      */
getDocumentRetrievingLatencyMillis()332     public int getDocumentRetrievingLatencyMillis() {
333         return mNativeDocumentRetrievingLatencyMillis;
334     }
335 
336     /** Returns the number of the results in the page returned were snippeted. */
getResultWithSnippetsCount()337     public int getResultWithSnippetsCount() {
338         return mNativeNumResultsWithSnippets;
339     }
340 
341     /** Returns time passed while waiting to acquire the lock during native function calls. */
getNativeLockAcquisitionLatencyMillis()342     public int getNativeLockAcquisitionLatencyMillis() {
343         return mNativeLockAcquisitionLatencyMillis;
344     }
345 
346     /** Returns time used to send data across the JNI boundary from java to native side. */
getJavaToNativeJniLatencyMillis()347     public int getJavaToNativeJniLatencyMillis() {
348         return mJavaToNativeJniLatencyMillis;
349     }
350 
351     /** Returns time used to send data across the JNI boundary from native to java side. */
getNativeToJavaJniLatencyMillis()352     public int getNativeToJavaJniLatencyMillis() {
353         return mNativeToJavaJniLatencyMillis;
354     }
355 
356     /** Returns the type of join performed. Blank if no join is performed */
getJoinType()357     public @JoinableValueType int getJoinType() {
358         return mJoinType;
359     }
360 
361     /** Returns the total number of joined documents in the current page. */
getNumJoinedResultsCurrentPage()362     public int getNumJoinedResultsCurrentPage() {
363         return mNativeNumJoinedResultsCurrentPage;
364     }
365 
366     /** Returns the time taken to join documents together. */
getJoinLatencyMillis()367     public int getJoinLatencyMillis() {
368         return mNativeJoinLatencyMillis;
369     }
370 
371     /** Returns a tag to indicate the source of this search, or {code null} if never set. */
372     @Nullable
getSearchSourceLogTag()373     public String getSearchSourceLogTag() {
374         return mSearchSourceLogTag;
375     }
376 
377     /** Builder for {@link SearchStats} */
378     public static class Builder {
379         @NonNull final String mPackageName;
380         @Nullable String mDatabase;
381         @AppSearchResult.ResultCode int mStatusCode;
382         int mTotalLatencyMillis;
383         int mRewriteSearchSpecLatencyMillis;
384         int mRewriteSearchResultLatencyMillis;
385         int mJavaLockAcquisitionLatencyMillis;
386         int mAclCheckLatencyMillis;
387         int mVisibilityScope;
388         int mNativeLatencyMillis;
389         int mNativeNumTerms;
390         int mNativeQueryLength;
391         int mNativeNumNamespacesFiltered;
392         int mNativeNumSchemaTypesFiltered;
393         int mNativeRequestedPageSize;
394         int mNativeNumResultsReturnedCurrentPage;
395         boolean mNativeIsFirstPage;
396         int mNativeParseQueryLatencyMillis;
397         int mNativeRankingStrategy;
398         int mNativeNumDocumentsScored;
399         int mNativeScoringLatencyMillis;
400         int mNativeRankingLatencyMillis;
401         int mNativeNumResultsWithSnippets;
402         int mNativeDocumentRetrievingLatencyMillis;
403         int mNativeLockAcquisitionLatencyMillis;
404         int mJavaToNativeJniLatencyMillis;
405         int mNativeToJavaJniLatencyMillis;
406         @JoinableValueType int mJoinType;
407         int mNativeNumJoinedResultsCurrentPage;
408         int mNativeJoinLatencyMillis;
409         @Nullable String mSearchSourceLogTag;
410 
411         /**
412          * Constructor
413          *
414          * @param visibilityScope scope for the corresponding search.
415          * @param packageName name of the calling package.
416          */
Builder(@isibilityScope int visibilityScope, @NonNull String packageName)417         public Builder(@VisibilityScope int visibilityScope, @NonNull String packageName) {
418             mVisibilityScope = visibilityScope;
419             mPackageName = Objects.requireNonNull(packageName);
420         }
421 
422         /** Sets the database used by the session. */
423         @CanIgnoreReturnValue
424         @NonNull
setDatabase(@ullable String database)425         public Builder setDatabase(@Nullable String database) {
426             mDatabase = database;
427             return this;
428         }
429 
430         /** Sets the status of the search. */
431         @CanIgnoreReturnValue
432         @NonNull
setStatusCode(@ppSearchResult.ResultCode int statusCode)433         public Builder setStatusCode(@AppSearchResult.ResultCode int statusCode) {
434             mStatusCode = statusCode;
435             return this;
436         }
437 
438         /** Sets total latency for the search. */
439         @CanIgnoreReturnValue
440         @NonNull
setTotalLatencyMillis(int totalLatencyMillis)441         public Builder setTotalLatencyMillis(int totalLatencyMillis) {
442             mTotalLatencyMillis = totalLatencyMillis;
443             return this;
444         }
445 
446         /** Sets time used to rewrite the search spec. */
447         @CanIgnoreReturnValue
448         @NonNull
setRewriteSearchSpecLatencyMillis(int rewriteSearchSpecLatencyMillis)449         public Builder setRewriteSearchSpecLatencyMillis(int rewriteSearchSpecLatencyMillis) {
450             mRewriteSearchSpecLatencyMillis = rewriteSearchSpecLatencyMillis;
451             return this;
452         }
453 
454         /** Sets time used to rewrite the search results. */
455         @CanIgnoreReturnValue
456         @NonNull
setRewriteSearchResultLatencyMillis(int rewriteSearchResultLatencyMillis)457         public Builder setRewriteSearchResultLatencyMillis(int rewriteSearchResultLatencyMillis) {
458             mRewriteSearchResultLatencyMillis = rewriteSearchResultLatencyMillis;
459             return this;
460         }
461 
462         /** Sets time passed while waiting to acquire the lock during Java function calls. */
463         @CanIgnoreReturnValue
464         @NonNull
setJavaLockAcquisitionLatencyMillis(int javaLockAcquisitionLatencyMillis)465         public Builder setJavaLockAcquisitionLatencyMillis(int javaLockAcquisitionLatencyMillis) {
466             mJavaLockAcquisitionLatencyMillis = javaLockAcquisitionLatencyMillis;
467             return this;
468         }
469 
470         /**
471          * Sets time spent on ACL checking, which is the time spent filtering namespaces based on
472          * package permissions and Android permission access.
473          */
474         @CanIgnoreReturnValue
475         @NonNull
setAclCheckLatencyMillis(int aclCheckLatencyMillis)476         public Builder setAclCheckLatencyMillis(int aclCheckLatencyMillis) {
477             mAclCheckLatencyMillis = aclCheckLatencyMillis;
478             return this;
479         }
480 
481         /** Sets overall time used for the native function calls. */
482         @CanIgnoreReturnValue
483         @NonNull
setNativeLatencyMillis(int nativeLatencyMillis)484         public Builder setNativeLatencyMillis(int nativeLatencyMillis) {
485             mNativeLatencyMillis = nativeLatencyMillis;
486             return this;
487         }
488 
489         /** Sets number of terms in the search string. */
490         @CanIgnoreReturnValue
491         @NonNull
setTermCount(int termCount)492         public Builder setTermCount(int termCount) {
493             mNativeNumTerms = termCount;
494             return this;
495         }
496 
497         /** Sets length of the search string. */
498         @CanIgnoreReturnValue
499         @NonNull
setQueryLength(int queryLength)500         public Builder setQueryLength(int queryLength) {
501             mNativeQueryLength = queryLength;
502             return this;
503         }
504 
505         /** Sets number of namespaces filtered. */
506         @CanIgnoreReturnValue
507         @NonNull
setFilteredNamespaceCount(int filteredNamespaceCount)508         public Builder setFilteredNamespaceCount(int filteredNamespaceCount) {
509             mNativeNumNamespacesFiltered = filteredNamespaceCount;
510             return this;
511         }
512 
513         /** Sets number of schema types filtered. */
514         @CanIgnoreReturnValue
515         @NonNull
setFilteredSchemaTypeCount(int filteredSchemaTypeCount)516         public Builder setFilteredSchemaTypeCount(int filteredSchemaTypeCount) {
517             mNativeNumSchemaTypesFiltered = filteredSchemaTypeCount;
518             return this;
519         }
520 
521         /** Sets the requested number of results in one page. */
522         @CanIgnoreReturnValue
523         @NonNull
setRequestedPageSize(int requestedPageSize)524         public Builder setRequestedPageSize(int requestedPageSize) {
525             mNativeRequestedPageSize = requestedPageSize;
526             return this;
527         }
528 
529         /** Sets the actual number of results returned in the current page. */
530         @CanIgnoreReturnValue
531         @NonNull
setCurrentPageReturnedResultCount(int currentPageReturnedResultCount)532         public Builder setCurrentPageReturnedResultCount(int currentPageReturnedResultCount) {
533             mNativeNumResultsReturnedCurrentPage = currentPageReturnedResultCount;
534             return this;
535         }
536 
537         /**
538          * Sets whether the function call is querying the first page. If it's not, Icing will fetch
539          * the results from cache so that some steps may be skipped.
540          */
541         @CanIgnoreReturnValue
542         @NonNull
setIsFirstPage(boolean nativeIsFirstPage)543         public Builder setIsFirstPage(boolean nativeIsFirstPage) {
544             mNativeIsFirstPage = nativeIsFirstPage;
545             return this;
546         }
547 
548         /**
549          * Sets time used to parse the query, including 2 parts: tokenizing and transforming tokens
550          * into an iterator tree.
551          */
552         @CanIgnoreReturnValue
553         @NonNull
setParseQueryLatencyMillis(int parseQueryLatencyMillis)554         public Builder setParseQueryLatencyMillis(int parseQueryLatencyMillis) {
555             mNativeParseQueryLatencyMillis = parseQueryLatencyMillis;
556             return this;
557         }
558 
559         /** Sets strategy of scoring and ranking. */
560         @CanIgnoreReturnValue
561         @NonNull
setRankingStrategy(@earchSpec.RankingStrategy int rankingStrategy)562         public Builder setRankingStrategy(@SearchSpec.RankingStrategy int rankingStrategy) {
563             mNativeRankingStrategy = rankingStrategy;
564             return this;
565         }
566 
567         /** Sets number of documents scored. */
568         @CanIgnoreReturnValue
569         @NonNull
setScoredDocumentCount(int scoredDocumentCount)570         public Builder setScoredDocumentCount(int scoredDocumentCount) {
571             mNativeNumDocumentsScored = scoredDocumentCount;
572             return this;
573         }
574 
575         /** Sets time used to score the raw results. */
576         @CanIgnoreReturnValue
577         @NonNull
setScoringLatencyMillis(int scoringLatencyMillis)578         public Builder setScoringLatencyMillis(int scoringLatencyMillis) {
579             mNativeScoringLatencyMillis = scoringLatencyMillis;
580             return this;
581         }
582 
583         /** Sets time used to rank the scored results. */
584         @CanIgnoreReturnValue
585         @NonNull
setRankingLatencyMillis(int rankingLatencyMillis)586         public Builder setRankingLatencyMillis(int rankingLatencyMillis) {
587             mNativeRankingLatencyMillis = rankingLatencyMillis;
588             return this;
589         }
590 
591         /** Sets time used to fetch the document protos. */
592         @CanIgnoreReturnValue
593         @NonNull
setDocumentRetrievingLatencyMillis(int documentRetrievingLatencyMillis)594         public Builder setDocumentRetrievingLatencyMillis(int documentRetrievingLatencyMillis) {
595             mNativeDocumentRetrievingLatencyMillis = documentRetrievingLatencyMillis;
596             return this;
597         }
598 
599         /** Sets how many snippets are calculated. */
600         @CanIgnoreReturnValue
601         @NonNull
setResultWithSnippetsCount(int resultWithSnippetsCount)602         public Builder setResultWithSnippetsCount(int resultWithSnippetsCount) {
603             mNativeNumResultsWithSnippets = resultWithSnippetsCount;
604             return this;
605         }
606 
607         /** Sets time passed while waiting to acquire the lock during native function calls. */
608         @CanIgnoreReturnValue
609         @NonNull
setNativeLockAcquisitionLatencyMillis( int nativeLockAcquisitionLatencyMillis)610         public Builder setNativeLockAcquisitionLatencyMillis(
611                 int nativeLockAcquisitionLatencyMillis) {
612             mNativeLockAcquisitionLatencyMillis = nativeLockAcquisitionLatencyMillis;
613             return this;
614         }
615 
616         /** Sets time used to send data across the JNI boundary from java to native side. */
617         @CanIgnoreReturnValue
618         @NonNull
setJavaToNativeJniLatencyMillis(int javaToNativeJniLatencyMillis)619         public Builder setJavaToNativeJniLatencyMillis(int javaToNativeJniLatencyMillis) {
620             mJavaToNativeJniLatencyMillis = javaToNativeJniLatencyMillis;
621             return this;
622         }
623 
624         /** Sets time used to send data across the JNI boundary from native to java side. */
625         @CanIgnoreReturnValue
626         @NonNull
setNativeToJavaJniLatencyMillis(int nativeToJavaJniLatencyMillis)627         public Builder setNativeToJavaJniLatencyMillis(int nativeToJavaJniLatencyMillis) {
628             mNativeToJavaJniLatencyMillis = nativeToJavaJniLatencyMillis;
629             return this;
630         }
631 
632         /** Sets whether or not this is a join query */
633         @CanIgnoreReturnValue
634         @NonNull
setJoinType(@oinableValueType int joinType)635         public Builder setJoinType(@JoinableValueType int joinType) {
636             mJoinType = joinType;
637             return this;
638         }
639 
640         /** Set the total number of joined documents in a page. */
641         @CanIgnoreReturnValue
642         @NonNull
setNativeNumJoinedResultsCurrentPage(int nativeNumJoinedResultsCurrentPage)643         public Builder setNativeNumJoinedResultsCurrentPage(int nativeNumJoinedResultsCurrentPage) {
644             mNativeNumJoinedResultsCurrentPage = nativeNumJoinedResultsCurrentPage;
645             return this;
646         }
647 
648         /** Sets time it takes to join documents together in icing. */
649         @CanIgnoreReturnValue
650         @NonNull
setNativeJoinLatencyMillis(int nativeJoinLatencyMillis)651         public Builder setNativeJoinLatencyMillis(int nativeJoinLatencyMillis) {
652             mNativeJoinLatencyMillis = nativeJoinLatencyMillis;
653             return this;
654         }
655 
656         /** Sets a tag to indicate the source of this search. */
657         @CanIgnoreReturnValue
658         @NonNull
setSearchSourceLogTag(@ullable String searchSourceLogTag)659         public Builder setSearchSourceLogTag(@Nullable String searchSourceLogTag) {
660             mSearchSourceLogTag = searchSourceLogTag;
661             return this;
662         }
663 
664         /**
665          * Constructs a new {@link SearchStats} from the contents of this {@link
666          * SearchStats.Builder}.
667          */
668         @NonNull
build()669         public SearchStats build() {
670             if (mDatabase == null) {
671                 Preconditions.checkState(
672                         mVisibilityScope != SearchStats.VISIBILITY_SCOPE_LOCAL,
673                         "database can not be null if visibilityScope is local.");
674             }
675 
676             return new SearchStats(/* builder= */ this);
677         }
678     }
679 }
680