From feda0393ad77f6fdc01991b500f76934f0c1aa3e Mon Sep 17 00:00:00 2001 From: Denis Rosa Date: Tue, 23 Apr 2024 09:54:02 +0200 Subject: [PATCH] adding limit to query automatically --- .../searchworkbench/SearchFileEditor.java | 51 ++++--------------- .../searchworkbench/SearchQueryExecutor.java | 25 +++++++-- 2 files changed, 31 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/couchbase/intellij/searchworkbench/SearchFileEditor.java b/src/main/java/com/couchbase/intellij/searchworkbench/SearchFileEditor.java index afbd17a3..04f4378b 100644 --- a/src/main/java/com/couchbase/intellij/searchworkbench/SearchFileEditor.java +++ b/src/main/java/com/couchbase/intellij/searchworkbench/SearchFileEditor.java @@ -4,7 +4,6 @@ import com.couchbase.intellij.database.ActiveCluster; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.EditorFactory; import com.intellij.openapi.fileEditor.*; @@ -34,7 +33,6 @@ import java.util.stream.Collectors; public class SearchFileEditor implements FileEditor, TextEditor { - private final EditorWrapper queryEditor; private final VirtualFile file; private final Project project; private final Map, Object> data = new HashMap<>(); @@ -57,12 +55,14 @@ public class SearchFileEditor implements FileEditor, TextEditor { private String selectedIdx; + private TextEditor queryEditor; + SearchFileEditor(Project project, VirtualFile file, String selectedBucket, String selectedIdx) { this.file = file; this.project = project; this.selectedBucket = selectedBucket; this.selectedIdx = selectedIdx; - this.queryEditor = new EditorWrapper(null, (TextEditor) TextEditorProvider.getInstance().createEditor(project, file)); + this.queryEditor = (TextEditor) TextEditorProvider.getInstance().createEditor(project, file); this.panel = new JPanel(new BorderLayout()); init(); } @@ -83,7 +83,7 @@ public void init() { buildToolbar(); panel.add(queryEditor.getComponent(), BorderLayout.CENTER); - queryEditor.getContentComponent().requestFocusInWindow(); + queryEditor.getComponent().requestFocusInWindow(); component = panel; } @@ -131,7 +131,7 @@ public void run(@NotNull ProgressIndicator indicator) { boolean query = SearchQueryExecutor.executeQuery(queryExecutionChannel, bucketCombo.getSelectedItem() == null ? null : bucketCombo.getSelectedItem().toString(), idxCombo.getSelectedItem() == null ? null : idxCombo.getSelectedItem().toString(), - queryEditor.getDocument().getText(), + queryEditor.getEditor().getDocument().getText(), project); executeGroup.replaceAction(cancelAction, executeAction); isExecutingQuery = false; @@ -213,7 +213,7 @@ public void run(@NotNull ProgressIndicator indicator) { panel.add(topPanel, BorderLayout.NORTH); - Runnable newConnectionListener = () -> { + newConnectionListener = () -> { bucketCombo.removeAllItems(); bucketCombo.removeActionListener(bucketComboListener); ActiveCluster.getInstance().get().buckets().getAllBuckets().keySet().forEach(bucketCombo::addItem); @@ -250,7 +250,10 @@ public void dispose() { if (newConnectionListener != null) { ActiveCluster.getInstance().deregisterNewConnectionListener(newConnectionListener); } - queryEditor.release(); + + if (queryEditor != null && queryEditor.getEditor() != null) { + EditorFactory.getInstance().releaseEditor(queryEditor.getEditor()); + } } @Override @@ -318,11 +321,7 @@ private boolean isSameConnection() { @Override public @NotNull Editor getEditor() { - if (queryEditor.textEditor != null) { - return queryEditor.textEditor.getEditor(); - } else { - return queryEditor.viewer; - } + return queryEditor.getEditor(); } @Override @@ -335,34 +334,6 @@ public void navigateTo(@NotNull Navigatable navigatable) { } - static class EditorWrapper { - private final Editor viewer; - private final TextEditor textEditor; - - public EditorWrapper(Editor viewer, TextEditor textEditor) { - this.textEditor = textEditor; - this.viewer = viewer; - } - - public JComponent getComponent() { - return textEditor == null ? viewer.getComponent() : textEditor.getComponent(); - } - - public JComponent getContentComponent() { - return textEditor == null ? viewer.getContentComponent() : textEditor.getEditor().getContentComponent(); - } - - public Document getDocument() { - return textEditor == null ? viewer.getDocument() : textEditor.getEditor().getDocument(); - } - - public void release() { - EditorFactory.getInstance().releaseEditor(viewer); - - } - } - - private List getIndexByBucket(String bucketName) { return ActiveCluster.getInstance().get().searchIndexes() .getAllIndexes().stream() diff --git a/src/main/java/com/couchbase/intellij/searchworkbench/SearchQueryExecutor.java b/src/main/java/com/couchbase/intellij/searchworkbench/SearchQueryExecutor.java index 27e81f12..38fda1d6 100644 --- a/src/main/java/com/couchbase/intellij/searchworkbench/SearchQueryExecutor.java +++ b/src/main/java/com/couchbase/intellij/searchworkbench/SearchQueryExecutor.java @@ -51,10 +51,27 @@ public static Boolean executeQuery(BlockingQueue queue, String bucket, getOutputWindow(project).setStatusAsLoading(); + long start = 0; + //automatically add the limit to the query try { + JsonObject queryJson = JsonObject.fromJson(query); + if (!queryJson.containsKey("size") && !queryJson.containsKey("limit")) { + queryJson.put("size", ActiveCluster.getInstance().getQueryLimit()); + } + query = queryJson.toString(); + } catch (Exception e) { + long end = System.currentTimeMillis(); + getOutputWindow(project).updateQueryStats(Arrays.asList((end - start) + " MS", "-", "-", "-", "-", "-"), null, CouchbaseQueryErrorUtil.parseQueryError("The query is not a valid JSON"), null, false); + return false; + } + + + try { + + start = System.currentTimeMillis(); - CompletableFuture futureResult = null; + CompletableFuture futureResult; if (indexName.contains(".")) { String[] split = indexName.split("\\."); @@ -101,15 +118,13 @@ public static Boolean executeQuery(BlockingQueue queue, String bucket, metricsList.add("-"); metricsList.add("-"); - getOutputWindow(project).updateQueryStats(Arrays.asList((end - start) + " MS", "-", "-", "-", "-", "-"), null, - CouchbaseQueryErrorUtil.parseQueryError(jsonObject.getString("error")), null, false); + getOutputWindow(project).updateQueryStats(Arrays.asList((end - start) + " MS", "-", "-", "-", "-", "-"), null, CouchbaseQueryErrorUtil.parseQueryError(jsonObject.getString("error")), null, false); } } catch (Exception e) { long end = System.currentTimeMillis(); - getOutputWindow(project).updateQueryStats(Arrays.asList((end - start) + " MS", "-", "-", "-", "-", "-"), null, - CouchbaseQueryErrorUtil.parseQueryError("An error occurred while executing the query: " + e.getMessage()), null, false); + getOutputWindow(project).updateQueryStats(Arrays.asList((end - start) + " MS", "-", "-", "-", "-", "-"), null, CouchbaseQueryErrorUtil.parseQueryError("An error occurred while executing the query: " + e.getMessage()), null, false); Log.error(e); }