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

Commit ae5ff12

Browse files
committed
remove unsigned methods
1 parent 72f2d82 commit ae5ff12

2 files changed

Lines changed: 63 additions & 94 deletions

File tree

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

Lines changed: 22 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -140,73 +140,39 @@ public static void appendNotNullMarkerNullOrderedLast(GrowableByteArrayOutputStr
140140
}
141141

142142
/**
143-
* Appends an unsigned long value in ascending (increasing) sort order.
143+
* Appends a boolean value in ascending (increasing) sort order.
144144
*
145-
* <p>This encodes a non-negative long value using variable-length encoding that preserves
146-
* lexicographic ordering. The encoding uses 1-9 bytes depending on the magnitude of the value.
145+
* <p>Boolean values are encoded using unsigned integer encoding where false=0 and true=1. This
146+
* preserves the natural ordering where false < true.
147147
*
148148
* @param out the output stream to append to
149-
* @param val the unsigned value to encode, must be in range [0, Long.MAX_VALUE]
150-
* @throws IllegalArgumentException if val is negative
149+
* @param value the boolean value to encode
151150
*/
152-
public static void appendUnsignedLongIncreasing(GrowableByteArrayOutputStream out, long val) {
153-
if (val < 0) {
154-
throw new IllegalArgumentException(
155-
"Unsigned long value must be non-negative: "
156-
+ val
157-
+ ". Values requiring the upper half of unsigned 64-bit range are not supported.");
158-
}
159-
byte[] buf = new byte[9]; // Max 9 bytes for value payload
160-
int len = 0;
161-
162-
long tempVal = val;
163-
buf[8 - len] = (byte) ((tempVal & 0x7F) << 1); // LSB is prefix-successor bit (0)
164-
tempVal >>= 7;
165-
len++;
166-
167-
while (tempVal > 0) {
168-
buf[8 - len] = (byte) (tempVal & 0xFF);
169-
tempVal >>= 8;
170-
len++;
171-
}
172-
173-
out.write((byte) (IS_KEY | (TYPE_UINT_1 + len - 1)));
174-
out.write(buf, 9 - len, len);
151+
public static void appendBoolIncreasing(GrowableByteArrayOutputStream out, boolean value) {
152+
// BOOL uses unsigned int encoding: false=0, true=1
153+
// For values 0 and 1, payload is always 1 byte
154+
int encoded = value ? 1 : 0;
155+
out.write((byte) (IS_KEY | TYPE_UINT_1)); // Header for 1-byte unsigned int
156+
out.write(
157+
(byte) (encoded << 1)); // Payload: value shifted left by 1 (LSB is prefix-successor bit)
175158
}
176159

177160
/**
178-
* Appends an unsigned long value in descending (decreasing) sort order.
161+
* Appends a boolean value in descending (decreasing) sort order.
179162
*
180-
* <p>This encodes a non-negative long value using variable-length encoding that preserves reverse
181-
* lexicographic ordering. The encoding uses 1-9 bytes depending on the magnitude of the value.
163+
* <p>Boolean values are encoded using unsigned integer encoding where false=0 and true=1, then
164+
* inverted for descending order. This preserves reverse ordering where true < false.
182165
*
183166
* @param out the output stream to append to
184-
* @param val the unsigned value to encode, must be in range [0, Long.MAX_VALUE]
185-
* @throws IllegalArgumentException if val is negative
167+
* @param value the boolean value to encode
186168
*/
187-
public static void appendUnsignedLongDecreasing(GrowableByteArrayOutputStream out, long val) {
188-
if (val < 0) {
189-
throw new IllegalArgumentException(
190-
"Unsigned long value must be non-negative: "
191-
+ val
192-
+ ". Values requiring the upper half of unsigned 64-bit range are not supported.");
193-
}
194-
byte[] buf = new byte[9];
195-
int len = 0;
196-
long tempVal = val;
197-
198-
buf[8 - len] = (byte) ((~(tempVal & 0x7F) & 0x7F) << 1);
199-
tempVal >>= 7;
200-
len++;
201-
202-
while (tempVal > 0) {
203-
buf[8 - len] = (byte) (~(tempVal & 0xFF));
204-
tempVal >>= 8;
205-
len++;
206-
}
207-
208-
out.write((byte) (IS_KEY | (TYPE_DECREASING_UINT_1 - len + 1)));
209-
out.write(buf, 9 - len, len);
169+
public static void appendBoolDecreasing(GrowableByteArrayOutputStream out, boolean value) {
170+
// BOOL uses decreasing unsigned int encoding: false=0, true=1, then inverted
171+
// For values 0 and 1, payload is always 1 byte
172+
int encoded = value ? 1 : 0;
173+
out.write(
174+
(byte) (IS_KEY | TYPE_DECREASING_UINT_1)); // Header for 1-byte decreasing unsigned int
175+
out.write((byte) ((~encoded & 0x7F) << 1)); // Inverted payload
210176
}
211177

212178
private static void appendIntInternal(

google-cloud-spanner/src/test/java/com/google/cloud/spanner/spi/v1/SsFormatTest.java

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -353,58 +353,61 @@ public void appendIntIncreasing_edgeCases() {
353353
}
354354
}
355355

356-
// ==================== Unsigned Integer Tests ====================
356+
// ==================== Boolean Tests ====================
357357

358358
@Test
359-
public void appendUnsignedLongIncreasing_preservesOrdering() {
360-
// Filter to only non-negative values for unsigned comparison
361-
List<Long> positiveValues = new ArrayList<>();
362-
for (long v : unsignedIntTestValues) {
363-
if (v >= 0) positiveValues.add(v);
364-
}
365-
positiveValues.sort(Long::compareUnsigned);
359+
public void appendBoolIncreasing_preservesOrdering() {
360+
GrowableByteArrayOutputStream outFalse = new GrowableByteArrayOutputStream();
361+
GrowableByteArrayOutputStream outTrue = new GrowableByteArrayOutputStream();
366362

367-
for (int i = 0; i < positiveValues.size() - 1; i++) {
368-
long v1 = positiveValues.get(i);
369-
long v2 = positiveValues.get(i + 1);
363+
SsFormat.appendBoolIncreasing(outFalse, false);
364+
SsFormat.appendBoolIncreasing(outTrue, true);
370365

371-
GrowableByteArrayOutputStream out1 = new GrowableByteArrayOutputStream();
372-
GrowableByteArrayOutputStream out2 = new GrowableByteArrayOutputStream();
366+
assertTrue(
367+
"Encoded false should be less than encoded true",
368+
UNSIGNED_BYTE_COMPARATOR.compare(outFalse.toByteArray(), outTrue.toByteArray()) < 0);
369+
}
373370

374-
SsFormat.appendUnsignedLongIncreasing(out1, v1);
375-
SsFormat.appendUnsignedLongIncreasing(out2, v2);
371+
@Test
372+
public void appendBoolIncreasing_encodesCorrectly() {
373+
GrowableByteArrayOutputStream outFalse = new GrowableByteArrayOutputStream();
374+
GrowableByteArrayOutputStream outTrue = new GrowableByteArrayOutputStream();
376375

377-
assertTrue(
378-
"Unsigned encoded "
379-
+ Long.toUnsignedString(v1)
380-
+ " should be less than "
381-
+ Long.toUnsignedString(v2),
382-
UNSIGNED_BYTE_COMPARATOR.compare(out1.toByteArray(), out2.toByteArray()) < 0);
383-
}
376+
SsFormat.appendBoolIncreasing(outFalse, false);
377+
SsFormat.appendBoolIncreasing(outTrue, true);
378+
379+
// false=0: header 0x80 (IS_KEY | TYPE_UINT_1), payload 0x00
380+
assertArrayEquals(new byte[] {(byte) 0x80, 0x00}, outFalse.toByteArray());
381+
// true=1: header 0x80, payload 0x02 (1 << 1)
382+
assertArrayEquals(new byte[] {(byte) 0x80, 0x02}, outTrue.toByteArray());
384383
}
385384

386385
@Test
387-
public void appendUnsignedLongIncreasing_rejectsNegativeValues() {
388-
GrowableByteArrayOutputStream out = new GrowableByteArrayOutputStream();
389-
assertThrows(
390-
IllegalArgumentException.class, () -> SsFormat.appendUnsignedLongIncreasing(out, -1));
386+
public void appendBoolDecreasing_reversesOrdering() {
387+
GrowableByteArrayOutputStream outFalse = new GrowableByteArrayOutputStream();
388+
GrowableByteArrayOutputStream outTrue = new GrowableByteArrayOutputStream();
389+
390+
SsFormat.appendBoolDecreasing(outFalse, false);
391+
SsFormat.appendBoolDecreasing(outTrue, true);
392+
393+
assertTrue(
394+
"Decreasing encoded false should be greater than encoded true",
395+
UNSIGNED_BYTE_COMPARATOR.compare(outFalse.toByteArray(), outTrue.toByteArray()) > 0);
391396
}
392397

393398
@Test
394-
public void appendUnsignedLongDecreasing_reversesOrdering() {
395-
long[] values = {0, 1, 100, 1000, Long.MAX_VALUE};
396-
397-
for (int i = 0; i < values.length - 1; i++) {
398-
GrowableByteArrayOutputStream out1 = new GrowableByteArrayOutputStream();
399-
GrowableByteArrayOutputStream out2 = new GrowableByteArrayOutputStream();
399+
public void appendBoolDecreasing_encodesCorrectly() {
400+
GrowableByteArrayOutputStream outFalse = new GrowableByteArrayOutputStream();
401+
GrowableByteArrayOutputStream outTrue = new GrowableByteArrayOutputStream();
400402

401-
SsFormat.appendUnsignedLongDecreasing(out1, values[i]);
402-
SsFormat.appendUnsignedLongDecreasing(out2, values[i + 1]);
403+
SsFormat.appendBoolDecreasing(outFalse, false);
404+
SsFormat.appendBoolDecreasing(outTrue, true);
403405

404-
assertTrue(
405-
"Decreasing unsigned encoded " + values[i] + " should be greater than " + values[i + 1],
406-
UNSIGNED_BYTE_COMPARATOR.compare(out1.toByteArray(), out2.toByteArray()) > 0);
407-
}
406+
// false=0 inverted: header 0xA8 (IS_KEY | TYPE_DECREASING_UINT_1), payload 0xFE (~0 & 0x7F) <<
407+
// 1
408+
assertArrayEquals(new byte[] {(byte) 0xA8, (byte) 0xFE}, outFalse.toByteArray());
409+
// true=1 inverted: header 0xA8, payload 0xFC (~1 & 0x7F) << 1
410+
assertArrayEquals(new byte[] {(byte) 0xA8, (byte) 0xFC}, outTrue.toByteArray());
408411
}
409412

410413
// ==================== String Tests ====================

0 commit comments

Comments
 (0)