-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
209 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
MatCalcitePlugin/src/com/github/vlsi/mat/calcite/cli/DumpFileNameCompleter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.github.vlsi.mat.calcite.cli; | ||
|
||
import org.jline.builtins.Completers; | ||
|
||
import java.nio.file.FileSystems; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.PathMatcher; | ||
|
||
public class DumpFileNameCompleter extends Completers.FileNameCompleter { | ||
private static final PathMatcher DUMP_FILE_MATCHER = | ||
FileSystems.getDefault().getPathMatcher("glob:**.{bin,hprof}"); | ||
|
||
@Override | ||
protected boolean accept(Path path) { | ||
return super.accept(path) | ||
&& (Files.isDirectory(path) || DUMP_FILE_MATCHER.matches(path)); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
MatCalcitePlugin/src/com/github/vlsi/mat/calcite/cli/MCPApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.github.vlsi.mat.calcite.cli; | ||
|
||
import sqlline.Application; | ||
import sqlline.CommandHandler; | ||
import sqlline.MCPSqlLine; | ||
import sqlline.SqlLine; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class MCPApplication extends Application { | ||
private static final Set<String> ALLOWED_SQLLINE_COMMANDS = new HashSet<String>() {{ | ||
add("quit"); | ||
add("history"); | ||
add("verbose"); | ||
add("run"); | ||
add("list"); | ||
add("all"); | ||
add("go"); | ||
add("script"); | ||
add("record"); | ||
add("brief"); | ||
add("close"); | ||
add("closeall"); | ||
add("outputformat"); | ||
add("set"); | ||
add("help"); | ||
add("reset"); | ||
add("save"); | ||
add("rerun"); | ||
add("prompthandler"); | ||
}}; | ||
|
||
@Override | ||
public Collection<CommandHandler> getCommandHandlers(SqlLine sqlLine) { | ||
Collection<CommandHandler> handlers = new ArrayList<>(); | ||
for (CommandHandler commandHandler : super.getCommandHandlers(sqlLine)) { | ||
if (ALLOWED_SQLLINE_COMMANDS.contains(commandHandler.getName())) { | ||
handlers.add(commandHandler); | ||
} | ||
} | ||
handlers.add(new OpenDumpCommandHandler((MCPSqlLine) sqlLine, new DumpFileNameCompleter(), "Open heap dump", | ||
"open_dump")); | ||
return handlers; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
MatCalcitePlugin/src/com/github/vlsi/mat/calcite/cli/OpenDumpCommandHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.github.vlsi.mat.calcite.cli; | ||
|
||
import org.eclipse.mat.SnapshotException; | ||
import org.eclipse.mat.snapshot.ISnapshot; | ||
import org.eclipse.mat.snapshot.SnapshotFactory; | ||
import org.eclipse.mat.util.VoidProgressListener; | ||
import org.jline.reader.Completer; | ||
import sqlline.AbstractCommandHandler; | ||
import sqlline.Commands; | ||
import sqlline.DispatchCallback; | ||
import sqlline.MCPDatabaseConnection; | ||
import sqlline.MCPSqlLine; | ||
|
||
import java.io.File; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class OpenDumpCommandHandler extends AbstractCommandHandler { | ||
public OpenDumpCommandHandler(MCPSqlLine sqlLine, List<Completer> completers, | ||
String helpText, String... cmds) { | ||
super(sqlLine, cmds, helpText, completers); | ||
} | ||
|
||
public OpenDumpCommandHandler(MCPSqlLine sqlLine, Completer completer, | ||
String helpText, String... cmds) { | ||
this(sqlLine, Collections.singletonList(completer), helpText, cmds); | ||
} | ||
|
||
public void execute(String line, DispatchCallback callback) { | ||
final String[] parts = sqlLine.split(line, " ", 0); | ||
if (parts.length != 2) { | ||
sqlLine.error("Usage: open_dump <path to file>"); | ||
callback.setToFailure(); | ||
return; | ||
} | ||
// replace ~/ with user directory | ||
final String filename = Commands.expand(parts[1]); | ||
if (!new File(filename).isFile()) { | ||
sqlLine.error("Dump file " + filename + " does not exists!"); | ||
callback.setToFailure(); | ||
return; | ||
} | ||
|
||
final MCPSqlLine mcpSqlLine = (MCPSqlLine) sqlLine; | ||
final MCPDatabaseConnection mcpDatabaseConnection; | ||
try { | ||
mcpDatabaseConnection = new MCPDatabaseConnection(mcpSqlLine, filename, openSnapshot(new File(filename))); | ||
} catch (SnapshotException e) { | ||
callback.setToFailure(); | ||
mcpSqlLine.error(e); | ||
return; | ||
} | ||
try { | ||
mcpSqlLine.setUpConnection(mcpDatabaseConnection); | ||
callback.setToSuccess(); | ||
} catch (Exception e) { | ||
mcpDatabaseConnection.close(); | ||
mcpSqlLine.removeConnection(mcpDatabaseConnection); | ||
callback.setToFailure(); | ||
mcpSqlLine.error(e); | ||
} | ||
} | ||
|
||
private static ISnapshot openSnapshot(File heapDump) throws SnapshotException { | ||
System.out.println("exists = " + heapDump.exists() + ", file = " + heapDump.getAbsolutePath()); | ||
return SnapshotFactory.openSnapshot(heapDump, new VoidProgressListener()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package sqlline; | ||
|
||
import com.github.vlsi.mat.calcite.CalciteDataSource; | ||
|
||
import org.eclipse.mat.snapshot.ISnapshot; | ||
|
||
import java.sql.SQLException; | ||
|
||
public class MCPDatabaseConnection extends DatabaseConnection { | ||
private final ISnapshot snapshot; | ||
|
||
public MCPDatabaseConnection(MCPSqlLine sqlLine, String filename, ISnapshot snapshot) { | ||
super(sqlLine, null, filename, "username", "password", null); | ||
this.snapshot = snapshot; | ||
} | ||
|
||
@Override | ||
boolean connect() throws SQLException { | ||
connection = CalciteDataSource.getConnection(snapshot); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package sqlline; | ||
|
||
import com.github.vlsi.mat.calcite.cli.MCPApplication; | ||
|
||
import java.lang.reflect.Proxy; | ||
import java.sql.Connection; | ||
import java.sql.DatabaseMetaData; | ||
import java.sql.SQLException; | ||
|
||
public class MCPSqlLine extends SqlLine { | ||
void setAppConfig(Application application) { | ||
super.setAppConfig(new MCPApplication()); | ||
} | ||
|
||
public void setUpConnection(final MCPDatabaseConnection mcpDatabaseConnection) throws SQLException { | ||
getDatabaseConnections().setConnection(mcpDatabaseConnection); | ||
Connection connection = getDatabaseConnection().getConnection(); | ||
mcpDatabaseConnection.meta = (DatabaseMetaData) Proxy.newProxyInstance( | ||
DatabaseMetaData.class.getClassLoader(), | ||
new Class[]{DatabaseMetaData.class}, | ||
new DatabaseMetaDataHandler(connection.getMetaData())); | ||
} | ||
|
||
public void removeConnection(final MCPDatabaseConnection mcpDatabaseConnection) { | ||
getDatabaseConnections().removeConnection(mcpDatabaseConnection); | ||
} | ||
} |