Skip to content

Commit 21f2dd3

Browse files
Add remaining API to OrderedCode
1 parent 29229d3 commit 21f2dd3

File tree

3 files changed

+165
-176
lines changed

3 files changed

+165
-176
lines changed

packages/firestore/src/index/ordered_code_writer.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717
import { debugAssert } from '../util/assert';
18-
18+
import { ByteString } from '../util/byte_string';
1919

2020
/** These constants are taken from the backend. */
2121
const MIN_SURROGATE = '\uD800';
@@ -26,6 +26,7 @@ const NULL_BYTE = 0xff; // Combined with ESCAPE1
2626
const SEPARATOR = 0x01; // Combined with ESCAPE1
2727

2828
const ESCAPE2 = 0xff;
29+
const INFINITY = 0xff; // Combined with ESCAPE2
2930
const FF_BYTE = 0x00; // Combined with ESCAPE2
3031

3132
const LONG_SIZE = 64;
@@ -77,7 +78,7 @@ export function numberOfLeadingZerosInByte(x: number): number {
7778
/** Counts the number of leading zeros in the given byte array. */
7879
function numberOfLeadingZeros(bytes: Uint8Array): number {
7980
debugAssert(
80-
bytes.length == 8,
81+
bytes.length === 8,
8182
'Can only count leading zeros in 64-bit numbers'
8283
);
8384
let leadingZeros = 0;
@@ -111,6 +112,26 @@ export class OrderedCodeWriter {
111112
buffer = new Uint8Array(DEFAULT_BUFFER_SIZE);
112113
position = 0;
113114

115+
writeBytesAscending(value: ByteString): void {
116+
const it = value[Symbol.iterator]();
117+
let byte = it.next();
118+
while (!byte.done) {
119+
this.writeByteAscending(byte.value);
120+
byte = it.next();
121+
}
122+
this.writeSeparatorAscending();
123+
}
124+
125+
writeBytesDescending(value: ByteString): void {
126+
const it = value[Symbol.iterator]();
127+
let byte = it.next();
128+
while (!byte.done) {
129+
this.writeByteDescending(byte.value);
130+
byte = it.next();
131+
}
132+
this.writeSeparatorDescending();
133+
}
134+
114135
/** Writes utf8 bytes into this byte sequence, ascending. */
115136
writeUtf8Ascending(sequence: string): void {
116137
for (const c of sequence) {
@@ -183,6 +204,24 @@ export class OrderedCodeWriter {
183204
}
184205
}
185206

207+
/**
208+
* Writes the "infinity" byte sequence that sorts after all other byte
209+
* sequences written in ascending order.
210+
*/
211+
writeInfinityAscending(): void {
212+
this.writeEscapedByteAscending(ESCAPE2);
213+
this.writeEscapedByteAscending(INFINITY);
214+
}
215+
216+
/**
217+
* Writes the "infinity" byte sequence that sorts before all other byte
218+
* sequences written in descending order.
219+
*/
220+
writeInfinityDescending(): void {
221+
this.writeEscapedByteDescending(ESCAPE2);
222+
this.writeEscapedByteDescending(INFINITY);
223+
}
224+
186225
/**
187226
* Encodes `val` into an encoding so that the order matches the IEEE 754
188227
* floating-point comparison results with the following exceptions:
@@ -278,4 +317,10 @@ export class OrderedCodeWriter {
278317
newBuffer.set(this.buffer); // copy old data
279318
this.buffer = newBuffer;
280319
}
320+
321+
seed(encodedBytes: Uint8Array): void {
322+
this.ensureAvailable(encodedBytes.length);
323+
this.buffer.set(encodedBytes, this.position);
324+
this.position += encodedBytes.length;
325+
}
281326
}

packages/firestore/src/util/byte_string.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ export class ByteString {
4343
return new ByteString(binaryString);
4444
}
4545

46+
[Symbol.iterator](): Iterator<number> {
47+
let i = 0;
48+
return {
49+
next: () => {
50+
if (i < this.binaryString.length) {
51+
return { value: this.binaryString.charCodeAt(i++), done: false };
52+
} else {
53+
return { value: undefined, done: true };
54+
}
55+
}
56+
};
57+
}
58+
4659
toBase64(): string {
4760
return encodeBase64(this.binaryString);
4861
}

0 commit comments

Comments
 (0)