Skip to content

Commit

Permalink
Add remaining API to OrderedCode
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Dec 16, 2021
1 parent 29229d3 commit 21f2dd3
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 176 deletions.
49 changes: 47 additions & 2 deletions packages/firestore/src/index/ordered_code_writer.ts
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/
import { debugAssert } from '../util/assert';

import { ByteString } from '../util/byte_string';

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

const ESCAPE2 = 0xff;
const INFINITY = 0xff; // Combined with ESCAPE2
const FF_BYTE = 0x00; // Combined with ESCAPE2

const LONG_SIZE = 64;
Expand Down Expand Up @@ -77,7 +78,7 @@ export function numberOfLeadingZerosInByte(x: number): number {
/** Counts the number of leading zeros in the given byte array. */
function numberOfLeadingZeros(bytes: Uint8Array): number {
debugAssert(
bytes.length == 8,
bytes.length === 8,
'Can only count leading zeros in 64-bit numbers'
);
let leadingZeros = 0;
Expand Down Expand Up @@ -111,6 +112,26 @@ export class OrderedCodeWriter {
buffer = new Uint8Array(DEFAULT_BUFFER_SIZE);
position = 0;

writeBytesAscending(value: ByteString): void {
const it = value[Symbol.iterator]();
let byte = it.next();
while (!byte.done) {
this.writeByteAscending(byte.value);
byte = it.next();
}
this.writeSeparatorAscending();
}

writeBytesDescending(value: ByteString): void {
const it = value[Symbol.iterator]();
let byte = it.next();
while (!byte.done) {
this.writeByteDescending(byte.value);
byte = it.next();
}
this.writeSeparatorDescending();
}

/** Writes utf8 bytes into this byte sequence, ascending. */
writeUtf8Ascending(sequence: string): void {
for (const c of sequence) {
Expand Down Expand Up @@ -183,6 +204,24 @@ export class OrderedCodeWriter {
}
}

/**
* Writes the "infinity" byte sequence that sorts after all other byte
* sequences written in ascending order.
*/
writeInfinityAscending(): void {
this.writeEscapedByteAscending(ESCAPE2);
this.writeEscapedByteAscending(INFINITY);
}

/**
* Writes the "infinity" byte sequence that sorts before all other byte
* sequences written in descending order.
*/
writeInfinityDescending(): void {
this.writeEscapedByteDescending(ESCAPE2);
this.writeEscapedByteDescending(INFINITY);
}

/**
* Encodes `val` into an encoding so that the order matches the IEEE 754
* floating-point comparison results with the following exceptions:
Expand Down Expand Up @@ -278,4 +317,10 @@ export class OrderedCodeWriter {
newBuffer.set(this.buffer); // copy old data
this.buffer = newBuffer;
}

seed(encodedBytes: Uint8Array): void {
this.ensureAvailable(encodedBytes.length);
this.buffer.set(encodedBytes, this.position);
this.position += encodedBytes.length;
}
}
13 changes: 13 additions & 0 deletions packages/firestore/src/util/byte_string.ts
Expand Up @@ -43,6 +43,19 @@ export class ByteString {
return new ByteString(binaryString);
}

[Symbol.iterator](): Iterator<number> {
let i = 0;
return {
next: () => {
if (i < this.binaryString.length) {
return { value: this.binaryString.charCodeAt(i++), done: false };
} else {
return { value: undefined, done: true };
}
}
};
}

toBase64(): string {
return encodeBase64(this.binaryString);
}
Expand Down

0 comments on commit 21f2dd3

Please sign in to comment.