Skip to content

Commit 4f836bc

Browse files
committed
dev
1 parent 201bb76 commit 4f836bc

File tree

1 file changed

+9
-53
lines changed

1 file changed

+9
-53
lines changed

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

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,57 +1042,39 @@ export class QueryCache {
10421042
return this.cacheDriver.testConnection();
10431043
}
10441044

1045-
/**
1046-
* Creates a compact, deterministic hash for arrays of schemas, tables, or columns.
1047-
* This avoids performance issues with JSON.stringify on large arrays.
1048-
*/
10491045
private createMetadataHash(items: any[]): string {
10501046
if (!items || items.length === 0) {
10511047
return 'empty';
10521048
}
10531049

1054-
// Sort items to ensure deterministic hashing regardless of input order
10551050
const sortedKeys = items
10561051
.map(item => {
10571052
if (item.schema_name && item.table_name) {
1058-
// For tables: "schema.table"
10591053
return `${item.schema_name}.${item.table_name}`;
10601054
} else if (item.schema_name && item.column_name) {
1061-
// For columns: "schema.table.column"
10621055
return `${item.schema_name}.${item.table_name}.${item.column_name}`;
10631056
} else if (item.schema_name) {
1064-
// For schemas: just the schema name
10651057
return item.schema_name;
10661058
}
1067-
return JSON.stringify(item); // fallback for unknown structures
1059+
return JSON.stringify(item);
10681060
})
1069-
.sort(); // Sort for deterministic results
1061+
.sort();
10701062

1071-
// Create a short hash using Node.js crypto
10721063
return crypto
10731064
.createHash('sha256')
10741065
.update(sortedKeys.join('|'))
10751066
.digest('hex')
1076-
.substring(0, 16); // Use first 16 chars for shorter keys
1067+
.substring(0, 16);
10771068
}
10781069

1079-
/**
1080-
* Creates a metadata query string for use with cacheQueryResult.
1081-
* This allows metadata operations to leverage the existing caching infrastructure.
1082-
* Uses hash-based parameters for efficiency but keeps original params for execution.
1083-
*/
10841070
private createMetadataQuery(operation: string, params: Record<string, any>): QueryWithParams {
10851071
return [
10861072
`METADATA:${operation}`,
1087-
[JSON.stringify(params)], // Keep original params for execution, cache key uses hash
1088-
{ external: false, renewalThreshold: 24 * 60 * 60 } // 24 hours default
1073+
[JSON.stringify(params)],
1074+
{ external: false, renewalThreshold: 24 * 60 * 60 }
10891075
];
10901076
}
10911077

1092-
/**
1093-
* Generic method for caching datasource metadata using existing cacheQueryResult infrastructure.
1094-
* This leverages renewal keys, in-memory caching, background renewal, and observability.
1095-
*/
10961078
private async queryDataSourceMetadata<T>(
10971079
operation: string,
10981080
params: Record<string, any>,
@@ -1107,35 +1089,30 @@ export class QueryCache {
11071089
const {
11081090
requestId,
11091091
forceRefresh = false,
1110-
renewalThreshold = 24 * 60 * 60, // 24 hours default
1111-
expiration = 7 * 24 * 60 * 60, // 7 days default
1092+
renewalThreshold = 24 * 60 * 60,
1093+
expiration = 7 * 24 * 60 * 60,
11121094
} = options;
11131095

1114-
// Create a compact hash for the cache key
11151096
let paramsHash = 'empty';
11161097
if (params.schemas) {
11171098
paramsHash = this.createMetadataHash(params.schemas);
11181099
} else if (params.tables) {
11191100
paramsHash = this.createMetadataHash(params.tables);
11201101
}
11211102

1122-
// Create a query-like structure for metadata operations with original params
11231103
const metadataQuery = this.createMetadataQuery(operation, params);
1124-
// Use hash in cache key for efficiency
11251104
const cacheKey: CacheKey = [`METADATA:${operation}`, paramsHash, dataSource];
11261105

1127-
// For metadata operations, we use a simpler renewal approach
1128-
// Time-based renewal key that changes every renewal period
11291106
const renewalKey = forceRefresh ? undefined : [
11301107
`METADATA_RENEWAL:${operation}`,
11311108
paramsHash,
11321109
dataSource,
1133-
Math.floor(Date.now() / (renewalThreshold * 1000)) // Time bucket for renewal
1110+
Math.floor(Date.now() / (renewalThreshold * 1000))
11341111
];
11351112

11361113
return this.cacheQueryResult(
11371114
metadataQuery,
1138-
[], // No SQL parameters for metadata queries
1115+
[],
11391116
cacheKey,
11401117
expiration,
11411118
{
@@ -1150,10 +1127,6 @@ export class QueryCache {
11501127
);
11511128
}
11521129

1153-
/**
1154-
* Queries datasource schema with caching and queue support.
1155-
* Returns list of schemas available in the datasource.
1156-
*/
11571130
public async queryDataSourceSchema(
11581131
dataSource: string = 'default',
11591132
options: {
@@ -1171,10 +1144,6 @@ export class QueryCache {
11711144
);
11721145
}
11731146

1174-
/**
1175-
* Queries tables for specific schemas with caching and queue support.
1176-
* Returns list of tables for the given schemas.
1177-
*/
11781147
public async queryTablesForSchemas(
11791148
schemas: QuerySchemasResult[],
11801149
dataSource: string = 'default',
@@ -1193,10 +1162,6 @@ export class QueryCache {
11931162
);
11941163
}
11951164

1196-
/**
1197-
* Queries columns for specific tables with caching and queue support.
1198-
* Returns list of columns for the given tables.
1199-
*/
12001165
public async queryColumnsForTables(
12011166
tables: QueryTablesResult[],
12021167
dataSource: string = 'default',
@@ -1215,19 +1180,13 @@ export class QueryCache {
12151180
);
12161181
}
12171182

1218-
/**
1219-
* Clears the cached datasource schema for a specific datasource.
1220-
*/
12211183
public async clearDataSourceSchemaCache(dataSource: string = 'default') {
12221184
const cacheKey: CacheKey = ['METADATA:GET_SCHEMAS', 'empty', dataSource];
12231185
const redisKey = this.queryRedisKey(cacheKey);
12241186
await this.cacheDriver.remove(redisKey);
12251187
this.logger('Cleared datasource schema cache', { dataSource });
12261188
}
12271189

1228-
/**
1229-
* Clears the cached tables for specific schemas.
1230-
*/
12311190
public async clearTablesForSchemasCache(
12321191
schemas: QuerySchemasResult[],
12331192
dataSource: string = 'default'
@@ -1243,9 +1202,6 @@ export class QueryCache {
12431202
});
12441203
}
12451204

1246-
/**
1247-
* Clears the cached columns for specific tables.
1248-
*/
12491205
public async clearColumnsForTablesCache(
12501206
tables: QueryTablesResult[],
12511207
dataSource: string = 'default'

0 commit comments

Comments
 (0)