• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.server.appsearch.external.localstorage.stats;
18 
19 import android.annotation.NonNull;
20 import android.app.appsearch.AppSearchResult;
21 
22 import java.util.Objects;
23 
24 /**
25  * A class for holding detailed stats to log for each individual document put by a {@link
26  * android.app.appsearch.AppSearchSession#put} call.
27  *
28  * @hide
29  */
30 public final class PutDocumentStats {
31     @NonNull private final String mPackageName;
32     @NonNull private final String mDatabase;
33     /**
34      * The status code returned by {@link AppSearchResult#getResultCode()} for the call or internal
35      * state.
36      */
37     @AppSearchResult.ResultCode private final int mStatusCode;
38 
39     private final int mTotalLatencyMillis;
40 
41     /** Time used to generate a document proto from a Bundle. */
42     private final int mGenerateDocumentProtoLatencyMillis;
43 
44     /** Time used to rewrite types and namespaces in the document. */
45     private final int mRewriteDocumentTypesLatencyMillis;
46 
47     /** Overall time used for the native function call. */
48     private final int mNativeLatencyMillis;
49 
50     /** Time used to store the document. */
51     private final int mNativeDocumentStoreLatencyMillis;
52 
53     /** Time used to index the document. It doesn't include the time to merge indices. */
54     private final int mNativeIndexLatencyMillis;
55 
56     /** Time used to merge the indices. */
57     private final int mNativeIndexMergeLatencyMillis;
58 
59     /** Document size in bytes. */
60     private final int mNativeDocumentSizeBytes;
61 
62     /** Number of tokens added to the index. */
63     private final int mNativeNumTokensIndexed;
64 
65     /**
66      * Whether the number of tokens to be indexed exceeded the max number of tokens per document.
67      */
68     private final boolean mNativeExceededMaxNumTokens;
69 
PutDocumentStats(@onNull Builder builder)70     PutDocumentStats(@NonNull Builder builder) {
71         Objects.requireNonNull(builder);
72         mPackageName = builder.mPackageName;
73         mDatabase = builder.mDatabase;
74         mStatusCode = builder.mStatusCode;
75         mTotalLatencyMillis = builder.mTotalLatencyMillis;
76         mGenerateDocumentProtoLatencyMillis = builder.mGenerateDocumentProtoLatencyMillis;
77         mRewriteDocumentTypesLatencyMillis = builder.mRewriteDocumentTypesLatencyMillis;
78         mNativeLatencyMillis = builder.mNativeLatencyMillis;
79         mNativeDocumentStoreLatencyMillis = builder.mNativeDocumentStoreLatencyMillis;
80         mNativeIndexLatencyMillis = builder.mNativeIndexLatencyMillis;
81         mNativeIndexMergeLatencyMillis = builder.mNativeIndexMergeLatencyMillis;
82         mNativeDocumentSizeBytes = builder.mNativeDocumentSizeBytes;
83         mNativeNumTokensIndexed = builder.mNativeNumTokensIndexed;
84         mNativeExceededMaxNumTokens = builder.mNativeExceededMaxNumTokens;
85     }
86 
87     /** Returns calling package name. */
88     @NonNull
getPackageName()89     public String getPackageName() {
90         return mPackageName;
91     }
92 
93     /** Returns calling database name. */
94     @NonNull
getDatabase()95     public String getDatabase() {
96         return mDatabase;
97     }
98 
99     /** Returns status code for this putDocument. */
100     @AppSearchResult.ResultCode
getStatusCode()101     public int getStatusCode() {
102         return mStatusCode;
103     }
104 
105     /** Returns total latency of this putDocument in millis. */
getTotalLatencyMillis()106     public int getTotalLatencyMillis() {
107         return mTotalLatencyMillis;
108     }
109 
110     /** Returns time spent on generating document proto, in milliseconds. */
getGenerateDocumentProtoLatencyMillis()111     public int getGenerateDocumentProtoLatencyMillis() {
112         return mGenerateDocumentProtoLatencyMillis;
113     }
114 
115     /** Returns time spent on rewriting types and namespaces in document, in milliseconds. */
getRewriteDocumentTypesLatencyMillis()116     public int getRewriteDocumentTypesLatencyMillis() {
117         return mRewriteDocumentTypesLatencyMillis;
118     }
119 
120     /** Returns time spent in native, in milliseconds. */
getNativeLatencyMillis()121     public int getNativeLatencyMillis() {
122         return mNativeLatencyMillis;
123     }
124 
125     /** Returns time spent on document store, in milliseconds. */
getNativeDocumentStoreLatencyMillis()126     public int getNativeDocumentStoreLatencyMillis() {
127         return mNativeDocumentStoreLatencyMillis;
128     }
129 
130     /** Returns time spent on indexing, in milliseconds. */
getNativeIndexLatencyMillis()131     public int getNativeIndexLatencyMillis() {
132         return mNativeIndexLatencyMillis;
133     }
134 
135     /** Returns time spent on merging indices, in milliseconds. */
getNativeIndexMergeLatencyMillis()136     public int getNativeIndexMergeLatencyMillis() {
137         return mNativeIndexMergeLatencyMillis;
138     }
139 
140     /** Returns document size, in bytes. */
getNativeDocumentSizeBytes()141     public int getNativeDocumentSizeBytes() {
142         return mNativeDocumentSizeBytes;
143     }
144 
145     /** Returns number of tokens indexed. */
getNativeNumTokensIndexed()146     public int getNativeNumTokensIndexed() {
147         return mNativeNumTokensIndexed;
148     }
149 
150     /**
151      * Returns whether the number of tokens to be indexed exceeded the max number of tokens per
152      * document.
153      */
getNativeExceededMaxNumTokens()154     public boolean getNativeExceededMaxNumTokens() {
155         return mNativeExceededMaxNumTokens;
156     }
157 
158     /** Builder for {@link PutDocumentStats}. */
159     public static class Builder {
160         @NonNull final String mPackageName;
161         @NonNull final String mDatabase;
162         @AppSearchResult.ResultCode int mStatusCode;
163         int mTotalLatencyMillis;
164         int mGenerateDocumentProtoLatencyMillis;
165         int mRewriteDocumentTypesLatencyMillis;
166         int mNativeLatencyMillis;
167         int mNativeDocumentStoreLatencyMillis;
168         int mNativeIndexLatencyMillis;
169         int mNativeIndexMergeLatencyMillis;
170         int mNativeDocumentSizeBytes;
171         int mNativeNumTokensIndexed;
172         boolean mNativeExceededMaxNumTokens;
173 
174         /** Builder for {@link PutDocumentStats} */
Builder(@onNull String packageName, @NonNull String database)175         public Builder(@NonNull String packageName, @NonNull String database) {
176             mPackageName = Objects.requireNonNull(packageName);
177             mDatabase = Objects.requireNonNull(database);
178         }
179 
180         /** Sets the status code. */
181         @NonNull
setStatusCode(@ppSearchResult.ResultCode int statusCode)182         public Builder setStatusCode(@AppSearchResult.ResultCode int statusCode) {
183             mStatusCode = statusCode;
184             return this;
185         }
186 
187         /** Sets total latency in millis. */
188         @NonNull
setTotalLatencyMillis(int totalLatencyMillis)189         public Builder setTotalLatencyMillis(int totalLatencyMillis) {
190             mTotalLatencyMillis = totalLatencyMillis;
191             return this;
192         }
193 
194         /** Sets how much time we spend for generating document proto, in milliseconds. */
195         @NonNull
setGenerateDocumentProtoLatencyMillis( int generateDocumentProtoLatencyMillis)196         public Builder setGenerateDocumentProtoLatencyMillis(
197                 int generateDocumentProtoLatencyMillis) {
198             mGenerateDocumentProtoLatencyMillis = generateDocumentProtoLatencyMillis;
199             return this;
200         }
201 
202         /**
203          * Sets how much time we spend for rewriting types and namespaces in document, in
204          * milliseconds.
205          */
206         @NonNull
setRewriteDocumentTypesLatencyMillis(int rewriteDocumentTypesLatencyMillis)207         public Builder setRewriteDocumentTypesLatencyMillis(int rewriteDocumentTypesLatencyMillis) {
208             mRewriteDocumentTypesLatencyMillis = rewriteDocumentTypesLatencyMillis;
209             return this;
210         }
211 
212         /** Sets the native latency, in milliseconds. */
213         @NonNull
setNativeLatencyMillis(int nativeLatencyMillis)214         public Builder setNativeLatencyMillis(int nativeLatencyMillis) {
215             mNativeLatencyMillis = nativeLatencyMillis;
216             return this;
217         }
218 
219         /** Sets how much time we spend on document store, in milliseconds. */
220         @NonNull
setNativeDocumentStoreLatencyMillis(int nativeDocumentStoreLatencyMillis)221         public Builder setNativeDocumentStoreLatencyMillis(int nativeDocumentStoreLatencyMillis) {
222             mNativeDocumentStoreLatencyMillis = nativeDocumentStoreLatencyMillis;
223             return this;
224         }
225 
226         /** Sets the native index latency, in milliseconds. */
227         @NonNull
setNativeIndexLatencyMillis(int nativeIndexLatencyMillis)228         public Builder setNativeIndexLatencyMillis(int nativeIndexLatencyMillis) {
229             mNativeIndexLatencyMillis = nativeIndexLatencyMillis;
230             return this;
231         }
232 
233         /** Sets how much time we spend on merging indices, in milliseconds. */
234         @NonNull
setNativeIndexMergeLatencyMillis(int nativeIndexMergeLatencyMillis)235         public Builder setNativeIndexMergeLatencyMillis(int nativeIndexMergeLatencyMillis) {
236             mNativeIndexMergeLatencyMillis = nativeIndexMergeLatencyMillis;
237             return this;
238         }
239 
240         /** Sets document size, in bytes. */
241         @NonNull
setNativeDocumentSizeBytes(int nativeDocumentSizeBytes)242         public Builder setNativeDocumentSizeBytes(int nativeDocumentSizeBytes) {
243             mNativeDocumentSizeBytes = nativeDocumentSizeBytes;
244             return this;
245         }
246 
247         /** Sets number of tokens indexed in native. */
248         @NonNull
setNativeNumTokensIndexed(int nativeNumTokensIndexed)249         public Builder setNativeNumTokensIndexed(int nativeNumTokensIndexed) {
250             mNativeNumTokensIndexed = nativeNumTokensIndexed;
251             return this;
252         }
253 
254         /**
255          * Sets whether the number of tokens to be indexed exceeded the max number of tokens per
256          * document.
257          */
258         @NonNull
setNativeExceededMaxNumTokens(boolean nativeExceededMaxNumTokens)259         public Builder setNativeExceededMaxNumTokens(boolean nativeExceededMaxNumTokens) {
260             mNativeExceededMaxNumTokens = nativeExceededMaxNumTokens;
261             return this;
262         }
263 
264         /**
265          * Creates a new {@link PutDocumentStats} object from the contents of this {@link Builder}
266          * instance.
267          */
268         @NonNull
build()269         public PutDocumentStats build() {
270             return new PutDocumentStats(/* builder= */ this);
271         }
272     }
273 }
274