PHOENIX-5678 : Cleanup anonymous classes used for BaseMutationPlan
Signed-off-by: Xinyi Yan <yanxinyi@apache.org>
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateFunctionCompiler.java b/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateFunctionCompiler.java
index 0e8036a..ea4f486 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateFunctionCompiler.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateFunctionCompiler.java
@@ -40,30 +40,46 @@
final StatementContext context = new StatementContext(statement);
final MetaDataClient client = new MetaDataClient(connectionToBe);
- return new BaseMutationPlan(context, create.getOperation()) {
+ return new CreateFunctionMutationPlan(context, create, client, connection);
+ }
- @Override
- public MutationState execute() throws SQLException {
- try {
- return client.createFunction(create);
- } finally {
- if (client.getConnection() != connection) {
- client.getConnection().close();
- }
+ private static class CreateFunctionMutationPlan extends BaseMutationPlan {
+
+ private final StatementContext context;
+ private final CreateFunctionStatement create;
+ private final MetaDataClient client;
+ private final PhoenixConnection connection;
+
+ private CreateFunctionMutationPlan(StatementContext context, CreateFunctionStatement create,
+ MetaDataClient client, PhoenixConnection connection) {
+ super(context, create.getOperation());
+ this.context = context;
+ this.create = create;
+ this.client = client;
+ this.connection = connection;
+ }
+
+ @Override
+ public MutationState execute() throws SQLException {
+ try {
+ return client.createFunction(create);
+ } finally {
+ if (client.getConnection() != connection) {
+ client.getConnection().close();
}
}
+ }
- @Override
- public ExplainPlan getExplainPlan() throws SQLException {
- return new ExplainPlan(Collections.singletonList("CREATE"
- + (create.getFunctionInfo().isReplace() ? " OR REPLACE" : "")
- + " FUNCTION"));
- }
+ @Override
+ public ExplainPlan getExplainPlan() throws SQLException {
+ return new ExplainPlan(Collections.singletonList("CREATE"
+ + (create.getFunctionInfo().isReplace() ? " OR REPLACE" : "")
+ + " FUNCTION"));
+ }
- @Override
- public StatementContext getContext() {
- return context;
- }
- };
+ @Override
+ public StatementContext getContext() {
+ return context;
+ }
}
}
\ No newline at end of file
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateSchemaCompiler.java b/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateSchemaCompiler.java
index 40d1fee..480b2b6 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateSchemaCompiler.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateSchemaCompiler.java
@@ -27,6 +27,7 @@
import org.apache.phoenix.schema.MetaDataClient;
public class CreateSchemaCompiler {
+
private final PhoenixStatement statement;
public CreateSchemaCompiler(PhoenixStatement statement) {
@@ -37,29 +38,44 @@
final PhoenixConnection connection = statement.getConnection();
final StatementContext context = new StatementContext(statement);
final MetaDataClient client = new MetaDataClient(connection);
+ return new CreateSchemaMutationPlan(context, create, client, connection);
+ }
- return new BaseMutationPlan(context, create.getOperation()) {
+ private static class CreateSchemaMutationPlan extends BaseMutationPlan {
- @Override
- public MutationState execute() throws SQLException {
- try {
- return client.createSchema(create);
- } finally {
- if (client.getConnection() != connection) {
- client.getConnection().close();
- }
+ private final StatementContext context;
+ private final CreateSchemaStatement create;
+ private final MetaDataClient client;
+ private final PhoenixConnection connection;
+
+ private CreateSchemaMutationPlan(StatementContext context, CreateSchemaStatement create,
+ MetaDataClient client, PhoenixConnection connection) {
+ super(context, create.getOperation());
+ this.context = context;
+ this.create = create;
+ this.client = client;
+ this.connection = connection;
+ }
+
+ @Override
+ public MutationState execute() throws SQLException {
+ try {
+ return client.createSchema(create);
+ } finally {
+ if (client.getConnection() != connection) {
+ client.getConnection().close();
}
}
+ }
- @Override
- public ExplainPlan getExplainPlan() throws SQLException {
- return new ExplainPlan(Collections.singletonList("CREATE SCHEMA"));
- }
+ @Override
+ public ExplainPlan getExplainPlan() throws SQLException {
+ return new ExplainPlan(Collections.singletonList("CREATE SCHEMA"));
+ }
- @Override
- public StatementContext getContext() {
- return context;
- }
- };
+ @Override
+ public StatementContext getContext() {
+ return context;
+ }
}
}
\ No newline at end of file
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateTableCompiler.java b/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateTableCompiler.java
index 776859e..6cbe41d 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateTableCompiler.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/CreateTableCompiler.java
@@ -207,25 +207,9 @@
}
final MetaDataClient client = new MetaDataClient(connection);
final PTable parent = parentToBe;
-
- return new BaseMutationPlan(context, operation) {
- @Override
- public MutationState execute() throws SQLException {
- try {
- return client.createTable(finalCreate, splits, parent, viewStatement, viewType, MetaDataUtil.getViewIndexIdDataType(), viewColumnConstants, isViewColumnReferenced);
- } finally {
- if (client.getConnection() != connection) {
- client.getConnection().close();
- }
- }
- }
-
- @Override
- public ExplainPlan getExplainPlan() throws SQLException {
- return new ExplainPlan(Collections.singletonList("CREATE TABLE"));
- }
- };
+ return new CreateTableMutationPlan(context, client, finalCreate, splits, parent,
+ viewStatement, viewType, viewColumnConstants, isViewColumnReferenced, connection);
}
public static class ColumnTrackingExpressionCompiler extends ExpressionCompiler {
@@ -364,4 +348,51 @@
}
}
+
+ private class CreateTableMutationPlan extends BaseMutationPlan {
+
+ private final MetaDataClient client;
+ private final CreateTableStatement finalCreate;
+ private final byte[][] splits;
+ private final PTable parent;
+ private final String viewStatement;
+ private final ViewType viewType;
+ private final byte[][] viewColumnConstants;
+ private final BitSet isViewColumnReferenced;
+ private final PhoenixConnection connection;
+
+ private CreateTableMutationPlan(StatementContext context, MetaDataClient client,
+ CreateTableStatement finalCreate, byte[][] splits, PTable parent,
+ String viewStatement, ViewType viewType, byte[][] viewColumnConstants,
+ BitSet isViewColumnReferenced, PhoenixConnection connection) {
+ super(context, CreateTableCompiler.this.operation);
+ this.client = client;
+ this.finalCreate = finalCreate;
+ this.splits = splits;
+ this.parent = parent;
+ this.viewStatement = viewStatement;
+ this.viewType = viewType;
+ this.viewColumnConstants = viewColumnConstants;
+ this.isViewColumnReferenced = isViewColumnReferenced;
+ this.connection = connection;
+ }
+
+ @Override
+ public MutationState execute() throws SQLException {
+ try {
+ return client.createTable(finalCreate, splits, parent, viewStatement,
+ viewType, MetaDataUtil.getViewIndexIdDataType(), viewColumnConstants,
+ isViewColumnReferenced);
+ } finally {
+ if (client.getConnection() != connection) {
+ client.getConnection().close();
+ }
+ }
+ }
+
+ @Override
+ public ExplainPlan getExplainPlan() throws SQLException {
+ return new ExplainPlan(Collections.singletonList("CREATE TABLE"));
+ }
+ }
}
\ No newline at end of file
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/PostLocalIndexDDLCompiler.java b/phoenix-core/src/main/java/org/apache/phoenix/compile/PostLocalIndexDDLCompiler.java
index ba6d10c..a173acb 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/PostLocalIndexDDLCompiler.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/PostLocalIndexDDLCompiler.java
@@ -95,27 +95,39 @@
// Go through MutationPlan abstraction so that we can create local indexes
// with a connectionless connection (which makes testing easier).
- return new BaseMutationPlan(plan.getContext(), Operation.UPSERT) {
+ return new PostLocalIndexDDLMutationPlan(plan, dataTable);
+ }
+ }
- @Override
- public MutationState execute() throws SQLException {
- connection.getMutationState().commitDDLFence(dataTable);
- Tuple tuple = plan.iterator().next();
- long rowCount = 0;
- if (tuple != null) {
- Cell kv = tuple.getValue(0);
- ImmutableBytesWritable tmpPtr = new ImmutableBytesWritable(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength());
- // A single Cell will be returned with the count(*) - we decode that here
- rowCount = PLong.INSTANCE.getCodec().decodeLong(tmpPtr, SortOrder.getDefault());
- }
- // The contract is to return a MutationState that contains the number of rows modified. In this
- // case, it's the number of rows in the data table which corresponds to the number of index
- // rows that were added.
- return new MutationState(0, 0, connection, rowCount);
- }
+ private class PostLocalIndexDDLMutationPlan extends BaseMutationPlan {
- };
+ private final QueryPlan plan;
+ private final PTable dataTable;
+
+ private PostLocalIndexDDLMutationPlan(QueryPlan plan, PTable dataTable) {
+ super(plan.getContext(), Operation.UPSERT);
+ this.plan = plan;
+ this.dataTable = dataTable;
+ }
+
+ @Override
+ public MutationState execute() throws SQLException {
+ connection.getMutationState().commitDDLFence(dataTable);
+ Tuple tuple = plan.iterator().next();
+ long rowCount = 0;
+ if (tuple != null) {
+ Cell kv = tuple.getValue(0);
+ ImmutableBytesWritable tmpPtr = new ImmutableBytesWritable(kv.getValueArray(),
+ kv.getValueOffset(), kv.getValueLength());
+ // A single Cell will be returned with the count(*) - we decode that here
+ rowCount = PLong.INSTANCE.getCodec().decodeLong(tmpPtr, SortOrder.getDefault());
}
- }
-
+ // The contract is to return a MutationState that contains the number of rows modified.
+ // In this case, it's the number of rows in the data table which corresponds to the
+ // number of index rows that were added.
+ return new MutationState(0, 0, connection, rowCount);
+ }
+
+ }
+
}
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java
index 0df11ae..6a405f4 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java
@@ -904,50 +904,62 @@
@Override
public MutationPlan compilePlan(final PhoenixStatement stmt, Sequence.ValueOp seqAction) throws SQLException {
final StatementContext context = new StatementContext(stmt);
- return new BaseMutationPlan(context, this.getOperation()) {
+ return new CustomMutationPlan(context, stmt);
+ }
- @Override
- public ParameterMetaData getParameterMetaData() {
- return PhoenixParameterMetaData.EMPTY_PARAMETER_META_DATA;
+ private class CustomMutationPlan extends BaseMutationPlan {
+
+ private final StatementContext context;
+ private final PhoenixStatement stmt;
+
+ private CustomMutationPlan(StatementContext context, PhoenixStatement stmt) {
+ super(context, ExecutableAddJarsStatement.this.getOperation());
+ this.context = context;
+ this.stmt = stmt;
+ }
+
+ @Override
+ public ParameterMetaData getParameterMetaData() {
+ return PhoenixParameterMetaData.EMPTY_PARAMETER_META_DATA;
+ }
+
+ @Override
+ public ExplainPlan getExplainPlan() throws SQLException {
+ return new ExplainPlan(Collections.singletonList("ADD JARS"));
+ }
+
+ @Override
+ public MutationState execute() throws SQLException {
+ String dynamicJarsDir = stmt.getConnection().getQueryServices().getProps()
+ .get(QueryServices.DYNAMIC_JARS_DIR_KEY);
+ if (dynamicJarsDir == null) {
+ throw new SQLException(QueryServices.DYNAMIC_JARS_DIR_KEY
+ + " is not configured for placing the jars.");
}
-
- @Override
- public ExplainPlan getExplainPlan() throws SQLException {
- return new ExplainPlan(Collections.singletonList("ADD JARS"));
+ dynamicJarsDir =
+ dynamicJarsDir.endsWith("/") ? dynamicJarsDir : dynamicJarsDir + '/';
+ Configuration conf = HBaseFactoryProvider.getConfigurationFactory()
+ .getConfiguration();
+ Path dynamicJarsDirPath = new Path(dynamicJarsDir);
+ for (LiteralParseNode jarPath : getJarPaths()) {
+ String jarPathStr = (String) jarPath.getValue();
+ if (!jarPathStr.endsWith(".jar")) {
+ throw new SQLException(jarPathStr + " is not a valid jar file path.");
+ }
}
-
- @Override
- public MutationState execute() throws SQLException {
- String dynamicJarsDir = stmt.getConnection().getQueryServices().getProps().get(QueryServices.DYNAMIC_JARS_DIR_KEY);
- if(dynamicJarsDir == null) {
- throw new SQLException(QueryServices.DYNAMIC_JARS_DIR_KEY+" is not configured for placing the jars.");
+ try {
+ FileSystem fs = dynamicJarsDirPath.getFileSystem(conf);
+ List<LiteralParseNode> jarPaths = getJarPaths();
+ for (LiteralParseNode jarPath : jarPaths) {
+ File f = new File((String) jarPath.getValue());
+ fs.copyFromLocalFile(new Path(f.getAbsolutePath()), new Path(
+ dynamicJarsDir + f.getName()));
}
- dynamicJarsDir =
- dynamicJarsDir.endsWith("/") ? dynamicJarsDir : dynamicJarsDir + '/';
- Configuration conf = HBaseFactoryProvider.getConfigurationFactory().getConfiguration();
- Path dynamicJarsDirPath = new Path(dynamicJarsDir);
- for (LiteralParseNode jarPath : getJarPaths()) {
- String jarPathStr = (String)jarPath.getValue();
- if(!jarPathStr.endsWith(".jar")) {
- throw new SQLException(jarPathStr + " is not a valid jar file path.");
- }
- }
-
- try {
- FileSystem fs = dynamicJarsDirPath.getFileSystem(conf);
- List<LiteralParseNode> jarPaths = getJarPaths();
- for (LiteralParseNode jarPath : jarPaths) {
- File f = new File((String) jarPath.getValue());
- fs.copyFromLocalFile(new Path(f.getAbsolutePath()), new Path(
- dynamicJarsDir + f.getName()));
- }
- } catch(IOException e) {
- throw new SQLException(e);
- }
- return new MutationState(0, 0, context.getConnection());
+ } catch (IOException e) {
+ throw new SQLException(e);
}
- };
-
+ return new MutationState(0, 0, context.getConnection());
+ }
}
}
@@ -1012,46 +1024,58 @@
@Override
public MutationPlan compilePlan(final PhoenixStatement stmt, Sequence.ValueOp seqAction) throws SQLException {
final StatementContext context = new StatementContext(stmt);
- return new BaseMutationPlan(context, this.getOperation()) {
+ return new CustomMutationPlan(context, stmt);
+ }
- @Override
- public ParameterMetaData getParameterMetaData() {
- return PhoenixParameterMetaData.EMPTY_PARAMETER_META_DATA;
+ private class CustomMutationPlan extends BaseMutationPlan {
+
+ private final StatementContext context;
+ private final PhoenixStatement stmt;
+
+ private CustomMutationPlan(StatementContext context, PhoenixStatement stmt) {
+ super(context, ExecutableDeleteJarStatement.this.getOperation());
+ this.context = context;
+ this.stmt = stmt;
+ }
+
+ @Override
+ public ParameterMetaData getParameterMetaData() {
+ return PhoenixParameterMetaData.EMPTY_PARAMETER_META_DATA;
+ }
+
+ @Override
+ public ExplainPlan getExplainPlan() throws SQLException {
+ return new ExplainPlan(Collections.singletonList("DELETE JAR"));
+ }
+
+ @Override
+ public MutationState execute() throws SQLException {
+ String dynamicJarsDir = stmt.getConnection().getQueryServices().getProps()
+ .get(QueryServices.DYNAMIC_JARS_DIR_KEY);
+ if (dynamicJarsDir == null) {
+ throw new SQLException(QueryServices.DYNAMIC_JARS_DIR_KEY
+ + " is not configured.");
}
-
- @Override
- public ExplainPlan getExplainPlan() throws SQLException {
- return new ExplainPlan(Collections.singletonList("DELETE JAR"));
- }
-
- @Override
- public MutationState execute() throws SQLException {
- String dynamicJarsDir = stmt.getConnection().getQueryServices().getProps().get(QueryServices.DYNAMIC_JARS_DIR_KEY);
- if (dynamicJarsDir == null) {
- throw new SQLException(QueryServices.DYNAMIC_JARS_DIR_KEY
- + " is not configured.");
+ dynamicJarsDir =
+ dynamicJarsDir.endsWith("/") ? dynamicJarsDir : dynamicJarsDir + '/';
+ Configuration conf = HBaseFactoryProvider.getConfigurationFactory()
+ .getConfiguration();
+ Path dynamicJarsDirPath = new Path(dynamicJarsDir);
+ try {
+ FileSystem fs = dynamicJarsDirPath.getFileSystem(conf);
+ String jarPathStr = (String)getJarPath().getValue();
+ if(!jarPathStr.endsWith(".jar")) {
+ throw new SQLException(jarPathStr + " is not a valid jar file path.");
}
- dynamicJarsDir =
- dynamicJarsDir.endsWith("/") ? dynamicJarsDir : dynamicJarsDir + '/';
- Configuration conf = HBaseFactoryProvider.getConfigurationFactory().getConfiguration();
- Path dynamicJarsDirPath = new Path(dynamicJarsDir);
- try {
- FileSystem fs = dynamicJarsDirPath.getFileSystem(conf);
- String jarPathStr = (String)getJarPath().getValue();
- if(!jarPathStr.endsWith(".jar")) {
- throw new SQLException(jarPathStr + " is not a valid jar file path.");
- }
- Path p = new Path(jarPathStr);
- if(fs.exists(p)) {
- fs.delete(p, false);
- }
- } catch(IOException e) {
- throw new SQLException(e);
+ Path p = new Path(jarPathStr);
+ if(fs.exists(p)) {
+ fs.delete(p, false);
}
- return new MutationState(0, 0, context.getConnection());
+ } catch(IOException e) {
+ throw new SQLException(e);
}
- };
-
+ return new MutationState(0, 0, context.getConnection());
+ }
}
}
@@ -1298,37 +1322,47 @@
@Override
public MutationPlan compilePlan(final PhoenixStatement stmt, Sequence.ValueOp seqAction) throws SQLException {
final StatementContext context = new StatementContext(stmt);
- return new BaseMutationPlan(context, this.getOperation()) {
+ return new CustomMutationPlan(context);
+ }
- @Override
- public StatementContext getContext() {
- return context;
- }
+ private class CustomMutationPlan extends BaseMutationPlan {
- @Override
- public ParameterMetaData getParameterMetaData() {
- return PhoenixParameterMetaData.EMPTY_PARAMETER_META_DATA;
- }
+ private final StatementContext context;
- @Override
- public ExplainPlan getExplainPlan() throws SQLException {
- return new ExplainPlan(Collections.singletonList("ALTER SESSION"));
- }
+ private CustomMutationPlan(StatementContext context) {
+ super(context, ExecutableAlterSessionStatement.this.getOperation());
+ this.context = context;
+ }
+
+ @Override
+ public StatementContext getContext() {
+ return context;
+ }
+
+ @Override
+ public ParameterMetaData getParameterMetaData() {
+ return PhoenixParameterMetaData.EMPTY_PARAMETER_META_DATA;
+ }
+
+ @Override
+ public ExplainPlan getExplainPlan() throws SQLException {
+ return new ExplainPlan(Collections.singletonList("ALTER SESSION"));
+ }
- @Override
- public MutationState execute() throws SQLException {
- Object consistency = getProps().get(PhoenixRuntime.CONSISTENCY_ATTRIB.toUpperCase());
- if(consistency != null) {
- if (((String)consistency).equalsIgnoreCase(Consistency.TIMELINE.toString())){
- getContext().getConnection().setConsistency(Consistency.TIMELINE);
- } else {
- getContext().getConnection().setConsistency(Consistency.STRONG);
- }
+ @Override
+ public MutationState execute() throws SQLException {
+ Object consistency = getProps()
+ .get(PhoenixRuntime.CONSISTENCY_ATTRIB.toUpperCase());
+ if(consistency != null) {
+ if (((String)consistency).equalsIgnoreCase(Consistency.TIMELINE.toString())){
+ getContext().getConnection().setConsistency(Consistency.TIMELINE);
+ } else {
+ getContext().getConnection().setConsistency(Consistency.STRONG);
}
- return new MutationState(0, 0, context.getConnection());
}
- };
+ return new MutationState(0, 0, context.getConnection());
+ }
}
}