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 androidx.appsearch.app; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.RestrictTo; 24 import androidx.appsearch.annotation.CanIgnoreReturnValue; 25 import androidx.appsearch.flags.FlaggedApi; 26 import androidx.appsearch.flags.Flags; 27 import androidx.appsearch.safeparcel.AbstractSafeParcelable; 28 import androidx.appsearch.safeparcel.SafeParcelable; 29 import androidx.appsearch.safeparcel.stub.StubCreators.StorageInfoCreator; 30 31 /** The response class of {@code AppSearchSession#getStorageInfo}. */ 32 @SafeParcelable.Class(creator = "StorageInfoCreator") 33 // TODO(b/384721898): Switch to JSpecify annotations 34 @SuppressWarnings({"HiddenSuperclass", "JSpecifyNullness"}) 35 public final class StorageInfo extends AbstractSafeParcelable { 36 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 37 @FlaggedApi(Flags.FLAG_ENABLE_SAFE_PARCELABLE_2) 38 public static final @NonNull Parcelable.Creator<StorageInfo> CREATOR = new StorageInfoCreator(); 39 40 @Field(id = 1, getter = "getSizeBytes") 41 private long mSizeBytes; 42 43 @Field(id = 2, getter = "getAliveDocumentsCount") 44 private int mAliveDocumentsCount; 45 46 @Field(id = 3, getter = "getAliveNamespacesCount") 47 private int mAliveNamespacesCount; 48 49 @Field(id = 4, getter = "getBlobsSizeBytes") 50 private long mBlobsSizeBytes; 51 52 @Field(id = 5, getter = "getBlobsCount") 53 private int mBlobsCount; 54 55 @Constructor StorageInfo( @aramid = 1) long sizeBytes, @Param(id = 2) int aliveDocumentsCount, @Param(id = 3) int aliveNamespacesCount, @Param(id = 4) long blobsSizeBytes, @Param(id = 5) int blobsCount)56 StorageInfo( 57 @Param(id = 1) long sizeBytes, 58 @Param(id = 2) int aliveDocumentsCount, 59 @Param(id = 3) int aliveNamespacesCount, 60 @Param(id = 4) long blobsSizeBytes, 61 @Param(id = 5) int blobsCount) { 62 mSizeBytes = sizeBytes; 63 mAliveDocumentsCount = aliveDocumentsCount; 64 mAliveNamespacesCount = aliveNamespacesCount; 65 mBlobsSizeBytes = blobsSizeBytes; 66 mBlobsCount = blobsCount; 67 } 68 69 /** Returns the estimated size of the session's database in bytes. */ getSizeBytes()70 public long getSizeBytes() { 71 return mSizeBytes; 72 } 73 74 /** 75 * Returns the number of alive documents in the current session. 76 * 77 * <p>Alive documents are documents that haven't been deleted and haven't exceeded the ttl as 78 * set in {@link GenericDocument.Builder#setTtlMillis}. 79 */ getAliveDocumentsCount()80 public int getAliveDocumentsCount() { 81 return mAliveDocumentsCount; 82 } 83 84 /** 85 * Returns the number of namespaces that have at least one alive document in the current 86 * session's database. 87 * 88 * <p>Alive documents are documents that haven't been deleted and haven't exceeded the ttl as 89 * set in {@link GenericDocument.Builder#setTtlMillis}. 90 */ getAliveNamespacesCount()91 public int getAliveNamespacesCount() { 92 return mAliveNamespacesCount; 93 } 94 95 /** 96 * Returns the total size of all blobs in the session's database in bytes. 97 * 98 * <p>Blobs are binary large objects associated with the documents in the database. Pending 99 * blobs that haven't been committed and orphan blobs that haven't been cleared will be counted 100 * along with alive blobs. 101 */ 102 @FlaggedApi(Flags.FLAG_ENABLE_BLOB_STORE) 103 @ExperimentalAppSearchApi getBlobsSizeBytes()104 public long getBlobsSizeBytes() { 105 return mBlobsSizeBytes; 106 } 107 108 /** 109 * Returns the total number of blobs in the session's database. 110 * 111 * <p>Blobs are binary large objects associated with the documents in the database. Pending 112 * blobs that haven't been committed and orphan blobs that haven't been cleared will be counted 113 * with alive blobs as well. 114 */ 115 @FlaggedApi(Flags.FLAG_ENABLE_BLOB_STORE) 116 @ExperimentalAppSearchApi getBlobsCount()117 public int getBlobsCount() { 118 return mBlobsCount; 119 } 120 121 /** Builder for {@link StorageInfo} objects. */ 122 public static final class Builder { 123 private long mSizeBytes; 124 private int mAliveDocumentsCount; 125 private int mAliveNamespacesCount; 126 private long mBlobsSizeBytes; 127 private int mBlobsCount; 128 129 /** Sets the size in bytes. */ 130 @CanIgnoreReturnValue setSizeBytes(long sizeBytes)131 public @NonNull StorageInfo.Builder setSizeBytes(long sizeBytes) { 132 mSizeBytes = sizeBytes; 133 return this; 134 } 135 136 /** Sets the number of alive documents. */ 137 @CanIgnoreReturnValue setAliveDocumentsCount(int aliveDocumentsCount)138 public @NonNull StorageInfo.Builder setAliveDocumentsCount(int aliveDocumentsCount) { 139 mAliveDocumentsCount = aliveDocumentsCount; 140 return this; 141 } 142 143 /** Sets the number of alive namespaces. */ 144 @CanIgnoreReturnValue setAliveNamespacesCount(int aliveNamespacesCount)145 public @NonNull StorageInfo.Builder setAliveNamespacesCount(int aliveNamespacesCount) { 146 mAliveNamespacesCount = aliveNamespacesCount; 147 return this; 148 } 149 150 /** Sets the size of stored blobs in bytes. */ 151 @CanIgnoreReturnValue 152 @FlaggedApi(Flags.FLAG_ENABLE_BLOB_STORE) 153 @ExperimentalAppSearchApi setBlobsSizeBytes(long blobsSizeBytes)154 public @NonNull StorageInfo.Builder setBlobsSizeBytes(long blobsSizeBytes) { 155 mBlobsSizeBytes = blobsSizeBytes; 156 return this; 157 } 158 159 /** Sets the number of stored blobs. */ 160 @CanIgnoreReturnValue 161 @FlaggedApi(Flags.FLAG_ENABLE_BLOB_STORE) 162 @ExperimentalAppSearchApi setBlobsCount(int blobsCount)163 public @NonNull StorageInfo.Builder setBlobsCount(int blobsCount) { 164 mBlobsCount = blobsCount; 165 return this; 166 } 167 168 /** Builds a {@link StorageInfo} object. */ build()169 public @NonNull StorageInfo build() { 170 return new StorageInfo(mSizeBytes, mAliveDocumentsCount, mAliveNamespacesCount, 171 mBlobsSizeBytes, mBlobsCount); 172 } 173 } 174 175 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 176 @FlaggedApi(Flags.FLAG_ENABLE_SAFE_PARCELABLE_2) 177 @Override writeToParcel(@onNull Parcel dest, int flags)178 public void writeToParcel(@NonNull Parcel dest, int flags) { 179 StorageInfoCreator.writeToParcel(this, dest, flags); 180 } 181 } 182