Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ If you are using Maven without the BOM, add this to your dependencies:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-firestore</artifactId>
<version>3.31.6</version>
<version>3.31.7</version>
</dependency>

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}