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

Commit d994efd

Browse files
committed
refactored appendInt method names + added tests for target range
1 parent c553e90 commit d994efd

4 files changed

Lines changed: 388 additions & 102 deletions

File tree

google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/SsFormat.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private SsFormat() {}
9393
// Offset to make negative timestamp seconds sort correctly
9494
private static final long TIMESTAMP_SECONDS_OFFSET = 1L << 63;
9595

96-
public static void appendCompositeTag(GrowableByteArrayOutputStream out, int tag) {
96+
public static void appendCompositeTag(UnsynchronizedByteArrayOutputStream out, int tag) {
9797
if (tag == K_OBJECT_EXISTENCE_TAG || tag <= 0 || tag > K_MAX_FIELD_TAG) {
9898
throw new IllegalArgumentException("Invalid tag value: " + tag);
9999
}
@@ -121,21 +121,21 @@ public static void appendCompositeTag(GrowableByteArrayOutputStream out, int tag
121121
}
122122
}
123123

124-
public static void appendNullOrderedFirst(GrowableByteArrayOutputStream out) {
124+
public static void appendNullOrderedFirst(UnsynchronizedByteArrayOutputStream out) {
125125
out.write((byte) (IS_KEY | TYPE_NULL_ORDERED_FIRST));
126126
out.write((byte) 0);
127127
}
128128

129-
public static void appendNullOrderedLast(GrowableByteArrayOutputStream out) {
129+
public static void appendNullOrderedLast(UnsynchronizedByteArrayOutputStream out) {
130130
out.write((byte) (IS_KEY | TYPE_NULL_ORDERED_LAST));
131131
out.write((byte) 0);
132132
}
133133

134-
public static void appendNotNullMarkerNullOrderedFirst(GrowableByteArrayOutputStream out) {
134+
public static void appendNotNullMarkerNullOrderedFirst(UnsynchronizedByteArrayOutputStream out) {
135135
out.write((byte) (IS_KEY | TYPE_NULLABLE_NOT_NULL_NULL_ORDERED_FIRST));
136136
}
137137

138-
public static void appendNotNullMarkerNullOrderedLast(GrowableByteArrayOutputStream out) {
138+
public static void appendNotNullMarkerNullOrderedLast(UnsynchronizedByteArrayOutputStream out) {
139139
out.write((byte) (IS_KEY | TYPE_NULLABLE_NOT_NULL_NULL_ORDERED_LAST));
140140
}
141141

@@ -148,7 +148,7 @@ public static void appendNotNullMarkerNullOrderedLast(GrowableByteArrayOutputStr
148148
* @param out the output stream to append to
149149
* @param value the boolean value to encode
150150
*/
151-
public static void appendBoolIncreasing(GrowableByteArrayOutputStream out, boolean value) {
151+
public static void appendBoolIncreasing(UnsynchronizedByteArrayOutputStream out, boolean value) {
152152
// BOOL uses unsigned int encoding: false=0, true=1
153153
// For values 0 and 1, payload is always 1 byte
154154
int encoded = value ? 1 : 0;
@@ -166,7 +166,7 @@ public static void appendBoolIncreasing(GrowableByteArrayOutputStream out, boole
166166
* @param out the output stream to append to
167167
* @param value the boolean value to encode
168168
*/
169-
public static void appendBoolDecreasing(GrowableByteArrayOutputStream out, boolean value) {
169+
public static void appendBoolDecreasing(UnsynchronizedByteArrayOutputStream out, boolean value) {
170170
// BOOL uses decreasing unsigned int encoding: false=0, true=1, then inverted
171171
// For values 0 and 1, payload is always 1 byte
172172
int encoded = value ? 1 : 0;
@@ -175,8 +175,8 @@ public static void appendBoolDecreasing(GrowableByteArrayOutputStream out, boole
175175
out.write((byte) ((~encoded & 0x7F) << 1)); // Inverted payload
176176
}
177177

178-
private static void appendIntInternal(
179-
GrowableByteArrayOutputStream out, long val, boolean decreasing, boolean isDouble) {
178+
private static void appendInt64Internal(
179+
UnsynchronizedByteArrayOutputStream out, long val, boolean decreasing, boolean isDouble) {
180180
if (decreasing) {
181181
val = ~val;
182182
}
@@ -237,33 +237,33 @@ private static void appendIntInternal(
237237
out.write(buf, 8 - len, len);
238238
}
239239

240-
public static void appendIntIncreasing(GrowableByteArrayOutputStream out, long value) {
241-
appendIntInternal(out, value, false, false);
240+
public static void appendInt64Increasing(UnsynchronizedByteArrayOutputStream out, long value) {
241+
appendInt64Internal(out, value, false, false);
242242
}
243243

244-
public static void appendIntDecreasing(GrowableByteArrayOutputStream out, long value) {
245-
appendIntInternal(out, value, true, false);
244+
public static void appendInt64Decreasing(UnsynchronizedByteArrayOutputStream out, long value) {
245+
appendInt64Internal(out, value, true, false);
246246
}
247247

248-
public static void appendDoubleIncreasing(GrowableByteArrayOutputStream out, double value) {
248+
public static void appendDoubleIncreasing(UnsynchronizedByteArrayOutputStream out, double value) {
249249
long enc = Double.doubleToRawLongBits(value);
250250
if (enc < 0) {
251251
// Transform negative doubles to maintain lexicographic sort order
252252
enc = Long.MIN_VALUE - enc;
253253
}
254-
appendIntInternal(out, enc, false, true);
254+
appendInt64Internal(out, enc, false, true);
255255
}
256256

257-
public static void appendDoubleDecreasing(GrowableByteArrayOutputStream out, double value) {
257+
public static void appendDoubleDecreasing(UnsynchronizedByteArrayOutputStream out, double value) {
258258
long enc = Double.doubleToRawLongBits(value);
259259
if (enc < 0) {
260260
enc = Long.MIN_VALUE - enc;
261261
}
262-
appendIntInternal(out, enc, true, true);
262+
appendInt64Internal(out, enc, true, true);
263263
}
264264

265265
private static void appendByteSequence(
266-
GrowableByteArrayOutputStream out, byte[] bytes, boolean decreasing) {
266+
UnsynchronizedByteArrayOutputStream out, byte[] bytes, boolean decreasing) {
267267
out.write((byte) (IS_KEY | (decreasing ? TYPE_DECREASING_STRING : TYPE_STRING)));
268268

269269
for (byte b : bytes) {
@@ -286,19 +286,19 @@ private static void appendByteSequence(
286286
out.write(SEP);
287287
}
288288

289-
public static void appendStringIncreasing(GrowableByteArrayOutputStream out, String value) {
289+
public static void appendStringIncreasing(UnsynchronizedByteArrayOutputStream out, String value) {
290290
appendByteSequence(out, value.getBytes(StandardCharsets.UTF_8), false);
291291
}
292292

293-
public static void appendStringDecreasing(GrowableByteArrayOutputStream out, String value) {
293+
public static void appendStringDecreasing(UnsynchronizedByteArrayOutputStream out, String value) {
294294
appendByteSequence(out, value.getBytes(StandardCharsets.UTF_8), true);
295295
}
296296

297-
public static void appendBytesIncreasing(GrowableByteArrayOutputStream out, byte[] value) {
297+
public static void appendBytesIncreasing(UnsynchronizedByteArrayOutputStream out, byte[] value) {
298298
appendByteSequence(out, value, false);
299299
}
300300

301-
public static void appendBytesDecreasing(GrowableByteArrayOutputStream out, byte[] value) {
301+
public static void appendBytesDecreasing(UnsynchronizedByteArrayOutputStream out, byte[] value) {
302302
appendByteSequence(out, value, true);
303303
}
304304

google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/GrowableByteArrayOutputStream.java renamed to google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/UnsynchronizedByteArrayOutputStream.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
* operations where synchronization is not required.
2828
*/
2929
@InternalApi
30-
public final class GrowableByteArrayOutputStream {
30+
public final class UnsynchronizedByteArrayOutputStream {
3131

3232
private byte[] buf;
3333
private int count;
3434

3535
/** Creates a new output stream with a default initial capacity of 32 bytes. */
36-
public GrowableByteArrayOutputStream() {
36+
public UnsynchronizedByteArrayOutputStream() {
3737
this(32);
3838
}
3939

@@ -43,7 +43,7 @@ public GrowableByteArrayOutputStream() {
4343
* @param initialCapacity the initial buffer size
4444
* @throws IllegalArgumentException if initialCapacity is negative
4545
*/
46-
public GrowableByteArrayOutputStream(int initialCapacity) {
46+
public UnsynchronizedByteArrayOutputStream(int initialCapacity) {
4747
if (initialCapacity < 0) {
4848
throw new IllegalArgumentException("Negative initial capacity: " + initialCapacity);
4949
}

0 commit comments

Comments
 (0)