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