diff --git a/README.md b/README.md index 58ad85428..c2d03a708 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ If you are using Maven without the BOM, add this to your dependencies: com.google.cloud google-cloud-firestore - 3.31.6 + 3.31.7 ``` diff --git a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentSnapshot.java b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentSnapshot.java index e1aab1cac..ac17b4520 100644 --- a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentSnapshot.java +++ b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentSnapshot.java @@ -468,4 +468,21 @@ public String toString() { "%s{doc=%s, fields=%s, readTime=%s, updateTime=%s, createTime=%s}", getClass().getSimpleName(), docRef, fields, readTime, updateTime, createTime); } + + /** + * Returns the size of the data in this snapshot in bytes. + * + * @return The size of the data in bytes. + */ + public int getDataSize() { + if (fields == null) { + return 0; + } + + int totalSize = 0; + for (Value value : fields.values()) { + totalSize += value.getSerializedSize(); + } + return totalSize; + } } diff --git a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/QueryTest.java b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/QueryTest.java index 78591cdda..e20a160d8 100644 --- a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/QueryTest.java +++ b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/QueryTest.java @@ -1521,4 +1521,22 @@ public void inequalityFiltersImplicitlyOrderedLexicographicallyWithExplicitOrder assertEquals(orderFields, query_.createImplicitOrderBy()); } + + @Test + public void documentSnapshotGetDataSize_existingDocument() { + DocumentSnapshot snapshot = SINGLE_FIELD_SNAPSHOT; + int expectedSize = 0; + for (Value value : snapshot.getProtoFields().values()) { + expectedSize += value.getSerializedSize(); + } + assertEquals(expectedSize, snapshot.getDataSize()); + } + + @Test + public void documentSnapshotGetDataSize_nonExistentDocument() { + DocumentSnapshot missingSnapshot = + DocumentSnapshot.fromMissing( + firestoreMock, firestoreMock.document("coll/doc"), Timestamp.now()); + assertEquals(0, missingSnapshot.getDataSize()); + } }