Skip to content

Commit cae4645

Browse files
Field Value migration (#2784)
1 parent 0d0bdc1 commit cae4645

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2722
-2173
lines changed

packages/firestore/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
# Unreleased
2+
- [changed] Changed the in-memory representation of Firestore documents to
3+
reduce memory allocations and improve performance. Calls to
4+
`DocumentSnapshot.getData()` and `DocumentSnapshot.toObject()` will see
5+
the biggest improvement.
6+
7+
# 1.10.1
28
- [fixed] Fixed an issue where the number value `-0.0` would lose its sign when
39
stored in Firestore.
410

packages/firestore/src/api/blob.ts

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -23,11 +23,7 @@ import {
2323
validateArgType,
2424
validateExactNumberOfArgs
2525
} from '../util/input_validation';
26-
import { primitiveComparator } from '../util/misc';
27-
import {
28-
binaryStringFromUint8Array,
29-
uint8ArrayFromBinaryString
30-
} from '../util/byte_string';
26+
import { ByteString } from '../util/byte_string';
3127

3228
/** Helper function to assert Uint8Array is available at runtime. */
3329
function assertUint8ArrayAvailable(): void {
@@ -57,24 +53,21 @@ function assertBase64Available(): void {
5753
* using the hack above to make sure no-one outside this module can call it.
5854
*/
5955
export class Blob {
60-
// Prefix with underscore to signal this is a private variable in JS and
61-
// prevent it showing up for autocompletion.
62-
// A binary string is a string with each char as Unicode code point in the
63-
// range of [0, 255], essentially simulating a byte array.
64-
private _binaryString: string;
56+
// Prefix with underscore to signal that we consider this not part of the
57+
// public API and to prevent it from showing up for autocompletion.
58+
_byteString: ByteString;
6559

66-
private constructor(binaryString: string) {
60+
constructor(byteString: ByteString) {
6761
assertBase64Available();
68-
this._binaryString = binaryString;
62+
this._byteString = byteString;
6963
}
7064

7165
static fromBase64String(base64: string): Blob {
7266
validateExactNumberOfArgs('Blob.fromBase64String', arguments, 1);
7367
validateArgType('Blob.fromBase64String', 'string', 1, base64);
7468
assertBase64Available();
7569
try {
76-
const binaryString = PlatformSupport.getPlatform().atob(base64);
77-
return new Blob(binaryString);
70+
return new Blob(ByteString.fromBase64String(base64));
7871
} catch (e) {
7972
throw new FirestoreError(
8073
Code.INVALID_ARGUMENT,
@@ -89,42 +82,27 @@ export class Blob {
8982
if (!(array instanceof Uint8Array)) {
9083
throw invalidClassError('Blob.fromUint8Array', 'Uint8Array', 1, array);
9184
}
92-
const binaryString = binaryStringFromUint8Array(array);
93-
return new Blob(binaryString);
85+
return new Blob(ByteString.fromUint8Array(array));
9486
}
9587

9688
toBase64(): string {
9789
validateExactNumberOfArgs('Blob.toBase64', arguments, 0);
9890
assertBase64Available();
99-
return PlatformSupport.getPlatform().btoa(this._binaryString);
91+
return this._byteString.toBase64();
10092
}
10193

10294
toUint8Array(): Uint8Array {
10395
validateExactNumberOfArgs('Blob.toUint8Array', arguments, 0);
10496
assertUint8ArrayAvailable();
105-
const buffer = uint8ArrayFromBinaryString(this._binaryString);
106-
return buffer;
97+
return this._byteString.toUint8Array();
10798
}
10899

109100
toString(): string {
110101
return 'Blob(base64: ' + this.toBase64() + ')';
111102
}
112103

113104
isEqual(other: Blob): boolean {
114-
return this._binaryString === other._binaryString;
115-
}
116-
117-
_approximateByteSize(): number {
118-
// Assume UTF-16 encoding in memory (see StringValue.approximateByteSize())
119-
return this._binaryString.length * 2;
120-
}
121-
122-
/**
123-
* Actually private to JS consumers of our API, so this function is prefixed
124-
* with an underscore.
125-
*/
126-
_compareTo(other: Blob): number {
127-
return primitiveComparator(this._binaryString, other._binaryString);
105+
return this._byteString.isEqual(other._byteString);
128106
}
129107
}
130108

0 commit comments

Comments
 (0)