|
34 | 34 | import com.google.firebase.firestore.util.Consumer;
|
35 | 35 | import com.google.firebase.firestore.util.Logger;
|
36 | 36 | import com.google.firebase.firestore.util.Supplier;
|
37 |
| -import java.io.File; |
38 | 37 | import java.io.UnsupportedEncodingException;
|
39 | 38 | import java.net.URLEncoder;
|
40 | 39 | import java.util.ArrayList;
|
@@ -77,7 +76,6 @@ public static String databaseName(String persistenceKey, DatabaseId databaseId)
|
77 | 76 | private final OpenHelper opener;
|
78 | 77 | private final LocalSerializer serializer;
|
79 | 78 | private SQLiteDatabase db;
|
80 |
| - private File databasePath; |
81 | 79 | private boolean started;
|
82 | 80 | private final SQLiteQueryCache queryCache;
|
83 | 81 | private final SQLiteRemoteDocumentCache remoteDocumentCache;
|
@@ -106,7 +104,6 @@ public SQLitePersistence(
|
106 | 104 | LruGarbageCollector.Params params) {
|
107 | 105 | String databaseName = databaseName(persistenceKey, databaseId);
|
108 | 106 | this.opener = new OpenHelper(context, databaseName);
|
109 |
| - this.databasePath = context.getDatabasePath(databaseName); |
110 | 107 | this.serializer = serializer;
|
111 | 108 | this.queryCache = new SQLiteQueryCache(this, this.serializer);
|
112 | 109 | this.remoteDocumentCache = new SQLiteRemoteDocumentCache(this, this.serializer);
|
@@ -199,7 +196,40 @@ <T> T runTransaction(String action, Supplier<T> operation) {
|
199 | 196 | }
|
200 | 197 |
|
201 | 198 | long getByteSize() {
|
202 |
| - return databasePath.length(); |
| 199 | + return getPageCount() * getPageSize(); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Gets the page size of the database. Typically 4096. |
| 204 | + * |
| 205 | + * @see https://www.sqlite.org/pragma.html#pragma_page_size |
| 206 | + */ |
| 207 | + private long getPageSize() { |
| 208 | + return pragma("page_size"); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Gets the number of pages in the database file. Multiplying this with the page size yields the |
| 213 | + * approximate size of the database on disk (including the WAL, if relevant). |
| 214 | + * |
| 215 | + * @see https://www.sqlite.org/pragma.html#pragma_page_count. |
| 216 | + */ |
| 217 | + private long getPageCount() { |
| 218 | + return pragma("page_count"); |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * @param pragmaName Must be a valid 'pragma-name' and must refer to a long value. |
| 223 | + * @see https://www.sqlite.org/pragma.html |
| 224 | + */ |
| 225 | + private long pragma(String pragmaName) { |
| 226 | + Cursor cursor = db.rawQuery("PRAGMA " + pragmaName, null); |
| 227 | + try { |
| 228 | + cursor.moveToFirst(); |
| 229 | + return cursor.getLong(/*column=*/ 0); |
| 230 | + } finally { |
| 231 | + cursor.close(); |
| 232 | + } |
203 | 233 | }
|
204 | 234 |
|
205 | 235 | /**
|
|
0 commit comments