Java tutorial
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.huawei.streaming.cql.semanticanalyzer; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Set; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.collect.Sets; import com.huawei.streaming.api.streams.Column; import com.huawei.streaming.api.streams.Schema; import com.huawei.streaming.cql.exception.SemanticAnalyzerException; import com.huawei.streaming.cql.semanticanalyzer.analyzecontext.AnalyzeContext; import com.huawei.streaming.cql.semanticanalyzer.parser.context.ParseContext; import com.huawei.streaming.exception.ErrorCode; /** * ? * */ public abstract class BaseAnalyzer implements SemanticAnalyzer { private static final Logger LOG = LoggerFactory.getLogger(BaseAnalyzer.class); /** * ?? * sql?????? */ private static final String DEFAULT_COLUMN_PRIFIX = "x_col_"; /** * ???? */ private static final String RENAME_COLUMN_POSTFIX = "_"; private List<Schema> allSchemas = null; private ParseContext parseContext; /** * <> * @param parseContext ? * @throws SemanticAnalyzerException ? */ public BaseAnalyzer(ParseContext parseContext) throws SemanticAnalyzerException { this.parseContext = parseContext; } /** * {@inheritDoc} * Java?? * --???? * --???? * ?--???? * ?--???? * --?? * --?? * -- * ?--?? * ?--?? * ?-- */ @Override public void init(List<Schema> schemas) throws SemanticAnalyzerException { /* * java?????? * init * ??????? */ createAnalyzeContext(); getAnalyzeContext().setParseContext(parseContext); getAnalyzeContext().validateParseContext(); allSchemas = schemas; } /** * analyze?? */ protected abstract void createAnalyzeContext(); /** * ?? * analyze?? * getAnalyzeContext????? * ??? * analyze???? * @return ? */ protected abstract AnalyzeContext getAnalyzeContext(); public List<Schema> getAllSchemas() { return allSchemas; } /** * ???schema * @param name ?? * @return schema * @throws SemanticAnalyzerException ? */ protected Schema getSchemaByName(String name) throws SemanticAnalyzerException { for (Schema schema : allSchemas) { if (compareSchema(name, schema)) { return schema; } } SemanticAnalyzerException exception = new SemanticAnalyzerException( ErrorCode.SEMANTICANALYZE_NOFOUND_STREAM, name); LOG.error("Can't find schema by name.", exception); throw exception; } /** * ???schema * @param name ?? * @param schemas schema * @return schema * @throws SemanticAnalyzerException ? */ public static Schema getSchemaByName(String name, List<Schema> schemas) throws SemanticAnalyzerException { for (Schema schema : schemas) { if (compareSchema(name, schema)) { return schema; } } SemanticAnalyzerException exception = new SemanticAnalyzerException( ErrorCode.SEMANTICANALYZE_NOFOUND_STREAM, name); LOG.error("Can't find schema by stream name.", exception); throw exception; } /** * schema?shcmea?? * schmea?????? * * @param schemas schema */ public static void setSchemaNameInAttributes(List<Schema> schemas) { for (int i = 0; i < schemas.size(); i++) { Schema s = schemas.get(i); for (Column attr : s.getCols()) { attr.setName(attr.getName().toLowerCase(Locale.US)); } } } /** * schema? * @param name schema?? * @return true */ protected boolean checkSchemaExists(String name) { for (Schema schema : allSchemas) { if (compareSchema(name, schema)) { return true; } } return false; } /** * Stream * ?stream???schema??schema?? * @param schemasInCQL ?schema? * @throws SemanticAnalyzerException schema???? */ protected void checkSchemas(List<Schema> schemasInCQL) throws SemanticAnalyzerException { Set<String> names = Sets.newHashSet(); for (Schema s : schemasInCQL) { /* * schema???? * ???? */ if (names.contains(s.getId())) { SemanticAnalyzerException exception = new SemanticAnalyzerException( ErrorCode.SEMANTICANALYZE_EXISTS_STREAM, s.getId()); LOG.error("Stream {} already exists.", s.getId(), exception); throw exception; } } } /** * ??????? * @param schema ???schema? * @param colName ?? * @return ?? */ protected String renameNewName(Schema schema, String colName) { /* * Schema? * ?????? * * ???????1 */ int count = 1; for (Column attr : schema.getCols()) { if (attr.getName().startsWith(colName + RENAME_COLUMN_POSTFIX)) { count++; } } return colName + RENAME_COLUMN_POSTFIX + count; } /** * ??? * @param schema ???schema? * @return ?? */ protected String createNewName(Schema schema) { int count = 0; if (schema != null && schema.getCols() != null) { for (Column attr : schema.getCols()) { if (attr.getName().startsWith(DEFAULT_COLUMN_PRIFIX)) { count++; } } } return DEFAULT_COLUMN_PRIFIX + count; } /** * ?schema * * @param schema schema * @return */ public static List<Column> getAttributes(Schema schema) { return schema.getCols(); } /** * ?? * select * * @param schemas schema * @return */ protected static List<Column> getAllAttributes(List<Schema> schemas) { List<Column> attrs = new ArrayList<Column>(); for (Schema schema : schemas) { attrs.addAll(getAttributes(schema)); } return attrs; } private static boolean compareSchema(String name, Schema schema) { if (!StringUtils.isEmpty(schema.getId()) && schema.getId().equalsIgnoreCase(name)) { return true; } if (!StringUtils.isEmpty(schema.getName()) && schema.getName().equalsIgnoreCase(name)) { return true; } if (!StringUtils.isEmpty(schema.getStreamName()) && schema.getStreamName().equalsIgnoreCase(name)) { return true; } return false; } /** * ??? * ?? * * @param attrName ?? * @param schema schema * @param schemas schema * @return ? */ public static List<Column> getAttributeByName(String attrName, Schema schema, List<Schema> schemas) { List<Column> allAttrs = null; if (schema == null) { allAttrs = getAllAttributes(schemas); } else { allAttrs = getAttributes(schema); } return getColumnByNameOrALias(allAttrs, attrName); } /** * ??? * ?? * * @param attrName ?? * @param schemas schema * @return ? */ public static List<Column> getAttributeByName(String attrName, List<Schema> schemas) { List<Column> allAttrs = getAllAttributes(schemas); return getColumnByNameOrALias(allAttrs, attrName); } private static List<Column> getColumnByNameOrALias(List<Column> columns, String nameOrAlias) { List<Column> res = new ArrayList<Column>(); for (Column attr : columns) { if (!StringUtils.isEmpty(attr.getName()) && attr.getName().equalsIgnoreCase(nameOrAlias)) { res.add(attr); continue; } if (!StringUtils.isEmpty(attr.getAlias()) && attr.getAlias().equalsIgnoreCase(nameOrAlias)) { res.add(attr); continue; } } return res; } }