|
@@ -0,0 +1,235 @@
|
|
|
+package com.sagacloud.hbase;
|
|
|
+/*
|
|
|
+ * Author: Jxing
|
|
|
+ * Create Time: 2019/3/13
|
|
|
+ */
|
|
|
+
|
|
|
+import com.alibaba.fastjson.*;
|
|
|
+import com.zillion.database.agent.ZillionCriteria;
|
|
|
+import com.zillion.database.agent.ZillionCriteria_all_column;
|
|
|
+import com.zillion.database.agent.ZillionMatchUtil;
|
|
|
+import com.zillion.database.agent.ZillionOrderByUtil;
|
|
|
+import com.zillion.util.table.ZillionTableIndex;
|
|
|
+import com.zillion.util.table.ZillionTableSchema;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class HbaseScanIndex {
|
|
|
+ /**
|
|
|
+ * @param
|
|
|
+ * @UpdateDate: 2018/9/14 10:55
|
|
|
+ * @Description: 根据条件获取属于哪个索引
|
|
|
+ * @return:
|
|
|
+ */
|
|
|
+ public static String indexname(ZillionTableSchema schema, String QueryType, JSONArray OrderBy, JSONArray Criterias) {
|
|
|
+ String indexname = null;
|
|
|
+ try {
|
|
|
+ JSONObject Criteria = null;
|
|
|
+ if (Criterias.size() > 0) {
|
|
|
+ Criteria = (JSONObject) Criterias.get(0);
|
|
|
+ }
|
|
|
+ boolean OrderBy_useless = true;
|
|
|
+ ZillionCriteria criteria_key = ZillionMatchUtil.CriteriaMatch(schema.ColumnMap, schema.Key, Criteria, true);
|
|
|
+ List<ZillionCriteria> criteria_keyList = new ArrayList();
|
|
|
+ ZillionTableIndex schema_index = null;
|
|
|
+ ZillionCriteria criteria_index = null;
|
|
|
+ List<ZillionCriteria> criteria_indexList = new ArrayList();
|
|
|
+ if (criteria_key != null) {
|
|
|
+ criteria_keyList.add(criteria_key);
|
|
|
+ if (QueryType.equals("select") && !ZillionOrderByUtil.Match(schema.Key, Criteria, OrderBy)) {
|
|
|
+ OrderBy_useless = false;
|
|
|
+ }
|
|
|
+ if (Criterias != null && Criterias.size() > 1) {
|
|
|
+ for (int i = 1; i < Criterias.size(); ++i) {
|
|
|
+ JSONObject CriteriaInner = (JSONObject) Criterias.get(i);
|
|
|
+ ZillionCriteria criteria_tmp = ZillionMatchUtil.CriteriaMatch(schema.ColumnMap, schema.Key, CriteriaInner, true);
|
|
|
+ if (criteria_tmp == null) {
|
|
|
+ throw new Exception(i + 1 + " Criteria must follow key's rule!");
|
|
|
+ }
|
|
|
+
|
|
|
+ criteria_keyList.add(criteria_tmp);
|
|
|
+ if (QueryType.equals("select") && !ZillionOrderByUtil.Match(schema.Key, CriteriaInner, OrderBy)) {
|
|
|
+ OrderBy_useless = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ boolean isKey = false;
|
|
|
+ if (schema.table_type != null) {
|
|
|
+ isKey = true;
|
|
|
+ }
|
|
|
+ ZillionCriteria criteria_tmp;
|
|
|
+ int i;
|
|
|
+ for (i = 0; i < schema.Indexes.size(); ++i) {
|
|
|
+ ZillionTableIndex index_tmp = (ZillionTableIndex) schema.Indexes.get(i);
|
|
|
+ criteria_tmp = ZillionMatchUtil.CriteriaMatch(schema.ColumnMap, index_tmp.Key, Criteria, isKey);
|
|
|
+ if (criteria_tmp != null) {
|
|
|
+ schema_index = index_tmp;
|
|
|
+ indexname = schema_index.index_name;
|
|
|
+ criteria_index = criteria_tmp;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (criteria_index != null) {
|
|
|
+ criteria_indexList.add(criteria_index);
|
|
|
+ if (QueryType.equals("select") && !ZillionOrderByUtil.Match(schema_index.Key, Criteria, OrderBy)) {
|
|
|
+ OrderBy_useless = false;
|
|
|
+ }
|
|
|
+ if (Criterias != null && Criterias.size() > 1) {
|
|
|
+ for (i = 1; i < Criterias.size(); ++i) {
|
|
|
+ JSONObject CriteriaInner = (JSONObject) Criterias.get(i);
|
|
|
+ criteria_tmp = ZillionMatchUtil.CriteriaMatch(schema.ColumnMap, schema_index.Key, CriteriaInner, isKey);
|
|
|
+ if (criteria_tmp == null) {
|
|
|
+ throw new Exception(i + 1 + " Criteria must follow index " + schema_index.index_name + "'s rule!");
|
|
|
+ }
|
|
|
+ criteria_indexList.add(criteria_tmp);
|
|
|
+ if (QueryType.equals("select") && !ZillionOrderByUtil.Match(schema_index.Key, CriteriaInner, OrderBy)) {
|
|
|
+ OrderBy_useless = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (schema.table_type != null && schema.table_type.equals("all_column")) {
|
|
|
+ if (Criterias != null && Criterias.size() > 1) {
|
|
|
+ throw new Exception("table of type all_column Criterias must follow rule!");
|
|
|
+ }
|
|
|
+ ZillionCriteria_all_column ZillionCriteria_all_column = ZillionMatchUtil.CriteriaMatch_all_column_best(schema, Criteria);
|
|
|
+ if (ZillionCriteria_all_column.isKey) {
|
|
|
+ criteria_key = ZillionCriteria_all_column.criteria;
|
|
|
+ criteria_keyList.add(criteria_key);
|
|
|
+ if (QueryType.equals("select") && !ZillionOrderByUtil.Match(schema.Key, Criteria, OrderBy)) {
|
|
|
+ OrderBy_useless = false;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ schema_index = (ZillionTableIndex) schema.Indexes.get(ZillionCriteria_all_column.index_index);
|
|
|
+ criteria_index = ZillionCriteria_all_column.criteria;
|
|
|
+ criteria_indexList.add(criteria_index);
|
|
|
+ if (QueryType.equals("select") && !ZillionOrderByUtil.Match(schema_index.Key, Criteria, OrderBy)) {
|
|
|
+ OrderBy_useless = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return indexname;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static ZillionTableIndex zillionTableIndex(ZillionTableSchema schema, JSONObject Criteria, JSONArray Criterias) {
|
|
|
+ JSONArray Cris = new JSONArray();
|
|
|
+ if (Criteria == null) {
|
|
|
+ Cris.add((JSONObject) Criterias.get(0));
|
|
|
+ } else {
|
|
|
+ Cris.add(Criteria);
|
|
|
+ }
|
|
|
+ return zillionTableIndex(schema, "select", new JSONArray(), Cris);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param
|
|
|
+ * @UpdateDate: 2018/9/14 10:55
|
|
|
+ * @Description: 根据条件获取是否是索引条件
|
|
|
+ * @return:
|
|
|
+ */
|
|
|
+ public static ZillionTableIndex zillionTableIndex(ZillionTableSchema schema, String QueryType, JSONArray OrderBy, JSONArray Criterias) {
|
|
|
+ ZillionTableIndex zillionTableIndex = null;
|
|
|
+ try {
|
|
|
+ JSONObject Criteria = null;
|
|
|
+ if (Criterias.size() > 0) {
|
|
|
+ Criteria = (JSONObject) Criterias.get(0);
|
|
|
+ }
|
|
|
+ boolean OrderBy_useless = true;
|
|
|
+ ZillionCriteria criteria_key = ZillionMatchUtil.CriteriaMatch(schema.ColumnMap, schema.Key, Criteria, true);
|
|
|
+ List<ZillionCriteria> criteria_keyList = new ArrayList();
|
|
|
+ ZillionTableIndex schema_index = null;
|
|
|
+ ZillionCriteria criteria_index = null;
|
|
|
+ List<ZillionCriteria> criteria_indexList = new ArrayList();
|
|
|
+ if (criteria_key != null) {
|
|
|
+ criteria_keyList.add(criteria_key);
|
|
|
+ if (QueryType.equals("select") && !ZillionOrderByUtil.Match(schema.Key, Criteria, OrderBy)) {
|
|
|
+ OrderBy_useless = false;
|
|
|
+ }
|
|
|
+ if (Criterias != null && Criterias.size() > 1) {
|
|
|
+ for (int i = 1; i < Criterias.size(); ++i) {
|
|
|
+ JSONObject CriteriaInner = (JSONObject) Criterias.get(i);
|
|
|
+ ZillionCriteria criteria_tmp = ZillionMatchUtil.CriteriaMatch(schema.ColumnMap, schema.Key, CriteriaInner, true);
|
|
|
+ if (criteria_tmp == null) {
|
|
|
+ throw new Exception(i + 1 + " Criteria must follow key's rule!");
|
|
|
+ }
|
|
|
+
|
|
|
+ criteria_keyList.add(criteria_tmp);
|
|
|
+ if (QueryType.equals("select") && !ZillionOrderByUtil.Match(schema.Key, CriteriaInner, OrderBy)) {
|
|
|
+ OrderBy_useless = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ boolean isKey = false;
|
|
|
+ if (schema.table_type != null) {
|
|
|
+ isKey = true;
|
|
|
+ }
|
|
|
+ ZillionCriteria criteria_tmp;
|
|
|
+ int i;
|
|
|
+ for (i = 0; i < schema.Indexes.size(); ++i) {
|
|
|
+ ZillionTableIndex index_tmp = (ZillionTableIndex) schema.Indexes.get(i);
|
|
|
+ criteria_tmp = ZillionMatchUtil.CriteriaMatch(schema.ColumnMap, index_tmp.Key, Criteria, isKey);
|
|
|
+ if (criteria_tmp != null) {
|
|
|
+ schema_index = index_tmp;
|
|
|
+ zillionTableIndex = schema_index;
|
|
|
+ criteria_index = criteria_tmp;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (criteria_index != null) {
|
|
|
+ criteria_indexList.add(criteria_index);
|
|
|
+ if (QueryType.equals("select") && !ZillionOrderByUtil.Match(schema_index.Key, Criteria, OrderBy)) {
|
|
|
+ OrderBy_useless = false;
|
|
|
+ }
|
|
|
+ if (Criterias != null && Criterias.size() > 1) {
|
|
|
+ for (i = 1; i < Criterias.size(); ++i) {
|
|
|
+ JSONObject CriteriaInner = (JSONObject) Criterias.get(i);
|
|
|
+ criteria_tmp = ZillionMatchUtil.CriteriaMatch(schema.ColumnMap, schema_index.Key, CriteriaInner, isKey);
|
|
|
+ if (criteria_tmp == null) {
|
|
|
+ throw new Exception(i + 1 + " Criteria must follow index " + schema_index.index_name + "'s rule!");
|
|
|
+ }
|
|
|
+ criteria_indexList.add(criteria_tmp);
|
|
|
+ if (QueryType.equals("select") && !ZillionOrderByUtil.Match(schema_index.Key, CriteriaInner, OrderBy)) {
|
|
|
+ OrderBy_useless = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (schema.table_type != null && schema.table_type.equals("all_column")) {
|
|
|
+ if (Criterias != null && Criterias.size() > 1) {
|
|
|
+ throw new Exception("table of type all_column Criterias must follow rule!");
|
|
|
+ }
|
|
|
+ ZillionCriteria_all_column ZillionCriteria_all_column = ZillionMatchUtil.CriteriaMatch_all_column_best(schema, Criteria);
|
|
|
+ if (ZillionCriteria_all_column.isKey) {
|
|
|
+ criteria_key = ZillionCriteria_all_column.criteria;
|
|
|
+ criteria_keyList.add(criteria_key);
|
|
|
+ if (QueryType.equals("select") && !ZillionOrderByUtil.Match(schema.Key, Criteria, OrderBy)) {
|
|
|
+ OrderBy_useless = false;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ schema_index = (ZillionTableIndex) schema.Indexes.get(ZillionCriteria_all_column.index_index);
|
|
|
+ criteria_index = ZillionCriteria_all_column.criteria;
|
|
|
+ criteria_indexList.add(criteria_index);
|
|
|
+ if (QueryType.equals("select") && !ZillionOrderByUtil.Match(schema_index.Key, Criteria, OrderBy)) {
|
|
|
+ OrderBy_useless = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return zillionTableIndex;
|
|
|
+ }
|
|
|
+}
|