Skip to content

Commit

Permalink
make autocomplete for large schemas faster (re getredash#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Short committed Dec 12, 2017
1 parent 7037603 commit 6c3ef66
Showing 1 changed file with 46 additions and 34 deletions.
80 changes: 46 additions & 34 deletions client/app/components/queries/query-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,49 @@ function queryEditor(QuerySnippet) {
pre($scope) {
$scope.syntax = $scope.syntax || 'sql';

const schemaCompleter = {
getCompletions(state, session, pos, prefix, callback) {
// make a variable for the auto completion in the query editor
$scope.autoCompleteSchema = $scope.schema; // removeExtraSchemaInfo(

if (prefix.length === 0 || !$scope.autoCompleteSchema) {
callback(null, []);
return;
}

if (!$scope.autoCompleteSchema.keywords) {
const keywords = {};

$scope.autoCompleteSchema.forEach((table) => {
keywords[table.name] = 'Table';

table.columns.forEach((c) => { // autoCompleteColumns
if (c.charAt(c.length - 1) === ')') {
let parensStartAt = c.indexOf('(') - 1;
c = c.substring(0, parensStartAt);
parensStartAt = 1; // linter complains without this line
}
// remove '[P] ' for partition keys
if (c.charAt(0) === '[') {
c = c.substring(4, c.length);
}
// keywords[c] = 'Column'; // dups columns
keywords[`${table.name}.${c}`] = 'Column';
});
});

$scope.autoCompleteSchema.keywords = map(keywords, (v, k) =>
({
name: k,
value: k,
score: 0,
meta: v,
}));
}
callback(null, $scope.autoCompleteSchema.keywords);
},
};

$scope.editorOptions = {
mode: 'json',
// require: ['ace/ext/language_tools'],
Expand Down Expand Up @@ -70,16 +113,18 @@ function queryEditor(QuerySnippet) {
editor.getSession().setMode(newMode);
});

$scope.$watch('schema', (newSchema, oldSchema) => {
$scope.$watch('autoCompleteSchema', (newSchema, oldSchema) => {
if (newSchema !== oldSchema) {
const tokensCount =
newSchema.reduce((totalLength, table) => totalLength + table.columns.length, 0);
// If there are too many tokens we disable live autocomplete,
// as it makes typing slower.
if (tokensCount > 5000) {
editor.setOption('enableLiveAutocompletion', false);
editor.setOption('enableBasicAutocompletion', false);
} else {
editor.setOption('enableLiveAutocompletion', true);
editor.setOption('enableBasicAutocompletion', true);
}
}
});
Expand All @@ -92,39 +137,6 @@ function queryEditor(QuerySnippet) {
},
};


const schemaCompleter = {
getCompletions(state, session, pos, prefix, callback) {
if (prefix.length === 0 || !$scope.schema) {
callback(null, []);
return;
}

if (!$scope.schema.keywords) {
const keywords = {};

$scope.schema.forEach((table) => {
keywords[table.name] = 'Table';

table.columns.forEach((c) => {
keywords[c] = 'Column';
keywords[`${table.name}.${c}`] = 'Column';
});
});

$scope.schema.keywords = map(keywords, (v, k) =>
({
name: k,
value: k,
score: 0,
meta: v,
}));
}
callback(null, $scope.schema.keywords);
},
};


window.ace.acequire(['ace/ext/language_tools'], (langTools) => {
langTools.addCompleter(schemaCompleter);
});
Expand Down

0 comments on commit 6c3ef66

Please sign in to comment.