Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 3 additions & 47 deletions src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {ModifiableBackupFields} from './backup';
import {CreateBackupCallback, CreateBackupResponse} from './cluster';
import {google} from '../protos/protos';
import {Duplex} from 'stream';
import {TableUtils} from './utils/table';

// See protos/google/rpc/code.proto
// (4=DEADLINE_EXCEEDED, 8=RESOURCE_EXHAUSTED, 10=ABORTED, 14=UNAVAILABLE)
Expand Down Expand Up @@ -485,21 +486,7 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`);
* ```
*/
static createPrefixRange(start: string): PrefixRange {
const prefix = start.replace(new RegExp('[\xff]+$'), '');
let endKey = '';
if (prefix) {
const position = prefix.length - 1;
const charCode = prefix.charCodeAt(position);
const nextChar = String.fromCharCode(charCode + 1);
endKey = prefix.substring(0, position) + nextChar;
}
return {
start,
end: {
value: endKey,
inclusive: !endKey,
},
};
return TableUtils.createPrefixRange(start);
}

create(options?: CreateTableOptions): Promise<CreateTableResponse>;
Expand Down Expand Up @@ -736,7 +723,6 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`);
const maxRetries = is.number(this.maxRetries) ? this.maxRetries! : 3;
let activeRequestStream: AbortableDuplex | null;
let rowKeys: string[];
const ranges = options.ranges || [];
let filter: {} | null;
const rowsLimit = options.limit || 0;
const hasLimit = rowsLimit !== 0;
Expand All @@ -747,37 +733,7 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`);

rowKeys = options.keys || [];

if (options.start || options.end) {
if (options.ranges || options.prefix || options.prefixes) {
throw new Error(
'start/end should be used exclusively to ranges/prefix/prefixes.'
);
}
ranges.push({
start: options.start!,
end: options.end!,
});
}

if (options.prefix) {
if (options.ranges || options.start || options.end || options.prefixes) {
throw new Error(
'prefix should be used exclusively to ranges/start/end/prefixes.'
);
}
ranges.push(Table.createPrefixRange(options.prefix));
}

if (options.prefixes) {
if (options.ranges || options.start || options.end || options.prefix) {
throw new Error(
'prefixes should be used exclusively to ranges/start/end/prefix.'
);
}
options.prefixes.forEach(prefix => {
ranges.push(Table.createPrefixRange(prefix));
});
}
const ranges = TableUtils.getRanges(options);

// If rowKeys and ranges are both empty, the request is a full table scan.
// Add an empty range to simplify the resumption logic.
Expand Down
69 changes: 69 additions & 0 deletions src/utils/table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {GetRowsOptions, PrefixRange} from '../table';

export class TableUtils {
static getRanges(options: GetRowsOptions) {
const ranges = options.ranges || [];
if (options.start || options.end) {
if (options.ranges || options.prefix || options.prefixes) {
throw new Error(
'start/end should be used exclusively to ranges/prefix/prefixes.'
);
}
ranges.push({
start: options.start!,
end: options.end!,
});
}
if (options.prefix) {
if (options.ranges || options.start || options.end || options.prefixes) {
throw new Error(
'prefix should be used exclusively to ranges/start/end/prefixes.'
);
}
ranges.push(this.createPrefixRange(options.prefix));
}
if (options.prefixes) {
if (options.ranges || options.start || options.end || options.prefix) {
throw new Error(
'prefixes should be used exclusively to ranges/start/end/prefix.'
);
}
options.prefixes.forEach(prefix => {
ranges.push(this.createPrefixRange(prefix));
});
}
return ranges;
}

static createPrefixRange(start: string): PrefixRange {
const prefix = start.replace(new RegExp('[\xff]+$'), '');
let endKey = '';
if (prefix) {
const position = prefix.length - 1;
const charCode = prefix.charCodeAt(position);
const nextChar = String.fromCharCode(charCode + 1);
endKey = prefix.substring(0, position) + nextChar;
}
return {
start,
end: {
value: endKey,
inclusive: !endKey,
},
};
}
}
7 changes: 4 additions & 3 deletions test/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {Row} from '../src/row.js';
import * as tblTypes from '../src/table';
import {Bigtable, RequestOptions} from '../src';
import {EventEmitter} from 'events';
import {TableUtils} from '../src/utils/table';

const sandbox = sinon.createSandbox();
const noop = () => {};
Expand Down Expand Up @@ -827,7 +828,7 @@ describe('Bigtable/Table', () => {

afterEach(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(Table as any).createPrefixRange.restore();
(TableUtils as any).createPrefixRange.restore();
});

it('should transform the prefix into a range', done => {
Expand All @@ -840,7 +841,7 @@ describe('Bigtable/Table', () => {
const fakePrefix = 'abc';

const prefixSpy = sandbox
.stub(Table, 'createPrefixRange')
.stub(TableUtils, 'createPrefixRange')
.returns(fakePrefixRange);

const rangeSpy = sandbox
Expand Down Expand Up @@ -871,7 +872,7 @@ describe('Bigtable/Table', () => {
{start: 'def', end: 'deg'},
] as {} as tblTypes.PrefixRange[];
const prefixSpy = sandbox
.stub(Table, 'createPrefixRange')
.stub(TableUtils, 'createPrefixRange')
.callsFake(() => {
const callIndex = prefixSpy.callCount - 1;
return prefixRanges[callIndex];
Expand Down