├── .gitignore
├── src
└── main
│ └── java
│ └── org
│ └── apache
│ └── cassandra
│ ├── unit
│ ├── Unit.java
│ ├── ColumnFamilyMetaData.java
│ ├── SColumn.java
│ ├── Cell.java
│ ├── Key.java
│ └── ColumnFamily.java
│ ├── gui
│ ├── control
│ │ └── callback
│ │ │ ├── RepaintCallback.java
│ │ │ ├── PropertiesCallback.java
│ │ │ └── SelectedColumnFamilyCallback.java
│ └── component
│ │ ├── dialog
│ │ ├── listener
│ │ │ └── WindowCloseedListener.java
│ │ ├── NodeInfoDialog.java
│ │ ├── TpstatsDialog.java
│ │ ├── KeyDialog.java
│ │ ├── KeyRangeDialog.java
│ │ ├── ConnectionDialog.java
│ │ ├── KeyspaceDialog.java
│ │ ├── CellPropertiesDialog.java
│ │ ├── ColumnFamilyMetaDataDialog.java
│ │ ├── RingDialog.java
│ │ ├── action
│ │ │ └── ColumnPopupAction.java
│ │ └── ColumnFamilyDialog.java
│ │ ├── model
│ │ └── ColumnFamilyMetaDataModel.java
│ │ └── panel
│ │ ├── ColumnTreePanel.java
│ │ ├── PropertiesPanel.java
│ │ └── KeyspaceTreePanel.java
│ ├── node
│ ├── Tpstats.java
│ ├── RingNode.java
│ ├── NodeInfo.java
│ └── TreeNode.java
│ ├── CassandraGUI.java
│ └── client
│ └── Client.java
├── .project
├── README
├── nbactions.xml
└── pom.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | .classpath
2 | bin/
3 | lib/
4 | target/
5 | .settings/
6 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/unit/Unit.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.unit;
2 |
3 | public interface Unit {
4 | }
5 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/control/callback/RepaintCallback.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.control.callback;
2 |
3 | import java.awt.Dimension;
4 |
5 | public interface RepaintCallback {
6 | public Dimension callback();
7 | }
8 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/control/callback/PropertiesCallback.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.control.callback;
2 |
3 | public interface PropertiesCallback {
4 | public void clusterCallback();
5 | public void keyspaceCallback(String keyspace);
6 | public void columnFamilyCallback(String keyspace, String columnFamily);
7 | }
8 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | cassandra-gui
4 | NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/control/callback/SelectedColumnFamilyCallback.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.control.callback;
2 |
3 | public interface SelectedColumnFamilyCallback {
4 | public void rangeCallback(String keyspaceName,
5 | String columnFamilyName,
6 | String startKey,
7 | String endKey,
8 | int rows);
9 |
10 | public void getCacllback(String keyspace,
11 | String columnFamily,
12 | String key);
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/component/dialog/listener/WindowCloseedListener.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.component.dialog.listener;
2 |
3 | import java.awt.event.WindowEvent;
4 | import java.awt.event.WindowListener;
5 |
6 | public abstract class WindowCloseedListener implements WindowListener {
7 |
8 | @Override
9 | public void windowOpened(WindowEvent e) {
10 | }
11 |
12 | @Override
13 | public void windowClosing(WindowEvent e) {
14 | closing();
15 | }
16 |
17 | @Override
18 | public void windowClosed(WindowEvent e) {
19 | }
20 |
21 | @Override
22 | public void windowIconified(WindowEvent e) {
23 | }
24 |
25 | @Override
26 | public void windowDeiconified(WindowEvent e) {
27 | }
28 |
29 | @Override
30 | public void windowActivated(WindowEvent e) {
31 | }
32 |
33 | @Override
34 | public void windowDeactivated(WindowEvent e) {
35 | }
36 |
37 | public abstract void closing();
38 | }
39 |
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
1 | # cassandra-gui
2 |
3 | a java GUI client for browsing/editing Cassandra - no high level libraries (e.g. Hector) are used, just cassandra + thrift
4 |
5 | I found the source as a google code project (http://code.google.com/a/apache-extras.org/p/cassandra-gui/) which supported Cassandra 0.7. Worked, but limited, assumed all types as String, so most browsing or updating would fail.
6 |
7 | I needed a tool to browse and add/edit test data, so my initial updates were to get to Cassandra 0.8, and to get updates to work correctly. But then I added support to obey validation types when working with the popups to accept or display values. And more recently, added support for Composite types. See Client::getAsBytes/getAsString for details.
8 |
9 | I was using git to track local changes, and attempted to contact original author/google group to contribute. So, just decided to push here for now.
10 |
11 | ## Building
12 |
13 | Maven was used in the original project, but I've been doing all my work in eclipse, so the pom may be out of date or just plain wrong. working on it.
14 |
15 | ## Usage
16 |
17 | When connected to Cassandra, viewer shows Keyspaces in left pane, open desired keyspace and right click on column family name to get choices. Properties appear in upper panel, data in lower, more context menus in data panel.
18 |
19 | ## Short TODO List
20 |
21 | -Support counter columns
22 | -Support index queries
23 |
24 |
25 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/unit/ColumnFamilyMetaData.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.unit;
2 |
3 | import org.apache.cassandra.thrift.IndexType;
4 |
5 | public class ColumnFamilyMetaData {
6 | private String columnName;
7 | private String valiDationClass;
8 | private IndexType indexType;
9 | private String indexName;
10 |
11 | /**
12 | * @return the columnName
13 | */
14 | public String getColumnName() {
15 | return columnName;
16 | }
17 |
18 | /**
19 | * @param columnName the columnName to set
20 | */
21 | public void setColumnName(String columnName) {
22 | this.columnName = columnName;
23 | }
24 |
25 | /**
26 | * @return the valiDationClass
27 | */
28 | public String getValiDationClass() {
29 | return valiDationClass;
30 | }
31 |
32 | /**
33 | * @param valiDationClass the valiDationClass to set
34 | */
35 | public void setValiDationClass(String valiDationClass) {
36 | this.valiDationClass = valiDationClass;
37 | }
38 |
39 | /**
40 | * @return the indexType
41 | */
42 | public IndexType getIndexType() {
43 | return indexType;
44 | }
45 |
46 | /**
47 | * @param indexType the indexType to set
48 | */
49 | public void setIndexType(IndexType indexType) {
50 | this.indexType = indexType;
51 | }
52 |
53 | /**
54 | * @return the indexName
55 | */
56 | public String getIndexName() {
57 | return indexName;
58 | }
59 |
60 | /**
61 | * @param indexName the indexName to set
62 | */
63 | public void setIndexName(String indexName) {
64 | this.indexName = indexName;
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/node/Tpstats.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.node;
2 |
3 | import java.io.Serializable;
4 |
5 | public class Tpstats implements Serializable {
6 | private static final long serialVersionUID = -7848179032971193937L;
7 |
8 | private String poolName;
9 | private int activeCount;
10 | private long pendingTasks;
11 | private long completedTasks;
12 |
13 | /**
14 | * @return the poolName
15 | */
16 | public String getPoolName() {
17 | return poolName;
18 | }
19 |
20 | /**
21 | * @param poolName the poolName to set
22 | */
23 | public void setPoolName(String poolName) {
24 | this.poolName = poolName;
25 | }
26 |
27 | /**
28 | * @return the activeCount
29 | */
30 | public int getActiveCount() {
31 | return activeCount;
32 | }
33 |
34 | /**
35 | * @param activeCount the activeCount to set
36 | */
37 | public void setActiveCount(int activeCount) {
38 | this.activeCount = activeCount;
39 | }
40 |
41 | /**
42 | * @return the pendingTasks
43 | */
44 | public long getPendingTasks() {
45 | return pendingTasks;
46 | }
47 |
48 | /**
49 | * @param pendingTasks the pendingTasks to set
50 | */
51 | public void setPendingTasks(long pendingTasks) {
52 | this.pendingTasks = pendingTasks;
53 | }
54 |
55 | /**
56 | * @return the completedTasks
57 | */
58 | public long getCompletedTasks() {
59 | return completedTasks;
60 | }
61 |
62 | /**
63 | * @param completedTasks the completedTasks to set
64 | */
65 | public void setCompletedTasks(long completedTasks) {
66 | this.completedTasks = completedTasks;
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/nbactions.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | run
5 |
6 | process-classes
7 | org.codehaus.mojo:exec-maven-plugin:1.2:exec
8 |
9 |
10 | -classpath %classpath org.apache.cassandra.CassandraGUI
11 | java
12 | runtime
13 |
14 |
15 |
16 | debug
17 |
18 | process-classes
19 | org.codehaus.mojo:exec-maven-plugin:1.2:exec
20 |
21 |
22 | -Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath org.apache.cassandra.CassandraGUI
23 | java
24 | runtime
25 | true
26 |
27 |
28 |
29 | profile
30 |
31 | process-classes
32 | org.codehaus.mojo:exec-maven-plugin:1.2:exec
33 |
34 |
35 | ${profiler.args} -classpath %classpath org.apache.cassandra.CassandraGUI
36 | ${profiler.java}
37 | profile
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/unit/SColumn.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.unit;
2 |
3 | import java.io.Serializable;
4 | import java.util.Map;
5 |
6 | import javax.swing.tree.DefaultMutableTreeNode;
7 |
8 | public class SColumn implements Unit, Serializable {
9 | private static final long serialVersionUID = -8041985483479505351L;
10 |
11 | private Unit parent;
12 | private String name;
13 | private DefaultMutableTreeNode treeNode;
14 | private Map cells;
15 |
16 | public SColumn() {
17 | }
18 |
19 | public SColumn(Unit parent, String name, Map cells) {
20 | this.parent = parent;
21 | this.name = name;
22 | this.cells = cells;
23 | }
24 |
25 | /**
26 | * @return the parent
27 | */
28 | public Unit getParent() {
29 | return parent;
30 | }
31 |
32 | /**
33 | * @param parent the parent to set
34 | */
35 | public void setParent(Unit parent) {
36 | this.parent = parent;
37 | }
38 |
39 | /**
40 | * @return the name
41 | */
42 | public String getName() {
43 | return name;
44 | }
45 |
46 | /**
47 | * @param name the name to set
48 | */
49 | public void setName(String name) {
50 | this.name = name;
51 | }
52 |
53 | /**
54 | * @return the treeNode
55 | */
56 | public DefaultMutableTreeNode getTreeNode() {
57 | return treeNode;
58 | }
59 |
60 | /**
61 | * @param treeNode the treeNode to set
62 | */
63 | public void setTreeNode(DefaultMutableTreeNode treeNode) {
64 | this.treeNode = treeNode;
65 | }
66 |
67 | /**
68 | * @return the cells
69 | */
70 | public Map getCells() {
71 | return cells;
72 | }
73 |
74 | /**
75 | * @param keys the keys to set
76 | */
77 | public void setCells(Map cells) {
78 | this.cells = cells;
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/node/RingNode.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.node;
2 |
3 | import java.io.Serializable;
4 | import java.util.List;
5 | import java.util.Map;
6 |
7 | import org.apache.cassandra.dht.Token;
8 |
9 | public class RingNode implements Serializable {
10 | private static final long serialVersionUID = 8351368757758010586L;
11 |
12 | private Map rangeMap;
13 | private List ranges;
14 | private List liveNodes;
15 | private List deadNodes;
16 | private Map loadMap;
17 |
18 | /**
19 | * @return the rangeMap
20 | */
21 | public Map getRangeMap() {
22 | return rangeMap;
23 | }
24 |
25 | /**
26 | * @param rangeMap the rangeMap to set
27 | */
28 | public void setRangeMap(Map rangeMap) {
29 | this.rangeMap = rangeMap;
30 | }
31 |
32 | /**
33 | * @return the ranges
34 | */
35 | public List getRanges() {
36 | return ranges;
37 | }
38 |
39 | /**
40 | * @param ranges the ranges to set
41 | */
42 | public void setRanges(List ranges) {
43 | this.ranges = ranges;
44 | }
45 |
46 | /**
47 | * @return the liveNodes
48 | */
49 | public List getLiveNodes() {
50 | return liveNodes;
51 | }
52 |
53 | /**
54 | * @param liveNodes the liveNodes to set
55 | */
56 | public void setLiveNodes(List liveNodes) {
57 | this.liveNodes = liveNodes;
58 | }
59 |
60 | /**
61 | * @return the deadNodes
62 | */
63 | public List getDeadNodes() {
64 | return deadNodes;
65 | }
66 |
67 | /**
68 | * @param deadNodes the deadNodes to set
69 | */
70 | public void setDeadNodes(List deadNodes) {
71 | this.deadNodes = deadNodes;
72 | }
73 |
74 | /**
75 | * @return the loadMap
76 | */
77 | public Map getLoadMap() {
78 | return loadMap;
79 | }
80 |
81 | /**
82 | * @param loadMap the loadMap to set
83 | */
84 | public void setLoadMap(Map loadMap) {
85 | this.loadMap = loadMap;
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/unit/Cell.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.unit;
2 |
3 | import java.io.Serializable;
4 | import java.util.Date;
5 |
6 | import javax.swing.tree.DefaultMutableTreeNode;
7 |
8 | public class Cell implements Unit, Serializable {
9 | private static final long serialVersionUID = 4517336493185234248L;
10 |
11 | private Unit parent;
12 | private String name;
13 | private String value;
14 | private Date date;
15 | private DefaultMutableTreeNode treeNode;
16 |
17 | public Cell() {
18 | }
19 |
20 | public Cell(Unit parent, String name, String value, Date date) {
21 | this.parent = parent;
22 | this.name = name;
23 | this.value = value;
24 | this.date = date;
25 | }
26 |
27 | /**
28 | * @return the parent
29 | */
30 | public Unit getParent() {
31 | return parent;
32 | }
33 |
34 | /**
35 | * @param parent the parent to set
36 | */
37 | public void setParent(Unit parent) {
38 | this.parent = parent;
39 | }
40 |
41 | /**
42 | * @return the name
43 | */
44 | public String getName() {
45 | return name;
46 | }
47 |
48 | /**
49 | * @param name the name to set
50 | */
51 | public void setName(String name) {
52 | this.name = name;
53 | }
54 |
55 | /**
56 | * @return the value
57 | */
58 | public String getValue() {
59 | return value;
60 | }
61 |
62 | /**
63 | * @param value the value to set
64 | */
65 | public void setValue(String value) {
66 | this.value = value;
67 | }
68 |
69 | /**
70 | * @return the date
71 | */
72 | public Date getDate() {
73 | return date;
74 | }
75 |
76 | /**
77 | * @param date the date to set
78 | */
79 | public void setDate(Date date) {
80 | this.date = date;
81 | }
82 |
83 | /**
84 | * @return the treeNode
85 | */
86 | public DefaultMutableTreeNode getTreeNode() {
87 | return treeNode;
88 | }
89 |
90 | /**
91 | * @param treeNode the treeNode to set
92 | */
93 | public void setTreeNode(DefaultMutableTreeNode treeNode) {
94 | this.treeNode = treeNode;
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/component/dialog/NodeInfoDialog.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.component.dialog;
2 |
3 | import java.awt.BorderLayout;
4 | import java.awt.FlowLayout;
5 | import java.awt.GridLayout;
6 | import java.awt.event.ActionEvent;
7 | import java.awt.event.ActionListener;
8 |
9 | import javax.swing.JButton;
10 | import javax.swing.JDialog;
11 | import javax.swing.JLabel;
12 | import javax.swing.JPanel;
13 | import javax.swing.JScrollPane;
14 |
15 | import org.apache.cassandra.node.NodeInfo;
16 |
17 | public class NodeInfoDialog extends JDialog {
18 | private static final long serialVersionUID = -5238189165348274251L;
19 |
20 | public NodeInfoDialog(NodeInfo nodeInfo) {
21 | JPanel inputPanel = new JPanel(new GridLayout(4, 2));
22 | inputPanel.add(new JLabel("Load: "));
23 | inputPanel.add(new JLabel(nodeInfo.getLoad()));
24 | inputPanel.add(new JLabel("Generation No: "));
25 | inputPanel.add(new JLabel(String.valueOf(nodeInfo.getGenerationNumber())));
26 | inputPanel.add(new JLabel("Uptime (seconds): "));
27 | inputPanel.add(new JLabel(String.valueOf(nodeInfo.getUptime())));
28 | inputPanel.add(new JLabel("Heap Memory (MB): "));
29 | inputPanel.add(new JLabel(String.format("%.2f", nodeInfo.getMemUsed()) + " / " +
30 | String.format("%.2f", nodeInfo.getMemMax())));
31 |
32 | JScrollPane scrollPane = new JScrollPane(inputPanel);
33 |
34 | JButton ok = new JButton("OK");
35 | ok.addActionListener(new ActionListener() {
36 | @Override
37 | public void actionPerformed(ActionEvent e) {
38 | setVisible(false);
39 | }
40 | });
41 |
42 | JPanel buttonPanel = new JPanel(new FlowLayout());
43 | buttonPanel.add(ok);
44 |
45 | JPanel panel = new JPanel(new BorderLayout());
46 | panel.add(scrollPane, BorderLayout.CENTER);
47 | panel.add(buttonPanel, BorderLayout.SOUTH);
48 |
49 | add(panel);
50 |
51 | pack();
52 | setModalityType(ModalityType.DOCUMENT_MODAL);
53 | setTitle("Node Info(" + nodeInfo.getEndpoint() + ")");
54 | setLocationRelativeTo(null);
55 | setModal(true);
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/node/NodeInfo.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.node;
2 |
3 | import java.io.Serializable;
4 |
5 | public class NodeInfo implements Serializable {
6 | private static final long serialVersionUID = -6585600091642457499L;
7 |
8 | private String endpoint;
9 | private String load;
10 | private int generationNumber;
11 | private long uptime;
12 | private double memUsed;
13 | private double memMax;
14 |
15 | /**
16 | * @return the endpoint
17 | */
18 | public String getEndpoint() {
19 | return endpoint;
20 | }
21 |
22 | /**
23 | * @param endpoint the endpoint to set
24 | */
25 | public void setEndpoint(String endpoint) {
26 | this.endpoint = endpoint;
27 | }
28 |
29 | /**
30 | * @return the load
31 | */
32 | public String getLoad() {
33 | return load;
34 | }
35 |
36 | /**
37 | * @param load the load to set
38 | */
39 | public void setLoad(String load) {
40 | this.load = load;
41 | }
42 |
43 | /**
44 | * @return the generationNumber
45 | */
46 | public int getGenerationNumber() {
47 | return generationNumber;
48 | }
49 |
50 | /**
51 | * @param generationNumber the generationNumber to set
52 | */
53 | public void setGenerationNumber(int generationNumber) {
54 | this.generationNumber = generationNumber;
55 | }
56 |
57 | /**
58 | * @return the uptime
59 | */
60 | public long getUptime() {
61 | return uptime;
62 | }
63 |
64 | /**
65 | * @param uptime the uptime to set
66 | */
67 | public void setUptime(long uptime) {
68 | this.uptime = uptime;
69 | }
70 |
71 | /**
72 | * @return the memUsed
73 | */
74 | public double getMemUsed() {
75 | return memUsed;
76 | }
77 |
78 | /**
79 | * @param memUsed the memUsed to set
80 | */
81 | public void setMemUsed(double memUsed) {
82 | this.memUsed = memUsed;
83 | }
84 |
85 | /**
86 | * @return the memMax
87 | */
88 | public double getMemMax() {
89 | return memMax;
90 | }
91 |
92 | /**
93 | * @param memMax the memMax to set
94 | */
95 | public void setMemMax(double memMax) {
96 | this.memMax = memMax;
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/component/model/ColumnFamilyMetaDataModel.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.component.model;
2 |
3 | import javax.swing.table.DefaultTableModel;
4 |
5 | import org.apache.cassandra.unit.ColumnFamilyMetaData;
6 |
7 | public class ColumnFamilyMetaDataModel extends DefaultTableModel {
8 | private static final long serialVersionUID = -1392184063849854796L;
9 |
10 | public static final int COLUMN_COLUMN_NAME = 0;
11 | public static final int COLUMN_VALIDATION_CLASS = 1;
12 | public static final int COLUMN_INDEX_TYPE = 2;
13 | public static final int COLUMN_INDEX_NAME = 3;
14 |
15 | private static final ColumnContext[] columnArray = {
16 | new ColumnContext("Column Name", String.class, true),
17 | new ColumnContext("Validation Class", String.class, true),
18 | new ColumnContext("Index Type", Integer.class, true),
19 | new ColumnContext("Index Name", String.class, true),
20 | new ColumnContext("", String.class, true)
21 | };
22 |
23 | public void add(ColumnFamilyMetaData metaData) {
24 | Object[] obj = { metaData.getColumnName(),
25 | metaData.getValiDationClass(),
26 | metaData.getIndexType(),
27 | metaData.getIndexName(),
28 | ""};
29 | super.addRow(obj);
30 | }
31 |
32 | @Override
33 | public boolean isCellEditable(int row, int column) {
34 | return columnArray[column].isEditable;
35 | }
36 |
37 | @Override
38 | public Class> getColumnClass(int columnIndex) {
39 | return columnArray[columnIndex].columnClass;
40 | }
41 |
42 | @Override
43 | public int getColumnCount() {
44 | return columnArray.length;
45 | }
46 |
47 | @Override
48 | public String getColumnName(int column) {
49 | return columnArray[column].columnName;
50 | }
51 |
52 | @SuppressWarnings("rawtypes")
53 | private static class ColumnContext {
54 | public final String columnName;
55 | public final Class columnClass;
56 | public final boolean isEditable;
57 |
58 | public ColumnContext(String columnName, Class columnClass, boolean isEditable) {
59 | this.columnName = columnName;
60 | this.columnClass = columnClass;
61 | this.isEditable = isEditable;
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/unit/Key.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.unit;
2 |
3 | import java.io.Serializable;
4 | import java.util.Map;
5 |
6 | import javax.swing.tree.DefaultMutableTreeNode;
7 |
8 | public class Key implements Unit, Serializable {
9 | private static final long serialVersionUID = 2675876416744532430L;
10 |
11 | private String name;
12 | private DefaultMutableTreeNode treeNode;
13 | private boolean superColumn;
14 | private Map sColumns;
15 | private Map cells;
16 |
17 | public Key() {
18 | }
19 |
20 | public Key(String name, Map sColumns, Map cells) {
21 | this.name = name;
22 | this.sColumns = sColumns;
23 | this.cells = cells;
24 | }
25 |
26 | /**
27 | * @return the name
28 | */
29 | public String getName() {
30 | return name;
31 | }
32 |
33 | /**
34 | * @param name the name to set
35 | */
36 | public void setName(String name) {
37 | this.name = name;
38 | }
39 |
40 | /**
41 | * @return the treeNode
42 | */
43 | public DefaultMutableTreeNode getTreeNode() {
44 | return treeNode;
45 | }
46 |
47 | /**
48 | * @param treeNode the treeNode to set
49 | */
50 | public void setTreeNode(DefaultMutableTreeNode treeNode) {
51 | this.treeNode = treeNode;
52 | }
53 |
54 | /**
55 | * @return the superColumn
56 | */
57 | public boolean isSuperColumn() {
58 | return superColumn;
59 | }
60 |
61 | /**
62 | * @param superColumn the superColumn to set
63 | */
64 | public void setSuperColumn(boolean superColumn) {
65 | this.superColumn = superColumn;
66 | }
67 |
68 | /**
69 | * @return the sColumns
70 | */
71 | public Map getSColumns() {
72 | return sColumns;
73 | }
74 |
75 | /**
76 | * @param sColumns the sColumns to set
77 | */
78 | public void setSColumns(Map sColumns) {
79 | this.sColumns = sColumns;
80 | }
81 |
82 | /**
83 | * @return the cells
84 | */
85 | public Map getCells() {
86 | return cells;
87 | }
88 |
89 | /**
90 | * @param cells the cells to set
91 | */
92 | public void setCells(Map cells) {
93 | this.cells = cells;
94 | }
95 | }
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/component/dialog/TpstatsDialog.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.component.dialog;
2 |
3 | import java.awt.BorderLayout;
4 | import java.awt.FlowLayout;
5 | import java.awt.event.ActionEvent;
6 | import java.awt.event.ActionListener;
7 | import java.util.List;
8 |
9 | import javax.swing.JButton;
10 | import javax.swing.JDialog;
11 | import javax.swing.JPanel;
12 | import javax.swing.JScrollPane;
13 | import javax.swing.JTable;
14 | import javax.swing.ListSelectionModel;
15 | import javax.swing.table.DefaultTableModel;
16 |
17 | import org.apache.cassandra.node.Tpstats;
18 |
19 | public class TpstatsDialog extends JDialog {
20 | private static final long serialVersionUID = -5287379277192919237L;
21 |
22 | private static final String[] columns = {"Pool Name", "Active", "Pending", "Completed"};
23 |
24 | public TpstatsDialog(String endpoint, List l) {
25 | final DefaultTableModel tableModel= new DefaultTableModel(columns, 0) {
26 | private static final long serialVersionUID = 7088445834198028640L;
27 |
28 | @Override
29 | public boolean isCellEditable(int row, int column) {
30 | return false;
31 | }
32 | };
33 |
34 | for (Tpstats t : l) {
35 | tableModel.addRow(new String[] {t.getPoolName(),
36 | String.valueOf(t.getActiveCount()),
37 | String.valueOf(t.getPendingTasks()),
38 | String.valueOf(t.getCompletedTasks())});
39 | }
40 |
41 | final JTable table = new JTable(tableModel);
42 | table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
43 | table.setAutoCreateRowSorter(true);
44 |
45 | JScrollPane scrollPane = new JScrollPane(table);
46 |
47 | JButton ok = new JButton("OK");
48 | ok.addActionListener(new ActionListener() {
49 | @Override
50 | public void actionPerformed(ActionEvent e) {
51 | setVisible(false);
52 | }
53 | });
54 |
55 | JPanel buttonPanel = new JPanel(new FlowLayout());
56 | buttonPanel.add(ok);
57 |
58 | JPanel panel = new JPanel(new BorderLayout());
59 | panel.add(scrollPane, BorderLayout.CENTER);
60 | panel.add(buttonPanel, BorderLayout.SOUTH);
61 |
62 | add(panel);
63 |
64 | pack();
65 | setModalityType(ModalityType.DOCUMENT_MODAL);
66 | setTitle("Tpstats(" + endpoint + ")");
67 | setLocationRelativeTo(null);
68 | setModal(true);
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/component/dialog/KeyDialog.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.component.dialog;
2 |
3 | import java.awt.BorderLayout;
4 | import java.awt.FlowLayout;
5 | import java.awt.GridLayout;
6 | import java.awt.event.ActionEvent;
7 | import java.awt.event.ActionListener;
8 |
9 | import javax.swing.JButton;
10 | import javax.swing.JDialog;
11 | import javax.swing.JLabel;
12 | import javax.swing.JOptionPane;
13 | import javax.swing.JPanel;
14 | import javax.swing.JTextField;
15 |
16 | public class KeyDialog extends JDialog {
17 | private static final long serialVersionUID = -6904488663855720774L;
18 |
19 | private class EnterAction implements ActionListener {
20 | @Override
21 | public void actionPerformed(ActionEvent e) {
22 | enterAction();
23 | }
24 | }
25 |
26 | private JTextField keyText = new JTextField();
27 | private boolean cancel = true;
28 | private String key;
29 |
30 | public KeyDialog() {
31 | keyText.addActionListener(new EnterAction());
32 |
33 | JPanel inputPanel = new JPanel(new GridLayout(1, 2));
34 | inputPanel.add(new JLabel("key: "));
35 | inputPanel.add(keyText);
36 |
37 | JButton ok = new JButton("OK");
38 | ok.addActionListener(new ActionListener() {
39 | @Override
40 | public void actionPerformed(ActionEvent e) {
41 | enterAction();
42 | }
43 | });
44 | JButton cancel = new JButton("Cancel");
45 | cancel.addActionListener(new ActionListener() {
46 | @Override
47 | public void actionPerformed(ActionEvent e) {
48 | setVisible(false);
49 | }
50 | });
51 |
52 | JPanel buttonPanel = new JPanel(new FlowLayout());
53 | buttonPanel.add(ok);
54 | buttonPanel.add(cancel);
55 |
56 | JPanel panel = new JPanel(new BorderLayout());
57 | panel.add(inputPanel, BorderLayout.CENTER);
58 | panel.add(buttonPanel, BorderLayout.SOUTH);
59 |
60 | add(panel);
61 |
62 | pack();
63 | setModalityType(ModalityType.DOCUMENT_MODAL);
64 | setTitle("input key");
65 | setLocationRelativeTo(null);
66 | setModal(true);
67 | }
68 |
69 | private void enterAction() {
70 | if (keyText.getText().isEmpty()) {
71 | JOptionPane.showMessageDialog(null, "Enter key.");
72 | keyText.requestFocus();
73 | return;
74 | }
75 |
76 | key = keyText.getText();
77 |
78 | setVisible(false);
79 | cancel = false;
80 | }
81 |
82 | /**
83 | * @return the cancel
84 | */
85 | public boolean isCancel() {
86 | return cancel;
87 | }
88 |
89 | /**
90 | * @return the key
91 | */
92 | public String getkey() {
93 | return key;
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/node/TreeNode.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.node;
2 |
3 | import java.io.Serializable;
4 | import java.util.Map;
5 |
6 | import javax.swing.tree.DefaultMutableTreeNode;
7 | import javax.swing.tree.DefaultTreeModel;
8 |
9 | import org.apache.cassandra.client.Client;
10 | import org.apache.cassandra.unit.Unit;
11 |
12 | public class TreeNode implements Serializable {
13 | private static final long serialVersionUID = -227448839733721587L;
14 |
15 | private Client client;
16 | private DefaultMutableTreeNode node;
17 | private DefaultTreeModel treeModel;
18 | private Unit unit;
19 | private Map unitMap;
20 | private Map keyMap;
21 |
22 | public TreeNode() {
23 | }
24 |
25 | public TreeNode(Client client,
26 | DefaultMutableTreeNode node,
27 | DefaultTreeModel treeModel,
28 | Unit unit,
29 | Map unitMap,
30 | Map keyMap) {
31 | this.client = client;
32 | this.node = node;
33 | this.treeModel = treeModel;
34 | this.unit = unit;
35 | this.unitMap = unitMap;
36 | this.keyMap = keyMap;
37 | }
38 |
39 | /**
40 | * @return the client
41 | */
42 | public Client getClient() {
43 | return client;
44 | }
45 |
46 | /**
47 | * @param client the client to set
48 | */
49 | public void setClient(Client client) {
50 | this.client = client;
51 | }
52 |
53 | /**
54 | * @return the node
55 | */
56 | public DefaultMutableTreeNode getNode() {
57 | return node;
58 | }
59 |
60 | /**
61 | * @param node the node to set
62 | */
63 | public void setNode(DefaultMutableTreeNode node) {
64 | this.node = node;
65 | }
66 |
67 | /**
68 | * @return the treeModel
69 | */
70 | public DefaultTreeModel getTreeModel() {
71 | return treeModel;
72 | }
73 |
74 | /**
75 | * @param treeModel the treeModel to set
76 | */
77 | public void setTreeModel(DefaultTreeModel treeModel) {
78 | this.treeModel = treeModel;
79 | }
80 |
81 | /**
82 | * @return the unit
83 | */
84 | public Unit getUnit() {
85 | return unit;
86 | }
87 |
88 | /**
89 | * @param unit the unit to set
90 | */
91 | public void setUnit(Unit unit) {
92 | this.unit = unit;
93 | }
94 |
95 | /**
96 | * @return the unitMap
97 | */
98 | public Map getUnitMap() {
99 | return unitMap;
100 | }
101 |
102 | /**
103 | * @param unitMap the unitMap to set
104 | */
105 | public void setUnitMap(Map unitMap) {
106 | this.unitMap = unitMap;
107 | }
108 |
109 | /**
110 | * @return the keyMap
111 | */
112 | public Map getKeyMap() {
113 | return keyMap;
114 | }
115 |
116 | /**
117 | * @param keyMap the keyMap to set
118 | */
119 | public void setKeyMap(Map keyMap) {
120 | this.keyMap = keyMap;
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/component/dialog/KeyRangeDialog.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.component.dialog;
2 |
3 | import java.awt.BorderLayout;
4 | import java.awt.FlowLayout;
5 | import java.awt.GridLayout;
6 | import java.awt.event.ActionEvent;
7 | import java.awt.event.ActionListener;
8 |
9 | import javax.swing.JButton;
10 | import javax.swing.JDialog;
11 | import javax.swing.JLabel;
12 | import javax.swing.JOptionPane;
13 | import javax.swing.JPanel;
14 | import javax.swing.JTextField;
15 |
16 | public class KeyRangeDialog extends JDialog {
17 | private static final long serialVersionUID = -7378362468372008181L;
18 |
19 | private boolean cancel = true;
20 | private String startKey;
21 | private String endKey;
22 |
23 | public KeyRangeDialog(){
24 | final JTextField startKeyText = new JTextField();
25 | final JTextField endKeyText = new JTextField();
26 |
27 | JPanel inputPanel = new JPanel(new GridLayout(2, 2));
28 | inputPanel.add(new JLabel("start key:"));
29 | inputPanel.add(startKeyText);
30 | inputPanel.add(new JLabel("end key:"));
31 | inputPanel.add(endKeyText);
32 |
33 | JButton ok = new JButton("OK");
34 | ok.addActionListener(new ActionListener() {
35 | @Override
36 | public void actionPerformed(ActionEvent e) {
37 | if (startKeyText.getText().isEmpty()) {
38 | JOptionPane.showMessageDialog(null, "Enter start key.");
39 | startKeyText.requestFocus();
40 | return;
41 | }
42 |
43 | if (endKeyText.getText().isEmpty()) {
44 | JOptionPane.showMessageDialog(null, "Enter end key.");
45 | endKeyText.requestFocus();
46 | return;
47 | }
48 |
49 | startKey = startKeyText.getText();
50 | endKey = endKeyText.getText();
51 |
52 | setVisible(false);
53 | cancel = false;
54 | }
55 | });
56 | JButton cancel = new JButton("Cancel");
57 | cancel.addActionListener(new ActionListener() {
58 | @Override
59 | public void actionPerformed(ActionEvent e) {
60 | setVisible(false);
61 | }
62 | });
63 |
64 | JPanel buttonPanel = new JPanel(new FlowLayout());
65 | buttonPanel.add(ok);
66 | buttonPanel.add(cancel);
67 |
68 | JPanel panel = new JPanel(new BorderLayout());
69 | panel.add(inputPanel, BorderLayout.CENTER);
70 | panel.add(buttonPanel, BorderLayout.SOUTH);
71 |
72 | add(panel);
73 |
74 | pack();
75 | setModalityType(ModalityType.DOCUMENT_MODAL);
76 | setTitle("key range");
77 | setLocationRelativeTo(null);
78 | setModal(true);
79 | }
80 |
81 | /**
82 | * @return the cancel
83 | */
84 | public boolean isCancel() {
85 | return cancel;
86 | }
87 |
88 | /**
89 | * @return the startKey
90 | */
91 | public String getStartKey() {
92 | return startKey;
93 | }
94 |
95 | /**
96 | * @return the endKey
97 | */
98 | public String getEndKey() {
99 | return endKey;
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | org.apache.cassandra
6 | cassandra-gui
7 | jar
8 | 0.7.0
9 | cassandra-gui
10 | http://maven.apache.org
11 |
12 |
13 | UTF-8
14 |
15 |
16 |
17 |
18 | dstovall.org
19 | http://dstovall.org/maven2/
20 |
21 |
22 |
23 |
24 | cassandra-gui
25 |
26 |
27 | org.apache.maven.plugins
28 | maven-compiler-plugin
29 |
30 | 1.6
31 | 1.6
32 | true
33 | UTF-8
34 |
35 |
36 |
37 | org.apache.maven.plugins
38 | maven-jar-plugin
39 |
40 |
41 |
42 | org.apache.cassandra.CassandraGUI
43 | true
44 |
45 |
46 |
47 |
48 |
49 | org.dstovall
50 | onejar-maven-plugin
51 |
52 |
53 | package
54 |
55 | one-jar
56 |
57 |
58 |
59 | 1.4.3
60 |
61 |
62 |
63 |
64 |
65 |
66 | junit
67 | junit
68 | 3.8.1
69 | test
70 |
71 |
72 |
73 | commons-codec
74 | commons-codec
75 | 1.4
76 |
77 |
78 | commons-lang
79 | commons-lang
80 | 2.1
81 |
82 |
83 | org.codehaus.plexus
84 | plexus-utils
85 | 1.1
86 |
87 |
88 |
89 | ch.qos.logback
90 | logback-core
91 | 0.9.18
92 |
93 |
94 | ch.qos.logback
95 | logback-classic
96 | 0.9.18
97 |
98 |
99 |
100 | jung
101 | jung
102 | 1.7.6
103 |
104 |
105 |
106 | org.apache.cassandra
107 | cassandra-thrift
108 | 1.0.5
109 |
110 |
111 | org.apache.cassandra
112 | cassandra-all
113 | 1.0.5
114 |
115 |
116 |
117 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/component/dialog/ConnectionDialog.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.component.dialog;
2 |
3 | import java.awt.BorderLayout;
4 | import java.awt.FlowLayout;
5 | import java.awt.GridLayout;
6 | import java.awt.event.ActionEvent;
7 | import java.awt.event.ActionListener;
8 |
9 | import javax.swing.*;
10 |
11 | import org.apache.cassandra.client.Client;
12 |
13 | /**
14 | * Connection Dialogue class to connect to the Cassandra cluster
15 | */
16 | public class ConnectionDialog extends JDialog {
17 | private static final long serialVersionUID = 8707158056959280058L;
18 |
19 | private class EnterAction implements ActionListener {
20 | @Override
21 | public void actionPerformed(ActionEvent e) {
22 | enterAction();
23 | }
24 | }
25 |
26 | private Client client;
27 | private JButton ok = new JButton("OK");
28 | private JTextField hostText = new JTextField();
29 | private JTextField thriftPortText = new JTextField();
30 | private JTextField jmxPortTextField = new JTextField();
31 |
32 | public ConnectionDialog(JFrame owner){
33 | super(owner);
34 |
35 | hostText.setText(Client.DEFAULT_THRIFT_HOST);
36 | thriftPortText.setText(String.valueOf(Client.DEFAULT_THRIFT_PORT));
37 | jmxPortTextField.setText(String.valueOf(Client.DEFAULT_JMX_PORT));
38 |
39 | hostText.addActionListener(new EnterAction());
40 | thriftPortText.addActionListener(new EnterAction());
41 | jmxPortTextField.addActionListener(new EnterAction());
42 |
43 | JPanel inputPanel = new JPanel(new GridLayout(3, 2));
44 | inputPanel.add(new JLabel("Host:"));
45 | inputPanel.add(hostText);
46 | inputPanel.add(new JLabel("Thrift Port:"));
47 | inputPanel.add(thriftPortText);
48 | inputPanel.add(new JLabel("JMX Port:"));
49 | inputPanel.add(jmxPortTextField);
50 |
51 | ok.addActionListener(new ActionListener() {
52 | @Override
53 | public void actionPerformed(ActionEvent e) {
54 | enterAction();
55 | }
56 | });
57 | JButton cancel = new JButton("Cancel");
58 | cancel.addActionListener(new ActionListener() {
59 | @Override
60 | public void actionPerformed(ActionEvent e) {
61 | client = null;
62 | setVisible(false);
63 | }
64 | });
65 |
66 | JPanel buttonPanel = new JPanel(new FlowLayout());
67 | buttonPanel.add(ok);
68 | buttonPanel.add(cancel);
69 |
70 | JPanel panel = new JPanel(new BorderLayout());
71 | panel.add(new JLabel("Connection Details"), BorderLayout.NORTH);
72 | panel.add(inputPanel, BorderLayout.CENTER);
73 | panel.add(buttonPanel, BorderLayout.SOUTH);
74 |
75 | inputPanel.setBorder(BorderFactory.createEtchedBorder());
76 | buttonPanel.setBorder(BorderFactory.createEtchedBorder());
77 |
78 | add(panel);
79 |
80 | pack();
81 | setModalityType(ModalityType.DOCUMENT_MODAL);
82 | setTitle("Connection Details");
83 | setLocationRelativeTo(null);
84 | setVisible(true);
85 | }
86 |
87 | private void enterAction() {
88 | if (hostText.getText().isEmpty()){
89 | JOptionPane.showMessageDialog(null, "Enter Hostname.");
90 | return;
91 | }
92 |
93 | String host = hostText.getText();
94 | int thriftPort =
95 | thriftPortText.getText().isEmpty() ?
96 | Client.DEFAULT_THRIFT_PORT :
97 | Integer.valueOf(thriftPortText.getText());
98 | int jmxPort =
99 | jmxPortTextField.getText().isEmpty() ?
100 | Client.DEFAULT_JMX_PORT :
101 | Integer.valueOf(jmxPortTextField.getText());
102 |
103 | client = new Client(host, thriftPort, jmxPort);
104 | try {
105 | client.connect();
106 | } catch (Exception e1) {
107 | JOptionPane.showMessageDialog(null, "Connection failed.");
108 | e1.printStackTrace();
109 | return;
110 | }
111 |
112 | setVisible(false);
113 | }
114 |
115 | /**
116 | * @return the client
117 | */
118 | public Client getClient() {
119 | return client;
120 | }
121 | }
122 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/CassandraGUI.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra;
2 |
3 |
4 | import java.awt.Dimension;
5 | import java.awt.Toolkit;
6 |
7 | import javax.swing.JFrame;
8 | import javax.swing.JSplitPane;
9 |
10 | import org.apache.cassandra.gui.component.dialog.ConnectionDialog;
11 | import org.apache.cassandra.gui.component.dialog.listener.WindowCloseedListener;
12 | import org.apache.cassandra.gui.component.panel.ColumnTreePanel;
13 | import org.apache.cassandra.gui.component.panel.KeyspaceTreePanel;
14 | import org.apache.cassandra.gui.component.panel.PropertiesPanel;
15 | import org.apache.cassandra.gui.control.callback.PropertiesCallback;
16 | import org.apache.cassandra.gui.control.callback.RepaintCallback;
17 | import org.apache.cassandra.gui.control.callback.SelectedColumnFamilyCallback;
18 |
19 | public class CassandraGUI extends JFrame {
20 | private static final long serialVersionUID = -7402974525268824644L;
21 |
22 | /**
23 | * @param args
24 | */
25 | public static void main(String[] args) {
26 | CassandraGUI gui = new CassandraGUI("Cassandra GUI");
27 | if (!gui.createAndShow()) {
28 | System.exit(0);
29 | }
30 | }
31 |
32 | public CassandraGUI(String title) {
33 | super(title);
34 | }
35 |
36 | public boolean createAndShow() {
37 | final ConnectionDialog dlg = new ConnectionDialog(this);
38 | if (dlg.getClient() == null) {
39 | return false;
40 | }
41 |
42 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
43 | addWindowListener(new WindowCloseedListener() {
44 | @Override
45 | public void closing() {
46 | dlg.getClient().disconnect();
47 | }
48 | });
49 |
50 | Toolkit.getDefaultToolkit().setDynamicLayout(true);
51 |
52 | final PropertiesPanel propertiesPane = new PropertiesPanel(dlg.getClient());
53 | final ColumnTreePanel columnTreePane = new ColumnTreePanel(dlg.getClient());
54 | final KeyspaceTreePanel keyspaceTreePanel = new KeyspaceTreePanel(dlg.getClient());
55 | keyspaceTreePanel.setcCallback(new SelectedColumnFamilyCallback() {
56 | @Override
57 | public void rangeCallback(String keyspaceName,
58 | String columnFamilyName,
59 | String startKey,
60 | String endKey,
61 | int rows) {
62 | columnTreePane.showRows(keyspaceName, columnFamilyName, startKey, endKey, rows);
63 | }
64 |
65 | @Override
66 | public void getCacllback(String keyspace, String columnFamily, String key) {
67 | columnTreePane.showRow(keyspace, columnFamily, key);
68 | }
69 | });
70 | keyspaceTreePanel.setPropertiesCallback(new PropertiesCallback() {
71 | @Override
72 | public void clusterCallback() {
73 | propertiesPane.showClusterProperties();
74 | columnTreePane.clear();
75 | }
76 |
77 | @Override
78 | public void keyspaceCallback(String keyspace) {
79 | propertiesPane.showKeyspaceProperties(keyspace);
80 | columnTreePane.clear();
81 | }
82 |
83 | @Override
84 | public void columnFamilyCallback(String keyspace, String columnFamily) {
85 | propertiesPane.showColumnFamilyProperties(keyspace, columnFamily);
86 | columnTreePane.clear();
87 | }
88 | });
89 |
90 | final JSplitPane rightSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
91 | rightSplitPane.setLeftComponent(propertiesPane);
92 | rightSplitPane.setRightComponent(columnTreePane);
93 | rightSplitPane.setOneTouchExpandable(true);
94 | rightSplitPane.setDividerSize(6);
95 |
96 | final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
97 | splitPane.setLeftComponent(keyspaceTreePanel);
98 | splitPane.setRightComponent(rightSplitPane);
99 | splitPane.setOneTouchExpandable(true);
100 | splitPane.setDividerSize(6);
101 |
102 | add(splitPane);
103 | setBounds(10, 10, 850, 650);
104 | setLocationRelativeTo(null);
105 |
106 | setVisible(true);
107 |
108 | splitPane.getLeftComponent().setSize(keyspaceTreePanel.getPreferredSize());
109 | keyspaceTreePanel.setrCallback(new RepaintCallback() {
110 | @Override
111 | public Dimension callback() {
112 | return splitPane.getLeftComponent().getSize();
113 | }
114 | });
115 |
116 | splitPane.getRightComponent().setSize(new Dimension(850 - keyspaceTreePanel.getPreferredSize().width,
117 | keyspaceTreePanel.getPreferredSize().height));
118 | columnTreePane.setrCallback(new RepaintCallback() {
119 | @Override
120 | public Dimension callback() {
121 | return rightSplitPane.getRightComponent().getSize();
122 | }
123 | });
124 | propertiesPane.setrCallback(new RepaintCallback() {
125 | @Override
126 | public Dimension callback() {
127 | return rightSplitPane.getLeftComponent().getSize();
128 | }
129 | });
130 |
131 | keyspaceTreePanel.repaint();
132 | keyspaceTreePanel.revalidate();
133 | columnTreePane.repaint();
134 | columnTreePane.revalidate();
135 | propertiesPane.repaint();
136 | propertiesPane.revalidate();
137 |
138 | return true;
139 | }
140 | }
141 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/component/dialog/KeyspaceDialog.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.component.dialog;
2 |
3 | import java.awt.BorderLayout;
4 | import java.awt.FlowLayout;
5 | import java.awt.GridLayout;
6 | import java.awt.event.ActionEvent;
7 | import java.awt.event.ActionListener;
8 | import java.util.HashMap;
9 | import java.util.Map;
10 | import java.util.Map.Entry;
11 |
12 | import javax.swing.JButton;
13 | import javax.swing.JComboBox;
14 | import javax.swing.JDialog;
15 | import javax.swing.JLabel;
16 | import javax.swing.JOptionPane;
17 | import javax.swing.JPanel;
18 | import javax.swing.JTextField;
19 |
20 | import org.apache.cassandra.client.Client;
21 |
22 | public class KeyspaceDialog extends JDialog {
23 | private static final long serialVersionUID = 3957978062006221011L;
24 |
25 | private class EnterAction implements ActionListener {
26 | @Override
27 | public void actionPerformed(ActionEvent e) {
28 | enterAction();
29 | }
30 | }
31 |
32 | private JTextField keyspaceText = new JTextField();
33 | private JTextField replicationFactorText = new JTextField();
34 | private JComboBox strategyBox = new JComboBox();
35 | private JTextField optionText = new JTextField();
36 |
37 | private boolean cancel = true;
38 | private String keyspaceName;
39 | private int replicationFactor;
40 | private String strategy;
41 | private Map strategyOptions = new HashMap();
42 |
43 | public KeyspaceDialog(String keyspaceName,
44 | int replicationFactor,
45 | String strategy,
46 | Map strategyOptions) {
47 | keyspaceText.setText(keyspaceName);
48 | keyspaceText.setEditable(false);
49 |
50 | replicationFactorText.setText(String.valueOf(replicationFactor));
51 |
52 | String selectedStrategy = null;
53 | for (Entry entry : Client.getStrategyMap().entrySet()) {
54 | if (entry.getValue().equals(strategy)) {
55 | selectedStrategy = entry.getKey();
56 | break;
57 | }
58 | }
59 |
60 | StringBuilder sb = new StringBuilder();
61 | for (Entry entry : strategyOptions.entrySet()) {
62 | sb.append(entry.getKey()).append("=").append(entry.getValue()).append(",");
63 | }
64 | if (sb.length() > 0) {
65 | optionText.setText(sb.substring(0, sb.length() - 1));
66 | }
67 |
68 | create(selectedStrategy);
69 | }
70 |
71 | public KeyspaceDialog() {
72 | create(null);
73 | }
74 |
75 | public void create(String selectedStrategy) {
76 | keyspaceText.addActionListener(new EnterAction());
77 | replicationFactorText.addActionListener(new EnterAction());
78 | optionText.addActionListener(new EnterAction());
79 |
80 | JPanel inputPanel = new JPanel(new GridLayout(4, 2));
81 | inputPanel.add(new JLabel("Keyspace Name: "));
82 | inputPanel.add(keyspaceText);
83 | inputPanel.add(new JLabel("Replication Factor: "));
84 | inputPanel.add(replicationFactorText);
85 |
86 | for (String s : Client.getStrategyMap().keySet()) {
87 | strategyBox.addItem(s);
88 | }
89 | if (selectedStrategy != null) {
90 | strategyBox.setSelectedItem(selectedStrategy);
91 | }
92 |
93 | inputPanel.add(new JLabel("Strategy: "));
94 | inputPanel.add(strategyBox);
95 |
96 | inputPanel.add(new JLabel("Strategy Options: =,=..."));
97 | inputPanel.add(optionText);
98 |
99 | JButton ok = new JButton("OK");
100 | ok.addActionListener(new ActionListener() {
101 | @Override
102 | public void actionPerformed(ActionEvent e) {
103 | enterAction();
104 | }
105 | });
106 | JButton cancel = new JButton("Cancel");
107 | cancel.addActionListener(new ActionListener() {
108 | @Override
109 | public void actionPerformed(ActionEvent e) {
110 | setVisible(false);
111 | }
112 | });
113 |
114 | JPanel buttonPanel = new JPanel(new FlowLayout());
115 | buttonPanel.add(ok);
116 | buttonPanel.add(cancel);
117 |
118 | JPanel panel = new JPanel(new BorderLayout());
119 | panel.add(inputPanel, BorderLayout.CENTER);
120 | panel.add(buttonPanel, BorderLayout.SOUTH);
121 |
122 | add(panel);
123 |
124 | pack();
125 | setModalityType(ModalityType.DOCUMENT_MODAL);
126 | setTitle("create or update keyspace");
127 | setLocationRelativeTo(null);
128 | setModal(true);
129 | }
130 |
131 | private void enterAction() {
132 | if (keyspaceText.getText().isEmpty()) {
133 | JOptionPane.showMessageDialog(null, "Enter Keyspace Name.");
134 | keyspaceText.requestFocus();
135 | return;
136 | }
137 | keyspaceName = keyspaceText.getText();
138 |
139 | if (replicationFactorText.getText().isEmpty()) {
140 | JOptionPane.showMessageDialog(null, "Enter Replication Factor.");
141 | replicationFactorText.requestFocus();
142 | return;
143 | }
144 | try {
145 | replicationFactor = Integer.valueOf(replicationFactorText.getText());
146 | } catch (NumberFormatException e) {
147 | JOptionPane.showMessageDialog(null, "number input Replication Factor.");
148 | replicationFactorText.requestFocus();
149 | return;
150 | }
151 |
152 | strategy = (String) strategyBox.getSelectedItem();
153 |
154 | String options = optionText.getText();
155 | if (options != null && !options.isEmpty()) {
156 | String[] split1 = options.split(",");
157 | for (String s : split1) {
158 | String[] split2 = s.split("=");
159 | if (split2.length != 2) {
160 | JOptionPane.showMessageDialog(null, "Strategy Options format error.");
161 | optionText.requestFocus();
162 | return;
163 | }
164 | strategyOptions.put(split2[0], split2[1]);
165 | }
166 | }
167 |
168 | setVisible(false);
169 | cancel = false;
170 | }
171 |
172 | /**
173 | * @return the cancel
174 | */
175 | public boolean isCancel() {
176 | return cancel;
177 | }
178 |
179 | /**
180 | * @return the keyspaceName
181 | */
182 | public String getKeyspaceName() {
183 | return keyspaceName;
184 | }
185 |
186 | /**
187 | * @return the replicationFactor
188 | */
189 | public int getReplicationFactor() {
190 | return replicationFactor;
191 | }
192 |
193 | /**
194 | * @return the strategy
195 | */
196 | public String getStrategy() {
197 | return strategy;
198 | }
199 |
200 | /**
201 | * @return the strategyOptions
202 | */
203 | public Map getStrategyOptions() {
204 | return strategyOptions;
205 | }
206 | }
207 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/component/dialog/CellPropertiesDialog.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.component.dialog;
2 |
3 | import java.awt.BorderLayout;
4 | import java.awt.FlowLayout;
5 | import java.awt.GridLayout;
6 | import java.awt.event.ActionEvent;
7 | import java.awt.event.ActionListener;
8 | import java.awt.event.MouseAdapter;
9 | import java.awt.event.MouseEvent;
10 | import java.util.HashMap;
11 | import java.util.Map;
12 |
13 | import javax.swing.AbstractAction;
14 | import javax.swing.Action;
15 | import javax.swing.JButton;
16 | import javax.swing.JDialog;
17 | import javax.swing.JLabel;
18 | import javax.swing.JOptionPane;
19 | import javax.swing.JPanel;
20 | import javax.swing.JPopupMenu;
21 | import javax.swing.JTextField;
22 | import javax.swing.SwingUtilities;
23 |
24 | public class CellPropertiesDialog extends JDialog {
25 | private static final long serialVersionUID = -7378362468372008181L;
26 |
27 | private class PopupAction extends AbstractAction {
28 | private static final long serialVersionUID = 4235052996425858520L;
29 |
30 | private static final int ACTION_COPY = 1;
31 |
32 | private JTextField text;
33 | private int action;
34 |
35 | public PopupAction(String name, JTextField text, int action) {
36 | this.text = text;
37 | this.action = action;
38 | putValue(Action.NAME, name);
39 | }
40 |
41 | @Override
42 | public void actionPerformed(ActionEvent e) {
43 | switch (action) {
44 | case ACTION_COPY:
45 | text.copy();
46 | break;
47 | }
48 | }
49 | }
50 |
51 | private class MousePopup extends MouseAdapter {
52 | private JTextField text;
53 |
54 | public MousePopup(JTextField text) {
55 | this.text = text;
56 | }
57 |
58 | @Override
59 | public void mouseReleased(MouseEvent e) {
60 | if (SwingUtilities.isRightMouseButton(e)) {
61 | JPopupMenu popup = new JPopupMenu();
62 | popup.add(new PopupAction("Copy", text, PopupAction.ACTION_COPY));
63 | popup.show(e.getComponent(), e.getX(), e.getY());
64 | }
65 | }
66 | }
67 |
68 | private class EnterAction implements ActionListener {
69 | @Override
70 | public void actionPerformed(ActionEvent e) {
71 | enterAction();
72 | }
73 | }
74 |
75 | public static final int OPERATION_KEY_INSERT = 1;
76 | public static final int OPERATION_KEY_SUPERCOLUMN_INSERT = 2;
77 | public static final int OPERATION_SUPERCOLUMN_INSERT = 3;
78 | public static final int OPERATION_CELL_INSERT = 4;
79 | public static final int OPERATION_CELL_UPDATE = 5;
80 |
81 | private static final String KEY = "key";
82 | private static final String SUPER_COLUMN = "super column";
83 | private static final String NAME = "name";
84 | private static final String VAELU = "value";
85 |
86 | private JTextField keyText = new JTextField();
87 | private JTextField superColumnText = new JTextField();
88 | private JTextField nameText = new JTextField();
89 | private JTextField valueText = new JTextField();
90 | private boolean cancel = true;
91 |
92 | private Map textFieldMap = new HashMap();
93 |
94 | public CellPropertiesDialog(int operation) {
95 | this(operation, "", "");
96 | }
97 |
98 | public CellPropertiesDialog(int operation, String name, String value){
99 | JPanel propertiesPane = null;
100 | switch (operation) {
101 | case OPERATION_KEY_INSERT:
102 | propertiesPane = new JPanel(new GridLayout(3, 2));
103 | propertiesPane.add(new JLabel(KEY + ": "));
104 | propertiesPane.add(keyText);
105 | textFieldMap.put(KEY, keyText);
106 | break;
107 | case OPERATION_KEY_SUPERCOLUMN_INSERT:
108 | propertiesPane = new JPanel(new GridLayout(4, 2));
109 | propertiesPane.add(new JLabel(KEY + ": "));
110 | propertiesPane.add(keyText);
111 | textFieldMap.put(KEY, keyText);
112 |
113 | propertiesPane.add(new JLabel(SUPER_COLUMN + ": "));
114 | propertiesPane.add(superColumnText);
115 | textFieldMap.put(SUPER_COLUMN, superColumnText);
116 | break;
117 | case OPERATION_SUPERCOLUMN_INSERT:
118 | propertiesPane = new JPanel(new GridLayout(3, 2));
119 | propertiesPane.add(new JLabel(SUPER_COLUMN + ": "));
120 | propertiesPane.add(superColumnText);
121 | textFieldMap.put(SUPER_COLUMN, superColumnText);
122 | break;
123 | case OPERATION_CELL_INSERT:
124 | nameText.addActionListener(new EnterAction());
125 | propertiesPane = new JPanel(new GridLayout(2, 2));
126 | break;
127 | case OPERATION_CELL_UPDATE:
128 | propertiesPane = new JPanel(new GridLayout(2, 2));
129 | nameText.setEditable(false);
130 | break;
131 | }
132 |
133 | nameText.setText(name);
134 | valueText.setText(value);
135 | valueText.addActionListener(new EnterAction());
136 |
137 | propertiesPane.add(new JLabel(NAME + ": "));
138 | propertiesPane.add(nameText);
139 | textFieldMap.put(NAME, nameText);
140 |
141 | propertiesPane.add(new JLabel(VAELU + ": "));
142 | propertiesPane.add(valueText);
143 | textFieldMap.put(VAELU, valueText);
144 |
145 | nameText.addMouseListener(new MousePopup(nameText));
146 | valueText.addMouseListener(new MousePopup(valueText));
147 |
148 | JButton ok = new JButton("OK");
149 | ok.addActionListener(new ActionListener() {
150 | @Override
151 | public void actionPerformed(ActionEvent e) {
152 | cancel = false;
153 | setVisible(false);
154 | }
155 | });
156 | JButton cancel = new JButton("Cancel");
157 | cancel.addActionListener(new ActionListener() {
158 | @Override
159 | public void actionPerformed(ActionEvent e) {
160 | setVisible(false);
161 | }
162 | });
163 |
164 | JPanel buttonPanel = new JPanel(new FlowLayout());
165 | buttonPanel.add(ok);
166 | buttonPanel.add(cancel);
167 |
168 | JPanel panel = new JPanel(new BorderLayout());
169 | panel.add(propertiesPane, BorderLayout.CENTER);
170 | panel.add(buttonPanel, BorderLayout.SOUTH);
171 |
172 | add(panel);
173 |
174 | pack();
175 | setModalityType(ModalityType.DOCUMENT_MODAL);
176 | setTitle("properties");
177 | setLocationRelativeTo(null);
178 | setModal(true);
179 | }
180 |
181 | private void enterAction() {
182 | for (String s : textFieldMap.keySet()) {
183 | JTextField t = textFieldMap.get(s);
184 | if (t.getText().isEmpty()) {
185 | JOptionPane.showMessageDialog(null, "Enter " + s);
186 | t.requestFocus();
187 | return;
188 | }
189 | }
190 |
191 | setVisible(false);
192 | cancel = false;
193 | }
194 |
195 | public boolean isCancel() {
196 | return cancel;
197 | }
198 |
199 | public String getKey() {
200 | return keyText.getText();
201 | }
202 |
203 | public String getSuperColumn() {
204 | return superColumnText.getText();
205 | }
206 |
207 | public String getName() {
208 | return nameText.getText();
209 | }
210 |
211 | public String getValue() {
212 | return valueText.getText();
213 | }
214 | }
215 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/unit/ColumnFamily.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.unit;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class ColumnFamily {
7 | private int id;
8 | private String columnFamilyName;
9 | private String columnType;
10 | private String comparator;
11 | private String subcomparator;
12 | private String comment;
13 | private String rowsCached;
14 | private String rowCacheSavePeriod;
15 | private String keysCached;
16 | private String keyCacheSavePeriod;
17 | private String readRepairChance;
18 | private String gcGrace;
19 | private String memtableOperations;
20 | private String memtableThroughput;
21 | private String memtableFlushAfter;
22 | private String defaultValidationClass;
23 | private String minCompactionThreshold;
24 | private String maxCompactionThreshold;
25 | private List metaDatas = new ArrayList();
26 |
27 | public ColumnFamily() {
28 | }
29 |
30 | /**
31 | * @return the id
32 | */
33 | public int getId() {
34 | return id;
35 | }
36 |
37 | /**
38 | * @param id the id to set
39 | */
40 | public void setId(int id) {
41 | this.id = id;
42 | }
43 |
44 | /**
45 | * @return the columnFamilyName
46 | */
47 | public String getColumnFamilyName() {
48 | return columnFamilyName;
49 | }
50 |
51 | /**
52 | * @param columnFamilyName the columnFamilyName to set
53 | */
54 | public void setColumnFamilyName(String columnFamilyName) {
55 | this.columnFamilyName = columnFamilyName;
56 | }
57 |
58 | /**
59 | * @return the columnType
60 | */
61 | public String getColumnType() {
62 | return columnType;
63 | }
64 |
65 | /**
66 | * @param columnType the columnType to set
67 | */
68 | public void setColumnType(String columnType) {
69 | this.columnType = columnType;
70 | }
71 |
72 | /**
73 | * @return the comparator
74 | */
75 | public String getComparator() {
76 | return comparator;
77 | }
78 |
79 | /**
80 | * @param comparator the comparator to set
81 | */
82 | public void setComparator(String comparator) {
83 | this.comparator = comparator;
84 | }
85 |
86 | /**
87 | * @return the subcomparator
88 | */
89 | public String getSubcomparator() {
90 | return subcomparator;
91 | }
92 |
93 | /**
94 | * @param subcomparator the subcomparator to set
95 | */
96 | public void setSubcomparator(String subcomparator) {
97 | this.subcomparator = subcomparator;
98 | }
99 |
100 | /**
101 | * @return the comment
102 | */
103 | public String getComment() {
104 | return comment;
105 | }
106 |
107 | /**
108 | * @param comment the comment to set
109 | */
110 | public void setComment(String comment) {
111 | this.comment = comment;
112 | }
113 |
114 | /**
115 | * @return the rowsCached
116 | */
117 | public String getRowsCached() {
118 | return rowsCached;
119 | }
120 |
121 | /**
122 | * @param rowsCached the rowsCached to set
123 | */
124 | public void setRowsCached(String rowsCached) {
125 | this.rowsCached = rowsCached;
126 | }
127 |
128 | /**
129 | * @return the rowCacheSavePeriod
130 | */
131 | public String getRowCacheSavePeriod() {
132 | return rowCacheSavePeriod;
133 | }
134 |
135 | /**
136 | * @param rowCacheSavePeriod the rowCacheSavePeriod to set
137 | */
138 | public void setRowCacheSavePeriod(String rowCacheSavePeriod) {
139 | this.rowCacheSavePeriod = rowCacheSavePeriod;
140 | }
141 |
142 | /**
143 | * @return the keysCached
144 | */
145 | public String getKeysCached() {
146 | return keysCached;
147 | }
148 |
149 | /**
150 | * @param keysCached the keysCached to set
151 | */
152 | public void setKeysCached(String keysCached) {
153 | this.keysCached = keysCached;
154 | }
155 |
156 | /**
157 | * @return the keyCacheSavePeriod
158 | */
159 | public String getKeyCacheSavePeriod() {
160 | return keyCacheSavePeriod;
161 | }
162 |
163 | /**
164 | * @param keyCacheSavePeriod the keyCacheSavePeriod to set
165 | */
166 | public void setKeyCacheSavePeriod(String keyCacheSavePeriod) {
167 | this.keyCacheSavePeriod = keyCacheSavePeriod;
168 | }
169 |
170 | /**
171 | * @return the readRepairChance
172 | */
173 | public String getReadRepairChance() {
174 | return readRepairChance;
175 | }
176 |
177 | /**
178 | * @param readRepairChance the readRepairChance to set
179 | */
180 | public void setReadRepairChance(String readRepairChance) {
181 | this.readRepairChance = readRepairChance;
182 | }
183 |
184 | /**
185 | * @return the gcGrace
186 | */
187 | public String getGcGrace() {
188 | return gcGrace;
189 | }
190 |
191 | /**
192 | * @param gcGrace the gcGrace to set
193 | */
194 | public void setGcGrace(String gcGrace) {
195 | this.gcGrace = gcGrace;
196 | }
197 |
198 | /**
199 | * @return the memtableOperations
200 | */
201 | public String getMemtableOperations() {
202 | return memtableOperations;
203 | }
204 |
205 | /**
206 | * @param memtableOperations the memtableOperations to set
207 | */
208 | public void setMemtableOperations(String memtableOperations) {
209 | this.memtableOperations = memtableOperations;
210 | }
211 |
212 | /**
213 | * @return the memtableThroughput
214 | */
215 | public String getMemtableThroughput() {
216 | return memtableThroughput;
217 | }
218 |
219 | /**
220 | * @param memtableThroughput the memtableThroughput to set
221 | */
222 | public void setMemtableThroughput(String memtableThroughput) {
223 | this.memtableThroughput = memtableThroughput;
224 | }
225 |
226 | /**
227 | * @return the memtableFlushAfter
228 | */
229 | public String getMemtableFlushAfter() {
230 | return memtableFlushAfter;
231 | }
232 |
233 | /**
234 | * @param memtableFlushAfter the memtableFlushAfter to set
235 | */
236 | public void setMemtableFlushAfter(String memtableFlushAfter) {
237 | this.memtableFlushAfter = memtableFlushAfter;
238 | }
239 |
240 | /**
241 | * @return the defaultValidationClass
242 | */
243 | public String getDefaultValidationClass() {
244 | return defaultValidationClass;
245 | }
246 |
247 | /**
248 | * @param defaultValidationClass the defaultValidationClass to set
249 | */
250 | public void setDefaultValidationClass(String defaultValidationClass) {
251 | this.defaultValidationClass = defaultValidationClass;
252 | }
253 |
254 | /**
255 | * @return the minCompactionThreshold
256 | */
257 | public String getMinCompactionThreshold() {
258 | return minCompactionThreshold;
259 | }
260 |
261 | /**
262 | * @param minCompactionThreshold the minCompactionThreshold to set
263 | */
264 | public void setMinCompactionThreshold(String minCompactionThreshold) {
265 | this.minCompactionThreshold = minCompactionThreshold;
266 | }
267 |
268 | /**
269 | * @return the maxCompactionThreshold
270 | */
271 | public String getMaxCompactionThreshold() {
272 | return maxCompactionThreshold;
273 | }
274 |
275 | /**
276 | * @param maxCompactionThreshold the maxCompactionThreshold to set
277 | */
278 | public void setMaxCompactionThreshold(String maxCompactionThreshold) {
279 | this.maxCompactionThreshold = maxCompactionThreshold;
280 | }
281 |
282 | /**
283 | * @return the metaDatas
284 | */
285 | public List getMetaDatas() {
286 | return metaDatas;
287 | }
288 |
289 | /**
290 | * @param metaDatas the metaDatas to set
291 | */
292 | public void setMetaDatas(List metaDatas) {
293 | this.metaDatas = metaDatas;
294 | }
295 | }
296 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/component/panel/ColumnTreePanel.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.component.panel;
2 |
3 | import java.awt.Cursor;
4 | import java.awt.Dimension;
5 | import java.awt.event.MouseAdapter;
6 | import java.awt.event.MouseEvent;
7 | import java.text.SimpleDateFormat;
8 | import java.util.HashMap;
9 | import java.util.Map;
10 |
11 | import javax.swing.JOptionPane;
12 | import javax.swing.JPanel;
13 | import javax.swing.JPopupMenu;
14 | import javax.swing.JScrollPane;
15 | import javax.swing.JTree;
16 | import javax.swing.SwingUtilities;
17 | import javax.swing.tree.DefaultMutableTreeNode;
18 | import javax.swing.tree.DefaultTreeModel;
19 | import javax.swing.tree.TreePath;
20 |
21 |
22 | import org.apache.cassandra.client.Client;
23 | import org.apache.cassandra.gui.component.dialog.action.ColumnPopupAction;
24 | import org.apache.cassandra.gui.control.callback.RepaintCallback;
25 | import org.apache.cassandra.node.TreeNode;
26 | import org.apache.cassandra.thrift.CfDef;
27 | import org.apache.cassandra.unit.Cell;
28 | import org.apache.cassandra.unit.Key;
29 | import org.apache.cassandra.unit.SColumn;
30 | import org.apache.cassandra.unit.Unit;
31 |
32 | public class ColumnTreePanel extends JPanel {
33 | private static final long serialVersionUID = -4236268406209844637L;
34 |
35 | private class MousePopup extends MouseAdapter {
36 | @Override
37 | public void mouseReleased(MouseEvent e) {
38 | if (SwingUtilities.isRightMouseButton(e)) {
39 | TreePath path = tree.getPathForLocation(e.getX(), e.getY());
40 | if (path == null) {
41 | return;
42 | }
43 |
44 | tree.setSelectionPath(path);
45 | DefaultMutableTreeNode node =
46 | (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
47 | Unit u = unitMap.get(node);
48 | TreeNode treeNode = new TreeNode(client,
49 | node,
50 | treeModel,
51 | u,
52 | unitMap,
53 | keyMap);
54 | JPopupMenu popup = new JPopupMenu();
55 | if (u == null) {
56 | popup.add(new ColumnPopupAction("add",
57 | ColumnPopupAction.OPERATION_PROPERTIES,
58 | superColumn,
59 | treeNode));
60 | } else {
61 | if (u instanceof Cell) {
62 | popup.add(new ColumnPopupAction("properties",
63 | ColumnPopupAction.OPERATION_PROPERTIES,
64 | superColumn,
65 | treeNode));
66 | } else {
67 | popup.add(new ColumnPopupAction("add",
68 | ColumnPopupAction.OPERATION_PROPERTIES,
69 | superColumn,
70 | treeNode));
71 | }
72 | popup.add(new ColumnPopupAction("remove",
73 | ColumnPopupAction.OPERATION_REMOVE,
74 | superColumn,
75 | treeNode));
76 | }
77 | popup.show(e.getComponent(), e.getX(), e.getY());
78 | }
79 | }
80 | }
81 |
82 | private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
83 | private static final String COLUMN_FAMILY_TYPE_SUPER = "Super";
84 |
85 | private Client client;
86 | private boolean superColumn;
87 |
88 | private RepaintCallback rCallback;
89 | private JScrollPane scrollPane;
90 | private JTree tree;
91 | private DefaultTreeModel treeModel;
92 |
93 | private Map unitMap = new HashMap();
94 | private Map keyMap = new HashMap();
95 |
96 | public ColumnTreePanel(Client client) {
97 | this.client = client;
98 | scrollPane = new JScrollPane();
99 | add(scrollPane);
100 | repaint();
101 | }
102 |
103 | @Override
104 | public void repaint() {
105 | if (scrollPane != null && rCallback != null) {
106 | Dimension d = rCallback.callback();
107 | scrollPane.setPreferredSize(new Dimension(d.width - 5,
108 | d.height - 5));
109 | scrollPane.repaint();
110 | }
111 | super.repaint();
112 | }
113 |
114 | public void showRow(String keyspace, String columnFamily, String key) {
115 | try {
116 | Map m = client.getColumnFamily(keyspace, columnFamily);
117 | if (m.get(CfDef._Fields.COLUMN_TYPE.name()).equals(COLUMN_FAMILY_TYPE_SUPER)) {
118 | client.setSuperColumn(true);
119 | superColumn = true;
120 | } else {
121 | client.setSuperColumn(false);
122 | superColumn = false;
123 | }
124 |
125 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
126 | Map l =
127 | client.getKey(keyspace, columnFamily, null, key);
128 | showTree(l);
129 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
130 | } catch (Exception e) {
131 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
132 | JOptionPane.showMessageDialog(null, "error: " + ((e.getMessage() != null) ? e.getMessage() : e));
133 | e.printStackTrace();
134 | }
135 | }
136 |
137 | public void showRows(String keyspace, String columnFamily, String startKey, String endKey, int rows) {
138 | try {
139 | Map m = client.getColumnFamily(keyspace, columnFamily);
140 | if (m.get(CfDef._Fields.COLUMN_TYPE.name()).equals(COLUMN_FAMILY_TYPE_SUPER)) {
141 | client.setSuperColumn(true);
142 | superColumn = true;
143 | } else {
144 | client.setSuperColumn(false);
145 | superColumn = false;
146 | }
147 |
148 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
149 | Map l =
150 | client.listKeyAndValues(keyspace, columnFamily, startKey, endKey, rows);
151 | showTree(l);
152 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
153 | } catch (Exception e) {
154 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
155 | JOptionPane.showMessageDialog(null, "error: " + ((e.getMessage() != null) ? e.getMessage() : e));
156 | e.printStackTrace();
157 | }
158 | }
159 |
160 | public void clear() {
161 | DefaultMutableTreeNode columnFamilyNode = new DefaultMutableTreeNode(client.getColumnFamily());
162 | treeModel = new DefaultTreeModel(columnFamilyNode);
163 | tree = new JTree(treeModel);
164 | tree.setRootVisible(true);
165 | scrollPane.getViewport().setView(tree);
166 | repaint();
167 | }
168 |
169 | private void showTree(Map l) {
170 | DefaultMutableTreeNode columnFamilyNode = new DefaultMutableTreeNode(client.getColumnFamily());
171 | treeModel = new DefaultTreeModel(columnFamilyNode);
172 | tree = new JTree(treeModel);
173 | tree.setRootVisible(true);
174 | tree.addMouseListener(new MousePopup());
175 |
176 | for (String keyName : l.keySet()) {
177 | Key k = l.get(keyName);
178 | DefaultMutableTreeNode keyNode = new DefaultMutableTreeNode(k.getName());
179 | k.setTreeNode(keyNode);
180 | columnFamilyNode.add(keyNode);
181 | unitMap.put(keyNode, k);
182 | keyMap.put(k.getName(), k);
183 | if (k.isSuperColumn()) {
184 | for (String sName : k.getSColumns().keySet()) {
185 | SColumn sc = k.getSColumns().get(sName);
186 | DefaultMutableTreeNode scNode = new DefaultMutableTreeNode(sc.getName());
187 | sc.setTreeNode(scNode);
188 | keyNode.add(scNode);
189 | unitMap.put(scNode, sc);
190 | for (String cName : sc.getCells().keySet()) {
191 | Cell c = sc.getCells().get(cName);
192 | DefaultMutableTreeNode cellNode =
193 | new DefaultMutableTreeNode(c.getName() + "=" + c.getValue() + ", " + DATE_FORMAT.format(c.getDate()));
194 | c.setTreeNode(cellNode);
195 | scNode.add(cellNode);
196 | unitMap.put(cellNode, c);
197 | }
198 | }
199 | } else {
200 | for (String cName : k.getCells().keySet()) {
201 | Cell c = k.getCells().get(cName);
202 | DefaultMutableTreeNode cellNode =
203 | new DefaultMutableTreeNode(c.getName() + "=" + c.getValue() + ", " + DATE_FORMAT.format(c.getDate()));
204 | c.setTreeNode(cellNode);
205 | keyNode.add(cellNode);
206 | unitMap.put(cellNode, c);
207 | }
208 | }
209 | }
210 |
211 | scrollPane.getViewport().setView(tree);
212 | repaint();
213 | }
214 |
215 | /**
216 | * @param rCallback the rCallback to set
217 | */
218 | public void setrCallback(RepaintCallback rCallback) {
219 | this.rCallback = rCallback;
220 | }
221 | }
222 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/component/dialog/ColumnFamilyMetaDataDialog.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.component.dialog;
2 |
3 | import java.awt.BorderLayout;
4 | import java.awt.Component;
5 | import java.awt.Dimension;
6 | import java.awt.FlowLayout;
7 | import java.awt.event.ActionEvent;
8 | import java.awt.event.ActionListener;
9 | import java.util.EventObject;
10 |
11 | import javax.swing.AbstractAction;
12 | import javax.swing.BorderFactory;
13 | import javax.swing.DefaultCellEditor;
14 | import javax.swing.JButton;
15 | import javax.swing.JComboBox;
16 | import javax.swing.JDialog;
17 | import javax.swing.JOptionPane;
18 | import javax.swing.JPanel;
19 | import javax.swing.JScrollPane;
20 | import javax.swing.JTable;
21 | import javax.swing.event.CellEditorListener;
22 | import javax.swing.event.ChangeEvent;
23 | import javax.swing.table.DefaultTableModel;
24 | import javax.swing.table.TableCellEditor;
25 | import javax.swing.table.TableCellRenderer;
26 | import javax.swing.table.TableColumn;
27 | import javax.swing.table.TableRowSorter;
28 |
29 | import org.apache.cassandra.client.Client;
30 | import org.apache.cassandra.gui.component.model.ColumnFamilyMetaDataModel;
31 | import org.apache.cassandra.thrift.IndexType;
32 | import org.apache.cassandra.unit.ColumnFamily;
33 | import org.apache.cassandra.unit.ColumnFamilyMetaData;
34 |
35 | public class ColumnFamilyMetaDataDialog extends JDialog {
36 | private static final long serialVersionUID = -2295468150799941163L;
37 |
38 | private static final int BUTTON_COLUMN = 4;
39 |
40 | public ColumnFamilyMetaDataDialog(final ColumnFamily columnFamily) {
41 | final ColumnFamilyMetaDataModel model = new ColumnFamilyMetaDataModel();
42 | final JTable table = new JTable(model);
43 | TableRowSorter sorter = new TableRowSorter(model);
44 | table.setRowSorter(sorter);
45 | sorter.setSortable(BUTTON_COLUMN, false);
46 |
47 | TableColumn colColumnName = table.getColumnModel().getColumn(0);
48 | colColumnName.setMinWidth(60);
49 | colColumnName.setMaxWidth(60);
50 | colColumnName.setResizable(false);
51 |
52 | TableColumn column = table.getColumnModel().getColumn(BUTTON_COLUMN);
53 | column.setCellRenderer(new DeleteButtonRenderer());
54 | column.setCellEditor(new DeleteButtonEditor(table));
55 | column.setMinWidth(20);
56 | column.setMaxWidth(20);
57 | column.setResizable(false);
58 |
59 | TableColumn colValidationClass = table.getColumnModel().getColumn(1);
60 | JComboBox validationClassCb = new JComboBox(Client.getValidationClassMap().values().toArray());
61 |
62 | validationClassCb.setBorder(BorderFactory.createEmptyBorder());
63 | colValidationClass.setCellEditor(new DefaultCellEditor(validationClassCb));
64 |
65 | TableColumn colIndexType = table.getColumnModel().getColumn(2);
66 | JComboBox indexTypeCb = new JComboBox(IndexType.values());
67 | indexTypeCb.setBorder(BorderFactory.createEmptyBorder());
68 | colIndexType.setCellEditor(new DefaultCellEditor(indexTypeCb));
69 |
70 | for (ColumnFamilyMetaData metaData : columnFamily.getMetaDatas()) {
71 | metaData.setValiDationClass(Client.getValidationClassMap().get(metaData.getValiDationClass()));
72 | model.add(metaData);
73 | }
74 |
75 | JPanel buttonPanel = new JPanel(new FlowLayout());
76 | buttonPanel.add(new JButton(new AbstractAction("add") {
77 | private static final long serialVersionUID = 5064662528876805962L;
78 |
79 | @Override public void actionPerformed(ActionEvent e) {
80 | model.add(new ColumnFamilyMetaData());
81 | }
82 | }));
83 |
84 | buttonPanel.add(new JButton(new AbstractAction("apply") {
85 | private static final long serialVersionUID = 5064662528876805962L;
86 |
87 | @Override public void actionPerformed(ActionEvent e) {
88 | columnFamily.getMetaDatas().clear();
89 | for (int i = 0; i < model.getRowCount(); i++) {
90 | ColumnFamilyMetaData metaData = new ColumnFamilyMetaData();
91 | String columnName = (String) model.getValueAt(i, ColumnFamilyMetaDataModel.COLUMN_COLUMN_NAME);
92 | if (columnName == null || columnName.isEmpty()) {
93 | JOptionPane.showMessageDialog(null, "Enter Column Name.");
94 | return;
95 | }
96 | metaData.setColumnName(columnName);
97 |
98 | String valiDationClass = (String) model.getValueAt(i, ColumnFamilyMetaDataModel.COLUMN_VALIDATION_CLASS);
99 | if (valiDationClass == null || valiDationClass.isEmpty()) {
100 | JOptionPane.showMessageDialog(null, "Enter ValiDation Class.");
101 | return;
102 | }
103 | metaData.setValiDationClass(valiDationClass);
104 |
105 | IndexType indexType = (IndexType) model.getValueAt(i, ColumnFamilyMetaDataModel.COLUMN_INDEX_TYPE);
106 | if (indexType != null) {
107 | metaData.setIndexType(indexType);
108 | }
109 |
110 | metaData.setIndexName((String) model.getValueAt(i, ColumnFamilyMetaDataModel.COLUMN_INDEX_NAME));
111 | columnFamily.getMetaDatas().add(metaData);
112 | }
113 | setVisible(false);
114 | }
115 | }));
116 |
117 | JPanel panel = new JPanel(new BorderLayout());
118 | panel.add(new JScrollPane(table), BorderLayout.CENTER);
119 | panel.add(buttonPanel, BorderLayout.SOUTH);
120 | add(panel);
121 |
122 | setPreferredSize(new Dimension(320, 200));
123 |
124 | pack();
125 | setModalityType(ModalityType.DOCUMENT_MODAL);
126 | setTitle("metadata detail");
127 | setLocationRelativeTo(null);
128 | setModal(true);
129 | }
130 |
131 | private class DeleteButton extends JButton {
132 | private static final long serialVersionUID = -6863132480711592499L;
133 |
134 | @Override
135 | public void updateUI() {
136 | super.updateUI();
137 | setBorder(BorderFactory.createEmptyBorder());
138 | setFocusable(false);
139 | setRolloverEnabled(false);
140 | setText("X");
141 | }
142 | }
143 |
144 | private class DeleteButtonRenderer extends DeleteButton implements TableCellRenderer {
145 | private static final long serialVersionUID = -4965652160429644303L;
146 |
147 | public DeleteButtonRenderer() {
148 | super();
149 | setName("Table.cellRenderer");
150 | }
151 |
152 | @Override
153 | public Component getTableCellRendererComponent(JTable table,
154 | Object value,
155 | boolean isSelected,
156 | boolean hasFocus,
157 | int row,
158 | int column) {
159 | return this;
160 | }
161 | }
162 |
163 | private class DeleteButtonEditor extends DeleteButton implements TableCellEditor {
164 | private static final long serialVersionUID = 7489312671827408326L;
165 |
166 | public DeleteButtonEditor(final JTable table) {
167 | super();
168 | addActionListener(new ActionListener() {
169 | @Override public void actionPerformed(ActionEvent e) {
170 | int row = table.convertRowIndexToModel(table.getEditingRow());
171 | fireEditingStopped();
172 | ((DefaultTableModel) table.getModel()).removeRow(row);
173 | }
174 | });
175 | }
176 |
177 | @Override
178 | public Component getTableCellEditorComponent(JTable table,
179 | Object value,
180 | boolean isSelected,
181 | int row,
182 | int column) {
183 | return this;
184 | }
185 |
186 | @Override
187 | public Object getCellEditorValue() {
188 | return "";
189 | }
190 |
191 | @Override
192 | public boolean isCellEditable(EventObject anEvent) {
193 | return true;
194 | }
195 |
196 | @Override
197 | public boolean shouldSelectCell(EventObject anEvent) {
198 | return true;
199 | }
200 |
201 | @Override
202 | public boolean stopCellEditing() {
203 | fireEditingStopped();
204 | return true;
205 | }
206 |
207 | @Override
208 | public void cancelCellEditing() {
209 | fireEditingCanceled();
210 | }
211 |
212 | @Override
213 | public void addCellEditorListener(CellEditorListener l) {
214 | listenerList.add(CellEditorListener.class, l);
215 | }
216 |
217 | @Override
218 | public void removeCellEditorListener(CellEditorListener l) {
219 | listenerList.remove(CellEditorListener.class, l);
220 | }
221 |
222 | protected void fireEditingStopped() {
223 | Object[] listeners = listenerList.getListenerList();
224 | for (int i = listeners.length - 2; i >=0; i -= 2) {
225 | if (listeners[i] == CellEditorListener.class) {
226 | if (changeEvent == null) {
227 | changeEvent = new ChangeEvent(this);
228 | }
229 | ((CellEditorListener) listeners[i + 1]).editingStopped(changeEvent);
230 | }
231 | }
232 | }
233 |
234 | protected void fireEditingCanceled() {
235 | Object[] listeners = listenerList.getListenerList();
236 | for (int i = listeners.length - 2; i >= 0; i -= 2) {
237 | if (listeners[i] == CellEditorListener.class) {
238 | if (changeEvent == null) {
239 | changeEvent = new ChangeEvent(this);
240 | }
241 | ((CellEditorListener) listeners[i + 1]).editingCanceled(changeEvent);
242 | }
243 | }
244 | }
245 | }
246 | }
247 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/component/panel/PropertiesPanel.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.component.panel;
2 |
3 | import java.awt.Component;
4 | import java.awt.Cursor;
5 | import java.awt.Dimension;
6 | import java.awt.Point;
7 | import java.awt.event.MouseAdapter;
8 | import java.awt.event.MouseEvent;
9 | import java.util.List;
10 | import java.util.Map;
11 | import java.util.Map.Entry;
12 |
13 | import javax.swing.JOptionPane;
14 | import javax.swing.JPanel;
15 | import javax.swing.JScrollPane;
16 | import javax.swing.JTable;
17 | import javax.swing.ListSelectionModel;
18 | import javax.swing.RowSorter;
19 | import javax.swing.SwingUtilities;
20 | import javax.swing.table.DefaultTableColumnModel;
21 | import javax.swing.table.DefaultTableModel;
22 | import javax.swing.table.JTableHeader;
23 | import javax.swing.table.TableCellRenderer;
24 | import javax.swing.table.TableColumn;
25 | import javax.swing.table.TableColumnModel;
26 | import javax.swing.table.TableModel;
27 | import javax.swing.table.TableRowSorter;
28 |
29 | import org.apache.cassandra.client.Client;
30 | import org.apache.cassandra.gui.component.dialog.RingDialog;
31 | import org.apache.cassandra.gui.control.callback.RepaintCallback;
32 | import org.apache.cassandra.thrift.KsDef;
33 |
34 | public class PropertiesPanel extends JPanel {
35 | private static final long serialVersionUID = 1452324774722196104L;
36 |
37 | private class Header extends JTableHeader {
38 | private static final long serialVersionUID = 6540121946461922362L;
39 |
40 | public Header(TableColumnModel columnModel) {
41 | super(columnModel);
42 | }
43 |
44 | @Override
45 | protected void processMouseEvent(MouseEvent e) {
46 | if (e.getID() == MouseEvent.MOUSE_CLICKED &&
47 | SwingUtilities.isLeftMouseButton(e)) {
48 | Cursor cur = super.getCursor();
49 | if (cur.getType() == Cursor.E_RESIZE_CURSOR) {
50 | int cc = e.getClickCount();
51 | if (cc % 2 == 1) {
52 | return;
53 | } else {
54 | Point pt = new Point(e.getX() - 3, e.getY());
55 | int vc = super.columnAtPoint(pt);
56 | if (vc >= 0) {
57 | sizeWidthToFitData(vc);
58 | e.consume();
59 | return;
60 | }
61 | }
62 | }
63 | }
64 | super.processMouseEvent(e);
65 | }
66 |
67 | public void sizeWidthToFitData(int vc) {
68 | JTable table = super.getTable();
69 | TableColumn tc = table.getColumnModel().getColumn(vc);
70 |
71 | int max = 0;
72 |
73 | int vrows = table.getRowCount();
74 | for (int i = 0; i < vrows; i++) {
75 | TableCellRenderer r = table.getCellRenderer(i, vc);
76 | Object value = table.getValueAt(i, vc);
77 | Component c = r.getTableCellRendererComponent(table, value, false, false, i, vc);
78 | int w = c.getPreferredSize().width;
79 | if (max < w) {
80 | max = w;
81 | }
82 | }
83 |
84 | tc.setPreferredWidth(max + 1);
85 | }
86 | }
87 |
88 | private static final String COLUMN_SNITCH = "Snitch";
89 | private static final String COLUMN_PARTITIONER = "Partitioner";
90 | private static final String COLUMN_SCHEMA_VERSIONS = "Schema versions: ";
91 | private static final String COLUMN_VERSION = "api version";
92 | private static final String COLUMN_NUMBER_OF_KEYSPACE = "Number of Keyspace";
93 | private static final String COLUMN_RING = "ring";
94 | private static final String COLUMN_DOUBLE_CLICK_VALUE = "view the details by double-clicking";
95 |
96 | private static final String COLUMN_REPLICATION_STRATEGY = "Replication Strategy";
97 | private static final String COLUMN_REPLICATION_FACTOR = "Replication Factor";
98 | private static final String COLUMN_NUMBER_OF_COLUMN_FAMILY = "Number of Column Family";
99 |
100 | private static final String[] columns = { "name", "value" };
101 |
102 | private Client client;
103 | private RepaintCallback rCallback;
104 | private JScrollPane scrollPane;
105 | private JTable table;
106 |
107 | private DefaultTableModel tableModel;
108 |
109 | public PropertiesPanel(final Client client) {
110 | this.client = client;
111 |
112 | tableModel = new DefaultTableModel(columns, 0) {
113 | private static final long serialVersionUID = 7088445834198028640L;
114 |
115 | @Override
116 | public boolean isCellEditable(int row, int column) {
117 | return false;
118 | }
119 | };
120 |
121 | table = new JTable(tableModel) {
122 | private static final long serialVersionUID = -5396565496344780783L;
123 |
124 | protected JTableHeader createDefaultTableHeader() {
125 | return new Header(super.columnModel);
126 | };
127 | };
128 | table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
129 | RowSorter sorter = new TableRowSorter(table.getModel());
130 | table.setRowSorter(sorter);
131 | table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
132 | table.setAutoCreateRowSorter(true);
133 | table.addMouseListener(new MouseAdapter() {
134 | @Override
135 | public void mouseClicked(MouseEvent me) {
136 | if(me.getClickCount() == 2) {
137 | Point point = me.getPoint();
138 | int row = table.convertRowIndexToModel(table.rowAtPoint(point));
139 | try {
140 | if (tableModel.getValueAt(row, 0).equals(COLUMN_RING)) {
141 | RingDialog rd = new RingDialog(client);
142 | rd.setVisible(true);
143 | }
144 | } catch (Exception e) {
145 | JOptionPane.showMessageDialog(null, "error: " + e.getMessage());
146 | e.printStackTrace();
147 | return;
148 | }
149 | }
150 | }
151 | });
152 |
153 | DefaultTableColumnModel columnModel = (DefaultTableColumnModel)table.getColumnModel();
154 | for (int i = 0 ; i < columnModel.getColumnCount() ; i++){
155 | TableColumn column = columnModel.getColumn(i);
156 | column.setPreferredWidth(350);
157 | }
158 |
159 | scrollPane = new JScrollPane(table);
160 | add(scrollPane);
161 | repaint();
162 | }
163 |
164 | @Override
165 | public void repaint() {
166 | if (scrollPane != null && rCallback != null) {
167 | Dimension d = rCallback.callback();
168 | scrollPane.setPreferredSize(new Dimension(d.width - 5,
169 | d.height - 5));
170 | scrollPane.repaint();
171 | }
172 | super.repaint();
173 | }
174 |
175 | public void showClusterProperties() {
176 | try {
177 | tableModel.setRowCount(0);
178 |
179 | tableModel.addRow(new String[] {COLUMN_SNITCH, client.describeSnitch()});
180 | tableModel.addRow(new String[] {COLUMN_PARTITIONER, client.describePartitioner()});
181 |
182 | Map> m = client.describeSchemaVersions();
183 | if (m != null) {
184 | for (Entry> entry : m.entrySet()) {
185 | if (entry.getKey() != null) {
186 | for (String s : entry.getValue()) {
187 | tableModel.addRow(new String[] {COLUMN_SCHEMA_VERSIONS + entry.getKey(), s});
188 | }
189 | }
190 | }
191 | }
192 |
193 | tableModel.addRow(new String[] {COLUMN_VERSION, client.descriveVersion()});
194 | int n = client.getKeyspaces().size();
195 | tableModel.addRow(new String[] {COLUMN_NUMBER_OF_KEYSPACE, String.valueOf(n)});
196 | tableModel.addRow(new String[] {COLUMN_RING, COLUMN_DOUBLE_CLICK_VALUE});
197 |
198 | } catch (Exception e) {
199 | JOptionPane.showMessageDialog(null, "error: " + e.getMessage());
200 | e.printStackTrace();
201 | return;
202 | }
203 |
204 | repaint();
205 | }
206 |
207 | public void showKeyspaceProperties(String keyspace) {
208 | try {
209 | tableModel.setRowCount(0);
210 | KsDef kd = client.describeKeyspace(keyspace);
211 |
212 | tableModel.addRow(new String[] {COLUMN_REPLICATION_FACTOR, String.valueOf(kd.getReplication_factor())});
213 | tableModel.addRow(new String[] {COLUMN_REPLICATION_STRATEGY, kd.getStrategy_class()});
214 | int n = client.getColumnFamilys(keyspace).size();
215 | tableModel.addRow(new String[] {COLUMN_NUMBER_OF_COLUMN_FAMILY, String.valueOf(n)});
216 |
217 | if (kd.getStrategy_options() != null) {
218 | for (Entry entry : kd.getStrategy_options().entrySet()) {
219 | tableModel.addRow(new String[] {entry.getKey(), entry.getValue()});
220 | }
221 | }
222 | } catch (Exception e) {
223 | JOptionPane.showMessageDialog(null, "error: " + e.getMessage());
224 | e.printStackTrace();
225 | return;
226 | }
227 |
228 | repaint();
229 | }
230 |
231 | public void showColumnFamilyProperties(String keyspace, String columnFamily) {
232 | try {
233 | tableModel.setRowCount(0);
234 | Map m = client.getColumnFamily(keyspace, columnFamily);
235 | for (Map.Entry e : m.entrySet()) {
236 | // FIXME ... column metadata is not showing up correctly, here... plus multi-line, or hover to see all, etc
237 | tableModel.addRow(new String[] {e.getKey(), (e.getValue() != null) ? e.getValue().toString() : ""});
238 | }
239 | } catch (Exception e) {
240 | JOptionPane.showMessageDialog(null, "error: " + e.getMessage());
241 | e.printStackTrace();
242 | return;
243 | }
244 |
245 | repaint();
246 | }
247 |
248 | /**
249 | * @param rCallback the rCallback to set
250 | */
251 | public void setrCallback(RepaintCallback rCallback) {
252 | this.rCallback = rCallback;
253 | }
254 | }
255 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/component/dialog/RingDialog.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.component.dialog;
2 |
3 | import java.awt.BorderLayout;
4 | import java.awt.Color;
5 | import java.awt.Cursor;
6 | import java.awt.FlowLayout;
7 | import java.awt.Paint;
8 | import java.awt.event.ActionEvent;
9 | import java.awt.event.ActionListener;
10 | import java.awt.event.MouseEvent;
11 | import java.awt.geom.Point2D;
12 | import java.util.HashMap;
13 | import java.util.List;
14 | import java.util.Map;
15 | import java.util.Set;
16 |
17 | import javax.swing.AbstractAction;
18 | import javax.swing.JButton;
19 | import javax.swing.JDialog;
20 | import javax.swing.JOptionPane;
21 | import javax.swing.JPanel;
22 | import javax.swing.JPopupMenu;
23 | import javax.swing.JScrollPane;
24 |
25 | import org.apache.cassandra.client.Client;
26 | import org.apache.cassandra.dht.Range;
27 | import org.apache.cassandra.dht.Token;
28 | import org.apache.cassandra.node.NodeInfo;
29 | import org.apache.cassandra.node.RingNode;
30 | import org.apache.cassandra.node.Tpstats;
31 |
32 | import edu.uci.ics.jung.graph.ArchetypeVertex;
33 | import edu.uci.ics.jung.graph.Vertex;
34 | import edu.uci.ics.jung.graph.decorators.StringLabeller;
35 | import edu.uci.ics.jung.graph.decorators.VertexPaintFunction;
36 | import edu.uci.ics.jung.graph.decorators.VertexStringer;
37 | import edu.uci.ics.jung.graph.impl.UndirectedSparseEdge;
38 | import edu.uci.ics.jung.graph.impl.UndirectedSparseGraph;
39 | import edu.uci.ics.jung.graph.impl.UndirectedSparseVertex;
40 | import edu.uci.ics.jung.visualization.Layout;
41 | import edu.uci.ics.jung.visualization.PickSupport;
42 | import edu.uci.ics.jung.visualization.PluggableRenderer;
43 | import edu.uci.ics.jung.visualization.ShapePickSupport;
44 | import edu.uci.ics.jung.visualization.VisualizationViewer;
45 | import edu.uci.ics.jung.visualization.contrib.CircleLayout;
46 | import edu.uci.ics.jung.visualization.control.AbstractPopupGraphMousePlugin;
47 | import edu.uci.ics.jung.visualization.control.LayoutScalingControl;
48 | import edu.uci.ics.jung.visualization.control.PickingGraphMousePlugin;
49 | import edu.uci.ics.jung.visualization.control.PluggableGraphMouse;
50 | import edu.uci.ics.jung.visualization.control.RotatingGraphMousePlugin;
51 | import edu.uci.ics.jung.visualization.control.ScalingGraphMousePlugin;
52 | import edu.uci.ics.jung.visualization.control.TranslatingGraphMousePlugin;
53 |
54 | public class RingDialog extends JDialog {
55 | private static final long serialVersionUID = 1543749033698969116L;
56 |
57 | private static final int NODE_STATUS_UP = 1;
58 | private static final int NODE_STATUS_DOWN = 2;
59 | private static final int NODE_STATUS_UNKNOWN = 3;
60 |
61 | private Client client;
62 |
63 | public RingDialog(Client client) {
64 | this.client = client;
65 |
66 | JScrollPane scrollPane = new JScrollPane(setupControls());
67 |
68 | JButton ok = new JButton("OK");
69 | ok.addActionListener(new ActionListener() {
70 | @Override
71 | public void actionPerformed(ActionEvent e) {
72 | setVisible(false);
73 | }
74 | });
75 |
76 | JPanel buttonPanel = new JPanel(new FlowLayout());
77 | buttonPanel.add(ok);
78 |
79 | JPanel panel = new JPanel(new BorderLayout());
80 | panel.add(scrollPane, BorderLayout.CENTER);
81 | panel.add(buttonPanel, BorderLayout.SOUTH);
82 |
83 | add(panel);
84 |
85 | pack();
86 | setModalityType(ModalityType.DOCUMENT_MODAL);
87 | setTitle("Ring");
88 | setLocationRelativeTo(null);
89 | setModal(true);
90 | }
91 |
92 | private VisualizationViewer setupControls() {
93 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
94 | final RingNode ringNode = client.listRing();
95 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
96 |
97 | final Map rangeMap = ringNode.getRangeMap();
98 | final List ranges = ringNode.getRanges();
99 | final List liveNodes = ringNode.getLiveNodes();
100 | final List deadNodes = ringNode.getDeadNodes();
101 | final Map loadMap = ringNode.getLoadMap();
102 |
103 | final Map statusMap = new HashMap();
104 | final Map endpointMap = new HashMap();
105 |
106 | final UndirectedSparseGraph graph = new UndirectedSparseGraph();
107 | final StringLabeller stringLabeller = StringLabeller.getLabeller(graph);
108 |
109 | final Vertex[] vertices = new Vertex[rangeMap.size()];
110 |
111 | int count = 0;
112 | for (Token range : ranges) {
113 | // List endpoints = rangeMap.get(range);
114 | String endpoints = rangeMap.get(range);
115 | // String primaryEndpoint = endpoints.get(0);
116 | String primaryEndpoint = rangeMap.get(range);
117 | String load = loadMap.containsKey(primaryEndpoint) ? loadMap.get(primaryEndpoint) : "?";
118 | String label = "" +
119 | "Address: " + primaryEndpoint + "
" +
120 | "Load: " + load + "
" +
121 | "Range: " + range.toString() +
122 | "";
123 |
124 | Vertex v = graph.addVertex(new UndirectedSparseVertex());
125 | vertices[count] = v;
126 | try {
127 | stringLabeller.setLabel(v, label);
128 | } catch (Exception e) {
129 | JOptionPane.showMessageDialog(null, "error: " + e.getMessage());
130 | e.printStackTrace();
131 | }
132 |
133 | statusMap.put(v, liveNodes.contains(primaryEndpoint) ? NODE_STATUS_UP
134 | : deadNodes.contains(primaryEndpoint) ? NODE_STATUS_DOWN
135 | : NODE_STATUS_UNKNOWN);
136 | endpointMap.put(v, primaryEndpoint);
137 |
138 | count++;
139 | }
140 |
141 | if (vertices.length > 1) {
142 | for (int i = 0; i < vertices.length; i++) {
143 | int index = 0;
144 | if (i+1 != vertices.length) {
145 | index = i + 1;
146 | }
147 |
148 | graph.addEdge(new UndirectedSparseEdge(vertices[i], vertices[index]));
149 | }
150 | }
151 |
152 | final Layout layout = new CircleLayout(graph);
153 | final PluggableRenderer renderer = new PluggableRenderer();
154 |
155 | renderer.setVertexStringer(new VertexStringer() {
156 | @Override
157 | public String getLabel(ArchetypeVertex v) {
158 | return stringLabeller.getLabel(v);
159 | }
160 | });
161 |
162 | renderer.setVertexPaintFunction(new VertexPaintFunction() {
163 | @Override
164 | public Paint getFillPaint(Vertex v) {
165 | Color c = Color.YELLOW;
166 | switch (statusMap.get(v)) {
167 | case NODE_STATUS_UP:
168 | c = Color.GREEN;
169 | break;
170 | case NODE_STATUS_DOWN:
171 | c = Color.RED;
172 | break;
173 | }
174 |
175 | return c;
176 | }
177 |
178 | @Override
179 | public Paint getDrawPaint(Vertex v) {
180 | Color c = Color.YELLOW;
181 | switch (statusMap.get(v)) {
182 | case NODE_STATUS_UP:
183 | c = Color.GREEN;
184 | break;
185 | case NODE_STATUS_DOWN:
186 | c = Color.RED;
187 | break;
188 | }
189 |
190 | return c;
191 | }
192 | });
193 |
194 | PluggableGraphMouse gm = new PluggableGraphMouse();
195 | gm.add(new PickingGraphMousePlugin());
196 | gm.add(new RotatingGraphMousePlugin());
197 | gm.add(new TranslatingGraphMousePlugin());
198 | gm.add(new ScalingGraphMousePlugin(new LayoutScalingControl(), 0));
199 | gm.add(new AbstractPopupGraphMousePlugin() {
200 | @Override
201 | protected void handlePopup(MouseEvent e) {
202 | final VisualizationViewer vv = (VisualizationViewer) e.getSource();
203 | final Point2D ivp = vv.inverseViewTransform(e.getPoint());
204 | PickSupport pickSupport = vv.getPickSupport();
205 | final Vertex vertex = pickSupport.getVertex(ivp.getX(), ivp.getY());
206 |
207 | if(pickSupport != null) {
208 | JPopupMenu popup = new JPopupMenu();
209 | if(vertex != null) {
210 | if (statusMap.get(vertex) == NODE_STATUS_UP) {
211 | popup.add(new AbstractAction("info") {
212 | private static final long serialVersionUID = -6992429747383272830L;
213 |
214 | @Override
215 | public void actionPerformed(ActionEvent ae) {
216 | try {
217 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
218 | NodeInfo ni = client.getNodeInfo(endpointMap.get(vertex));
219 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
220 |
221 | NodeInfoDialog nid = new NodeInfoDialog(ni);
222 | nid.setVisible(true);
223 | } catch (Exception e) {
224 | JOptionPane.showMessageDialog(null, "error: " + e.getMessage());
225 | e.printStackTrace();
226 | }
227 | }
228 | });
229 |
230 | popup.add(new AbstractAction("tpstats") {
231 | private static final long serialVersionUID = 6511117264115071716L;
232 |
233 | @Override
234 | public void actionPerformed(ActionEvent ae) {
235 | try {
236 | String endpoint = endpointMap.get(vertex);
237 |
238 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
239 | List l = client.getTpstats(endpoint);
240 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
241 |
242 | TpstatsDialog td = new TpstatsDialog(endpoint, l);
243 | td.setVisible(true);
244 | } catch (Exception e) {
245 | JOptionPane.showMessageDialog(null, "error: " + e.getMessage());
246 | e.printStackTrace();
247 | }
248 | }
249 | });
250 | }
251 | }
252 |
253 | if(popup.getComponentCount() > 0) {
254 | popup.show(vv, e.getX(), e.getY());
255 | }
256 | }
257 | }
258 | });
259 |
260 | final VisualizationViewer viewer = new VisualizationViewer(layout, renderer);
261 | viewer.setGraphMouse(gm);
262 | viewer.setPickSupport(new ShapePickSupport(viewer, viewer, renderer, 2));
263 |
264 | return viewer;
265 | }
266 | }
267 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/component/dialog/action/ColumnPopupAction.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.component.dialog.action;
2 |
3 | import java.awt.event.ActionEvent;
4 | import java.text.SimpleDateFormat;
5 | import java.util.Date;
6 | import java.util.HashMap;
7 | import java.util.Map;
8 |
9 | import javax.swing.AbstractAction;
10 | import javax.swing.Action;
11 | import javax.swing.JOptionPane;
12 | import javax.swing.tree.DefaultMutableTreeNode;
13 |
14 | import org.apache.cassandra.client.Client;
15 | import org.apache.cassandra.gui.component.dialog.CellPropertiesDialog;
16 | import org.apache.cassandra.node.TreeNode;
17 | import org.apache.cassandra.unit.Cell;
18 | import org.apache.cassandra.unit.Key;
19 | import org.apache.cassandra.unit.SColumn;
20 | import org.apache.cassandra.unit.Unit;
21 |
22 | public class ColumnPopupAction extends AbstractAction {
23 | private static final long serialVersionUID = -4419251468566465640L;
24 |
25 | private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
26 | "yyyy-MM-dd HH:mm:ss.SSS");
27 |
28 | public static final int OPERATION_PROPERTIES = 1;
29 | public static final int OPERATION_REMOVE = 2;
30 |
31 | private int operation;
32 | private boolean isSuperColumn;
33 | private TreeNode treeNode;
34 | private Client client;
35 |
36 | public ColumnPopupAction(String name, int operation, boolean isSuperColumn,
37 | TreeNode treeNode) {
38 | this.operation = operation;
39 | this.treeNode = treeNode;
40 | this.isSuperColumn = isSuperColumn;
41 | this.client = treeNode.getClient();
42 | putValue(Action.NAME, name);
43 | }
44 |
45 | @Override
46 | public void actionPerformed(ActionEvent ae) {
47 | Unit u = treeNode.getUnit();
48 | switch (operation) {
49 | case OPERATION_PROPERTIES:
50 | if (u == null) {
51 | insertKeyCell();
52 | } else if (u instanceof Key) {
53 | insertCell();
54 | } else if (u instanceof SColumn) {
55 | insertSuperColumnCell();
56 | } else if (u instanceof Cell) {
57 | updateCell();
58 | }
59 |
60 | break;
61 | case OPERATION_REMOVE:
62 | String msg = "";
63 | if (u instanceof Key) {
64 | msg = "key";
65 | } else if (u instanceof SColumn) {
66 | msg = "super column";
67 | } else {
68 | msg = "column";
69 | }
70 |
71 | int status = JOptionPane.showConfirmDialog(null, "Delete a " + msg
72 | + " " + getName() + "?", "confirm",
73 | JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
74 | if (status == JOptionPane.YES_OPTION) {
75 | remove();
76 | }
77 |
78 | break;
79 | }
80 | }
81 |
82 | private void insertKeyCell() {
83 | CellPropertiesDialog cpdlg = new CellPropertiesDialog(
84 | isSuperColumn ? CellPropertiesDialog.OPERATION_KEY_SUPERCOLUMN_INSERT
85 | : CellPropertiesDialog.OPERATION_KEY_INSERT);
86 | cpdlg.setVisible(true);
87 | if (cpdlg.isCancel()) {
88 | return;
89 | }
90 |
91 | String key = cpdlg.getKey();
92 | String superColumn = cpdlg.getSuperColumn();
93 | String name = cpdlg.getName();
94 | String value = cpdlg.getValue();
95 |
96 | boolean keyFound = false;
97 | boolean superColumnFound = false;
98 | boolean cellFound = false;
99 |
100 | Key k = null;
101 | try {
102 | k = (Key) treeNode.getKeyMap().get(key);
103 | if (k == null) {
104 | Map m = client.getKey(client.getKeyspace(), client
105 | .getColumnFamily(), superColumn.isEmpty() ? null
106 | : superColumn, key);
107 | k = m.get(key);
108 | if (k == null) {
109 | k = new Key(key, new HashMap(),
110 | new HashMap());
111 | k.setSuperColumn(isSuperColumn);
112 | }
113 | } else {
114 | keyFound = true;
115 | }
116 | } catch (Exception e) {
117 | JOptionPane.showMessageDialog(null, "error: " + ((e.getMessage() != null) ? e.getMessage() : e));
118 | e.printStackTrace();
119 | return;
120 | }
121 |
122 | SColumn s = null;
123 | DefaultMutableTreeNode sn = null;
124 | if (isSuperColumn) {
125 | s = k.getSColumns().get(superColumn);
126 | if (s == null) {
127 | s = new SColumn(k, superColumn, new HashMap());
128 | sn = new DefaultMutableTreeNode(s.getName());
129 | s.setTreeNode(sn);
130 | s.setParent(k);
131 | } else {
132 | sn = s.getTreeNode();
133 | superColumnFound = true;
134 | }
135 | }
136 |
137 | Date d = null;
138 | try {
139 | d = client.insertColumn(client.getKeyspace(), client
140 | .getColumnFamily(), key, s == null ? null : superColumn,
141 | name, value);
142 | } catch (Exception e) {
143 | JOptionPane.showMessageDialog(null, "error: " + e.getMessage());
144 | e.printStackTrace();
145 | return;
146 | }
147 |
148 | DefaultMutableTreeNode kn = k.getTreeNode();
149 | if (kn == null) {
150 | kn = new DefaultMutableTreeNode(key);
151 | k.setTreeNode(kn);
152 | }
153 |
154 | if (isSuperColumn) {
155 | Cell c = s.getCells().get(name);
156 | DefaultMutableTreeNode cn = new DefaultMutableTreeNode(name + "="
157 | + value + ", " + DATE_FORMAT.format(d));
158 | if (c == null) {
159 | c = new Cell(s, name, value, d);
160 | c.setTreeNode(cn);
161 | if (isSuperColumn) {
162 | c.setParent(s);
163 | } else {
164 | c.setParent(k);
165 | }
166 | } else {
167 | c.getTreeNode().setUserObject(cn);
168 | cellFound = true;
169 | }
170 | s.getCells().put(name, c);
171 | k.getSColumns().put(superColumn, s);
172 |
173 | if (!cellFound) {
174 | sn.add(cn);
175 | }
176 |
177 | if (!superColumnFound) {
178 | kn.add(sn);
179 | }
180 |
181 | if (!keyFound) {
182 | treeNode.getNode().add(kn);
183 | }
184 |
185 | treeNode.getUnitMap().put(kn, k);
186 | treeNode.getUnitMap().put(sn, s);
187 | treeNode.getUnitMap().put(cn, c);
188 | } else {
189 | Cell c = k.getCells().get(name);
190 | DefaultMutableTreeNode cn = new DefaultMutableTreeNode(name + "="
191 | + value + ", " + DATE_FORMAT.format(d));
192 | if (c == null) {
193 | c = new Cell(s, name, value, d);
194 | c.setTreeNode(cn);
195 | c.setParent(k);
196 | } else {
197 | c.getTreeNode().setUserObject(cn);
198 | cellFound = true;
199 | }
200 | k.getCells().put(name, c);
201 |
202 | if (!cellFound) {
203 | kn.add(cn);
204 | }
205 |
206 | if (!keyFound) {
207 | treeNode.getNode().add(kn);
208 | }
209 |
210 | treeNode.getUnitMap().put(kn, k);
211 | treeNode.getUnitMap().put(cn, c);
212 | }
213 |
214 | treeNode.getKeyMap().put(key, k);
215 |
216 | treeNode.getTreeModel().nodeChanged(treeNode.getNode());
217 | treeNode.getTreeModel().reload(treeNode.getNode());
218 | }
219 |
220 | private void insertCell() {
221 | Key k = (Key) treeNode.getUnit();
222 |
223 | CellPropertiesDialog cpdlg = new CellPropertiesDialog(
224 | isSuperColumn ? CellPropertiesDialog.OPERATION_SUPERCOLUMN_INSERT
225 | : CellPropertiesDialog.OPERATION_CELL_INSERT);
226 | cpdlg.setVisible(true);
227 | if (cpdlg.isCancel()) {
228 | return;
229 | }
230 |
231 | SColumn s = null;
232 | if (isSuperColumn) {
233 | s = new SColumn(k, cpdlg.getSuperColumn(),
234 | new HashMap());
235 | }
236 |
237 | Date d = null;
238 | try {
239 | d = client.insertColumn(client.getKeyspace(),
240 | client.getColumnFamily(), k.getName(),
241 | s == null ? null : s.getName(), cpdlg.getName(),
242 | cpdlg.getValue());
243 | } catch (Exception e) {
244 | JOptionPane.showMessageDialog(null, "error: " + ((e.getMessage() != null) ? e.getMessage() : e));
245 | e.printStackTrace();
246 | }
247 |
248 | if (isSuperColumn) {
249 | Cell c = new Cell(s, cpdlg.getName(), cpdlg.getValue(), d);
250 | DefaultMutableTreeNode cn = new DefaultMutableTreeNode(c.getName()
251 | + "=" + c.getValue() + ", "
252 | + DATE_FORMAT.format(c.getDate()));
253 | s.getCells().put(c.getName(), c);
254 |
255 | DefaultMutableTreeNode sn = new DefaultMutableTreeNode(s.getName());
256 | sn.add(cn);
257 | treeNode.getNode().add(sn);
258 |
259 | treeNode.getUnitMap().put(sn, s);
260 | treeNode.getUnitMap().put(cn, c);
261 | } else {
262 | Cell c = new Cell(k, cpdlg.getName(), cpdlg.getValue(), d);
263 | DefaultMutableTreeNode cn = new DefaultMutableTreeNode(c.getName()
264 | + "=" + c.getValue() + ", "
265 | + DATE_FORMAT.format(c.getDate()));
266 | k.getCells().put(c.getName(), c);
267 | treeNode.getNode().add(cn);
268 |
269 | treeNode.getUnitMap().put(cn, c);
270 | }
271 |
272 | treeNode.getTreeModel().reload(treeNode.getNode());
273 | }
274 |
275 | private void insertSuperColumnCell() {
276 | SColumn s = (SColumn) treeNode.getUnit();
277 | CellPropertiesDialog cpdlg = new CellPropertiesDialog(CellPropertiesDialog.OPERATION_CELL_INSERT);
278 | cpdlg.setVisible(true);
279 | if (cpdlg.isCancel()) {
280 | return;
281 | }
282 |
283 | Key k = (Key) s.getParent();
284 |
285 | Date d = null;
286 | try {
287 | d = client.insertColumn(client.getKeyspace(),
288 | client.getColumnFamily(),
289 | k.getName(),
290 | s.getName(),
291 | cpdlg.getName(),
292 | cpdlg.getValue());
293 | } catch (Exception e) {
294 | JOptionPane.showMessageDialog(null, "error: " + ((e.getMessage() != null) ? e.getMessage() : e));
295 | e.printStackTrace();
296 | }
297 |
298 | Cell c = new Cell(s, cpdlg.getName(), cpdlg.getValue(), d);
299 | DefaultMutableTreeNode cn = new DefaultMutableTreeNode(c.getName() + "=" + c.getValue() + ", " +
300 | DATE_FORMAT.format(c.getDate()));
301 | s.getCells().put(c.getName(), c);
302 | treeNode.getUnitMap().put(cn, c);
303 |
304 | treeNode.getNode().add(cn);
305 | treeNode.getTreeModel().reload(treeNode.getNode());
306 | }
307 |
308 | private void updateCell() {
309 | Cell c = (Cell) treeNode.getUnit();
310 | CellPropertiesDialog cpdlg = new CellPropertiesDialog(
311 | CellPropertiesDialog.OPERATION_CELL_UPDATE, c.getName(),
312 | c.getValue());
313 | cpdlg.setVisible(true);
314 | if (cpdlg.isCancel()) {
315 | return;
316 | }
317 |
318 | Key k = null;
319 | SColumn s = null;
320 |
321 | Unit parentUnit = c.getParent();
322 | if (parentUnit instanceof SColumn) {
323 | s = (SColumn) parentUnit;
324 | k = (Key) s.getParent();
325 | } else {
326 | k = (Key) parentUnit;
327 | }
328 |
329 | Date d = null;
330 | try {
331 | d = client.insertColumn(client.getKeyspace(),
332 | client.getColumnFamily(), k.getName(),
333 | s == null ? null : s.getName(), cpdlg.getName(),
334 | cpdlg.getValue());
335 | } catch (Exception e) {
336 | JOptionPane.showMessageDialog(null, "error: " + e.getMessage());
337 | e.printStackTrace();
338 | }
339 |
340 | c.setName(cpdlg.getName());
341 | c.setValue(cpdlg.getValue());
342 | c.setDate(d);
343 |
344 | treeNode.getNode().setUserObject(
345 | new DefaultMutableTreeNode(c.getName() + "=" + c.getValue()
346 | + ", " + DATE_FORMAT.format(c.getDate())));
347 | treeNode.getTreeModel().nodeChanged(treeNode.getNode());
348 | }
349 |
350 | private void remove() {
351 | try {
352 | if (treeNode.getUnit() instanceof Key) {
353 | Key k = (Key) treeNode.getUnit();
354 | client.removeKey(client.getKeyspace(),
355 | client.getColumnFamily(), k.getName());
356 |
357 | if (isSuperColumn) {
358 | for (SColumn s : k.getSColumns().values()) {
359 | treeNode.getUnitMap().remove(s.getTreeNode());
360 | for (Cell c : s.getCells().values()) {
361 | treeNode.getUnitMap().remove(c.getTreeNode());
362 | }
363 | }
364 | } else {
365 | for (Cell c : k.getCells().values()) {
366 | treeNode.getUnitMap().remove(c.getTreeNode());
367 | }
368 | }
369 |
370 | k.getCells().clear();
371 | k.getSColumns().clear();
372 | treeNode.getNode().removeAllChildren();
373 | treeNode.getTreeModel().reload(treeNode.getNode());
374 | } else if (treeNode.getUnit() instanceof SColumn) {
375 | SColumn s = (SColumn) treeNode.getUnit();
376 | Key k = (Key) s.getParent();
377 | client.removeSuperColumn(client.getKeyspace(),
378 | client.getColumnFamily(), k.getName(), s.getName());
379 | k.getSColumns().remove(s.getName());
380 |
381 | for (Cell c : s.getCells().values()) {
382 | treeNode.getUnitMap().remove(c.getTreeNode());
383 | }
384 |
385 | removeNode((DefaultMutableTreeNode) treeNode.getNode()
386 | .getParent(), treeNode.getNode());
387 | } else {
388 | Cell c = (Cell) treeNode.getUnit();
389 | treeNode.getUnitMap().remove(c.getTreeNode());
390 | Unit parent = c.getParent();
391 | if (parent instanceof Key) {
392 | Key k = (Key) parent;
393 | client.removeColumn(client.getKeyspace(),
394 | client.getColumnFamily(), k.getName(), c.getName());
395 | k.getCells().remove(c.getName());
396 |
397 | removeNode((DefaultMutableTreeNode) treeNode.getNode()
398 | .getParent(), treeNode.getNode());
399 | } else if (parent instanceof SColumn) {
400 | SColumn s = (SColumn) parent;
401 | Key k = (Key) s.getParent();
402 | client.removeColumn(client.getKeyspace(),
403 | client.getColumnFamily(), k.getName(), s.getName(),
404 | c.getName());
405 | s.getCells().remove(c.getName());
406 |
407 | DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) treeNode
408 | .getNode().getParent();
409 | removeNode(parentNode, treeNode.getNode());
410 |
411 | if (s.getCells().isEmpty()) {
412 | k.getSColumns().remove(s.getName());
413 | removeNode(
414 | (DefaultMutableTreeNode) parentNode.getParent(),
415 | parentNode);
416 | }
417 | }
418 | }
419 | } catch (Exception e) {
420 | JOptionPane.showMessageDialog(null, "error: " + e.getMessage());
421 | e.printStackTrace();
422 | }
423 | }
424 |
425 | private void removeNode(DefaultMutableTreeNode parentNode,
426 | DefaultMutableTreeNode node) {
427 | if (parentNode != null && node != null) {
428 | node.removeFromParent();
429 | treeNode.getTreeModel().reload(parentNode);
430 | }
431 | }
432 |
433 | private String getName() {
434 | if (treeNode.getUnit() instanceof Key) {
435 | return ((Key) treeNode.getUnit()).getName();
436 | } else if (treeNode.getUnit() instanceof SColumn) {
437 | return ((SColumn) treeNode.getUnit()).getName();
438 | }
439 |
440 | return ((Cell) treeNode.getUnit()).getName();
441 | }
442 | }
443 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/component/dialog/ColumnFamilyDialog.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.component.dialog;
2 |
3 | import java.awt.BorderLayout;
4 | import java.awt.FlowLayout;
5 | import java.awt.GridLayout;
6 | import java.awt.event.ActionEvent;
7 | import java.awt.event.ActionListener;
8 |
9 | import javax.swing.JButton;
10 | import javax.swing.JComboBox;
11 | import javax.swing.JDialog;
12 | import javax.swing.JLabel;
13 | import javax.swing.JOptionPane;
14 | import javax.swing.JPanel;
15 | import javax.swing.JTextField;
16 |
17 | import org.apache.cassandra.client.Client;
18 | import org.apache.cassandra.client.Client.ColumnType;
19 | import org.apache.cassandra.unit.ColumnFamily;
20 |
21 | public class ColumnFamilyDialog extends JDialog {
22 | private static final long serialVersionUID = -13548946072075769L;
23 |
24 | private class EnterAction implements ActionListener {
25 | @Override
26 | public void actionPerformed(ActionEvent e) {
27 | enterAction();
28 | }
29 | }
30 |
31 | private class ComparatorAction implements ActionListener {
32 | @Override
33 | public void actionPerformed(ActionEvent e) {
34 | if (((String) columnTypeBox.getSelectedItem()).equals(Client.ColumnType.SUPER.toString()) &&
35 | !update) {
36 | subComparatorTypeBox.setEnabled(true);
37 | } else {
38 | subComparatorTypeBox.setEnabled(false);
39 | }
40 | }
41 | }
42 |
43 | private JTextField columnFamilyText = new JTextField();
44 | private JComboBox columnTypeBox = new JComboBox();
45 | private JComboBox comparatorTypeBox = new JComboBox();
46 | private JComboBox subComparatorTypeBox = new JComboBox();
47 | private JTextField commentText = new JTextField();
48 | private JTextField rowsCachedText = new JTextField();
49 | private JTextField rowCacheSavePeriodText = new JTextField();
50 | private JTextField keysCachedText = new JTextField();
51 | private JTextField keyCacheSavePeriodText = new JTextField();
52 | private JTextField readRepairChanceText = new JTextField();
53 | private JTextField gcGraceText = new JTextField();
54 | private JTextField memtableOperationsText = new JTextField();
55 | private JTextField memtableThroughputText = new JTextField();
56 | private JTextField memtableFlushAfterText = new JTextField();
57 | private JTextField defaultValidationClassText = new JTextField();
58 | private JTextField minCompactionThresholdText = new JTextField();
59 | private JTextField maxCompactionThresholdText = new JTextField();
60 | private ColumnFamilyMetaDataDialog metaDataDialog;
61 |
62 | private boolean cancel = true;
63 | private boolean update = false;
64 | private ColumnFamily cf = new ColumnFamily();
65 |
66 | public ColumnFamilyDialog(ColumnFamily cf) {
67 | this.cf = cf;
68 | update = true;
69 | columnFamilyText.setText(cf.getColumnFamilyName());
70 | columnFamilyText.setEnabled(false);
71 |
72 | comparatorTypeBox.setEnabled(false);
73 | subComparatorTypeBox.setEnabled(false);
74 |
75 | commentText.setText(cf.getComment());
76 | rowsCachedText.setText(cf.getRowsCached());
77 | rowCacheSavePeriodText.setText(cf.getRowCacheSavePeriod());
78 | keysCachedText.setText(cf.getKeysCached());
79 | keyCacheSavePeriodText.setText(cf.getKeyCacheSavePeriod());
80 | readRepairChanceText.setText(cf.getReadRepairChance());
81 | gcGraceText.setText(cf.getGcGrace());
82 | memtableOperationsText.setText(cf.getMemtableOperations());
83 | memtableThroughputText.setText(cf.getMemtableThroughput());
84 | memtableFlushAfterText.setText(cf.getMemtableFlushAfter());
85 | defaultValidationClassText.setText(cf.getDefaultValidationClass());
86 | minCompactionThresholdText.setText(cf.getMinCompactionThreshold());
87 | maxCompactionThresholdText.setText(cf.getMaxCompactionThreshold());
88 |
89 | create(cf);
90 | }
91 |
92 | public ColumnFamilyDialog() {
93 | create(null);
94 | }
95 |
96 | public void create(ColumnFamily selectedColumnFamily) {
97 | columnFamilyText.addActionListener(new EnterAction());
98 | commentText.addActionListener(new EnterAction());
99 | rowsCachedText.addActionListener(new EnterAction());
100 | rowCacheSavePeriodText.addActionListener(new EnterAction());
101 | keysCachedText.addActionListener(new EnterAction());
102 | keyCacheSavePeriodText.addActionListener(new EnterAction());
103 | readRepairChanceText.addActionListener(new EnterAction());
104 | gcGraceText.addActionListener(new EnterAction());
105 | memtableOperationsText.addActionListener(new EnterAction());
106 | memtableThroughputText.addActionListener(new EnterAction());
107 | memtableFlushAfterText.addActionListener(new EnterAction());
108 | defaultValidationClassText.addActionListener(new EnterAction());
109 | minCompactionThresholdText.addActionListener(new EnterAction());
110 | maxCompactionThresholdText.addActionListener(new EnterAction());
111 |
112 | JPanel inputPanel = new JPanel(new GridLayout(18, 2));
113 |
114 | // ColumnFamily Name
115 | addJTextField(inputPanel, "Column Family Name: ", columnFamilyText);
116 |
117 | // Column Type
118 | for (ColumnType ct : Client.ColumnType.values()) {
119 | columnTypeBox.addItem(ct.toString());
120 | }
121 | if (selectedColumnFamily != null) {
122 | columnTypeBox.setSelectedItem(selectedColumnFamily.getColumnType());
123 | } else {
124 | columnTypeBox.setSelectedItem("");
125 | }
126 | columnTypeBox.addActionListener(new ComparatorAction());
127 | inputPanel.add(new JLabel("Column Type: "));
128 | inputPanel.add(columnTypeBox);
129 |
130 | // Comparator
131 | for (String ct : Client.getComparatorTypeMap().values()) {
132 | comparatorTypeBox.addItem(ct);
133 | }
134 | if (selectedColumnFamily != null) {
135 | comparatorTypeBox.setSelectedItem(Client.getComparatorTypeMap().get(selectedColumnFamily.getComparator()));
136 | }
137 | inputPanel.add(new JLabel("Comparator Type: "));
138 | inputPanel.add(comparatorTypeBox);
139 |
140 | // SubComparator
141 | for (String ct : Client.getComparatorTypeMap().values()) {
142 | subComparatorTypeBox.addItem(ct);
143 | }
144 | if (selectedColumnFamily != null &&
145 | selectedColumnFamily.getSubcomparator() != null) {
146 | subComparatorTypeBox.setSelectedItem(Client.getComparatorTypeMap().get(selectedColumnFamily.getSubcomparator()));
147 | } else {
148 | subComparatorTypeBox.setSelectedItem("");
149 | }
150 | inputPanel.add(new JLabel("SubComparator Type: "));
151 | inputPanel.add(subComparatorTypeBox);
152 |
153 | // comment
154 | addJTextField(inputPanel, "comment: ", commentText);
155 |
156 | // Rows Cached
157 | addJTextField(inputPanel, "rows cached: ", rowsCachedText);
158 |
159 | // Row Cached Save Period
160 | addJTextField(inputPanel, "rows cached save period: ", rowCacheSavePeriodText);
161 |
162 | // Keys Cached
163 | addJTextField(inputPanel, "keys cached: ", keysCachedText);
164 |
165 | // Key Cached Save Period
166 | addJTextField(inputPanel, "Key Cached Save Period: ", keyCacheSavePeriodText);
167 |
168 | // Read Repair Chance
169 | addJTextField(inputPanel, "Read Repair Chance: ", readRepairChanceText);
170 |
171 | // GC Grace
172 | addJTextField(inputPanel, "GC Grace: ", gcGraceText);
173 |
174 | // Memtable Operations
175 | addJTextField(inputPanel, "Memtable Operations: ", memtableOperationsText);
176 |
177 | // MemTable Throughput
178 | addJTextField(inputPanel, "Table Throughput: ", memtableThroughputText);
179 |
180 | // MemTable Flush After
181 | addJTextField(inputPanel, "MemTable Flush After: ", memtableFlushAfterText);
182 |
183 | // Default Validation Class
184 | addJTextField(inputPanel, "Default Validation Class: ", defaultValidationClassText);
185 |
186 | // Min Compaction Threshold
187 | addJTextField(inputPanel, "Min Compaction Threshold: ", minCompactionThresholdText);
188 |
189 | // Max Compaction Threshold
190 | addJTextField(inputPanel, "Max Compaction Threshold: ", maxCompactionThresholdText);
191 |
192 | // column metadata
193 | metaDataDialog = new ColumnFamilyMetaDataDialog(cf);
194 | inputPanel.add(new JLabel("Column MetaData: "));
195 | JButton detail = new JButton("detail");
196 | detail.addActionListener(new ActionListener() {
197 | @Override
198 | public void actionPerformed(ActionEvent e) {
199 | metaDataDialog.setVisible(true);
200 | }
201 | });
202 | inputPanel.add(detail);
203 |
204 | // buttons
205 | JButton ok = new JButton("OK");
206 | ok.addActionListener(new ActionListener() {
207 | @Override
208 | public void actionPerformed(ActionEvent e) {
209 | enterAction();
210 | }
211 | });
212 | JButton cancel = new JButton("Cancel");
213 | cancel.addActionListener(new ActionListener() {
214 | @Override
215 | public void actionPerformed(ActionEvent e) {
216 | setVisible(false);
217 | }
218 | });
219 |
220 | JPanel buttonPanel = new JPanel(new FlowLayout());
221 | buttonPanel.add(ok);
222 | buttonPanel.add(cancel);
223 |
224 | JPanel panel = new JPanel(new BorderLayout());
225 | panel.add(inputPanel, BorderLayout.CENTER);
226 | panel.add(buttonPanel, BorderLayout.SOUTH);
227 |
228 | add(panel);
229 |
230 | pack();
231 | setModalityType(ModalityType.DOCUMENT_MODAL);
232 | setTitle("create or update column family");
233 | setLocationRelativeTo(null);
234 | setModal(true);
235 | }
236 |
237 | private void addJTextField(JPanel inputPanel, String label, JTextField field) {
238 | inputPanel.add(new JLabel(label));
239 | inputPanel.add(field);
240 | }
241 |
242 | private void enterAction() {
243 | // ColumnFamily Name
244 | if (columnFamilyText.getText().isEmpty()) {
245 | JOptionPane.showMessageDialog(null, "Enter Column Family Name.");
246 | columnFamilyText.requestFocus();
247 | return;
248 | }
249 | cf.setColumnFamilyName(columnFamilyText.getText());
250 |
251 | // Column Type
252 | cf.setColumnType((String) columnTypeBox.getSelectedItem());
253 |
254 | // Comparator
255 | cf.setComparator((String) comparatorTypeBox.getSelectedItem());
256 |
257 | // SubComparator Type
258 | if (((String) columnTypeBox.getSelectedItem()).equals(Client.ColumnType.SUPER.toString())) {
259 | cf.setSubcomparator((String) subComparatorTypeBox.getSelectedItem());
260 | }
261 |
262 | // comment
263 | cf.setComment(commentText.getText());
264 |
265 | // Rows Cached
266 | if (!rowsCachedText.getText().isEmpty()) {
267 | try {
268 | Double.valueOf(rowsCachedText.getText());
269 | } catch (NumberFormatException e) {
270 | JOptionPane.showMessageDialog(null, "number input Rows Cached.");
271 | rowsCachedText.requestFocus();
272 | return;
273 | }
274 | cf.setRowsCached(rowsCachedText.getText());
275 | }
276 |
277 | // Row Cached Save Period
278 | if (!rowCacheSavePeriodText.getText().isEmpty()) {
279 | try {
280 | Integer.valueOf(rowCacheSavePeriodText.getText());
281 | } catch (NumberFormatException e) {
282 | JOptionPane.showMessageDialog(null, "number input Key Cached Save Period.");
283 | rowCacheSavePeriodText.requestFocus();
284 | return;
285 | }
286 | cf.setRowsCached(rowCacheSavePeriodText.getText());
287 | }
288 |
289 | // Keys Cached
290 | if (!keysCachedText.getText().isEmpty()) {
291 | try {
292 | Double.valueOf(keysCachedText.getText());
293 | } catch (NumberFormatException e) {
294 | JOptionPane.showMessageDialog(null, "number input Keys Cached.");
295 | keysCachedText.requestFocus();
296 | return;
297 | }
298 | cf.setKeysCached(keysCachedText.getText());
299 | }
300 |
301 | // Key Cached Save Period
302 | if (!keyCacheSavePeriodText.getText().isEmpty()) {
303 | try {
304 | Integer.valueOf(keyCacheSavePeriodText.getText());
305 | } catch (NumberFormatException e) {
306 | JOptionPane.showMessageDialog(null, "number input Key Cached Save Period.");
307 | keyCacheSavePeriodText.requestFocus();
308 | return;
309 | }
310 | cf.setKeyCacheSavePeriod(keyCacheSavePeriodText.getText());
311 | }
312 |
313 | // Read Repair Chance
314 | if (!readRepairChanceText.getText().isEmpty()) {
315 | try {
316 | Double.valueOf(readRepairChanceText.getText());
317 | } catch (NumberFormatException e) {
318 | JOptionPane.showMessageDialog(null, "number input Read Repair Chance.");
319 | readRepairChanceText.requestFocus();
320 | return;
321 | }
322 | cf.setReadRepairChance(readRepairChanceText.getText());
323 | }
324 |
325 | // GC Grace
326 | if (!gcGraceText.getText().isEmpty()) {
327 | try {
328 | Integer.valueOf(gcGraceText.getText());
329 | } catch (NumberFormatException e) {
330 | JOptionPane.showMessageDialog(null, "number input GC Grace.");
331 | gcGraceText.requestFocus();
332 | return;
333 | }
334 | cf.setGcGrace(gcGraceText.getText());
335 | }
336 |
337 | // Memtable Operations
338 | if (!memtableOperationsText.getText().isEmpty()) {
339 | try {
340 | Double.valueOf(memtableOperationsText.getText());
341 | } catch (NumberFormatException e) {
342 | JOptionPane.showMessageDialog(null, "number input Memtable Operations.");
343 | memtableOperationsText.requestFocus();
344 | return;
345 | }
346 | cf.setMemtableOperations(memtableOperationsText.getText());
347 | }
348 |
349 | // MemTable Throughput
350 | if (!memtableThroughputText.getText().isEmpty()) {
351 | try {
352 | Integer.valueOf(memtableThroughputText.getText());
353 | } catch (NumberFormatException e) {
354 | JOptionPane.showMessageDialog(null, "number input MemTable Throughput.");
355 | memtableThroughputText.requestFocus();
356 | return;
357 | }
358 | cf.setMemtableThroughput(memtableThroughputText.getText());
359 | }
360 |
361 | // MemTable Flush After
362 | if (!memtableFlushAfterText.getText().isEmpty()) {
363 | try {
364 | Integer.valueOf(memtableFlushAfterText.getText());
365 | } catch (NumberFormatException e) {
366 | JOptionPane.showMessageDialog(null, "number input MemTable Flush After.");
367 | memtableFlushAfterText.requestFocus();
368 | return;
369 | }
370 | cf.setMemtableFlushAfter(memtableFlushAfterText.getText());
371 | }
372 |
373 | // Default Validation Class
374 | cf.setDefaultValidationClass(defaultValidationClassText.getText());
375 |
376 | // Min Compaction Threshold
377 | if (!minCompactionThresholdText.getText().isEmpty()) {
378 | try {
379 | Integer.valueOf(minCompactionThresholdText.getText());
380 | } catch (NumberFormatException e) {
381 | JOptionPane.showMessageDialog(null, "number input Min Compaction Threshold.");
382 | minCompactionThresholdText.requestFocus();
383 | return;
384 | }
385 | cf.setMinCompactionThreshold(minCompactionThresholdText.getText());
386 | }
387 |
388 | // Max Compaction Threshold
389 | if (!maxCompactionThresholdText.getText().isEmpty()) {
390 | try {
391 | Integer.valueOf(maxCompactionThresholdText.getText());
392 | } catch (NumberFormatException e) {
393 | JOptionPane.showMessageDialog(null, "number input Max Compaction Threshold.");
394 | maxCompactionThresholdText.requestFocus();
395 | return;
396 | }
397 | cf.setMaxCompactionThreshold(maxCompactionThresholdText.getText());
398 | }
399 |
400 | setVisible(false);
401 | cancel = false;
402 | }
403 |
404 | /**
405 | * @return the cancel
406 | */
407 | public boolean isCancel() {
408 | return cancel;
409 | }
410 |
411 | /**
412 | * @return the info
413 | */
414 | public ColumnFamily getColumnFamily() {
415 | return cf;
416 | }
417 | }
418 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/gui/component/panel/KeyspaceTreePanel.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.gui.component.panel;
2 |
3 | import java.awt.Cursor;
4 | import java.awt.Dimension;
5 | import java.awt.event.ActionEvent;
6 | import java.awt.event.MouseAdapter;
7 | import java.awt.event.MouseEvent;
8 | import java.util.ArrayList;
9 | import java.util.Collections;
10 | import java.util.List;
11 | import java.util.Set;
12 |
13 | import javax.swing.AbstractAction;
14 | import javax.swing.Action;
15 | import javax.swing.JOptionPane;
16 | import javax.swing.JPanel;
17 | import javax.swing.JPopupMenu;
18 | import javax.swing.JScrollPane;
19 | import javax.swing.JTree;
20 | import javax.swing.SwingUtilities;
21 | import javax.swing.event.TreeSelectionEvent;
22 | import javax.swing.event.TreeSelectionListener;
23 | import javax.swing.tree.DefaultMutableTreeNode;
24 | import javax.swing.tree.DefaultTreeModel;
25 | import javax.swing.tree.TreePath;
26 |
27 | import org.apache.cassandra.client.Client;
28 | import org.apache.cassandra.gui.component.dialog.ColumnFamilyDialog;
29 | import org.apache.cassandra.gui.component.dialog.KeyDialog;
30 | import org.apache.cassandra.gui.component.dialog.KeyRangeDialog;
31 | import org.apache.cassandra.gui.component.dialog.KeyspaceDialog;
32 | import org.apache.cassandra.gui.control.callback.PropertiesCallback;
33 | import org.apache.cassandra.gui.control.callback.RepaintCallback;
34 | import org.apache.cassandra.gui.control.callback.SelectedColumnFamilyCallback;
35 | import org.apache.cassandra.thrift.InvalidRequestException;
36 | import org.apache.cassandra.thrift.KsDef;
37 | import org.apache.cassandra.thrift.NotFoundException;
38 | import org.apache.cassandra.unit.ColumnFamily;
39 | import org.apache.thrift.TException;
40 |
41 | /**
42 | * The keyspace panel
43 | */
44 | public class KeyspaceTreePanel extends JPanel implements TreeSelectionListener {
45 | private static final long serialVersionUID = 5481365703729222288L;
46 |
47 | private class PopupAction extends AbstractAction {
48 | private static final long serialVersionUID = 4235052996425858520L;
49 |
50 | public static final int OPERATION_ROWS = 1;
51 | public static final int OPERATION_KEYRANGE = 2;
52 | public static final int OPERATION_KEY = 3;
53 | public static final int OPERATION_CREATE_KEYSPACE = 4;
54 | public static final int OPERATION_REMOVE_KEYSPACE = 5;
55 | public static final int OPERATION_UPDATE_KEYSPACE = 6;
56 | public static final int OPERAITON_CREATE_COLUMN_FAMILY = 7;
57 | public static final int OPERATION_REMOVE_COLUMN_FAMILY = 8;
58 | public static final int OPERATION_TRUNCATE_COLUMN_FAMILY = 9;
59 | public static final int OPERATION_UPDATE_COLUMN_FAMILY = 10;
60 | public static final int OPERATION_REFRESH_CLUSTER = 11;
61 |
62 | public static final int ROWS_1000 = 1000;
63 |
64 | private int operation;
65 | private DefaultMutableTreeNode node;
66 |
67 | public PopupAction(String name, int operation, DefaultMutableTreeNode node) {
68 | this.operation = operation;
69 | this.node = node;
70 | putValue(Action.NAME, name);
71 | }
72 |
73 | @Override
74 | public void actionPerformed(ActionEvent e) {
75 | int status = 0;
76 | KeyspaceDialog ksd = null;
77 | ColumnFamilyDialog cfd = null;
78 | switch (operation) {
79 | case OPERATION_CREATE_KEYSPACE:
80 | ksd = new KeyspaceDialog();
81 | ksd.setVisible(true);
82 | if (ksd.isCancel()) {
83 | return;
84 | }
85 |
86 | try {
87 | client.addKeyspace(ksd.getKeyspaceName(),
88 | ksd.getStrategy(),
89 | ksd.getStrategyOptions(),
90 | ksd.getReplicationFactor());
91 | } catch (Exception ex) {
92 | JOptionPane.showMessageDialog(null, "error: " + ex.toString());
93 | ex.printStackTrace();
94 | return;
95 | }
96 |
97 | node.add(new DefaultMutableTreeNode(ksd.getKeyspaceName()));
98 | treeModel.reload(node);
99 | break;
100 | case OPERATION_UPDATE_KEYSPACE:
101 | KsDef ksDef;
102 | try {
103 | ksDef = client.describeKeyspace(lastSelectedKeysapce);
104 | } catch (Exception ex) {
105 | JOptionPane.showMessageDialog(null, "error: " + ex.toString());
106 | ex.printStackTrace();
107 | return;
108 | }
109 |
110 | ksd = new KeyspaceDialog(lastSelectedKeysapce,
111 | ksDef.getReplication_factor(),
112 | ksDef.getStrategy_class(),
113 | ksDef.getStrategy_options());
114 | ksd.setVisible(true);
115 | if (ksd.isCancel()) {
116 | return;
117 | }
118 |
119 | try {
120 | client.updateKeyspace(ksd.getKeyspaceName(),
121 | ksd.getStrategy(),
122 | ksd.getStrategyOptions(),
123 | ksd.getReplicationFactor());
124 | } catch (Exception ex) {
125 | JOptionPane.showMessageDialog(null, "error: " + ex.toString());
126 | ex.printStackTrace();
127 | return;
128 | }
129 |
130 | propertiesCallback.keyspaceCallback(lastSelectedKeysapce);
131 | break;
132 | case OPERATION_REMOVE_KEYSPACE:
133 | if (lastSelectedKeysapce == null) {
134 | return;
135 | }
136 | status = JOptionPane.showConfirmDialog(null,
137 | "Delete a keyspace " + lastSelectedKeysapce + "?",
138 | "confirm",
139 | JOptionPane.YES_NO_OPTION,
140 | JOptionPane.QUESTION_MESSAGE);
141 | if (status == JOptionPane.YES_OPTION) {
142 | try {
143 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
144 | client.dropKeyspace(lastSelectedKeysapce);
145 | deletedKeyspace = lastSelectedKeysapce;
146 | DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
147 | node.removeFromParent();
148 | treeModel.reload(parent);
149 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
150 | } catch (Exception ex) {
151 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
152 | JOptionPane.showMessageDialog(null, "error: " + ex.toString());
153 | ex.printStackTrace();
154 | return;
155 | }
156 | }
157 |
158 | break;
159 | case OPERATION_REFRESH_CLUSTER:
160 | refreshTree();
161 | break;
162 | case OPERAITON_CREATE_COLUMN_FAMILY:
163 | if (lastSelectedKeysapce == null) {
164 | return;
165 | }
166 |
167 | cfd = new ColumnFamilyDialog();
168 | cfd.setVisible(true);
169 | if (cfd.isCancel()) {
170 | return;
171 | }
172 |
173 | try {
174 | client.addColumnFamily(lastSelectedKeysapce, cfd.getColumnFamily());
175 | } catch (Exception ex) {
176 | JOptionPane.showMessageDialog(null, "error: " + ex.toString());
177 | ex.printStackTrace();
178 | return;
179 | }
180 |
181 | node.add(new DefaultMutableTreeNode(cfd.getColumnFamily().getColumnFamilyName()));
182 | treeModel.reload(node);
183 | break;
184 | case OPERATION_UPDATE_COLUMN_FAMILY:
185 | if (lastSelectedKeysapce == null ||
186 | lastSelectedColumnFamily == null) {
187 | return;
188 | }
189 |
190 | ColumnFamily cf = null;
191 | try {
192 | cf = client.getColumnFamilyBean(lastSelectedKeysapce, lastSelectedColumnFamily);
193 | } catch (Exception ex) {
194 | JOptionPane.showMessageDialog(null, "error: " + ex.toString());
195 | ex.printStackTrace();
196 | return;
197 | }
198 |
199 | cfd = new ColumnFamilyDialog(cf);
200 | cfd.setVisible(true);
201 | if (cfd.isCancel()) {
202 | return;
203 | }
204 |
205 | try {
206 | client.updateColumnFamily(lastSelectedKeysapce, cfd.getColumnFamily());
207 | } catch (Exception ex) {
208 | JOptionPane.showMessageDialog(null, "error: " + ex.toString());
209 | ex.printStackTrace();
210 | return;
211 | }
212 | break;
213 | case OPERATION_REMOVE_COLUMN_FAMILY:
214 | if (lastSelectedKeysapce == null ||
215 | lastSelectedColumnFamily == null) {
216 | return;
217 | }
218 |
219 | status = JOptionPane.showConfirmDialog(null,
220 | "Delete a column family " + lastSelectedColumnFamily + "?",
221 | "confirm",
222 | JOptionPane.YES_NO_OPTION,
223 | JOptionPane.QUESTION_MESSAGE);
224 | if (status == JOptionPane.YES_OPTION) {
225 | try {
226 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
227 | client.dropColumnFamily(lastSelectedKeysapce, lastSelectedColumnFamily);
228 | deletedColumnFamily = lastSelectedColumnFamily;
229 | DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
230 | node.removeFromParent();
231 | treeModel.reload(parent);
232 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
233 | } catch (Exception ex) {
234 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
235 | JOptionPane.showMessageDialog(null, "error: " + ex.toString());
236 | ex.printStackTrace();
237 | return;
238 | }
239 | }
240 | break;
241 | case OPERATION_TRUNCATE_COLUMN_FAMILY:
242 | if (lastSelectedKeysapce == null ||
243 | lastSelectedColumnFamily == null) {
244 | return;
245 | }
246 |
247 | status = JOptionPane.showConfirmDialog(null,
248 | "truncarte column family " + lastSelectedColumnFamily + "?",
249 | "confirm",
250 | JOptionPane.YES_NO_OPTION,
251 | JOptionPane.QUESTION_MESSAGE);
252 | if (status == JOptionPane.YES_OPTION) {
253 | try {
254 | setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
255 | client.truncateColumnFamily(lastSelectedKeysapce, lastSelectedColumnFamily);
256 | cCallback.rangeCallback(lastSelectedKeysapce,
257 | lastSelectedColumnFamily,
258 | "",
259 | "",
260 | ROWS_1000);
261 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
262 | } catch (Exception ex) {
263 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
264 | JOptionPane.showMessageDialog(null, "error: " + ex.toString());
265 | ex.printStackTrace();
266 | return;
267 | }
268 | }
269 | break;
270 | case OPERATION_ROWS:
271 | case OPERATION_KEYRANGE:
272 | if (lastSelectedKeysapce == null ||
273 | lastSelectedColumnFamily == null) {
274 | return;
275 | }
276 |
277 | String startKey = "";
278 | String endKey = "";
279 |
280 | if (operation == OPERATION_KEYRANGE) {
281 | KeyRangeDialog krd = new KeyRangeDialog();
282 | krd.setVisible(true);
283 | if (krd.isCancel()) {
284 | return;
285 | }
286 |
287 | startKey = krd.getStartKey();
288 | endKey = krd.getEndKey();
289 | }
290 |
291 | cCallback.rangeCallback(lastSelectedKeysapce,
292 | lastSelectedColumnFamily,
293 | startKey,
294 | endKey,
295 | ROWS_1000);
296 | break;
297 | case OPERATION_KEY:
298 | if (lastSelectedKeysapce == null ||
299 | lastSelectedColumnFamily == null) {
300 | return;
301 | }
302 |
303 | KeyDialog kd = new KeyDialog();
304 | kd.setVisible(true);
305 | if (kd.isCancel()) {
306 | return;
307 | }
308 |
309 | cCallback.getCacllback(lastSelectedKeysapce,
310 | lastSelectedColumnFamily,
311 | kd.getkey());
312 | break;
313 | }
314 | }
315 | }
316 |
317 | private class MousePopup extends MouseAdapter {
318 | @Override
319 | public void mouseReleased(MouseEvent e) {
320 | if (SwingUtilities.isRightMouseButton(e)) {
321 | TreePath path = tree.getPathForLocation(e.getX(), e.getY());
322 | if (path == null) {
323 | return;
324 | }
325 |
326 | tree.setSelectionPath(path);
327 | DefaultMutableTreeNode node =
328 | (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
329 | JPopupMenu popup = new JPopupMenu();
330 | switch (path.getPathCount()) {
331 | case TREE_CLUSTER:
332 | popup.add(new PopupAction("create keysapce", PopupAction.OPERATION_CREATE_KEYSPACE, node));
333 | popup.add(new PopupAction("refresh", PopupAction.OPERATION_REFRESH_CLUSTER, node));
334 | popup.show(e.getComponent(), e.getX(), e.getY());
335 | break;
336 | case TREE_KEYSPACE:
337 | lastSelectedKeysapce = (String) node.getUserObject();
338 | popup.add(new PopupAction("properties", PopupAction.OPERATION_UPDATE_KEYSPACE, node));
339 | popup.add(new PopupAction("remove", PopupAction.OPERATION_REMOVE_KEYSPACE, node));
340 | popup.add(new PopupAction("create column family", PopupAction.OPERAITON_CREATE_COLUMN_FAMILY, node));
341 | popup.add(new PopupAction("refresh", PopupAction.OPERATION_REFRESH_CLUSTER, node));
342 | popup.show(e.getComponent(), e.getX(), e.getY());
343 | break;
344 | case TREE_COLUMN_FAMILY:
345 | String columnFamily = (String) node.getUserObject();
346 | DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
347 | lastSelectedKeysapce = (String) parent.getUserObject();
348 | lastSelectedColumnFamily = columnFamily;
349 |
350 | popup.add(new PopupAction("show 1000 rows", PopupAction.OPERATION_ROWS, node));
351 | popup.add(new PopupAction("key range rows", PopupAction.OPERATION_KEYRANGE, node));
352 | popup.add(new PopupAction("get key", PopupAction.OPERATION_KEY, node));
353 | popup.add(new PopupAction("properties", PopupAction.OPERATION_UPDATE_COLUMN_FAMILY, node));
354 | popup.add(new PopupAction("truncate column family", PopupAction.OPERATION_TRUNCATE_COLUMN_FAMILY, node));
355 | popup.add(new PopupAction("remove column family", PopupAction.OPERATION_REMOVE_COLUMN_FAMILY, node));
356 | popup.show(e.getComponent(), e.getX(), e.getY());
357 | break;
358 | default:
359 | lastSelectedKeysapce = null;
360 | lastSelectedColumnFamily = null;
361 | break;
362 | }
363 | }
364 | }
365 | }
366 |
367 | private static final int TREE_CLUSTER = 1;
368 | private static final int TREE_KEYSPACE = 2;
369 | private static final int TREE_COLUMN_FAMILY = 3;
370 |
371 | private Client client;
372 |
373 | private PropertiesCallback propertiesCallback;
374 | private SelectedColumnFamilyCallback cCallback;
375 | private RepaintCallback rCallback;
376 |
377 | private JScrollPane scrollPane;
378 | private String lastSelectedKeysapce;
379 | private String lastSelectedColumnFamily;
380 | private String deletedKeyspace;
381 | private String deletedColumnFamily;
382 | private JTree tree;
383 | private DefaultTreeModel treeModel;
384 |
385 | public KeyspaceTreePanel(Client client) {
386 | this.client = client;
387 | createTree();
388 | }
389 |
390 | public void createTree() {
391 | try {
392 | DefaultMutableTreeNode clusterNode =
393 | new DefaultMutableTreeNode(client.describeClusterName());
394 | treeModel = new DefaultTreeModel(clusterNode);
395 | tree = new JTree(treeModel);
396 | tree.setRootVisible(true);
397 | tree.addMouseListener(new MousePopup());
398 | tree.addTreeSelectionListener(this);
399 |
400 | List ks = null;
401 | try {
402 | ks = new ArrayList(client.getKeyspaces());
403 | } catch (InvalidRequestException e) {
404 | //TODO - Handle eligantly
405 | e.printStackTrace();
406 | }
407 | Collections.sort(ks);
408 | for (KsDef keyspace : ks) {
409 | DefaultMutableTreeNode keyspaceNode = new DefaultMutableTreeNode(keyspace.getName());
410 | clusterNode.add(keyspaceNode);
411 | try {
412 | Set cfs = client.getColumnFamilys(keyspace.getName());
413 | for (String columnFamily : cfs) {
414 | keyspaceNode.add(new DefaultMutableTreeNode(columnFamily));
415 | }
416 | } catch (NotFoundException e) {
417 | JOptionPane.showMessageDialog(null, "error: " + e.getMessage());
418 | e.printStackTrace();
419 | } catch (InvalidRequestException invReq) {
420 | JOptionPane.showMessageDialog(null, "error: " + invReq.getMessage());
421 | invReq.printStackTrace();
422 | }
423 | }
424 | } catch (TException e) {
425 | JOptionPane.showMessageDialog(null, "error: " + e.getMessage());
426 | e.printStackTrace();
427 | return;
428 | }
429 |
430 | scrollPane = new JScrollPane();
431 | scrollPane.getViewport().setView(tree);
432 | add(scrollPane);
433 | repaint();
434 | }
435 |
436 | public void refreshTree() {
437 | try {
438 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) treeModel.getRoot();
439 | node.removeAllChildren();
440 |
441 | List ks = null;
442 | try {
443 | ks = new ArrayList(client.getKeyspaces());
444 | } catch (InvalidRequestException e) {
445 | e.printStackTrace();
446 | }
447 | Collections.sort(ks);
448 | for (KsDef keyspace : ks) {
449 | DefaultMutableTreeNode keyspaceNode = new DefaultMutableTreeNode(keyspace.getName());
450 | node.add(keyspaceNode);
451 | try {
452 | Set cfs = client.getColumnFamilys(keyspace.getName());
453 | for (String columnFamily : cfs) {
454 | keyspaceNode.add(new DefaultMutableTreeNode(columnFamily));
455 | }
456 | } catch (NotFoundException e) {
457 | JOptionPane.showMessageDialog(null, "error: " + e.getMessage());
458 | e.printStackTrace();
459 | } catch (InvalidRequestException invReq) {
460 | JOptionPane.showMessageDialog(null, "error: " + invReq.getMessage());
461 | invReq.printStackTrace();
462 | }
463 | }
464 | treeModel.reload();
465 | } catch (TException e) {
466 | JOptionPane.showMessageDialog(null, "error: " + e.getMessage());
467 | e.printStackTrace();
468 | return;
469 | }
470 | }
471 |
472 | @Override
473 | public void valueChanged(TreeSelectionEvent e) {
474 | String keyspace;
475 | String columnFamily;
476 |
477 | switch (e.getPath().getPathCount()) {
478 | case TREE_CLUSTER:
479 | propertiesCallback.clusterCallback();
480 | break;
481 | case TREE_KEYSPACE:
482 | keyspace = e.getPath().getPath()[TREE_KEYSPACE - 1].toString();
483 | if (!keyspace.equals(deletedKeyspace)) {
484 | propertiesCallback.keyspaceCallback(keyspace);
485 | }
486 | break;
487 | case TREE_COLUMN_FAMILY:
488 | keyspace = e.getPath().getPath()[TREE_KEYSPACE - 1].toString();
489 | columnFamily = e.getPath().getPath()[TREE_COLUMN_FAMILY - 1].toString();
490 | if (!columnFamily.equals(deletedColumnFamily)) {
491 | propertiesCallback.columnFamilyCallback(keyspace, columnFamily);
492 | }
493 | break;
494 | }
495 | }
496 |
497 | @Override
498 | public void repaint() {
499 | if (scrollPane != null && rCallback != null) {
500 | Dimension d = rCallback.callback();
501 | scrollPane.setPreferredSize(new Dimension(d.width - 10,
502 | d.height - 10));
503 | scrollPane.repaint();
504 | }
505 | super.repaint();
506 | }
507 |
508 | /**
509 | * @param propertiesCallback the propertiesCallback to set
510 | */
511 | public void setPropertiesCallback(PropertiesCallback propertiesCallback) {
512 | this.propertiesCallback = propertiesCallback;
513 | }
514 |
515 | /**
516 | * @param cCallback the cCallback to set
517 | */
518 | public void setcCallback(SelectedColumnFamilyCallback cCallback) {
519 | this.cCallback = cCallback;
520 | }
521 |
522 | /**
523 | * @param rCallback the rCallback to set
524 | */
525 | public void setrCallback(RepaintCallback rCallback) {
526 | this.rCallback = rCallback;
527 | }
528 | }
529 |
--------------------------------------------------------------------------------
/src/main/java/org/apache/cassandra/client/Client.java:
--------------------------------------------------------------------------------
1 | package org.apache.cassandra.client;
2 |
3 | import java.io.IOException;
4 | import java.io.UnsupportedEncodingException;
5 | import java.lang.management.MemoryUsage;
6 | import java.nio.ByteBuffer;
7 | import java.util.ArrayList;
8 | import java.util.Collections;
9 | import java.util.Date;
10 | import java.util.HashMap;
11 | import java.util.Iterator;
12 | import java.util.LinkedList;
13 | import java.util.List;
14 | import java.util.Map;
15 | import java.util.Map.Entry;
16 | import java.util.Set;
17 | import java.util.TreeMap;
18 | import java.util.TreeSet;
19 |
20 | import org.apache.cassandra.concurrent.IExecutorMBean;
21 | import org.apache.cassandra.concurrent.JMXEnabledThreadPoolExecutorMBean;
22 | import org.apache.cassandra.config.ConfigurationException;
23 | import org.apache.cassandra.db.marshal.AbstractCompositeType;
24 | import org.apache.cassandra.db.marshal.AbstractType;
25 | import org.apache.cassandra.db.marshal.TypeParser;
26 | import org.apache.cassandra.dht.Token;
27 | import org.apache.cassandra.node.NodeInfo;
28 | import org.apache.cassandra.node.RingNode;
29 | import org.apache.cassandra.node.Tpstats;
30 | import org.apache.cassandra.thrift.Cassandra;
31 | import org.apache.cassandra.thrift.CfDef;
32 | import org.apache.cassandra.thrift.Column;
33 | import org.apache.cassandra.thrift.ColumnDef;
34 | import org.apache.cassandra.thrift.ColumnOrSuperColumn;
35 | import org.apache.cassandra.thrift.ColumnParent;
36 | import org.apache.cassandra.thrift.ColumnPath;
37 | import org.apache.cassandra.thrift.ConsistencyLevel;
38 | import org.apache.cassandra.thrift.InvalidRequestException;
39 | import org.apache.cassandra.thrift.KeyRange;
40 | import org.apache.cassandra.thrift.KeySlice;
41 | import org.apache.cassandra.thrift.KsDef;
42 | import org.apache.cassandra.thrift.NotFoundException;
43 | import org.apache.cassandra.thrift.SchemaDisagreementException;
44 | import org.apache.cassandra.thrift.SlicePredicate;
45 | import org.apache.cassandra.thrift.SliceRange;
46 | import org.apache.cassandra.thrift.SuperColumn;
47 | import org.apache.cassandra.thrift.TimedOutException;
48 | import org.apache.cassandra.thrift.TokenRange;
49 | import org.apache.cassandra.thrift.UnavailableException;
50 | import org.apache.cassandra.tools.NodeProbe;
51 | import org.apache.cassandra.unit.Cell;
52 | import org.apache.cassandra.unit.ColumnFamily;
53 | import org.apache.cassandra.unit.ColumnFamilyMetaData;
54 | import org.apache.cassandra.unit.Key;
55 | import org.apache.cassandra.unit.SColumn;
56 | import org.apache.thrift.TException;
57 | import org.apache.thrift.protocol.TBinaryProtocol;
58 | import org.apache.thrift.protocol.TProtocol;
59 | import org.apache.thrift.transport.TFramedTransport;
60 | import org.apache.thrift.transport.TSocket;
61 | import org.apache.thrift.transport.TTransport;
62 | import org.apache.thrift.transport.TTransportException;
63 |
64 | /**
65 | * Client class to interact with Cassandara cluster
66 | *
67 | */
68 | public class Client {
69 | public static final String DEFAULT_THRIFT_HOST = "localhost";
70 | public static final int DEFAULT_THRIFT_PORT = 9160;
71 | public static final int DEFAULT_JMX_PORT = 7199;
72 |
73 | public enum ColumnType {
74 | SUPER("Super"),
75 | STANDARD("Standard");
76 |
77 | private String type;
78 |
79 | private ColumnType(String type) {
80 | this.type = type;
81 | }
82 |
83 | public String toString() {
84 | return type;
85 | }
86 | }
87 |
88 | private TTransport transport;
89 | private TProtocol protocol;
90 | private Cassandra.Client client;
91 | private NodeProbe probe;
92 |
93 | private boolean connected = false;
94 | private String host;
95 | private int thriftPort;
96 | private int jmxPort;
97 |
98 | private String keyspace;
99 | private String columnFamily;
100 | private boolean superColumn;
101 |
102 | public Client() {
103 | this(DEFAULT_THRIFT_HOST, DEFAULT_THRIFT_PORT, DEFAULT_JMX_PORT);
104 | }
105 |
106 | public Client(String host) {
107 | this(host, DEFAULT_THRIFT_PORT, DEFAULT_JMX_PORT);
108 | }
109 |
110 | public Client(String host, int thriftPort, int jmxPort) {
111 | this.host = host;
112 | this.thriftPort = thriftPort;
113 | this.jmxPort = jmxPort;
114 | }
115 |
116 | public void connect()
117 | throws TTransportException, IOException, InterruptedException {
118 | if (!connected) {
119 | // Updating the transport to Framed one as it has been depreciated with Cassandra 0.7.0
120 | transport = new TFramedTransport(new TSocket(host, thriftPort));
121 | protocol = new TBinaryProtocol(transport);
122 | client = new Cassandra.Client(protocol);
123 | probe = new NodeProbe(host, jmxPort);
124 | transport.open();
125 | connected = true;
126 | }
127 | }
128 |
129 | public void disconnect() {
130 | if (connected) {
131 | transport.close();
132 | connected = false;
133 | }
134 | }
135 |
136 | public boolean isConnected() {
137 | return connected;
138 | }
139 |
140 | public String describeClusterName() throws TException {
141 | return client.describe_cluster_name();
142 | }
143 |
144 | public String descriveVersion() throws TException {
145 | return client.describe_version();
146 | }
147 |
148 | public String describeSnitch() throws TException {
149 | return client.describe_snitch();
150 | }
151 |
152 | public Map> describeSchemaVersions() throws InvalidRequestException, TException {
153 | return client.describe_schema_versions();
154 | }
155 |
156 | public String describePartitioner() throws TException {
157 | return client.describe_partitioner();
158 | }
159 |
160 | public List describeRing(String keyspace)
161 | throws TException, InvalidRequestException {
162 | this.keyspace = keyspace;
163 | return client.describe_ring(keyspace);
164 | }
165 |
166 | public RingNode listRing() {
167 | RingNode r = new RingNode();
168 | r.setRangeMap(probe.getTokenToEndpointMap());
169 | List ranges = new ArrayList(r.getRangeMap().keySet());
170 | Collections.sort(ranges);
171 | r.setRanges(ranges);
172 |
173 | r.setLiveNodes(probe.getLiveNodes());
174 | r.setDeadNodes(probe.getUnreachableNodes());
175 | r.setLoadMap(probe.getLoadMap());
176 |
177 | return r;
178 | }
179 |
180 | public NodeInfo getNodeInfo(String endpoint) throws IOException, InterruptedException {
181 | NodeProbe p = new NodeProbe(endpoint, jmxPort);
182 |
183 | NodeInfo ni = new NodeInfo();
184 | ni.setEndpoint(endpoint);
185 | ni.setLoad(p.getLoadString());
186 | ni.setGenerationNumber(p.getCurrentGenerationNumber());
187 | ni.setUptime(p.getUptime() / 1000);
188 |
189 | MemoryUsage heapUsage = p.getHeapMemoryUsage();
190 | ni.setMemUsed((double) heapUsage.getUsed() / (1024 * 1024));
191 | ni.setMemMax((double) heapUsage.getMax() / (1024 * 1024));
192 |
193 | return ni;
194 | }
195 |
196 | public List getTpstats(String endpoint) throws IOException, InterruptedException {
197 | List l = new ArrayList();
198 |
199 | NodeProbe p = new NodeProbe(endpoint, jmxPort);
200 | Iterator> threads = p.getThreadPoolMBeanProxies();
201 | for (;threads.hasNext();) {
202 | Entry thread = threads.next();
203 |
204 | Tpstats tp = new Tpstats();
205 | tp.setPoolName(thread.getKey());
206 |
207 | IExecutorMBean threadPoolProxy = thread.getValue();
208 | tp.setActiveCount(threadPoolProxy.getActiveCount());
209 | tp.setPendingTasks(threadPoolProxy.getPendingTasks());
210 | tp.setCompletedTasks(threadPoolProxy.getCompletedTasks());
211 | l.add(tp);
212 | }
213 |
214 | return l;
215 | }
216 |
217 | public List getKeyspaces() throws TException, InvalidRequestException {
218 | return client.describe_keyspaces();
219 | }
220 |
221 | public KsDef describeKeyspace(String keyspaceName) throws NotFoundException, InvalidRequestException, TException {
222 | return client.describe_keyspace(keyspaceName);
223 | }
224 |
225 | public void addKeyspace(String keyspaceName,
226 | String strategy,
227 | Map strategyOptions,
228 | int replicationFactgor) throws InvalidRequestException, TException, SchemaDisagreementException {
229 | KsDef ksDef = new KsDef(keyspaceName, strategy, new LinkedList());
230 | ksDef.setReplication_factor(replicationFactgor); // TODO should be provided in strategy options
231 | if (strategyOptions != null) {
232 | ksDef.setStrategy_options(strategyOptions);
233 | }
234 |
235 | client.system_add_keyspace(ksDef);
236 | }
237 |
238 | public void updateKeyspace(String keyspaceName,
239 | String strategy,
240 | Map strategyOptions,
241 | int replicationFactgor) throws InvalidRequestException, TException, SchemaDisagreementException {
242 | KsDef ksDef = new KsDef(keyspaceName, strategy, new LinkedList());
243 | ksDef.setReplication_factor(replicationFactgor); // TODO should be provided in strategy options
244 | if (strategyOptions != null) {
245 | ksDef.setStrategy_options(strategyOptions);
246 | }
247 |
248 | client.system_update_keyspace(ksDef);
249 | }
250 |
251 | public void dropKeyspace(String keyspaceName) throws InvalidRequestException, TException, SchemaDisagreementException {
252 | client.system_drop_keyspace(keyspaceName);
253 | }
254 |
255 | public void addColumnFamily(String keyspaceName,
256 | ColumnFamily cf) throws InvalidRequestException, TException, SchemaDisagreementException {
257 | this.keyspace = keyspaceName;
258 | CfDef cfDef = new CfDef(keyspaceName, cf.getColumnFamilyName());
259 | cfDef.setColumn_type(cf.getColumnType());
260 |
261 | if (!isEmpty(cf.getComparator())) {
262 | cfDef.setComparator_type(cf.getComparator());
263 | }
264 |
265 | if (cf.getComparator().equals(ColumnType.SUPER)) {
266 | if (!isEmpty(cf.getSubcomparator())) {
267 | cfDef.setSubcomparator_type(cf.getSubcomparator());
268 | }
269 | }
270 |
271 | if (!isEmpty(cf.getComment())) {
272 | cfDef.setComment(cf.getComment());
273 | }
274 |
275 | if (!isEmpty(cf.getRowsCached())) {
276 | cfDef.setRow_cache_size(Double.valueOf(cf.getRowsCached()));
277 | }
278 |
279 | if (!isEmpty(cf.getRowCacheSavePeriod())) {
280 | cfDef.setRow_cache_save_period_in_seconds(Integer.valueOf(cf.getRowCacheSavePeriod()));
281 | }
282 |
283 | if (!isEmpty(cf.getKeysCached())) {
284 | cfDef.setKey_cache_size(Double.valueOf(cf.getKeysCached()));
285 | }
286 |
287 | if (!isEmpty(cf.getKeyCacheSavePeriod())) {
288 | cfDef.setKey_cache_save_period_in_seconds(Integer.valueOf(cf.getKeyCacheSavePeriod()));
289 | }
290 |
291 | if (!isEmpty(cf.getReadRepairChance())) {
292 | cfDef.setRead_repair_chance(Double.valueOf(cf.getReadRepairChance()));
293 | }
294 |
295 | if (!isEmpty(cf.getGcGrace())) {
296 | cfDef.setGc_grace_seconds(Integer.valueOf(cf.getGcGrace()));
297 | }
298 |
299 | if (!cf.getMetaDatas().isEmpty()) {
300 | List l = new ArrayList();
301 | for (ColumnFamilyMetaData metaData : cf.getMetaDatas()) {
302 | ColumnDef cd = new ColumnDef();
303 | cd.setName(metaData.getColumnName().getBytes());
304 |
305 | if (metaData.getValiDationClass() != null) {
306 | cd.setValidation_class(metaData.getValiDationClass());
307 | }
308 | if (metaData.getIndexType() != null) {
309 | cd.setIndex_type(metaData.getIndexType());
310 | }
311 | if (metaData.getIndexName() != null) {
312 | cd.setIndex_name(metaData.getIndexName());
313 | }
314 |
315 | l.add(cd);
316 | }
317 | cfDef.setColumn_metadata(l);
318 | }
319 |
320 | if (!isEmpty(cf.getMemtableOperations())) {
321 | // FIXME @Deprecated cfDef.setMemtable_operations_in_millions(Double.valueOf(cf.getMemtableOperations()));
322 | }
323 |
324 | if (!isEmpty(cf.getMemtableThroughput())) {
325 | // FIXME @Deprecated cfDef.setMemtable_throughput_in_mb(Integer.valueOf(cf.getMemtableThroughput()));
326 | }
327 |
328 | if (!isEmpty(cf.getMemtableFlushAfter())) {
329 | // FIXME @Deprecated cfDef.setMemtable_flush_after_mins(Integer.valueOf(cf.getMemtableFlushAfter()));
330 | }
331 |
332 | if (!isEmpty(cf.getDefaultValidationClass())) {
333 | cfDef.setDefault_validation_class(cf.getDefaultValidationClass());
334 | }
335 |
336 | if (!isEmpty(cf.getMinCompactionThreshold())) {
337 | cfDef.setMin_compaction_threshold(Integer.valueOf(cf.getMinCompactionThreshold()));
338 | }
339 |
340 | if (!isEmpty(cf.getMaxCompactionThreshold())) {
341 | cfDef.setMax_compaction_threshold(Integer.valueOf(cf.getMaxCompactionThreshold()));
342 | }
343 |
344 | client.set_keyspace(keyspaceName);
345 | client.system_add_column_family(cfDef);
346 | }
347 |
348 | public void updateColumnFamily(String keyspaceName,
349 | ColumnFamily cf) throws InvalidRequestException, TException, SchemaDisagreementException {
350 | this.keyspace = keyspaceName;
351 | CfDef cfDef = new CfDef(keyspaceName, cf.getColumnFamilyName());
352 | cfDef.setId(cf.getId());
353 | cfDef.setColumn_type(cf.getColumnType());
354 |
355 | if (!isEmpty(cf.getComparator())) {
356 | cfDef.setComparator_type(cf.getComparator());
357 | }
358 |
359 | if (cf.getColumnType().equalsIgnoreCase(ColumnType.SUPER.name())) {
360 | if (!isEmpty(cf.getSubcomparator())) {
361 | cfDef.setSubcomparator_type(cf.getSubcomparator());
362 | }
363 | }
364 |
365 | if (!isEmpty(cf.getComment())) {
366 | cfDef.setComment(cf.getComment());
367 | }
368 |
369 | if (!isEmpty(cf.getRowsCached())) {
370 | cfDef.setRow_cache_size(Double.valueOf(cf.getRowsCached()));
371 | }
372 |
373 | if (!isEmpty(cf.getRowCacheSavePeriod())) {
374 | cfDef.setRow_cache_save_period_in_seconds(Integer.valueOf(cf.getRowCacheSavePeriod()));
375 | }
376 |
377 | if (!isEmpty(cf.getKeysCached())) {
378 | cfDef.setKey_cache_size(Double.valueOf(cf.getKeysCached()));
379 | }
380 |
381 | if (!isEmpty(cf.getKeyCacheSavePeriod())) {
382 | cfDef.setKey_cache_save_period_in_seconds(Integer.valueOf(cf.getKeyCacheSavePeriod()));
383 | }
384 |
385 | if (!isEmpty(cf.getReadRepairChance())) {
386 | cfDef.setRead_repair_chance(Double.valueOf(cf.getReadRepairChance()));
387 | }
388 |
389 | if (!isEmpty(cf.getGcGrace())) {
390 | cfDef.setGc_grace_seconds(Integer.valueOf(cf.getGcGrace()));
391 | }
392 |
393 | if (!cf.getMetaDatas().isEmpty()) {
394 | List l = new ArrayList();
395 | for (ColumnFamilyMetaData metaData : cf.getMetaDatas()) {
396 | ColumnDef cd = new ColumnDef();
397 | cd.setName(metaData.getColumnName().getBytes());
398 |
399 | if (metaData.getValiDationClass() != null) {
400 | cd.setValidation_class(metaData.getValiDationClass());
401 | }
402 | if (metaData.getIndexType() != null) {
403 | cd.setIndex_type(metaData.getIndexType());
404 | }
405 | if (metaData.getIndexName() != null) {
406 | cd.setIndex_name(metaData.getIndexName());
407 | }
408 |
409 | l.add(cd);
410 | }
411 | cfDef.setColumn_metadata(l);
412 | }
413 |
414 | if (!isEmpty(cf.getMemtableOperations())) {
415 | // FIXME @Deprecated cfDef.setMemtable_operations_in_millions(Double.valueOf(cf.getMemtableOperations()));
416 | }
417 |
418 | if (!isEmpty(cf.getMemtableThroughput())) {
419 | // FIXME @Deprecated cfDef.setMemtable_throughput_in_mb(Integer.valueOf(cf.getMemtableThroughput()));
420 | }
421 |
422 | if (!isEmpty(cf.getMemtableFlushAfter())) {
423 | // FIXME @Deprecated cfDef.setMemtable_flush_after_mins(Integer.valueOf(cf.getMemtableFlushAfter()));
424 | }
425 |
426 | if (!isEmpty(cf.getDefaultValidationClass())) {
427 | cfDef.setDefault_validation_class(cf.getDefaultValidationClass());
428 | }
429 |
430 | if (!isEmpty(cf.getMinCompactionThreshold())) {
431 | cfDef.setMin_compaction_threshold(Integer.valueOf(cf.getMinCompactionThreshold()));
432 | }
433 |
434 | if (!isEmpty(cf.getMaxCompactionThreshold())) {
435 | cfDef.setMax_compaction_threshold(Integer.valueOf(cf.getMaxCompactionThreshold()));
436 | }
437 |
438 | client.set_keyspace(keyspaceName);
439 | client.system_update_column_family(cfDef);
440 | }
441 |
442 | public void dropColumnFamily(String keyspaceName, String columnFamilyName) throws InvalidRequestException, TException, SchemaDisagreementException {
443 | this.keyspace = keyspaceName;
444 | client.set_keyspace(keyspaceName);
445 | client.system_drop_column_family(columnFamilyName);
446 | }
447 |
448 | public void truncateColumnFamily(String keyspaceName, String columnFamilyName)
449 | throws InvalidRequestException, TException, UnavailableException {
450 | this.keyspace = keyspaceName;
451 | this.columnFamily = columnFamilyName;
452 | client.set_keyspace(keyspaceName);
453 | client.truncate(columnFamilyName);
454 | }
455 |
456 | /**
457 | *
458 | * Retrieve Column metadata from a given keyspace
459 | *
460 | * @param keyspace
461 | * @param columnFamily
462 | * @return
463 | * @throws NotFoundException
464 | * @throws TException
465 | * @throws InvalidRequestException
466 | */
467 | public Map getColumnFamily(String keyspace, String columnFamily)
468 | throws NotFoundException, TException, InvalidRequestException {
469 | this.keyspace = keyspace;
470 | this.columnFamily = columnFamily;
471 |
472 | for (Iterator cfIterator = client.describe_keyspace(keyspace).getCf_defsIterator(); cfIterator.hasNext();) {
473 | CfDef next = cfIterator.next();
474 | if (columnFamily.equalsIgnoreCase(next.getName())) {
475 | Map columnMetadata = new HashMap();
476 |
477 | CfDef._Fields[] fields = CfDef._Fields.values();
478 |
479 | for (int i = 0; i < fields.length; i++) {
480 | CfDef._Fields field = fields[i];
481 | // FIXME jdl changed back to objects, will just have to track down other NPE, etc..
482 | // using string concat to avoin NPE, if the value is not null
483 | // need to find an elegant solution
484 | columnMetadata.put(field.name(), next.getFieldValue(field));
485 | }
486 |
487 | return columnMetadata;
488 | }
489 | }
490 | System.out.println("returning null");
491 | return null;
492 | }
493 |
494 | public ColumnFamily getColumnFamilyBean(String keyspace, String columnFamily)
495 | throws NotFoundException, TException, InvalidRequestException, UnsupportedEncodingException {
496 | this.keyspace = keyspace;
497 | this.columnFamily = columnFamily;
498 |
499 | for (Iterator cfIterator = client.describe_keyspace(keyspace).getCf_defsIterator(); cfIterator.hasNext();) {
500 | CfDef cd = cfIterator.next();
501 | if (columnFamily.equalsIgnoreCase(cd.getName())) {
502 | ColumnFamily cf = new ColumnFamily();
503 | cf.setId(cd.getId());
504 | cf.setColumnFamilyName(cd.getName());
505 | cf.setColumnType(cd.getColumn_type());
506 | cf.setComparator(cd.getComparator_type());
507 | cf.setSubcomparator(cd.getSubcomparator_type());
508 | cf.setComment(cd.getComment());
509 | cf.setRowsCached(String.valueOf(cd.getRow_cache_size()));
510 | cf.setRowCacheSavePeriod(String.valueOf(cd.getRow_cache_save_period_in_seconds()));
511 | cf.setKeysCached(String.valueOf(cd.getKey_cache_size()));
512 | cf.setKeyCacheSavePeriod(String.valueOf(cd.getKey_cache_save_period_in_seconds()));
513 | cf.setReadRepairChance(String.valueOf(cd.getRead_repair_chance()));
514 | cf.setGcGrace(String.valueOf(cd.getGc_grace_seconds()));
515 | // FIXME @Deprecated cf.setMemtableOperations(String.valueOf(cd.getMemtable_operations_in_millions()));
516 | // FIXME @Deprecated cf.setMemtableThroughput(String.valueOf(cd.getMemtable_throughput_in_mb()));
517 | // FIXME @Deprecated cf.setMemtableFlushAfter(String.valueOf(cd.getMemtable_flush_after_mins()));
518 | cf.setDefaultValidationClass(cd.getDefault_validation_class());
519 | cf.setMinCompactionThreshold(String.valueOf(cd.getMin_compaction_threshold()));
520 | cf.setMaxCompactionThreshold(String.valueOf(cd.getMax_compaction_threshold()));
521 | for (ColumnDef cdef : cd.getColumn_metadata()) {
522 | ColumnFamilyMetaData cfmd = new ColumnFamilyMetaData();
523 | cfmd.setColumnName(new String(cdef.getName(), "UTF8"));
524 | cfmd.setValiDationClass(cdef.getValidation_class());
525 | cfmd.setIndexType(cdef.getIndex_type());
526 | cfmd.setIndexName(cdef.getIndex_name());
527 | cf.getMetaDatas().add(cfmd);
528 | }
529 |
530 | return cf;
531 | }
532 | }
533 |
534 | System.out.println("returning null");
535 | return null;
536 | }
537 |
538 | public Set getColumnFamilys(String keyspace)
539 | throws NotFoundException, TException, InvalidRequestException {
540 | this.keyspace = keyspace;
541 |
542 | Set s = new TreeSet();
543 |
544 | for (Iterator cfIterator = client.describe_keyspace(keyspace).getCf_defsIterator(); cfIterator.hasNext();) {
545 | CfDef next = cfIterator.next();
546 | s.add(next.getName());
547 | }
548 | return s;
549 | }
550 |
551 | public int countColumnsRecord(String keyspace, String columnFamily, String key)
552 | throws InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException {
553 | this.keyspace = keyspace;
554 | this.columnFamily = columnFamily;
555 | Map cfdata = getColumnFamily(keyspace, columnFamily);
556 |
557 | ColumnParent colParent = new ColumnParent(columnFamily);
558 | //TODO - Verify if its working fine
559 | return client.get_count(getAsBytes(key, cfdata.get("KEY_VALIDATION_CLASS").toString()), colParent, null, ConsistencyLevel.ONE);
560 | }
561 |
562 | public int countSuperColumnsRecord(String keyspace, String columnFamily, String superColumn, String key)
563 | throws InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException {
564 | this.keyspace = keyspace;
565 | this.columnFamily = columnFamily;
566 | Map cfdata = getColumnFamily(keyspace, columnFamily);
567 |
568 | ColumnParent colParent = new ColumnParent(columnFamily);
569 | colParent.setSuper_column(superColumn.getBytes());
570 | // TODO - verify if its working fine
571 | return client.get_count(getAsBytes(key, cfdata.get("KEY_VALIDATION_CLASS").toString()), colParent, null, ConsistencyLevel.ONE);
572 | }
573 |
574 | @SuppressWarnings("unchecked")
575 | public Date insertColumn(String keyspace,
576 | String columnFamily,
577 | String key,
578 | String superColumn,
579 | String column,
580 | String value)
581 | throws InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException {
582 | this.keyspace = keyspace;
583 | this.columnFamily = columnFamily;
584 | Map cfdata = getColumnFamily(keyspace, columnFamily);
585 |
586 | ColumnParent parent;
587 |
588 | parent = new ColumnParent(columnFamily);
589 | long timestamp = System.currentTimeMillis() * 1000;
590 | Column col = null;
591 | if (superColumn != null) {
592 | parent.setSuper_column(getAsBytes(superColumn, cfdata.get("COMPARATOR_TYPE").toString()));
593 | col = new Column(getAsBytes(column, cfdata.get("SUBCOMPARATOR_TYPE").toString()));
594 | } else {
595 | col = new Column(getAsBytes(column, cfdata.get("COMPARATOR_TYPE").toString()));
596 | }
597 |
598 | // FIXME.... need to somehow allow a default of UTF8, helps to enter values, perhaps when default is BYTES? cause want to Long if that is the default
599 | // TODO add functionality similar to cli 'assume' command, doesn't change column family definition, only affects the gui, would want for reads as well
600 | // can easily override with metadata...
601 | col.setValue(getAsBytes(value, getValidationType(col.bufferForName(), (List)cfdata.get("COLUMN_METADATA"), cfdata.get("DEFAULT_VALIDATION_CLASS").toString())));
602 | col.setTimestamp(timestamp);
603 |
604 | client.set_keyspace(keyspace);
605 | client.insert(getAsBytes(key, cfdata.get("KEY_VALIDATION_CLASS").toString()), parent, col, ConsistencyLevel.ONE);
606 |
607 | return new Date(timestamp / 1000);
608 | }
609 |
610 | public void removeKey(String keyspace, String columnFamily, String key)
611 | throws InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException {
612 | this.keyspace = keyspace;
613 | this.columnFamily = columnFamily;
614 | Map cfdata = getColumnFamily(keyspace, columnFamily);
615 |
616 | ColumnPath colPath = new ColumnPath(columnFamily);
617 | long timestamp = System.currentTimeMillis() * 1000;
618 |
619 | client.set_keyspace(keyspace);
620 | client.remove(getAsBytes(key, cfdata.get("KEY_VALIDATION_CLASS").toString()), colPath, timestamp, ConsistencyLevel.ONE);
621 | }
622 |
623 | public void removeSuperColumn(String keyspace, String columnFamily, String key, String superColumn)
624 | throws InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException {
625 | ColumnPath colPath = new ColumnPath(columnFamily);
626 | colPath.setSuper_column(superColumn.getBytes());
627 | long timestamp = System.currentTimeMillis() * 1000;
628 | Map cfdata = getColumnFamily(keyspace, columnFamily);
629 |
630 | client.set_keyspace(keyspace);
631 | client.remove(getAsBytes(key, cfdata.get("KEY_VALIDATION_CLASS").toString()), colPath, timestamp, ConsistencyLevel.ONE);
632 | }
633 |
634 | public void removeColumn(String keyspace, String columnFamily, String key, String column)
635 | throws InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException {
636 | this.keyspace = keyspace;
637 | this.columnFamily = columnFamily;
638 | Map cfdata = getColumnFamily(keyspace, columnFamily);
639 |
640 | ColumnPath colPath = new ColumnPath(columnFamily);
641 | //colPath.setColumn(column.getBytes());
642 | colPath.setColumn(getAsBytes(column, cfdata.get("COMPARATOR_TYPE").toString()));
643 | long timestamp = System.currentTimeMillis() * 1000;
644 |
645 | client.set_keyspace(keyspace);
646 | client.remove(getAsBytes(key, cfdata.get("KEY_VALIDATION_CLASS").toString()), colPath, timestamp, ConsistencyLevel.ONE);
647 | }
648 |
649 | public void removeColumn(String keyspace, String columnFamily, String key, String superColumn, String column)
650 | throws InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException {
651 | this.keyspace = keyspace;
652 | this.columnFamily = columnFamily;
653 | Map cfdata = getColumnFamily(keyspace, columnFamily);
654 |
655 | ColumnPath colPath = new ColumnPath(columnFamily);
656 | colPath.setSuper_column(getAsBytes(superColumn, cfdata.get("COMPARATOR_TYPE").toString()));
657 | colPath.setColumn(getAsBytes(column, cfdata.get("SUBCOMPARATOR_TYPE").toString()));
658 |
659 | long timestamp = System.currentTimeMillis() * 1000;
660 |
661 | client.set_keyspace(keyspace);
662 | client.remove(getAsBytes(key, cfdata.get("KEY_VALIDATION_CLASS").toString()), colPath, timestamp, ConsistencyLevel.ONE);
663 | }
664 |
665 | public Map getKey(String keyspace, String columnFamily, String superColumn, String key)
666 | throws InvalidRequestException, UnavailableException, TimedOutException, TException, UnsupportedEncodingException, NotFoundException {
667 | this.keyspace = keyspace;
668 | this.columnFamily = columnFamily;
669 | Map cfdata = getColumnFamily(keyspace, columnFamily);
670 |
671 | Map m = new TreeMap();
672 |
673 | ColumnParent columnParent = new ColumnParent(columnFamily);
674 | if (superColumn != null) {
675 | columnParent.setSuper_column(superColumn.getBytes());
676 | }
677 |
678 | SliceRange sliceRange = new SliceRange();
679 | sliceRange.setStart(new byte[0]);
680 | sliceRange.setFinish(new byte[0]);
681 |
682 | SlicePredicate slicePredicate = new SlicePredicate();
683 | slicePredicate.setSlice_range(sliceRange);
684 |
685 | List l = null;
686 | try {
687 | l = client.get_slice(getAsBytes(key, cfdata.get("KEY_VALIDATION_CLASS").toString()), columnParent, slicePredicate, ConsistencyLevel.ONE);
688 | } catch (Exception e) {
689 | return m;
690 | }
691 |
692 | Key k = new Key(key, new TreeMap(), new TreeMap());
693 | for (ColumnOrSuperColumn column : l) {
694 | k.setSuperColumn(column.isSetSuper_column());
695 | if (column.isSetSuper_column()) {
696 | SuperColumn scol = column.getSuper_column();
697 | String scolName = getAsString(scol.bufferForName(), cfdata.get("COMPARATOR_TYPE").toString());
698 | SColumn s = new SColumn(k, scolName, new TreeMap());
699 | for (Column col : scol.getColumns()) {
700 | String name = getAsString(col.bufferForName(), cfdata.get("SUBCOMPARATOR_TYPE").toString());
701 | @SuppressWarnings("unchecked")
702 | String val = getAsString(col.bufferForValue(), getValidationType(col.bufferForName(), (List)cfdata.get("COLUMN_METADATA"), cfdata.get("DEFAULT_VALIDATION_CLASS").toString()));
703 | Cell c = new Cell(s,
704 | name, val,
705 | new Date(col.getTimestamp() / 1000));
706 | s.getCells().put(c.getName(), c);
707 | }
708 |
709 | k.getSColumns().put(s.getName(), s);
710 | } else {
711 | Column col = column.getColumn();
712 | String name = getAsString(col.bufferForName(), cfdata.get("COMPARATOR_TYPE").toString());
713 | @SuppressWarnings("unchecked")
714 | String val = getAsString(col.bufferForValue(), getValidationType(col.bufferForName(), (List)cfdata.get("COLUMN_METADATA"), cfdata.get("DEFAULT_VALIDATION_CLASS").toString()));
715 | Cell c = new Cell(k,
716 | name, val,
717 | new Date(col.getTimestamp() / 1000));
718 | k.getCells().put(c.getName(), c);
719 | }
720 |
721 | m.put(k.getName(), k);
722 | }
723 |
724 | return m;
725 | }
726 |
727 | @SuppressWarnings("rawtypes")
728 | private ByteBuffer getAsBytes(String str, String marshalType) {
729 | ByteBuffer bytes = null;
730 |
731 | try {
732 | AbstractType abstractType = TypeParser.parse(marshalType);
733 | if (str.isEmpty() && abstractType instanceof AbstractCompositeType) {
734 | // count the types and add empty : for each ... silly way but perhaps only way for now
735 | // form will be something like "CompositeType(UTF8Type, LongType, ....)
736 | int i = marshalType.indexOf('('); // don't start till open paren (
737 | while ((i = marshalType.indexOf(',',i)) > 0) {
738 | str += ":";
739 | i++;
740 | }
741 | }
742 | bytes = abstractType.fromString(str);
743 | } catch (ConfigurationException e) {
744 | e.printStackTrace();
745 | }
746 | return bytes;
747 | }
748 |
749 | @SuppressWarnings("rawtypes")
750 | private String getAsString(java.nio.ByteBuffer bytes, String marshalType) {
751 | String val = null;
752 | //val = java.nio.charset.Charset.defaultCharset().decode(bytes.asReadOnlyBuffer()).toString();
753 | try {
754 | AbstractType abstractType = TypeParser.parse(marshalType);
755 | val = abstractType.getString(bytes);
756 | } catch (ConfigurationException e) {
757 | e.printStackTrace();
758 | }
759 | return val;
760 | }
761 |
762 | private String getValidationType(java.nio.ByteBuffer nameBytes, List columns, String defaultType) {
763 | String validationType = defaultType;
764 | if (columns != null) {
765 | for (ColumnDef cdef: columns) {
766 | if (nameBytes.compareTo(cdef.bufferForName()) == 0) {
767 | validationType = cdef.getValidation_class();
768 | }
769 | }
770 | }
771 |
772 | return validationType;
773 | }
774 |
775 | public Map listKeyAndValues(String keyspace, String columnFamily, String startKey, String endKey, int rows)
776 | throws Exception {
777 | this.keyspace = keyspace;
778 | this.columnFamily = columnFamily;
779 | Map cfdata = getColumnFamily(keyspace, columnFamily);
780 | //System.out.println(cfdata.toString());
781 |
782 | Map m = new TreeMap();
783 |
784 | ColumnParent columnParent = new ColumnParent(columnFamily);
785 |
786 | KeyRange keyRange = new KeyRange(rows);
787 |
788 | keyRange.setStart_key(getAsBytes(startKey, cfdata.get("KEY_VALIDATION_CLASS").toString()));
789 | keyRange.setEnd_key(getAsBytes(endKey, cfdata.get("KEY_VALIDATION_CLASS").toString()));
790 |
791 | SliceRange sliceRange = new SliceRange();
792 | sliceRange.setStart(new byte[0]);
793 | sliceRange.setFinish(new byte[0]);
794 |
795 | SlicePredicate slicePredicate = new SlicePredicate();
796 | slicePredicate.setSlice_range(sliceRange);
797 | client.set_keyspace(keyspace);
798 |
799 | List keySlices = null;
800 | try {
801 | keySlices = client.get_range_slices(columnParent, slicePredicate, keyRange, ConsistencyLevel.ONE);
802 | } catch (UnavailableException e) {
803 | return m;
804 | }
805 |
806 | for (KeySlice keySlice : keySlices) {
807 | String keyName = getAsString(keySlice.bufferForKey(), cfdata.get("KEY_VALIDATION_CLASS").toString());
808 | Key key = new Key(keyName, new TreeMap(), new TreeMap());
809 |
810 | for (ColumnOrSuperColumn column : keySlice.getColumns()) {
811 | key.setSuperColumn(column.isSetSuper_column());
812 | if (column.isSetSuper_column()) {
813 | SuperColumn scol = column.getSuper_column();
814 | String scolName = getAsString(scol.bufferForName(), cfdata.get("COMPARATOR_TYPE").toString());
815 | SColumn s = new SColumn(key, scolName, new TreeMap());
816 | for (Column col : scol.getColumns()) {
817 | String name = getAsString(col.bufferForName(), cfdata.get("SUBCOMPARATOR_TYPE").toString());
818 | @SuppressWarnings("unchecked")
819 | String val = getAsString(col.bufferForValue(), getValidationType(col.bufferForName(), (List)cfdata.get("COLUMN_METADATA"), cfdata.get("DEFAULT_VALIDATION_CLASS").toString()));
820 |
821 | Cell c = new Cell(s,
822 | name,
823 | val,
824 | new Date(col.getTimestamp() / 1000));
825 | s.getCells().put(c.getName(), c);
826 | }
827 |
828 | key.getSColumns().put(s.getName(), s);
829 | } else if (column.isSetColumn()){
830 | Column col = column.getColumn();
831 | String name = getAsString(col.bufferForName(), cfdata.get("COMPARATOR_TYPE").toString());
832 | @SuppressWarnings("unchecked")
833 | String val = getAsString(col.bufferForValue(), getValidationType(col.bufferForName(), (List)cfdata.get("COLUMN_METADATA"), cfdata.get("DEFAULT_VALIDATION_CLASS").toString()));
834 |
835 | Cell c = new Cell(key,
836 | name,
837 | val,
838 | new Date(col.getTimestamp() / 1000));
839 | key.getCells().put(c.getName(), c);
840 | } else {
841 | throw new Exception("Unsupported Column type");
842 | }
843 | }
844 |
845 | m.put(key.getName(), key);
846 | }
847 |
848 | return m;
849 | }
850 |
851 | private boolean isEmpty(String s) {
852 | return s == null || s.isEmpty();
853 | }
854 |
855 | /**
856 | * @return the keyspace
857 | */
858 | public String getKeyspace() {
859 | return keyspace;
860 | }
861 |
862 | /**
863 | * @param keyspace the keyspace to set
864 | */
865 | public void setKeyspace(String keyspace) {
866 | this.keyspace = keyspace;
867 | }
868 |
869 | /**
870 | * @return the columnFamily
871 | */
872 | public String getColumnFamily() {
873 | return columnFamily;
874 | }
875 |
876 | /**
877 | * @param columnFamily the columnFamily to set
878 | */
879 | public void setColumnFamily(String columnFamily) {
880 | this.columnFamily = columnFamily;
881 | }
882 |
883 | /**
884 | * @return the superColumn
885 | */
886 | public boolean isSuperColumn() {
887 | return superColumn;
888 | }
889 |
890 | /**
891 | * @param superColumn the superColumn to set
892 | */
893 | public void setSuperColumn(boolean superColumn) {
894 | this.superColumn = superColumn;
895 | }
896 |
897 | /**
898 | * @return the strategyMap
899 | */
900 | public static Map getStrategyMap() {
901 | Map strategyMap = new TreeMap();
902 | strategyMap.put("SimpleStrategy", "org.apache.cassandra.locator.SimpleStrategy");
903 | strategyMap.put("LocalStrategy", "org.apache.cassandra.locator.LocalStrategy");
904 | strategyMap.put("NetworkTopologyStrategy", "org.apache.cassandra.locator.NetworkTopologyStrategy");
905 | strategyMap.put("OldNetworkTopologyStrategy", "org.apache.cassandra.locator.OldNetworkTopologyStrategy");
906 | return strategyMap;
907 | }
908 |
909 | public static Map getComparatorTypeMap() {
910 | Map comparatorMap = new TreeMap();
911 | comparatorMap.put("org.apache.cassandra.db.marshal.AsciiType", "AsciiType");
912 | comparatorMap.put("org.apache.cassandra.db.marshal.BytesType", "BytesType");
913 | comparatorMap.put("org.apache.cassandra.db.marshal.LexicalUUIDType", "LexicalUUIDType");
914 | comparatorMap.put("org.apache.cassandra.db.marshal.LongType", "LongType");
915 | comparatorMap.put("org.apache.cassandra.db.marshal.TimeUUIDType", "TimeUUIDType");
916 | comparatorMap.put("org.apache.cassandra.db.marshal.UTF8Type", "UTF8Type");
917 |
918 | return comparatorMap;
919 | }
920 |
921 | public static Map getValidationClassMap() {
922 | Map validationClassMap = new TreeMap();
923 | validationClassMap.put("org.apache.cassandra.db.marshal.AsciiType", "AsciiType");
924 | validationClassMap.put("org.apache.cassandra.db.marshal.BytesType", "BytesType");
925 | validationClassMap.put("org.apache.cassandra.db.marshal.IntegerType", "IntegerType");
926 | validationClassMap.put("org.apache.cassandra.db.marshal.LongType", "LongType");
927 | validationClassMap.put("org.apache.cassandra.db.marshal.TimeUUIDType", "TimeUUIDType");
928 | validationClassMap.put("org.apache.cassandra.db.marshal.UTF8Type", "UTF8Type");
929 |
930 | return validationClassMap;
931 | }
932 | }
933 |
--------------------------------------------------------------------------------