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

Commit 74d4877

Browse files
authored
feat: Add support for seven new types (#1)
* feat: type definition for six types. * formatting and add some encoding logic. * Add class mapper and data converter logic. * Add Int32Value class. * Add ordering logic. * Add unit tests for encoding, decoding, and ordering. * Add roundtrip (write and read back) integration tests. * More integration tests with filter and ordering. * fix: use a valid Bson ObjectId. * Address feedback 1. * Address feedback 2. * Address feedback 3. * Fix the parameter ordering for BsonBinaryData.
1 parent d63cff2 commit 74d4877

20 files changed

Lines changed: 2070 additions & 103 deletions
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.firestore;
18+
19+
import com.google.firestore.v1.MapValue;
20+
import com.google.protobuf.ByteString;
21+
import java.io.Serializable;
22+
import java.util.Objects;
23+
import javax.annotation.Nonnull;
24+
25+
/** Represents a BSON Binary Data type in Firestore documents. */
26+
public class BsonBinaryData implements Serializable {
27+
private static final long serialVersionUID = 1830984831902814656L;
28+
private final int subtype;
29+
private final ByteString data;
30+
31+
private BsonBinaryData(int subtype, @Nonnull ByteString data) {
32+
// By definition the subtype should be 1 byte and should therefore
33+
// have a value between 0 and 255
34+
if (subtype < 0 || subtype > 255) {
35+
throw new IllegalArgumentException(
36+
"The subtype for BsonBinaryData must be a value in the inclusive [0, 255] range.");
37+
}
38+
this.subtype = subtype;
39+
this.data = data;
40+
}
41+
42+
/**
43+
* Creates a new BsonBinaryData instance from the provided ByteString and subtype.
44+
*
45+
* @param subtype The subtype to use for this instance.
46+
* @param byteString The byteString to use for this instance.
47+
* @return The new BsonBinaryData instance
48+
*/
49+
@Nonnull
50+
public static BsonBinaryData fromByteString(int subtype, @Nonnull ByteString byteString) {
51+
return new BsonBinaryData(subtype, byteString);
52+
}
53+
54+
/**
55+
* Creates a new BsonBinaryData instance from the provided bytes and subtype. Makes a copy of the
56+
* bytes passed in.
57+
*
58+
* @param subtype The subtype to use for this instance.
59+
* @param bytes The bytes to use for this instance.
60+
* @return The new BsonBinaryData instance
61+
*/
62+
@Nonnull
63+
public static BsonBinaryData fromBytes(int subtype, @Nonnull byte[] bytes) {
64+
return new BsonBinaryData(subtype, ByteString.copyFrom(bytes));
65+
}
66+
67+
/**
68+
* Returns the underlying data as a ByteString.
69+
*
70+
* @return The data as a ByteString.
71+
*/
72+
@Nonnull
73+
public ByteString dataAsByteString() {
74+
return data;
75+
}
76+
77+
/**
78+
* Returns a copy of the underlying data as a byte[] array.
79+
*
80+
* @return The data as a byte[] array.
81+
*/
82+
@Nonnull
83+
public byte[] dataAsBytes() {
84+
return data.toByteArray();
85+
}
86+
87+
/**
88+
* Returns the subtype of this binary data.
89+
*
90+
* @return The subtype of the binary data.
91+
*/
92+
public int subtype() {
93+
return this.subtype;
94+
}
95+
96+
/**
97+
* Returns true if this BsonBinaryData is equal to the provided object.
98+
*
99+
* @param obj The object to compare against.
100+
* @return Whether this BsonBinaryData is equal to the provided object.
101+
*/
102+
@Override
103+
public boolean equals(Object obj) {
104+
if (this == obj) {
105+
return true;
106+
}
107+
if (obj == null || getClass() != obj.getClass()) {
108+
return false;
109+
}
110+
BsonBinaryData other = (BsonBinaryData) obj;
111+
return Objects.equals(this.subtype, other.subtype) && Objects.equals(this.data, other.data);
112+
}
113+
114+
@Override
115+
public int hashCode() {
116+
return Objects.hash(this.subtype, this.data);
117+
}
118+
119+
@Nonnull
120+
@Override
121+
public String toString() {
122+
return "BsonBinaryData { subtype=" + this.subtype + ", data=" + this.data.toString() + " }";
123+
}
124+
125+
MapValue toProto() {
126+
return UserDataConverter.encodeBsonBinaryData(subtype, data);
127+
}
128+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.firestore;
18+
19+
import com.google.firestore.v1.MapValue;
20+
import java.io.Serializable;
21+
import java.util.Objects;
22+
import javax.annotation.Nonnull;
23+
24+
/** Represents a BSON ObjectId type in Firestore documents. */
25+
public class BsonObjectId implements Serializable {
26+
private static final long serialVersionUID = 430753173775328933L;
27+
public final String value;
28+
29+
/**
30+
* Constructor that creates a new BSON ObjectId value with the given value.
31+
*
32+
* @param oid The 24-character hex string representing the ObjectId.
33+
*/
34+
public BsonObjectId(@Nonnull String oid) {
35+
this.value = oid;
36+
}
37+
38+
MapValue toProto() {
39+
return UserDataConverter.encodeBsonObjectId(value);
40+
}
41+
42+
/**
43+
* Returns true if this BsonObjectId is equal to the provided object.
44+
*
45+
* @param obj The object to compare against.
46+
* @return Whether this BsonObjectId is equal to the provided object.
47+
*/
48+
@Override
49+
public boolean equals(Object obj) {
50+
if (this == obj) {
51+
return true;
52+
}
53+
if (obj == null || getClass() != obj.getClass()) {
54+
return false;
55+
}
56+
BsonObjectId other = (BsonObjectId) obj;
57+
return Objects.equals(this.value, other.value);
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
return Objects.hash(this.value);
63+
}
64+
65+
@Nonnull
66+
@Override
67+
public String toString() {
68+
return "BsonObjectId { value=" + this.value + " }";
69+
}
70+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.firestore;
18+
19+
import com.google.firestore.v1.MapValue;
20+
import java.io.Serializable;
21+
import java.util.Objects;
22+
import javax.annotation.Nonnull;
23+
24+
/** Represents a BSON Timestamp type in Firestore documents. */
25+
public class BsonTimestamp implements Serializable {
26+
private static final long serialVersionUID = -1693962317170687337L;
27+
public final long seconds;
28+
public final long increment;
29+
30+
/**
31+
* Constructor that creates a new BSON Timestamp value with the given values.
32+
*
33+
* @param seconds An unsigned 32-bit integer value stored as long representing the seconds.
34+
* @param increment An unsigned 32-bit integer value stored as long representing the increment.
35+
*/
36+
public BsonTimestamp(long seconds, long increment) {
37+
if (seconds < 0 || seconds > 4294967295L) {
38+
throw new IllegalArgumentException(
39+
"BsonTimestamp 'seconds' must be in the range of a 32-bit unsigned integer.");
40+
}
41+
if (increment < 0 || increment > 4294967295L) {
42+
throw new IllegalArgumentException(
43+
"BsonTimestamp 'increment' must be in the range of a 32-bit unsigned integer.");
44+
}
45+
this.seconds = seconds;
46+
this.increment = increment;
47+
}
48+
49+
MapValue toProto() {
50+
return UserDataConverter.encodeBsonTimestampValue(seconds, increment);
51+
}
52+
53+
/**
54+
* Returns true if this BsonTimestampValue is equal to the provided object.
55+
*
56+
* @param obj The object to compare against.
57+
* @return Whether this BsonTimestampValue is equal to the provided object.
58+
*/
59+
@Override
60+
public boolean equals(Object obj) {
61+
if (this == obj) {
62+
return true;
63+
}
64+
if (obj == null || getClass() != obj.getClass()) {
65+
return false;
66+
}
67+
BsonTimestamp other = (BsonTimestamp) obj;
68+
return Objects.equals(this.seconds, other.seconds)
69+
&& Objects.equals(this.increment, other.increment);
70+
}
71+
72+
@Override
73+
public int hashCode() {
74+
return Objects.hash(this.seconds, this.increment);
75+
}
76+
77+
@Nonnull
78+
@Override
79+
public String toString() {
80+
return "BsonTimestamp { seconds=" + this.seconds + ", increment=" + this.increment + " }";
81+
}
82+
}

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,90 @@ public VectorValue getVectorValue(@Nonnull String field) {
398398
return (VectorValue) get(field);
399399
}
400400

401+
/**
402+
* Returns the value of the field as a MinKey.
403+
*
404+
* @param field The path to the field.
405+
* @throws RuntimeException if the value is not a MinKey.
406+
* @return The value of the field.
407+
*/
408+
@Nullable
409+
public MinKey getMinKey(@Nonnull String field) {
410+
return (MinKey) get(field);
411+
}
412+
413+
/**
414+
* Returns the value of the field as a MaxKey.
415+
*
416+
* @param field The path to the field.
417+
* @throws RuntimeException if the value is not a MaxKey.
418+
* @return The value of the field.
419+
*/
420+
@Nullable
421+
public MaxKey getMaxKey(@Nonnull String field) {
422+
return (MaxKey) get(field);
423+
}
424+
425+
/**
426+
* Returns the value of the field as a RegexValue.
427+
*
428+
* @param field The path to the field.
429+
* @throws RuntimeException if the value is not a RegexValue.
430+
* @return The value of the field.
431+
*/
432+
@Nullable
433+
public RegexValue getRegexValue(@Nonnull String field) {
434+
return (RegexValue) get(field);
435+
}
436+
437+
/**
438+
* Returns the value of the field as a 32-bit integer.
439+
*
440+
* @param field The path to the field.
441+
* @throws RuntimeException if the value is not a Int32Value.
442+
* @return The value of the field.
443+
*/
444+
@Nullable
445+
public Int32Value getInt32Value(@Nonnull String field) {
446+
return (Int32Value) get(field);
447+
}
448+
449+
/**
450+
* Returns the value of the field as a BsonObjectId.
451+
*
452+
* @param field The path to the field.
453+
* @throws RuntimeException if the value is not a BsonObjectId.
454+
* @return The value of the field.
455+
*/
456+
@Nullable
457+
public BsonObjectId getBsonObjectId(@Nonnull String field) {
458+
return (BsonObjectId) get(field);
459+
}
460+
461+
/**
462+
* Returns the value of the field as a BsonTimestampValue.
463+
*
464+
* @param field The path to the field.
465+
* @throws RuntimeException if the value is not a BsonTimestampValue.
466+
* @return The value of the field.
467+
*/
468+
@Nullable
469+
public BsonTimestamp getBsonTimestampValue(@Nonnull String field) {
470+
return (BsonTimestamp) get(field);
471+
}
472+
473+
/**
474+
* Returns the value of the field as a BsonBinaryData.
475+
*
476+
* @param field The path to the field.
477+
* @throws RuntimeException if the value is not a BsonBinaryData.
478+
* @return The value of the field.
479+
*/
480+
@Nullable
481+
public BsonBinaryData getBsonBinaryData(@Nonnull String field) {
482+
return (BsonBinaryData) get(field);
483+
}
484+
401485
/**
402486
* Gets the reference to the document.
403487
*

0 commit comments

Comments
 (0)