ignite-gg9499 - minor refactoring and cleanup
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/98df8e16 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/98df8e16 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/98df8e16 Branch: refs/heads/sprint-1 Commit: 98df8e167c080803110872bb05b88cc264018882 Parents: 88571b9 Author: S.Vladykin <svlady...@gridgain.com> Authored: Wed Dec 24 01:14:50 2014 +0300 Committer: S.Vladykin <svlady...@gridgain.com> Committed: Wed Dec 24 01:14:50 2014 +0300 ---------------------------------------------------------------------- .../processors/query/h2/sql/GridOperation.java | 4 +- .../query/h2/sql/GridQueryParser.java | 148 ++++++++++++++++--- .../processors/query/h2/sql/GridQueryUtils.java | 148 ------------------- .../processors/query/h2/sql/GridSqlElement.java | 6 +- .../processors/query/h2/sql/GridQueryTest.java | 4 +- 5 files changed, 133 insertions(+), 177 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/98df8e16/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridOperation.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridOperation.java b/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridOperation.java index c38ae85..a52e33e 100644 --- a/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridOperation.java +++ b/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridOperation.java @@ -29,7 +29,7 @@ public class GridOperation extends GridSqlElement { * @param opType Op type. * @param arg argument. */ - public GridOperation(@NotNull GridOperationType opType, @NotNull GridSqlElement arg) { + public GridOperation(GridOperationType opType, GridSqlElement arg) { this(opType); addChild(arg); @@ -40,7 +40,7 @@ public class GridOperation extends GridSqlElement { * @param left Left. * @param right Right. */ - public GridOperation(@NotNull GridOperationType opType, @NotNull GridSqlElement left, @NotNull GridSqlElement right) { + public GridOperation(GridOperationType opType, GridSqlElement left, GridSqlElement right) { this(opType); addChild(left); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/98df8e16/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryParser.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryParser.java b/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryParser.java index 780c662..5b5a434 100644 --- a/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryParser.java +++ b/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryParser.java @@ -10,7 +10,6 @@ package org.gridgain.grid.kernal.processors.query.h2.sql; import org.apache.ignite.*; -import org.gridgain.grid.kernal.processors.query.h2.sql.GridQueryUtils.*; import org.h2.command.dml.*; import org.h2.expression.*; import org.h2.result.*; @@ -22,7 +21,6 @@ import java.util.*; import java.util.Set; import static org.gridgain.grid.kernal.processors.query.h2.sql.GridOperationType.*; -import static org.gridgain.grid.kernal.processors.query.h2.sql.GridQueryUtils.*; /** * H2 Query parser. @@ -42,7 +40,7 @@ public class GridQueryParser { private static final Getter<Select, Expression> CONDITION = getter(Select.class, "condition"); /** */ - private static final Field GROUP_INDEXES = GridQueryUtils.getField(Select.class, "groupIndex"); + private static final Getter<Select, int[]> GROUP_INDEXES = getter(Select.class, "groupIndex"); /** */ private static final Getter<Operation, Integer> OPERATION_TYPE = getter(Operation.class, "opType"); @@ -72,6 +70,63 @@ public class GridQueryParser { private static final Getter<ConditionAndOr, Expression> ANDOR_RIGHT = getter(ConditionAndOr.class, "right"); /** */ + private static final Getter<TableView, Query> VIEW_QUERY = getter(TableView.class, "viewQuery"); + + /** */ + private static final Getter<TableFilter, String> ALIAS = getter(TableFilter.class, "alias"); + + /** */ + private static final Getter<Select, Integer> HAVING_INDEX = getter(Select.class, "havingIndex"); + + /** */ + private static final Getter<ConditionIn, Expression> LEFT_CI = getter(ConditionIn.class, "left"); + + /** */ + private static final Getter<ConditionIn, List<Expression>> VALUE_LIST_CI = getter(ConditionIn.class, "valueList"); + + /** */ + private static final Getter<ConditionInConstantSet, Expression> LEFT_CICS = + getter(ConditionInConstantSet.class, "left"); + + /** */ + private static final Getter<ConditionInConstantSet, List<Expression>> VALUE_LIST_CICS = + getter(ConditionInConstantSet.class, "valueList"); + + /** */ + private static final Getter<ConditionInSelect, Expression> LEFT_CIS = getter(ConditionInSelect.class, "left"); + + /** */ + private static final Getter<ConditionInSelect, Boolean> ALL = getter(ConditionInSelect.class, "all"); + + /** */ + private static final Getter<ConditionInSelect, Integer> COMPARE_TYPE = getter(ConditionInSelect.class, + "compareType"); + + /** */ + private static final Getter<ConditionInSelect, Query> QUERY = getter(ConditionInSelect.class, "query"); + + /** */ + private static final Getter<CompareLike, Expression> LEFT = getter(CompareLike.class, "left"); + + /** */ + private static final Getter<CompareLike, Expression> RIGHT = getter(CompareLike.class, "right"); + + /** */ + private static final Getter<CompareLike, Expression> ESCAPE = getter(CompareLike.class, "escape"); + + /** */ + private static final Getter<CompareLike, Boolean> REGEXP_CL = getter(CompareLike.class, "regexp"); + + /** */ + private static final Getter<Aggregate, Boolean> DISTINCT = getter(Aggregate.class, "distinct"); + + /** */ + private static final Getter<Aggregate, Integer> TYPE = getter(Aggregate.class, "type"); + + /** */ + private static final Getter<Aggregate, Expression> ON = getter(Aggregate.class, "on"); + + /** */ private final IdentityHashMap<Object, Object> h2ObjToGridObj = new IdentityHashMap<>(); /** @@ -86,7 +141,7 @@ public class GridQueryParser { if (tbl instanceof TableBase) res = new GridTable(tbl.getSchema().getName(), tbl.getName()); else if (tbl instanceof TableView) { - Query qry = GridQueryUtils.getFieldValue(tbl, "viewQuery"); + Query qry = VIEW_QUERY.get((TableView)tbl); assert0(qry instanceof Select, qry); @@ -95,7 +150,7 @@ public class GridQueryParser { else throw new IgniteException("Unsupported query: " + filter); - String alias = GridQueryUtils.getFieldValue(filter, "alias"); + String alias = ALIAS.get(filter); if (alias != null) res = new GridAlias(alias, res, false); @@ -152,7 +207,7 @@ public class GridQueryParser { ArrayList<Expression> expressions = select.getExpressions(); - int[] grpIdx = GridQueryUtils.getFieldValue(GROUP_INDEXES, select); + int[] grpIdx = GROUP_INDEXES.get(select); if (grpIdx != null) { for (int idx : grpIdx) @@ -161,7 +216,7 @@ public class GridQueryParser { assert0(select.getHaving() == null, select); - int havingIdx = GridQueryUtils.getFieldValue(select, "havingIndex"); + int havingIdx = HAVING_INDEX.get(select); if (havingIdx >= 0) res.having(toGridExpression(expressions.get(havingIdx))); @@ -276,9 +331,9 @@ public class GridQueryParser { if (expression instanceof ConditionIn) { GridOperation res = new GridOperation(IN); - res.addChild(toGridExpression((Expression)GridQueryUtils.getFieldValue(expression, "left"))); + res.addChild(toGridExpression(LEFT_CI.get((ConditionIn)expression))); - List<Expression> vals = GridQueryUtils.getFieldValue(expression, "valueList"); + List<Expression> vals = VALUE_LIST_CI.get((ConditionIn)expression); for (Expression val : vals) res.addChild(toGridExpression(val)); @@ -289,9 +344,9 @@ public class GridQueryParser { if (expression instanceof ConditionInConstantSet) { GridOperation res = new GridOperation(IN); - res.addChild(toGridExpression((Expression)GridQueryUtils.getFieldValue(expression, "left"))); + res.addChild(toGridExpression(LEFT_CICS.get((ConditionInConstantSet)expression))); - List<Expression> vals = GridQueryUtils.getFieldValue(expression, "valueList"); + List<Expression> vals = VALUE_LIST_CICS.get((ConditionInConstantSet)expression); for (Expression val : vals) res.addChild(toGridExpression(val)); @@ -302,15 +357,15 @@ public class GridQueryParser { if (expression instanceof ConditionInSelect) { GridOperation res = new GridOperation(IN); - boolean all = GridQueryUtils.getFieldValue(expression, "all"); - int compareType = GridQueryUtils.getFieldValue(expression, "compareType"); + boolean all = ALL.get((ConditionInSelect)expression); + int compareType = COMPARE_TYPE.get((ConditionInSelect)expression); assert0(!all, expression); assert0(compareType == Comparison.EQUAL, expression); - res.addChild(toGridExpression((Expression)GridQueryUtils.getFieldValue(expression, "left"))); + res.addChild(toGridExpression(LEFT_CIS.get((ConditionInSelect)expression))); - Query qry = GridQueryUtils.getFieldValue(expression, "query"); + Query qry = QUERY.get((ConditionInSelect)expression); assert0(qry instanceof Select, qry); @@ -320,13 +375,12 @@ public class GridQueryParser { } if (expression instanceof CompareLike) { - assert0(GridQueryUtils.getFieldValue(expression, "escape") == null, expression); + assert0(ESCAPE.get((CompareLike)expression) == null, expression); - boolean regexp = GridQueryUtils.getFieldValue(expression, "regexp"); + boolean regexp = REGEXP_CL.get((CompareLike)expression); - return new GridOperation(regexp ? REGEXP : LIKE, - toGridExpression((Expression)GridQueryUtils.getFieldValue(expression, "left")), - toGridExpression((Expression)GridQueryUtils.getFieldValue(expression, "right"))); + return new GridOperation(regexp ? REGEXP : LIKE, toGridExpression(LEFT.get((CompareLike)expression)), + toGridExpression(RIGHT.get((CompareLike)expression))); } if (expression instanceof Function) { @@ -348,10 +402,10 @@ public class GridQueryParser { return new GridSqlParameter(((Parameter)expression).getIndex()); if (expression instanceof Aggregate) { - GridAggregateFunction res = new GridAggregateFunction(GridQueryUtils.<Boolean>getFieldValue(expression, - "distinct"), GridQueryUtils.<Integer>getFieldValue(expression, "type")); + GridAggregateFunction res = new GridAggregateFunction(DISTINCT.get((Aggregate)expression), + TYPE.get((Aggregate)expression)); - Expression on = GridQueryUtils.getFieldValue(expression, "on"); + Expression on = ON.get((Aggregate)expression); if (on != null) res.addChild(toGridExpression(on)); @@ -371,4 +425,52 @@ public class GridQueryParser { if (!cond) throw new IgniteException("Unsupported query: " + o); } + + /** + * @param cls Class. + * @param fldName Fld name. + */ + private static <T, R> Getter<T, R> getter(Class<T> cls, String fldName) { + Field field; + + try { + field = cls.getDeclaredField(fldName); + } + catch (NoSuchFieldException e) { + throw new RuntimeException(e); + } + + field.setAccessible(true); + + return new Getter<>(field); + } + + /** + * Field getter. + */ + @SuppressWarnings("unchecked") + private static class Getter<T, R> { + /** */ + private final Field fld; + + /** + * @param fld Fld. + */ + private Getter(Field fld) { + this.fld = fld; + } + + /** + * @param obj Object. + * @return Result. + */ + public R get(T obj) { + try { + return (R)fld.get(obj); + } + catch (IllegalAccessException e) { + throw new IgniteException(e); + } + } + } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/98df8e16/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryUtils.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryUtils.java b/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryUtils.java deleted file mode 100644 index 0ffac88..0000000 --- a/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryUtils.java +++ /dev/null @@ -1,148 +0,0 @@ -/* @java.file.header */ - -/* _________ _____ __________________ _____ - * __ ____/___________(_)______ /__ ____/______ ____(_)_______ - * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ - * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / - * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ - */ - -package org.gridgain.grid.kernal.processors.query.h2.sql; - -import org.h2.table.*; - -import java.lang.reflect.*; -import java.util.*; - -/** - * - */ -public class GridQueryUtils { - - /** - * @param res - * @param tbl - */ - public static void extractTables(Set<TableBase> res, Table tbl) { - if (tbl instanceof TableBase) { - res.add((TableBase)tbl); - - return; - } - - if (tbl instanceof TableView) { - List<Table> tableList = getTables((TableView)tbl); - - for (Table table : tableList) { - extractTables(res, table); - } - } - else - throw new IllegalArgumentException("Unknown table type: " + tbl); - } - - /** - * @param view Table view. - * @return List of tables. - */ - public static List<Table> getTables(TableView view) { - try { - Field field = TableView.class.getDeclaredField("tables"); - - field.setAccessible(true); - - return (List<Table>)field.get(view); - } - catch (NoSuchFieldException | IllegalAccessException e) { - throw new RuntimeException(e); - } - } - - /** - * - * @param fld - * @param obj - * @param <T> - * @return - */ - public static <T> T getFieldValue(Field fld, Object obj) { - try { - return (T)fld.get(obj); - } - catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - } - - /** - * @param cls - * @param fieldName - * @return - */ - public static Field getField(Class<?> cls, String fieldName) { - Field field; - try { - field = cls.getDeclaredField(fieldName); - } - catch (NoSuchFieldException e) { - throw new RuntimeException(e); - } - - field.setAccessible(true); - - return field; - } - - /** - * @param cls Class. - * @param obj Object. - * @param fldName Fld name. - */ - public static <C> Object getFieldValue(Class<? super C> cls, C obj, String fldName) { - Field field = getField(cls, fldName); - - return getFieldValue(field, obj); - } - - /** - * @param obj Object. - * @param fldName Fld name. - */ - public static <T> T getFieldValue(Object obj, String fldName) { - return (T)getFieldValue((Class<? super Object>)obj.getClass(), obj, fldName); - } - - /** - * @param cls Class. - * @param fldName Fld name. - */ - public static <T, R> Getter<T, R> getter(Class<T> cls, String fldName) { - Field field = getField(cls, fldName); - - return new Getter<>(field); - } - - /** - * - */ - @SuppressWarnings("PublicInnerClass") - public static class Getter<T, R> { - /** */ - private final Field fld; - - /** - * @param fld Fld. - */ - private Getter(Field fld) { - this.fld = fld; - } - - /** - * @param obj Object. - * @return Result. - */ - public R get(T obj) { - return getFieldValue(fld, obj); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/98df8e16/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSqlElement.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSqlElement.java b/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSqlElement.java index f569261..c658e24 100644 --- a/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSqlElement.java +++ b/modules/indexing/src/main/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridSqlElement.java @@ -33,7 +33,9 @@ public abstract class GridSqlElement implements Cloneable { /** * @param expr Expr. */ - public void addChild(@NotNull GridSqlElement expr) { + public void addChild(GridSqlElement expr) { + assert expr != null; + children.add(expr); } @@ -53,7 +55,7 @@ public abstract class GridSqlElement implements Cloneable { /** {@inheritDoc} */ @SuppressWarnings("CloneCallsConstructors") - @Override protected GridSqlElement clone() { + @Override protected GridSqlElement clone() throws CloneNotSupportedException { try { GridSqlElement res = (GridSqlElement)super.clone(); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/98df8e16/modules/indexing/src/test/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryTest.java b/modules/indexing/src/test/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryTest.java index 099bfc1..f4f3c71 100644 --- a/modules/indexing/src/test/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryTest.java +++ b/modules/indexing/src/test/java/org/gridgain/grid/kernal/processors/query/h2/sql/GridQueryTest.java @@ -15,6 +15,7 @@ import org.gridgain.grid.kernal.*; import org.gridgain.grid.kernal.processors.cache.*; import org.gridgain.grid.kernal.processors.query.*; import org.gridgain.grid.kernal.processors.query.h2.*; +import org.gridgain.grid.util.typedef.internal.*; import org.h2.command.*; import org.h2.command.dml.*; import org.h2.engine.*; @@ -150,8 +151,7 @@ public class GridQueryTest extends GridCacheAbstractQuerySelfTest { GridQueryProcessor qryProcessor = ctx.query(); - GridH2Indexing idx = (GridH2Indexing)GridQueryUtils.getFieldValue(GridQueryProcessor.class, qryProcessor, - "idx"); + GridH2Indexing idx = U.field(qryProcessor, "idx"); return (JdbcConnection)idx.connectionForSpace(null); }