Skip to content

Commit 7bcb371

Browse files
committed
align refactor some types
1 parent 918e1d9 commit 7bcb371

File tree

5 files changed

+26
-27
lines changed

5 files changed

+26
-27
lines changed

packages/cubejs-query-orchestrator/src/orchestrator/PreAggregationLoadCache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TableStructure } from '@cubejs-backend/base-driver';
22
import { DriverFactory } from './DriverFactory';
3-
import { QueryCache, QueryTuple, QueryWithParams } from './QueryCache';
3+
import { QueryCache, QueryWithParams } from './QueryCache';
44
import {
55
PreAggregationDescription,
66
PreAggregations,
@@ -189,7 +189,7 @@ export class PreAggregationLoadCache {
189189
}
190190

191191
public async keyQueryResult(sqlQuery: QueryWithParams, waitForRenew: boolean, priority: number) {
192-
const [query, values, queryOptions]: QueryTuple = Array.isArray(sqlQuery) ? sqlQuery : [sqlQuery, [], {}];
192+
const [query, values, queryOptions]: QueryWithParams = Array.isArray(sqlQuery) ? sqlQuery : [sqlQuery, [], {}];
193193

194194
if (!this.queryResults[this.queryCache.queryRedisKey([query, values])]) {
195195
this.queryResults[this.queryCache.queryRedisKey([query, values])] = await this.queryCache.cacheQueryResult(

packages/cubejs-query-orchestrator/src/orchestrator/PreAggregationLoader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
UnloadOptions
1313
} from '@cubejs-backend/base-driver';
1414
import { DriverFactory } from './DriverFactory';
15-
import { PreAggTableToTempTableNames, QueryCache, QueryTuple } from './QueryCache';
15+
import { PreAggTableToTempTableNames, QueryCache, QueryWithParams } from './QueryCache';
1616
import { ContinueWaitError } from './ContinueWaitError';
1717
import { LargeStreamWarning } from './StreamObjectsCounter';
1818
import {
@@ -30,7 +30,7 @@ import { PreAggregationLoadCache } from './PreAggregationLoadCache';
3030

3131
type IndexesSql = { sql: [string, unknown[]], indexName: string }[];
3232

33-
type QueryKey = [QueryTuple, IndexesSql, InvalidationKeys] | [QueryTuple, InvalidationKeys];
33+
type QueryKey = [QueryWithParams, IndexesSql, InvalidationKeys] | [QueryWithParams, InvalidationKeys];
3434

3535
type QueryOptions = {
3636
queryKey: QueryKey;

packages/cubejs-query-orchestrator/src/orchestrator/PreAggregationPartitionRangeLoader.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from '@cubejs-backend/shared';
1414
import { InlineTable, TableStructure } from '@cubejs-backend/base-driver';
1515
import { DriverFactory } from './DriverFactory';
16-
import { QueryCache, QueryTuple, QueryWithParams } from './QueryCache';
16+
import { QueryCache, QueryWithParams } from './QueryCache';
1717
import {
1818
getLastUpdatedAtTimestamp,
1919
LAMBDA_TABLE_PREFIX,
@@ -83,8 +83,8 @@ export class PreAggregationPartitionRangeLoader {
8383
this.compilerCacheFn = options.compilerCacheFn || ((subKey, cacheFn) => cacheFn());
8484
}
8585

86-
private async loadRangeQuery(rangeQuery: QueryTuple, partitionRange?: QueryDateRange) {
87-
const [query, values, queryOptions]: QueryTuple = rangeQuery;
86+
private async loadRangeQuery(rangeQuery: QueryWithParams, partitionRange?: QueryDateRange) {
87+
const [query, values, queryOptions]: QueryWithParams = rangeQuery;
8888
const invalidate =
8989
this.preAggregation.invalidateKeyQueries?.[0]
9090
? this.preAggregation.invalidateKeyQueries[0].slice(0, 2)

packages/cubejs-query-orchestrator/src/orchestrator/QueryCache.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
InlineTables,
1010
CacheDriverInterface,
1111
TableStructure,
12-
DriverInterface,
12+
DriverInterface, QueryKey,
1313
} from '@cubejs-backend/base-driver';
1414

1515
import { QueryQueue } from './QueryQueue';
@@ -28,14 +28,12 @@ type QueryOptions = {
2828
incremental?: boolean;
2929
};
3030

31-
export type QueryTuple = [
31+
export type QueryWithParams = [
3232
sql: string,
33-
params: unknown[],
33+
params: string[],
3434
options?: QueryOptions
3535
];
3636

37-
export type QueryWithParams = QueryTuple;
38-
3937
export type Query = {
4038
requestId?: string;
4139
dataSource: string;
@@ -84,7 +82,7 @@ export type PreAggTableToTempTable = [
8482

8583
export type PreAggTableToTempTableNames = [string, { targetTableName: string; }];
8684

87-
export type CacheKeyItem = string | string[] | QueryTuple | QueryTuple[] | undefined;
85+
export type CacheKeyItem = string | string[] | QueryWithParams | QueryWithParams[] | undefined;
8886

8987
export type CacheKey =
9088
[CacheKeyItem, CacheKeyItem] |
@@ -382,7 +380,7 @@ export class QueryCache {
382380
public static replacePreAggregationTableNames(
383381
queryAndParams: string | QueryWithParams,
384382
preAggregationsTablesToTempTables: PreAggTableToTempTableNames[],
385-
): string | QueryTuple {
383+
): string | QueryWithParams {
386384
const [keyQuery, params, queryOptions] = Array.isArray(queryAndParams)
387385
? queryAndParams
388386
: [queryAndParams, []];
@@ -404,7 +402,7 @@ export class QueryCache {
404402
* queries and with the `stream.Writable` instance for the persistent.
405403
*/
406404
public async queryWithRetryAndRelease(
407-
query: string | QueryTuple,
405+
query: string | QueryWithParams,
408406
values: string[],
409407
{
410408
cacheKey,
@@ -665,9 +663,9 @@ export class QueryCache {
665663
}
666664

667665
public startRenewCycle(
668-
query: string | QueryTuple,
666+
query: string | QueryWithParams,
669667
values: string[],
670-
cacheKeyQueries: (string | QueryTuple)[],
668+
cacheKeyQueries: (string | QueryWithParams)[],
671669
expireSecs: number,
672670
cacheKey: CacheKey,
673671
renewalThreshold: any,
@@ -700,9 +698,9 @@ export class QueryCache {
700698
}
701699

702700
public renewQuery(
703-
query: string | QueryTuple,
701+
query: string | QueryWithParams,
704702
values: string[],
705-
cacheKeyQueries: (string | QueryTuple)[],
703+
cacheKeyQueries: (string | QueryWithParams)[],
706704
expireSecs: number,
707705
cacheKey: CacheKey,
708706
renewalThreshold: any,
@@ -719,7 +717,7 @@ export class QueryCache {
719717
) {
720718
options = options || { dataSource: 'default' };
721719
return Promise.all(
722-
this.loadRefreshKeys(<QueryTuple[]>cacheKeyQueries, expireSecs, options),
720+
this.loadRefreshKeys(<QueryWithParams[]>cacheKeyQueries, expireSecs, options),
723721
)
724722
.catch(e => {
725723
if (e instanceof ContinueWaitError) {
@@ -782,11 +780,11 @@ export class QueryCache {
782780
}
783781
) {
784782
return cacheKeyQueries.map((q) => {
785-
const [query, values, queryOptions]: QueryTuple = Array.isArray(q) ? q : [q, [], {}];
783+
const [query, values, queryOptions]: QueryWithParams = Array.isArray(q) ? q : [q, [], {}];
786784
return this.cacheQueryResult(
787785
query,
788-
<string[]>values,
789-
[query, <string[]>values],
786+
values,
787+
[query, values],
790788
expireSecs,
791789
{
792790
renewalThreshold: this.options.refreshKeyRenewalThreshold || queryOptions?.renewalThreshold || 2 * 60,
@@ -808,7 +806,7 @@ export class QueryCache {
808806
) => this.cacheDriver.withLock(`lock:${key}`, callback, ttl, true);
809807

810808
public async cacheQueryResult(
811-
query: string | QueryTuple,
809+
query: string | QueryWithParams,
812810
values: string[],
813811
cacheKey: CacheKey,
814812
expiration: number,
@@ -990,7 +988,7 @@ export class QueryCache {
990988
return null;
991989
}
992990

993-
public queryRedisKey(cacheKey): string {
991+
public queryRedisKey(cacheKey: CacheKey): string {
994992
return this.getKey('SQL_QUERY_RESULT', getCacheHash(cacheKey) as any);
995993
}
996994

packages/cubejs-query-orchestrator/src/orchestrator/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import crypto from 'crypto';
22

33
import { getProcessUid } from '@cubejs-backend/shared';
44
import { QueryKey, QueryKeyHash } from '@cubejs-backend/base-driver';
5+
import { CacheKey } from './QueryCache';
56

67
/**
78
* Unique process ID regexp.
@@ -11,13 +12,13 @@ export const processUidRE = /^[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-
1112
/**
1213
* Returns query hash by specified `queryKey`.
1314
*/
14-
export function getCacheHash(queryKey: QueryKey, processUid?: string): QueryKeyHash {
15+
export function getCacheHash(queryKey: QueryKey | CacheKey, processUid?: string): QueryKeyHash {
1516
processUid = processUid || getProcessUid();
1617
if (typeof queryKey === 'string' && queryKey.length < 256) {
1718
return queryKey as any;
1819
}
1920

20-
if (typeof queryKey === 'object' && queryKey.persistent) {
21+
if (typeof queryKey === 'object' && 'persistent' in queryKey && queryKey.persistent) {
2122
return `${crypto
2223
.createHash('md5')
2324
.update(JSON.stringify(queryKey))

0 commit comments

Comments
 (0)