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 import android.app.appsearch.annotation.CanIgnoreReturnValue;
22 
23 import java.util.Objects;
24 
25 /**
26  * A class for holding detailed stats to log for each individual document put by a {@link
27  * android.app.appsearch.AppSearchSession#put} call.
28  *
29  * @hide
30  */
31 public final class PutDocumentStats {
32     @NonNull private final String mPackageName;
33     @NonNull private final String mDatabase;
34 
35     /**
36      * The status code returned by {@link AppSearchResult#getResultCode()} for the call or internal
37      * state.
38      */
39     @AppSearchResult.ResultCode private final int mStatusCode;
40 
41     private final int mTotalLatencyMillis;
42 
43     /** Time used to generate a document proto from a Bundle. */
44     private final int mGenerateDocumentProtoLatencyMillis;
45 
46     /** Time used to rewrite types and namespaces in the document. */
47     private final int mRewriteDocumentTypesLatencyMillis;
48 
49     /** Overall time used for the native function call. */
50     private final int mNativeLatencyMillis;
51 
52     /** Time used to store the document. */
53     private final int mNativeDocumentStoreLatencyMillis;
54 
55     /** Time used to index the document. It doesn't include the time to merge indices. */
56     private final int mNativeIndexLatencyMillis;
57 
58     /** Time used to merge the indices. */
59     private final int mNativeIndexMergeLatencyMillis;
60 
61     /** Document size in bytes. */
62     private final int mNativeDocumentSizeBytes;
63 
64     /** Number of tokens added to the index. */
65     private final int mNativeNumTokensIndexed;
66 
67     /**
68      * Time used to index all indexable string terms in the document. It does not include the time
69      * to merge indices.
70      */
71     private final int mNativeTermIndexLatencyMillis;
72 
73     /** Time used to index all indexable integers in the document. */
74     private final int mNativeIntegerIndexLatencyMillis;
75 
76     /** Time used to index all qualified id join strings in the document. */
77     private final int mNativeQualifiedIdJoinIndexLatencyMillis;
78 
79     /** Time used to sort and merge the lite index's hit buffer. */
80     private final int mNativeLiteIndexSortLatencyMillis;
81 
PutDocumentStats(@onNull Builder builder)82     PutDocumentStats(@NonNull Builder builder) {
83         Objects.requireNonNull(builder);
84         mPackageName = builder.mPackageName;
85         mDatabase = builder.mDatabase;
86         mStatusCode = builder.mStatusCode;
87         mTotalLatencyMillis = builder.mTotalLatencyMillis;
88         mGenerateDocumentProtoLatencyMillis = builder.mGenerateDocumentProtoLatencyMillis;
89         mRewriteDocumentTypesLatencyMillis = builder.mRewriteDocumentTypesLatencyMillis;
90         mNativeLatencyMillis = builder.mNativeLatencyMillis;
91         mNativeDocumentStoreLatencyMillis = builder.mNativeDocumentStoreLatencyMillis;
92         mNativeIndexLatencyMillis = builder.mNativeIndexLatencyMillis;
93         mNativeIndexMergeLatencyMillis = builder.mNativeIndexMergeLatencyMillis;
94         mNativeDocumentSizeBytes = builder.mNativeDocumentSizeBytes;
95         mNativeNumTokensIndexed = builder.mNativeNumTokensIndexed;
96         mNativeTermIndexLatencyMillis = builder.mNativeTermIndexLatencyMillis;
97         mNativeIntegerIndexLatencyMillis = builder.mNativeIntegerIndexLatencyMillis;
98         mNativeQualifiedIdJoinIndexLatencyMillis = builder.mNativeQualifiedIdJoinIndexLatencyMillis;
99         mNativeLiteIndexSortLatencyMillis = builder.mNativeLiteIndexSortLatencyMillis;
100     }
101 
102     /** Returns calling package name. */
103     @NonNull
getPackageName()104     public String getPackageName() {
105         return mPackageName;
106     }
107 
108     /** Returns calling database name. */
109     @NonNull
getDatabase()110     public String getDatabase() {
111         return mDatabase;
112     }
113 
114     /** Returns status code for this putDocument. */
115     @AppSearchResult.ResultCode
getStatusCode()116     public int getStatusCode() {
117         return mStatusCode;
118     }
119 
120     /** Returns total latency of this putDocument in millis. */
getTotalLatencyMillis()121     public int getTotalLatencyMillis() {
122         return mTotalLatencyMillis;
123     }
124 
125     /** Returns time spent on generating document proto, in milliseconds. */
getGenerateDocumentProtoLatencyMillis()126     public int getGenerateDocumentProtoLatencyMillis() {
127         return mGenerateDocumentProtoLatencyMillis;
128     }
129 
130     /** Returns time spent on rewriting types and namespaces in document, in milliseconds. */
getRewriteDocumentTypesLatencyMillis()131     public int getRewriteDocumentTypesLatencyMillis() {
132         return mRewriteDocumentTypesLatencyMillis;
133     }
134 
135     /** Returns time spent in native, in milliseconds. */
getNativeLatencyMillis()136     public int getNativeLatencyMillis() {
137         return mNativeLatencyMillis;
138     }
139 
140     /** Returns time spent on document store, in milliseconds. */
getNativeDocumentStoreLatencyMillis()141     public int getNativeDocumentStoreLatencyMillis() {
142         return mNativeDocumentStoreLatencyMillis;
143     }
144 
145     /** Returns time spent on indexing, in milliseconds. */
getNativeIndexLatencyMillis()146     public int getNativeIndexLatencyMillis() {
147         return mNativeIndexLatencyMillis;
148     }
149 
150     /** Returns time spent on merging indices, in milliseconds. */
getNativeIndexMergeLatencyMillis()151     public int getNativeIndexMergeLatencyMillis() {
152         return mNativeIndexMergeLatencyMillis;
153     }
154 
155     /** Returns document size, in bytes. */
getNativeDocumentSizeBytes()156     public int getNativeDocumentSizeBytes() {
157         return mNativeDocumentSizeBytes;
158     }
159 
160     /** Returns number of tokens indexed. */
getNativeNumTokensIndexed()161     public int getNativeNumTokensIndexed() {
162         return mNativeNumTokensIndexed;
163     }
164 
165     /** Returns time spent on term indexing, in milliseconds. */
getNativeTermIndexLatencyMillis()166     public int getNativeTermIndexLatencyMillis() {
167         return mNativeTermIndexLatencyMillis;
168     }
169 
170     /** Returns time spent on integer indexing, in milliseconds. */
getNativeIntegerIndexLatencyMillis()171     public int getNativeIntegerIndexLatencyMillis() {
172         return mNativeIntegerIndexLatencyMillis;
173     }
174 
175     /** Returns time spent on qualified id join indexing, in milliseconds. */
getNativeQualifiedIdJoinIndexLatencyMillis()176     public int getNativeQualifiedIdJoinIndexLatencyMillis() {
177         return mNativeQualifiedIdJoinIndexLatencyMillis;
178     }
179 
180     /** Returns time spent sorting and merging the lite index, in milliseconds. */
getNativeLiteIndexSortLatencyMillis()181     public int getNativeLiteIndexSortLatencyMillis() {
182         return mNativeLiteIndexSortLatencyMillis;
183     }
184 
185     /** Builder for {@link PutDocumentStats}. */
186     public static class Builder {
187         @NonNull final String mPackageName;
188         @NonNull final String mDatabase;
189         @AppSearchResult.ResultCode int mStatusCode;
190         int mTotalLatencyMillis;
191         int mGenerateDocumentProtoLatencyMillis;
192         int mRewriteDocumentTypesLatencyMillis;
193         int mNativeLatencyMillis;
194         int mNativeDocumentStoreLatencyMillis;
195         int mNativeIndexLatencyMillis;
196         int mNativeIndexMergeLatencyMillis;
197         int mNativeDocumentSizeBytes;
198         int mNativeNumTokensIndexed;
199         int mNativeTermIndexLatencyMillis;
200         int mNativeIntegerIndexLatencyMillis;
201         int mNativeQualifiedIdJoinIndexLatencyMillis;
202         int mNativeLiteIndexSortLatencyMillis;
203 
204         /** Builder for {@link PutDocumentStats} */
Builder(@onNull String packageName, @NonNull String database)205         public Builder(@NonNull String packageName, @NonNull String database) {
206             mPackageName = Objects.requireNonNull(packageName);
207             mDatabase = Objects.requireNonNull(database);
208         }
209 
210         /** Sets the status code. */
211         @CanIgnoreReturnValue
212         @NonNull
setStatusCode(@ppSearchResult.ResultCode int statusCode)213         public Builder setStatusCode(@AppSearchResult.ResultCode int statusCode) {
214             mStatusCode = statusCode;
215             return this;
216         }
217 
218         /** Sets total latency in millis. */
219         @CanIgnoreReturnValue
220         @NonNull
setTotalLatencyMillis(int totalLatencyMillis)221         public Builder setTotalLatencyMillis(int totalLatencyMillis) {
222             mTotalLatencyMillis = totalLatencyMillis;
223             return this;
224         }
225 
226         /** Sets how much time we spend for generating document proto, in milliseconds. */
227         @CanIgnoreReturnValue
228         @NonNull
setGenerateDocumentProtoLatencyMillis( int generateDocumentProtoLatencyMillis)229         public Builder setGenerateDocumentProtoLatencyMillis(
230                 int generateDocumentProtoLatencyMillis) {
231             mGenerateDocumentProtoLatencyMillis = generateDocumentProtoLatencyMillis;
232             return this;
233         }
234 
235         /**
236          * Sets how much time we spend for rewriting types and namespaces in document, in
237          * milliseconds.
238          */
239         @CanIgnoreReturnValue
240         @NonNull
setRewriteDocumentTypesLatencyMillis(int rewriteDocumentTypesLatencyMillis)241         public Builder setRewriteDocumentTypesLatencyMillis(int rewriteDocumentTypesLatencyMillis) {
242             mRewriteDocumentTypesLatencyMillis = rewriteDocumentTypesLatencyMillis;
243             return this;
244         }
245 
246         /** Sets the native latency, in milliseconds. */
247         @CanIgnoreReturnValue
248         @NonNull
setNativeLatencyMillis(int nativeLatencyMillis)249         public Builder setNativeLatencyMillis(int nativeLatencyMillis) {
250             mNativeLatencyMillis = nativeLatencyMillis;
251             return this;
252         }
253 
254         /** Sets how much time we spend on document store, in milliseconds. */
255         @CanIgnoreReturnValue
256         @NonNull
setNativeDocumentStoreLatencyMillis(int nativeDocumentStoreLatencyMillis)257         public Builder setNativeDocumentStoreLatencyMillis(int nativeDocumentStoreLatencyMillis) {
258             mNativeDocumentStoreLatencyMillis = nativeDocumentStoreLatencyMillis;
259             return this;
260         }
261 
262         /** Sets the native index latency, in milliseconds. */
263         @CanIgnoreReturnValue
264         @NonNull
setNativeIndexLatencyMillis(int nativeIndexLatencyMillis)265         public Builder setNativeIndexLatencyMillis(int nativeIndexLatencyMillis) {
266             mNativeIndexLatencyMillis = nativeIndexLatencyMillis;
267             return this;
268         }
269 
270         /** Sets how much time we spend on merging indices, in milliseconds. */
271         @CanIgnoreReturnValue
272         @NonNull
setNativeIndexMergeLatencyMillis(int nativeIndexMergeLatencyMillis)273         public Builder setNativeIndexMergeLatencyMillis(int nativeIndexMergeLatencyMillis) {
274             mNativeIndexMergeLatencyMillis = nativeIndexMergeLatencyMillis;
275             return this;
276         }
277 
278         /** Sets document size, in bytes. */
279         @CanIgnoreReturnValue
280         @NonNull
setNativeDocumentSizeBytes(int nativeDocumentSizeBytes)281         public Builder setNativeDocumentSizeBytes(int nativeDocumentSizeBytes) {
282             mNativeDocumentSizeBytes = nativeDocumentSizeBytes;
283             return this;
284         }
285 
286         /** Sets number of tokens indexed in native. */
287         @CanIgnoreReturnValue
288         @NonNull
setNativeNumTokensIndexed(int nativeNumTokensIndexed)289         public Builder setNativeNumTokensIndexed(int nativeNumTokensIndexed) {
290             mNativeNumTokensIndexed = nativeNumTokensIndexed;
291             return this;
292         }
293 
294         /** Sets the native term indexing time, in millis. */
295         @CanIgnoreReturnValue
296         @NonNull
setNativeTermIndexLatencyMillis(int nativeTermIndexLatencyMillis)297         public Builder setNativeTermIndexLatencyMillis(int nativeTermIndexLatencyMillis) {
298             mNativeTermIndexLatencyMillis = nativeTermIndexLatencyMillis;
299             return this;
300         }
301 
302         /** Sets the native integer indexing time, in millis. */
303         @CanIgnoreReturnValue
304         @NonNull
setNativeIntegerIndexLatencyMillis(int nativeIntegerIndexLatencyMillis)305         public Builder setNativeIntegerIndexLatencyMillis(int nativeIntegerIndexLatencyMillis) {
306             mNativeIntegerIndexLatencyMillis = nativeIntegerIndexLatencyMillis;
307             return this;
308         }
309 
310         /** Sets the native qualified id indexing time, in millis. */
311         @CanIgnoreReturnValue
312         @NonNull
setNativeQualifiedIdJoinIndexLatencyMillis( int nativeQualifiedIdJoinIndexLatencyMillis)313         public Builder setNativeQualifiedIdJoinIndexLatencyMillis(
314                 int nativeQualifiedIdJoinIndexLatencyMillis) {
315             mNativeQualifiedIdJoinIndexLatencyMillis = nativeQualifiedIdJoinIndexLatencyMillis;
316             return this;
317         }
318 
319         /** Sets the native lite index sort latency, in millis. */
320         @CanIgnoreReturnValue
321         @NonNull
setNativeLiteIndexSortLatencyMillis(int nativeLiteIndexSortLatencyMillis)322         public Builder setNativeLiteIndexSortLatencyMillis(int nativeLiteIndexSortLatencyMillis) {
323             mNativeLiteIndexSortLatencyMillis = nativeLiteIndexSortLatencyMillis;
324             return this;
325         }
326 
327         /**
328          * Creates a new {@link PutDocumentStats} object from the contents of this {@link Builder}
329          * instance.
330          */
331         @NonNull
build()332         public PutDocumentStats build() {
333             return new PutDocumentStats(/* builder= */ this);
334         }
335     }
336 }
337