Skip to content

Commit e035c55

Browse files
committed
refactor(firestore): Merge BsonBinaryData into Blob
This PR stacks on PR #2378 and implements the refactoring to merge BsonBinaryData into Blob, as requested by the user. - Remove isBson property from Blob.java - Update UserDataConverter to check blob.subtype() != 0 - Add BSON binary subtype 0 roundtrip unit tests - Add BSON binary subtype 1 local sorting verification tests
1 parent 3369a14 commit e035c55

4 files changed

Lines changed: 24 additions & 20 deletions

File tree

java-firestore/google-cloud-firestore/src/main/java/com/google/cloud/firestore/Blob.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,14 @@ public final class Blob implements Serializable {
2828

2929
private final ByteString byteString;
3030
private final int subtype;
31-
private final boolean isBson;
3231

33-
private Blob(ByteString byteString, int subtype, boolean isBson) {
32+
private Blob(ByteString byteString, int subtype) {
3433
if (subtype < 0 || subtype > 255) {
3534
throw new IllegalArgumentException(
3635
"The subtype for Blob must be a value in the inclusive [0, 255] range.");
3736
}
3837
this.byteString = byteString;
3938
this.subtype = subtype;
40-
this.isBson = isBson;
4139
}
4240

4341
/**
@@ -49,7 +47,7 @@ private Blob(ByteString byteString, int subtype, boolean isBson) {
4947
*/
5048
@Nonnull
5149
public static Blob fromByteString(@Nonnull ByteString byteString) {
52-
return new Blob(byteString, 0, false);
50+
return new Blob(byteString, 0);
5351
}
5452

5553
/**
@@ -61,7 +59,7 @@ public static Blob fromByteString(@Nonnull ByteString byteString) {
6159
*/
6260
@Nonnull
6361
public static Blob fromBytes(@Nonnull byte[] bytes) {
64-
return new Blob(ByteString.copyFrom(bytes), 0, false);
62+
return new Blob(ByteString.copyFrom(bytes), 0);
6563
}
6664

6765
/**
@@ -73,7 +71,7 @@ public static Blob fromBytes(@Nonnull byte[] bytes) {
7371
*/
7472
@Nonnull
7573
public static Blob createBsonBinary(@Nonnull byte[] bytes) {
76-
return new Blob(ByteString.copyFrom(bytes), 0, true);
74+
return new Blob(ByteString.copyFrom(bytes), 0);
7775
}
7876

7977
/**
@@ -85,7 +83,7 @@ public static Blob createBsonBinary(@Nonnull byte[] bytes) {
8583
*/
8684
@Nonnull
8785
public static Blob createBsonBinary(@Nonnull ByteString data) {
88-
return new Blob(data, 0, true);
86+
return new Blob(data, 0);
8987
}
9088

9189
/**
@@ -98,7 +96,7 @@ public static Blob createBsonBinary(@Nonnull ByteString data) {
9896
*/
9997
@Nonnull
10098
public static Blob createBsonBinary(int subtype, @Nonnull byte[] bytes) {
101-
return new Blob(ByteString.copyFrom(bytes), subtype, true);
99+
return new Blob(ByteString.copyFrom(bytes), subtype);
102100
}
103101

104102
/**
@@ -111,7 +109,7 @@ public static Blob createBsonBinary(int subtype, @Nonnull byte[] bytes) {
111109
*/
112110
@Nonnull
113111
public static Blob createBsonBinary(int subtype, @Nonnull ByteString data) {
114-
return new Blob(data, subtype, true);
112+
return new Blob(data, subtype);
115113
}
116114

117115
/**
@@ -144,15 +142,6 @@ public int subtype() {
144142
return this.subtype;
145143
}
146144

147-
/**
148-
* Returns whether this Blob represents a BSON binary data type.
149-
*
150-
* @return True if BSON representation, false if native representation.
151-
*/
152-
public boolean isBson() {
153-
return this.isBson;
154-
}
155-
156145
/**
157146
* Returns true if this Blob is equal to the provided object.
158147
*
@@ -194,6 +183,6 @@ public String toString() {
194183
+ this.byteString.size()
195184
+ ")";
196185
}
197-
return "Blob{subtype=" + this.subtype + ", isBson=" + this.isBson + ", data=" + dataStr + "}";
186+
return "Blob{subtype=" + this.subtype + ", data=" + dataStr + "}";
198187
}
199188
}

java-firestore/google-cloud-firestore/src/main/java/com/google/cloud/firestore/UserDataConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ static Value encodeValue(
173173
return Value.newBuilder().setGeoPointValue(geopoint.toProto()).build();
174174
} else if (sanitizedObject instanceof Blob) {
175175
Blob blob = (Blob) sanitizedObject;
176-
if (blob.isBson() && blob.subtype() != 0) {
176+
if (blob.subtype() != 0) {
177177
return Value.newBuilder()
178178
.setMapValue(encodeBsonBinaryData(blob.subtype(), blob.toByteString()))
179179
.build();

java-firestore/google-cloud-firestore/src/test/java/com/google/cloud/firestore/ExtendedTypesTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,4 +544,13 @@ public void canEncodeAndDecodeBsonBinaryData() {
544544
.build();
545545
assertEncodesAndDecodesCorrectly(proto, bsonBinaryData);
546546
}
547+
548+
@Test
549+
public void canEncodeAndDecodeBsonBinaryDataSubtype0() {
550+
Blob bsonBinaryData = Blob.createBsonBinary(0, new byte[] {1, 2, 3});
551+
Value proto =
552+
Value.newBuilder().setBytesValue(ByteString.copyFrom(new byte[] {1, 2, 3})).build();
553+
// BSON binary subtype 0 matches standard native Blob
554+
assertEncodesAndDecodesCorrectly(proto, bsonBinaryData);
555+
}
547556
}

java-firestore/google-cloud-firestore/src/test/java/com/google/cloud/firestore/OrderTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ public void verifyOrder() {
128128
groups.add(new Value[] {blobValue(new byte[] {0, 1, 2, 4, 3})});
129129
groups.add(new Value[] {blobValue(new byte[] {127})});
130130

131+
// BSON Binary Data Subtype 1
132+
groups.add(new Value[] {bsonBinaryData(1, new byte[] {})});
133+
groups.add(new Value[] {bsonBinaryData(1, new byte[] {0}), bsonBinaryData(1, new byte[] {0})});
134+
groups.add(new Value[] {bsonBinaryData(1, new byte[] {0, 1, 2, 3, 4})});
135+
groups.add(new Value[] {bsonBinaryData(1, new byte[] {0, 1, 2, 4, 3})});
136+
131137
// BSON Binary Data
132138
groups.add(new Value[] {bsonBinaryData(5, new byte[] {})});
133139
groups.add(new Value[] {bsonBinaryData(5, new byte[] {0}), bsonBinaryData(5, new byte[] {0})});

0 commit comments

Comments
 (0)