Skip to content

Commit

Permalink
adding limit to query automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
deniswsrosa committed Apr 23, 2024
1 parent f44c4bc commit feda039
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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<Key<?>, Object> data = new HashMap<>();
Expand All @@ -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();
}
Expand All @@ -83,7 +83,7 @@ public void init() {

buildToolbar();
panel.add(queryEditor.getComponent(), BorderLayout.CENTER);
queryEditor.getContentComponent().requestFocusInWindow();
queryEditor.getComponent().requestFocusInWindow();
component = panel;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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<String> getIndexByBucket(String bucketName) {
return ActiveCluster.getInstance().get().searchIndexes()
.getAllIndexes().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,27 @@ public static Boolean executeQuery(BlockingQueue<Boolean> 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<String> futureResult = null;
CompletableFuture<String> futureResult;

if (indexName.contains(".")) {
String[] split = indexName.split("\\.");
Expand Down Expand Up @@ -101,15 +118,13 @@ public static Boolean executeQuery(BlockingQueue<Boolean> 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);
}

Expand Down

0 comments on commit feda039

Please sign in to comment.