Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

Commit

Permalink
fix: Integrate the SQL cache and collect performance data
Browse files Browse the repository at this point in the history
  • Loading branch information
kgilpin committed Feb 4, 2022
1 parent 691c051 commit b0d393b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
10 changes: 4 additions & 6 deletions src/appMapIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { AppMap, sqlWarning, normalizeSQL, parseSQL, Event } from '@appland/mode
import { QueryAST } from './types';
import LRUCache from 'lru-cache';

const NormalizedSQLBySQLString = new LRUCache<string, string>({ max: 2000 });
const ASTBySQLString = new LRUCache<string, QueryAST>({ max: 2000 });
const NormalizedSQLBySQLString = new LRUCache<string, string>({ max: 10000 });
const ASTBySQLString = new LRUCache<string, QueryAST>({ max: 1000 });

export default class AppMapIndex {
constructor(public appMap: AppMap) {}
Expand All @@ -18,11 +18,9 @@ export default class AppMapIndex {
ast = parseSQL(sql);
} catch {
sqlWarning(`Unable to parse query: ${sql}`);
ast = [] as any as QueryAST;
}
if (ast) {
ASTBySQLString.set(sql, ast);
}
ast ||= [] as any as QueryAST;
ASTBySQLString.set(sql, ast);
}
return ast;
}
Expand Down
7 changes: 2 additions & 5 deletions src/database/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { EventNavigator, Event } from '@appland/models';
import { visit } from './visit';
import { Event } from '@appland/models';
import { AppMapIndex, EventFilter, QueryAST } from '../types';
import { URL } from 'url';
import { EventNavigator } from '@appland/models';
import appMap from '@appland/client/dist/src/appMap';
import { normalizeSQL } from '@appland/models';

export interface SQLEvent {
sql: string;
Expand Down Expand Up @@ -150,7 +147,7 @@ export function* sqlStrings(
continue;
}

const sql = normalizeSQL(e.event.sql.sql, e.event.sql.database_type);
const sql = appMapIndex.sqlNormalized(e.event);

yield { event: e.event, sql };
}
Expand Down
10 changes: 5 additions & 5 deletions src/rules/unbatchedMaterializedQuery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseSQL, Event } from '@appland/models';
import { Rule, RuleLogic } from '../types';
import { Event } from '@appland/models';
import { AppMapIndex, Rule, RuleLogic } from '../types';
import { visit } from '../database/visit';
import { URL } from 'url';
import parseRuleDescription from './lib/parseRuleDescription';
Expand All @@ -8,9 +8,9 @@ function isMaterialized(e: Event): boolean {
return e.ancestors().some(({ labels }) => labels.has(DAOMaterialize));
}

function isApplicable(e: Event): boolean {
function isApplicable(e: Event, appMapIndex: AppMapIndex): boolean {
try {
const ast = parseSQL(e.sqlQuery!);
const ast = appMapIndex.sqlAST(e);
let isSelect = false;
let isCount = false;
let hasLimitClause = false;
Expand Down Expand Up @@ -55,7 +55,7 @@ function isApplicable(e: Event): boolean {

function build(): RuleLogic {
return {
matcher: (e) => isApplicable(e),
matcher: (e, appMapIndex: AppMapIndex) => isApplicable(e, appMapIndex),
where: (e) => !!e.sqlQuery,
};
}
Expand Down
1 change: 1 addition & 0 deletions src/scope/sqlTransactionScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function iterateTransaction(
if (!event.isCall()) continue;
transaction.push(event);
if (!event.sql) continue;
// TODO: This should be routing through the AppMapIndex AST cache.
const sql = parseSQL(event.sql.sql);
if (!sql) continue;
if (isBegin(sql)) throw new Error('Transaction started within a transaction.');
Expand Down

0 comments on commit b0d393b

Please sign in to comment.