• 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 android.app.appsearch;
18 
19 import android.annotation.NonNull;
20 import android.app.appsearch.annotation.CanIgnoreReturnValue;
21 import android.os.Bundle;
22 
23 import java.util.Objects;
24 
25 /** The response class of {@code AppSearchSession#getStorageInfo}. */
26 public class StorageInfo {
27 
28     private static final String SIZE_BYTES_FIELD = "sizeBytes";
29     private static final String ALIVE_DOCUMENTS_COUNT = "aliveDocumentsCount";
30     private static final String ALIVE_NAMESPACES_COUNT = "aliveNamespacesCount";
31 
32     private final Bundle mBundle;
33 
StorageInfo(@onNull Bundle bundle)34     StorageInfo(@NonNull Bundle bundle) {
35         mBundle = Objects.requireNonNull(bundle);
36     }
37 
38     /**
39      * Returns the {@link Bundle} populated by this builder.
40      *
41      * @hide
42      */
43     @NonNull
getBundle()44     public Bundle getBundle() {
45         return mBundle;
46     }
47 
48     /** Returns the estimated size of the session's database in bytes. */
getSizeBytes()49     public long getSizeBytes() {
50         return mBundle.getLong(SIZE_BYTES_FIELD);
51     }
52 
53     /**
54      * Returns the number of alive documents in the current session.
55      *
56      * <p>Alive documents are documents that haven't been deleted and haven't exceeded the ttl as
57      * set in {@link GenericDocument.Builder#setTtlMillis}.
58      */
getAliveDocumentsCount()59     public int getAliveDocumentsCount() {
60         return mBundle.getInt(ALIVE_DOCUMENTS_COUNT);
61     }
62 
63     /**
64      * Returns the number of namespaces that have at least one alive document in the current
65      * session's database.
66      *
67      * <p>Alive documents are documents that haven't been deleted and haven't exceeded the ttl as
68      * set in {@link GenericDocument.Builder#setTtlMillis}.
69      */
getAliveNamespacesCount()70     public int getAliveNamespacesCount() {
71         return mBundle.getInt(ALIVE_NAMESPACES_COUNT);
72     }
73 
74     /** Builder for {@link StorageInfo} objects. */
75     public static final class Builder {
76         private long mSizeBytes;
77         private int mAliveDocumentsCount;
78         private int mAliveNamespacesCount;
79 
80         /** Sets the size in bytes. */
81         @CanIgnoreReturnValue
82         @NonNull
setSizeBytes(long sizeBytes)83         public StorageInfo.Builder setSizeBytes(long sizeBytes) {
84             mSizeBytes = sizeBytes;
85             return this;
86         }
87 
88         /** Sets the number of alive documents. */
89         @CanIgnoreReturnValue
90         @NonNull
setAliveDocumentsCount(int aliveDocumentsCount)91         public StorageInfo.Builder setAliveDocumentsCount(int aliveDocumentsCount) {
92             mAliveDocumentsCount = aliveDocumentsCount;
93             return this;
94         }
95 
96         /** Sets the number of alive namespaces. */
97         @CanIgnoreReturnValue
98         @NonNull
setAliveNamespacesCount(int aliveNamespacesCount)99         public StorageInfo.Builder setAliveNamespacesCount(int aliveNamespacesCount) {
100             mAliveNamespacesCount = aliveNamespacesCount;
101             return this;
102         }
103 
104         /** Builds a {@link StorageInfo} object. */
105         @NonNull
build()106         public StorageInfo build() {
107             Bundle bundle = new Bundle();
108             bundle.putLong(SIZE_BYTES_FIELD, mSizeBytes);
109             bundle.putInt(ALIVE_DOCUMENTS_COUNT, mAliveDocumentsCount);
110             bundle.putInt(ALIVE_NAMESPACES_COUNT, mAliveNamespacesCount);
111             return new StorageInfo(bundle);
112         }
113     }
114 }
115