Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.

Commit 8b73fd2

Browse files
committed
refactor: Merge BsonBinaryData into Blob
1 parent e86f611 commit 8b73fd2

10 files changed

Lines changed: 154 additions & 195 deletions

File tree

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

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,67 @@ public final class Blob implements Serializable {
2727
private static final long serialVersionUID = 1441087101882481208L;
2828

2929
private final ByteString byteString;
30+
private final int subtype;
31+
private final boolean isBson;
3032

31-
private Blob(ByteString byteString) {
33+
private Blob(ByteString byteString, int subtype, boolean isBson) {
34+
if (subtype < 0 || subtype > 255) {
35+
throw new IllegalArgumentException(
36+
"The subtype for Blob must be a value in the inclusive [0, 255] range.");
37+
}
3238
this.byteString = byteString;
39+
this.subtype = subtype;
40+
this.isBson = isBson;
3341
}
3442

3543
/**
36-
* Creates a new Blob instance from the provided ByteString.
44+
* Creates a new Blob instance from the provided ByteString. Defaults to subtype 0 and native
45+
* representation.
3746
*
3847
* @param byteString The byteString to use for this Blob instance.
3948
* @return The new Blob instance
4049
*/
4150
@Nonnull
4251
public static Blob fromByteString(@Nonnull ByteString byteString) {
43-
return new Blob(byteString);
52+
return new Blob(byteString, 0, false);
4453
}
4554

4655
/**
4756
* Creates a new Blob instance from the provided bytes. Makes a copy of the bytes passed in.
57+
* Defaults to subtype 0 and native representation.
4858
*
4959
* @param bytes The bytes to use for this Blob instance.
5060
* @return The new Blob instance
5161
*/
5262
@Nonnull
5363
public static Blob fromBytes(@Nonnull byte[] bytes) {
54-
return new Blob(ByteString.copyFrom(bytes));
64+
return new Blob(ByteString.copyFrom(bytes), 0, false);
65+
}
66+
67+
/**
68+
* Creates a new Blob instance from the provided ByteString and subtype. Sets representation to
69+
* BSON.
70+
*
71+
* @param subtype The subtype to use for this instance.
72+
* @param byteString The byteString to use for this Blob instance.
73+
* @return The new Blob instance
74+
*/
75+
@Nonnull
76+
public static Blob fromByteString(int subtype, @Nonnull ByteString byteString) {
77+
return new Blob(byteString, subtype, true);
78+
}
79+
80+
/**
81+
* Creates a new Blob instance from the provided bytes and subtype. Makes a copy of the bytes
82+
* passed in. Sets representation to BSON.
83+
*
84+
* @param subtype The subtype to use for this instance.
85+
* @param bytes The bytes to use for this Blob instance.
86+
* @return The new Blob instance
87+
*/
88+
@Nonnull
89+
public static Blob fromBytes(int subtype, @Nonnull byte[] bytes) {
90+
return new Blob(ByteString.copyFrom(bytes), subtype, true);
5591
}
5692

5793
/**
@@ -74,6 +110,24 @@ public byte[] toBytes() {
74110
return byteString.toByteArray();
75111
}
76112

113+
/**
114+
* Returns the subtype of this binary data.
115+
*
116+
* @return The subtype of the binary data.
117+
*/
118+
public int subtype() {
119+
return this.subtype;
120+
}
121+
122+
/**
123+
* Returns whether this Blob represents a BSON binary data type.
124+
*
125+
* @return True if BSON representation, false if native representation.
126+
*/
127+
public boolean isBson() {
128+
return this.isBson;
129+
}
130+
77131
/**
78132
* Returns true if this Blob is equal to the provided object.
79133
*
@@ -89,11 +143,27 @@ public boolean equals(Object obj) {
89143
return false;
90144
}
91145
Blob blob = (Blob) obj;
92-
return Objects.equals(byteString, blob.byteString);
146+
return this.subtype == blob.subtype && Objects.equals(byteString, blob.byteString);
93147
}
94148

95149
@Override
96150
public int hashCode() {
97-
return Objects.hash(byteString);
151+
return Objects.hash(byteString, subtype);
152+
}
153+
154+
com.google.firestore.v1.MapValue toProto() {
155+
return UserDataConverter.encodeBsonBinaryData(subtype, byteString);
156+
}
157+
158+
@Nonnull
159+
@Override
160+
public String toString() {
161+
return "Blob{subtype="
162+
+ this.subtype
163+
+ ", isBson="
164+
+ this.isBson
165+
+ ", data="
166+
+ this.byteString.toString()
167+
+ "}";
98168
}
99169
}

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

Lines changed: 0 additions & 128 deletions
This file was deleted.

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -482,17 +482,7 @@ public BsonTimestamp getBsonTimestamp(@Nonnull String field) {
482482
return (BsonTimestamp) get(field);
483483
}
484484

485-
/**
486-
* Returns the value of the field as a BsonBinaryData.
487-
*
488-
* @param field The path to the field.
489-
* @throws RuntimeException if the value is not a BsonBinaryData.
490-
* @return The value of the field.
491-
*/
492-
@Nullable
493-
public BsonBinaryData getBsonBinaryData(@Nonnull String field) {
494-
return (BsonBinaryData) get(field);
495-
}
485+
496486

497487
/**
498488
* Gets the reference to the document.

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ enum TypeOrder implements Comparable<TypeOrder> {
4343
BSON_TIMESTAMP,
4444
STRING,
4545
BLOB,
46-
BSON_BINARY,
4746
REF,
4847
BSON_OBJECT_ID,
4948
GEO_POINT,
@@ -100,7 +99,7 @@ static TypeOrder fromMapValue(MapValue mapValue) {
10099
case BSON_TIMESTAMP:
101100
return TypeOrder.BSON_TIMESTAMP;
102101
case BSON_BINARY_DATA:
103-
return TypeOrder.BSON_BINARY;
102+
return TypeOrder.BLOB;
104103
case UNKNOWN:
105104
case NONE:
106105
default:
@@ -160,8 +159,7 @@ public int compare(@Nonnull Value left, @Nonnull Value right) {
160159
return compareBsonObjectId(left, right);
161160
case BSON_TIMESTAMP:
162161
return compareBsonTimestamp(left, right);
163-
case BSON_BINARY:
164-
return compareBsonBinary(left, right);
162+
165163
default:
166164
throw new IllegalArgumentException("Cannot compare " + leftType);
167165
}
@@ -226,11 +224,25 @@ public static int compareUtf8Strings(String left, String right) {
226224
}
227225

228226
private int compareBlobs(Value left, Value right) {
229-
ByteString leftBytes = left.getBytesValue();
230-
ByteString rightBytes = right.getBytesValue();
227+
ByteString leftBytes = getEffectiveBytes(left);
228+
ByteString rightBytes = getEffectiveBytes(right);
231229
return compareByteStrings(leftBytes, rightBytes);
232230
}
233231

232+
private static ByteString getEffectiveBytes(Value value) {
233+
if (value.hasBytesValue()) {
234+
return ByteString.copyFrom(new byte[] {0}).concat(value.getBytesValue());
235+
}
236+
if (value.hasMapValue() && UserDataConverter.isBsonBinaryData(value.getMapValue())) {
237+
return value
238+
.getMapValue()
239+
.getFieldsMap()
240+
.get(MapType.RESERVED_BSON_BINARY_KEY)
241+
.getBytesValue();
242+
}
243+
throw new IllegalArgumentException("Cannot get effective bytes for non-blob value: " + value);
244+
}
245+
234246
static int compareByteStrings(ByteString leftBytes, ByteString rightBytes) {
235247
int size = Math.min(leftBytes.size(), rightBytes.size());
236248
for (int i = 0; i < size; i++) {
@@ -412,13 +424,7 @@ private int compareBsonTimestamp(Value left, Value right) {
412424
return secondsDiff != 0 ? secondsDiff : Long.compare(lhs.increment, rhs.increment);
413425
}
414426

415-
private int compareBsonBinary(Value left, Value right) {
416-
ByteString lhs =
417-
left.getMapValue().getFieldsMap().get(MapType.RESERVED_BSON_BINARY_KEY).getBytesValue();
418-
ByteString rhs =
419-
right.getMapValue().getFieldsMap().get(MapType.RESERVED_BSON_BINARY_KEY).getBytesValue();
420-
return compareByteStrings(lhs, rhs);
421-
}
427+
422428

423429
private boolean isNaN(Value value) {
424430
if (value.hasDoubleValue() && Double.isNaN(value.getDoubleValue())) {

0 commit comments

Comments
 (0)