-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathappMapIndex.ts
39 lines (33 loc) · 1.26 KB
/
appMapIndex.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { AppMap, normalizeSQL, parseSQL, Event } from '@appland/models';
import { QueryAST } from './types';
import LRUCache from 'lru-cache';
const NormalizedSQLBySQLString = new LRUCache<string, string>({ max: 10000 });
const ASTBySQLString = new LRUCache<string, QueryAST | 'parse-error'>({ max: 1000 });
export default class AppMapIndex {
constructor(public appMap: AppMap) {}
sqlAST(event: Event): QueryAST | undefined {
if (!event.sql) throw new Error(`${event.fqid} is not a SQL query`);
const sql = this.sqlNormalized(event);
let result: QueryAST | undefined;
const cachedAST = ASTBySQLString.get(sql);
if (cachedAST === 'parse-error') {
result = undefined;
} else if (cachedAST) {
result = cachedAST;
} else {
result = parseSQL(sql);
ASTBySQLString.set(sql, result ? result : 'parse-error');
}
return result;
}
sqlNormalized(event: Event): string {
if (!event.sql) throw new Error(`${event.fqid} is not a SQL query`);
const cacheKey = [event.sql.database_type, event.sql.sql].join(':');
let sql = NormalizedSQLBySQLString.get(cacheKey);
if (!sql) {
sql = normalizeSQL(event.sql.sql, event.sql.database_type);
NormalizedSQLBySQLString.set(cacheKey, sql);
}
return sql;
}
}