From 337fecb62139c5b48128b11093ca874fffd30237 Mon Sep 17 00:00:00 2001 From: Daniel Bruce Date: Fri, 23 Oct 2026 13:46:10 +0000 Subject: [PATCH 1/5] fix(bigtable): add getOrInsert and getOrInsertComputed to EncodedKeyMap --- .../bigtable/src/execute-query/values.ts | 26 ++++++++ handwritten/bigtable/test/base64keymap.ts | 59 +++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/handwritten/bigtable/src/execute-query/values.ts b/handwritten/bigtable/src/execute-query/values.ts index 258a25d11268..15f9ab4f6d11 100644 --- a/handwritten/bigtable/src/execute-query/values.ts +++ b/handwritten/bigtable/src/execute-query/values.ts @@ -227,4 +227,30 @@ export class EncodedKeyMap this.map_impl.set(_parseBufferToMapKey(key), [key, value]); return this; } + + getOrInsert( + key: string | bigint | Uint8Array | null, + defaultValue: SqlValue, + ): SqlValue { + if (this.has(key)) { + return this.get(key) as SqlValue; + } + this.set(key, defaultValue); + return defaultValue; + } + + getOrInsertComputed( + key: string | bigint | Uint8Array | null, + callback: (key: string | bigint | Uint8Array | null) => SqlValue, + ): SqlValue { + if (typeof callback !== 'function') { + throw new TypeError('callback is not a function'); + } + if (this.has(key)) { + return this.get(key) as SqlValue; + } + const value = callback(key); + this.set(key, value); + return value; + } } diff --git a/handwritten/bigtable/test/base64keymap.ts b/handwritten/bigtable/test/base64keymap.ts index 7446126bff45..3c53c67411df 100644 --- a/handwritten/bigtable/test/base64keymap.ts +++ b/handwritten/bigtable/test/base64keymap.ts @@ -180,5 +180,64 @@ describe('Bigtable/EncodedKeyMap', () => { assert.deepStrictEqual(resultForEach[1][0], 'stringKey1'); assert.deepStrictEqual(resultForEach[1][1], 'valueForString1'); }); + it('test getOrInsert and getOrInsertComputed', () => { + const map = new EncodedKeyMap([ + [Buffer.from('existingBuffer'), 'initialBufferValue'], + ['existingNullValue', null], + ]); + + assert.strictEqual( + map.getOrInsert(Buffer.from('existingBuffer'), 'fallbackValue'), + 'initialBufferValue', + ); + assert.strictEqual( + map.getOrInsert('existingNullValue', 'fallbackForNull'), + null, + ); + assert.strictEqual( + map.getOrInsert(Buffer.from('newBuffer'), 'insertedBufferValue'), + 'insertedBufferValue', + ); + assert.strictEqual( + map.get(Buffer.from('newBuffer')), + 'insertedBufferValue', + ); + + let callbackCount = 0; + assert.strictEqual( + map.getOrInsertComputed(Buffer.from('existingBuffer'), () => { + callbackCount++; + return 'computedFallback'; + }), + 'initialBufferValue', + ); + assert.strictEqual(callbackCount, 0); + + const computedKey = Buffer.from('computedBuffer'); + assert.strictEqual( + map.getOrInsertComputed(computedKey, key => { + callbackCount++; + assert.strictEqual(key, computedKey); + return 'computedBufferValue'; + }), + 'computedBufferValue', + ); + assert.strictEqual(callbackCount, 1); + assert.strictEqual( + map.get(Buffer.from('computedBuffer')), + 'computedBufferValue', + ); + + assert.throws( + () => + map.getOrInsertComputed( + 'existingNullValue', + null as unknown as ( + key: string | bigint | Uint8Array | null, + ) => SqlValue, + ), + TypeError, + ); + }); }); }); From dc750b28c4b2b0ab09e9d3822de9e0434eb19407 Mon Sep 17 00:00:00 2001 From: Daniel Bruce Date: Fri, 23 Oct 2026 14:01:31 +0000 Subject: [PATCH 2/5] [Independent review follow-ups] address independent Gemini review comments (round 1) --- .../bigtable/src/execute-query/values.ts | 16 ++++++---- handwritten/bigtable/test/base64keymap.ts | 31 +++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/handwritten/bigtable/src/execute-query/values.ts b/handwritten/bigtable/src/execute-query/values.ts index 15f9ab4f6d11..191487aa1bc4 100644 --- a/handwritten/bigtable/src/execute-query/values.ts +++ b/handwritten/bigtable/src/execute-query/values.ts @@ -232,10 +232,12 @@ export class EncodedKeyMap key: string | bigint | Uint8Array | null, defaultValue: SqlValue, ): SqlValue { - if (this.has(key)) { - return this.get(key) as SqlValue; + const parsedKey = _parseBufferToMapKey(key); + const existing = this.map_impl.get(parsedKey); + if (existing !== undefined) { + return existing[1]; } - this.set(key, defaultValue); + this.map_impl.set(parsedKey, [key, defaultValue]); return defaultValue; } @@ -246,11 +248,13 @@ export class EncodedKeyMap if (typeof callback !== 'function') { throw new TypeError('callback is not a function'); } - if (this.has(key)) { - return this.get(key) as SqlValue; + const parsedKey = _parseBufferToMapKey(key); + const existing = this.map_impl.get(parsedKey); + if (existing !== undefined) { + return existing[1]; } const value = callback(key); - this.set(key, value); + this.map_impl.set(parsedKey, [key, value]); return value; } } diff --git a/handwritten/bigtable/test/base64keymap.ts b/handwritten/bigtable/test/base64keymap.ts index 3c53c67411df..ede6eee2dfa8 100644 --- a/handwritten/bigtable/test/base64keymap.ts +++ b/handwritten/bigtable/test/base64keymap.ts @@ -238,6 +238,37 @@ describe('Bigtable/EncodedKeyMap', () => { ), TypeError, ); + + // Existing null value does not trigger callback in getOrInsertComputed + assert.strictEqual( + map.getOrInsertComputed('existingNullValue', () => { + callbackCount++; + return 'shouldNotBeCalled'; + }), + null, + ); + assert.strictEqual(callbackCount, 1); + + // bigint, null, and Uint8Array keys + assert.strictEqual(map.getOrInsert(42n, 'bigintVal'), 'bigintVal'); + assert.strictEqual(map.getOrInsert(42n, 'otherBigintVal'), 'bigintVal'); + assert.strictEqual(map.getOrInsert(null, 'nullKeyVal'), 'nullKeyVal'); + assert.strictEqual( + map.getOrInsertComputed(new Uint8Array([1, 2, 3]), () => 'uint8Val'), + 'uint8Val', + ); + assert.strictEqual( + map.get(new Uint8Array([1, 2, 3])), + 'uint8Val', + ); + + // Error thrown by callback propagates and does not insert entry + assert.throws(() => { + map.getOrInsertComputed('throwingKey', () => { + throw new Error('computation failed'); + }); + }, /computation failed/); + assert.strictEqual(map.has('throwingKey'), false); }); }); }); From 5da790b7ddf92c3d6cb8990844c23e72a53a4969 Mon Sep 17 00:00:00 2001 From: Daniel Bruce Date: Fri, 23 Oct 2026 15:14:11 +0000 Subject: [PATCH 3/5] [Address CI errors] use ReadonlyMap implements clause without adding methods and fix lint --- .../bigtable/src/execute-query/values.ts | 42 +-------- handwritten/bigtable/test/base64keymap.ts | 92 ++----------------- 2 files changed, 15 insertions(+), 119 deletions(-) diff --git a/handwritten/bigtable/src/execute-query/values.ts b/handwritten/bigtable/src/execute-query/values.ts index 191487aa1bc4..f5ee1cab81d8 100644 --- a/handwritten/bigtable/src/execute-query/values.ts +++ b/handwritten/bigtable/src/execute-query/values.ts @@ -38,10 +38,7 @@ export type ExecuteQueryParameterValue = | null; export type SqlValue = - | ExecuteQueryParameterValue - | Struct - | Array - | BigtableMap; + ExecuteQueryParameterValue | Struct | Array | BigtableMap; /** * A custom class created to allow setting year, month and date to 0. @@ -117,9 +114,10 @@ type MapKey = bigint | string | Uint8Array | null; // internal representation of the map contains the original key and value tuple type _MapValue = [MapKey, SqlValue]; -export class EncodedKeyMap - implements Map -{ +export class EncodedKeyMap implements ReadonlyMap< + bigint | string | Uint8Array | null, + SqlValue +> { private map_impl: Map; /** * Class representing a Map Value returned by ExecuteQuery. Native JS Map @@ -227,34 +225,4 @@ export class EncodedKeyMap this.map_impl.set(_parseBufferToMapKey(key), [key, value]); return this; } - - getOrInsert( - key: string | bigint | Uint8Array | null, - defaultValue: SqlValue, - ): SqlValue { - const parsedKey = _parseBufferToMapKey(key); - const existing = this.map_impl.get(parsedKey); - if (existing !== undefined) { - return existing[1]; - } - this.map_impl.set(parsedKey, [key, defaultValue]); - return defaultValue; - } - - getOrInsertComputed( - key: string | bigint | Uint8Array | null, - callback: (key: string | bigint | Uint8Array | null) => SqlValue, - ): SqlValue { - if (typeof callback !== 'function') { - throw new TypeError('callback is not a function'); - } - const parsedKey = _parseBufferToMapKey(key); - const existing = this.map_impl.get(parsedKey); - if (existing !== undefined) { - return existing[1]; - } - const value = callback(key); - this.map_impl.set(parsedKey, [key, value]); - return value; - } } diff --git a/handwritten/bigtable/test/base64keymap.ts b/handwritten/bigtable/test/base64keymap.ts index ede6eee2dfa8..00580b887156 100644 --- a/handwritten/bigtable/test/base64keymap.ts +++ b/handwritten/bigtable/test/base64keymap.ts @@ -180,95 +180,23 @@ describe('Bigtable/EncodedKeyMap', () => { assert.deepStrictEqual(resultForEach[1][0], 'stringKey1'); assert.deepStrictEqual(resultForEach[1][1], 'valueForString1'); }); - it('test getOrInsert and getOrInsertComputed', () => { + it('implements ReadonlyMap without adding extra methods', () => { const map = new EncodedKeyMap([ [Buffer.from('existingBuffer'), 'initialBufferValue'], ['existingNullValue', null], ]); - - assert.strictEqual( - map.getOrInsert(Buffer.from('existingBuffer'), 'fallbackValue'), - 'initialBufferValue', - ); - assert.strictEqual( - map.getOrInsert('existingNullValue', 'fallbackForNull'), - null, - ); - assert.strictEqual( - map.getOrInsert(Buffer.from('newBuffer'), 'insertedBufferValue'), - 'insertedBufferValue', - ); - assert.strictEqual( - map.get(Buffer.from('newBuffer')), - 'insertedBufferValue', - ); - - let callbackCount = 0; + const readonlyMap: ReadonlyMap< + string | bigint | Uint8Array | null, + SqlValue + > = map; + const es2023Map: Map = map; assert.strictEqual( - map.getOrInsertComputed(Buffer.from('existingBuffer'), () => { - callbackCount++; - return 'computedFallback'; - }), + readonlyMap.get(Buffer.from('existingBuffer')), 'initialBufferValue', ); - assert.strictEqual(callbackCount, 0); - - const computedKey = Buffer.from('computedBuffer'); - assert.strictEqual( - map.getOrInsertComputed(computedKey, key => { - callbackCount++; - assert.strictEqual(key, computedKey); - return 'computedBufferValue'; - }), - 'computedBufferValue', - ); - assert.strictEqual(callbackCount, 1); - assert.strictEqual( - map.get(Buffer.from('computedBuffer')), - 'computedBufferValue', - ); - - assert.throws( - () => - map.getOrInsertComputed( - 'existingNullValue', - null as unknown as ( - key: string | bigint | Uint8Array | null, - ) => SqlValue, - ), - TypeError, - ); - - // Existing null value does not trigger callback in getOrInsertComputed - assert.strictEqual( - map.getOrInsertComputed('existingNullValue', () => { - callbackCount++; - return 'shouldNotBeCalled'; - }), - null, - ); - assert.strictEqual(callbackCount, 1); - - // bigint, null, and Uint8Array keys - assert.strictEqual(map.getOrInsert(42n, 'bigintVal'), 'bigintVal'); - assert.strictEqual(map.getOrInsert(42n, 'otherBigintVal'), 'bigintVal'); - assert.strictEqual(map.getOrInsert(null, 'nullKeyVal'), 'nullKeyVal'); - assert.strictEqual( - map.getOrInsertComputed(new Uint8Array([1, 2, 3]), () => 'uint8Val'), - 'uint8Val', - ); - assert.strictEqual( - map.get(new Uint8Array([1, 2, 3])), - 'uint8Val', - ); - - // Error thrown by callback propagates and does not insert entry - assert.throws(() => { - map.getOrInsertComputed('throwingKey', () => { - throw new Error('computation failed'); - }); - }, /computation failed/); - assert.strictEqual(map.has('throwingKey'), false); + assert.strictEqual(es2023Map.get('existingNullValue'), null); + assert.strictEqual('getOrInsert' in map, false); + assert.strictEqual('getOrInsertComputed' in map, false); }); }); }); From cf58149638c2aeed3cb0ffc39ec490155728b11d Mon Sep 17 00:00:00 2001 From: Daniel Bruce Date: Fri, 23 Oct 2026 15:55:53 +0000 Subject: [PATCH 4/5] [Address CI errors] address gemini-code-assist review comment on base64keymap test --- handwritten/bigtable/test/base64keymap.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/handwritten/bigtable/test/base64keymap.ts b/handwritten/bigtable/test/base64keymap.ts index 00580b887156..527319240f45 100644 --- a/handwritten/bigtable/test/base64keymap.ts +++ b/handwritten/bigtable/test/base64keymap.ts @@ -189,12 +189,11 @@ describe('Bigtable/EncodedKeyMap', () => { string | bigint | Uint8Array | null, SqlValue > = map; - const es2023Map: Map = map; assert.strictEqual( readonlyMap.get(Buffer.from('existingBuffer')), 'initialBufferValue', ); - assert.strictEqual(es2023Map.get('existingNullValue'), null); + assert.strictEqual(readonlyMap.get('existingNullValue'), null); assert.strictEqual('getOrInsert' in map, false); assert.strictEqual('getOrInsertComputed' in map, false); }); From 4f018a03c7b13eef33cf4d249b7877aaf124bc85 Mon Sep 17 00:00:00 2001 From: Daniel Bruce Date: Fri, 23 Oct 2026 15:17:14 +0000 Subject: [PATCH 5/5] [Style Maintenance] align stack changes with CONTRIBUTING.md and gts coding style guidelines --- .../bigtable/src/execute-query/values.ts | 23 +++++++++++-------- handwritten/bigtable/test/base64keymap.ts | 1 + 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/handwritten/bigtable/src/execute-query/values.ts b/handwritten/bigtable/src/execute-query/values.ts index f5ee1cab81d8..f1434ec35ae0 100644 --- a/handwritten/bigtable/src/execute-query/values.ts +++ b/handwritten/bigtable/src/execute-query/values.ts @@ -114,20 +114,25 @@ type MapKey = bigint | string | Uint8Array | null; // internal representation of the map contains the original key and value tuple type _MapValue = [MapKey, SqlValue]; +/** + * Class representing a Map Value returned by ExecuteQuery. Native JS Map + * does not support Buffer comparison - it compares object id and not + * buffer values. This class solves this by encoding Buffer keys as + * base64 strings. + * Please note that an empty string and an empty buffer have the same + * representation, however, we do not ever use mixed key types (all keys are + * always all buffers or all strings) so we don't need to handle this. + * + * Implements `ReadonlyMap` rather than `Map` so emitted declarations remain + * compatible with TypeScript 7.0+ (`--target esnext`) without adding ESNext + * `Map` upsert methods (`getOrInsert` / `getOrInsertComputed`). + */ export class EncodedKeyMap implements ReadonlyMap< bigint | string | Uint8Array | null, SqlValue > { private map_impl: Map; - /** - * Class representing a Map Value returned by ExecuteQuery. Native JS Map - * does not support Buffer comparison - it compares object id and not - * buffer values. This class solves this by encoding Buffer keys as - * base64 strings. - * Please note that an empty string and an empty buffer have the same - * representation, however, we do not ever use mixed key types (all keys are - * always all buffers or all strings) so we don't need to handle this. - */ + constructor( entries?: ReadonlyArray< [bigint | string | Uint8Array | null, SqlValue] diff --git a/handwritten/bigtable/test/base64keymap.ts b/handwritten/bigtable/test/base64keymap.ts index 527319240f45..af0a83758a4a 100644 --- a/handwritten/bigtable/test/base64keymap.ts +++ b/handwritten/bigtable/test/base64keymap.ts @@ -194,6 +194,7 @@ describe('Bigtable/EncodedKeyMap', () => { 'initialBufferValue', ); assert.strictEqual(readonlyMap.get('existingNullValue'), null); + assert.strictEqual(map.get(Buffer.from('missingBuffer')), undefined); assert.strictEqual('getOrInsert' in map, false); assert.strictEqual('getOrInsertComputed' in map, false); });