Skip to content

Commit

Permalink
Merge pull request MyCATApache#23 from karakapi/master
Browse files Browse the repository at this point in the history
  List<SQLAnnotation>
  • Loading branch information
yanjunli authored Sep 24, 2017
2 parents 2fdd7b5 + 0fdc5a3 commit 86f2a41
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import io.mycat.proxy.ProxyRuntime;

import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -51,7 +53,11 @@ public void parse(BufferSQLContext context, MycatSession session) {
intHashTables[j] = context.getTableIntHash(j);
}
try {
dynamicAnnotationManager.get().process(schemaName, SQLType.getSQLTypeByValue(sqltype), intHashTables, context).run();
List<SQLAnnotation> collect = new ArrayList<>();
dynamicAnnotationManager.get().collect(schemaName, SQLType.getSQLTypeByValue(sqltype), intHashTables, context, collect);
System.out.println(collect.toString());
collect.forEach((c) -> c.apply(context));

} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -75,10 +81,18 @@ public static void listen() {
try {
while (true) {
WatchKey key = watcher.take();//todo 线程复用,用 poll比较好?
boolean flag = false;
for (WatchEvent<?> event: key.pollEvents()) {
String str = event.context().toString();
if ("actions.yaml".equals(str)|| "annotations.yaml".equals(str)) {
flag=true;
break;
}
}
if (flag){
System.out.println("动态注解更新次数" + count.incrementAndGet());
init();
}
System.out.println("动态注解更新次数" + count.incrementAndGet());
init();
boolean valid = key.reset();
if (!valid) {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
/**
* Created by jamie on 2017/9/15.
*/
public class CacheResult implements SQLAnnotation<BufferSQLContext>{
Map<String,String> args;
public class CacheResult extends SQLAnnotation{
public CacheResult() {
if (isDebug)
System.out.println("=>CacheResult 对象本身的构造 初始化");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
/**
* Created by jamie on 2017/9/15.
*/
public class MonintorSQL implements SQLAnnotation<BufferSQLContext>{
Map<String,String> args;
public class MonintorSQL extends SQLAnnotation{
@Override
public void init(Map<String,String> args) {
this.args=args;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
package io.mycat.mycat2.sqlannotations;

import io.mycat.mycat2.sqlparser.BufferSQLContext;

import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

/**
* Created by jamie on 2017/9/15.
*/
public interface SQLAnnotation<T> extends Function<T, T> {
boolean isDebug=true;
void init(Map<String,String> args);
public abstract class SQLAnnotation {
static final boolean isDebug=true;
String method;
abstract public void init(Map<String,String> args);
abstract public BufferSQLContext apply(BufferSQLContext t);

public String getMethod() {
return method;
}

public void setMethod(String method) {
this.method = method;
}
Map<String,String> args;

public Map<String, String> getArgs() {
return args;
}

public void setArgs(Map<String, String> args) {
this.args = args;
}

@Override
public String toString() {
return "SQLAnnotation{" +
"method='" + method + '\'' +
", args=" + args +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.mycat.mycat2.sqlannotations;

import io.mycat.mycat2.sqlparser.BufferSQLContext;

import java.util.ArrayList;
import java.util.List;

/**
* Created by jamie on 2017/9/23.
*/
public class SQLAnnotationList {
List<SQLAnnotation> sqlAnnotations;
public SQLAnnotationList() {
this.sqlAnnotations = new ArrayList<>();
}

public List<SQLAnnotation> getSqlAnnotations() {
return sqlAnnotations;
}

public void setSqlAnnotations(List<SQLAnnotation> sqlAnnotations) {
this.sqlAnnotations = sqlAnnotations;
}

public BufferSQLContext apply(BufferSQLContext context){
int size=sqlAnnotations.size();
for (int i = 0; i <size ; i++) {
sqlAnnotations.get(i).apply(context);
}
return context;
}

@Override
public String toString() {
return "SQLAnnotationList{" +
"sqlAnnotations=" + sqlAnnotations +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
/**
* Created by jamie on 2017/9/15.
*/
public class SQLCach implements SQLAnnotation<BufferSQLContext>{

Map<String,String> args;
public class SQLCach extends SQLAnnotation{
@Override
public void init(Map<String,String> args) {
this.args=args;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
package io.mycat.mycat2.sqlparser.byteArrayInterface.dynamicAnnotation;

import io.mycat.mycat2.sqlannotations.SQLAnnotation;
import io.mycat.mycat2.sqlannotations.SQLAnnotationList;
import io.mycat.mycat2.sqlparser.BufferSQLContext;
import io.mycat.mycat2.sqlparser.byteArrayInterface.dynamicAnnotation.impl.DynamicAnnotation;
import io.mycat.mycat2.sqlparser.byteArrayInterface.dynamicAnnotation.impl.DynamicAnnotationKeyRoute;
import io.mycat.mycat2.sqlparser.byteArrayInterface.dynamicAnnotation.impl.SQLType;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.List;

/**
* Created by jamie on 2017/9/19.
*/
public interface DynamicAnnotationManager {


public Runnable process(int schema, int sqltype, int[] tables,BufferSQLContext context) throws Exception ;
public Runnable process(int schema, int sqltype, int[] tables, BufferSQLContext context) throws Exception;

/**
* 动态注解先匹配chema的名字,再sql类型,在匹配表名,在匹配条件
*
* @param
* @return
*/
//public void processNow(int schema, int sqltype, int[] tables, BufferSQLContext context) throws Exception ;

public default Runnable process(String schema, SQLType sqltype, int[] tables, BufferSQLContext context) throws Exception {

return process(schema.hashCode(), sqltype.getValue(), tables, context);
}

public default Runnable process(String schema, SQLType sqltype, String[] tables, BufferSQLContext context) throws Exception {
return process(schema.hashCode(), sqltype.getValue(), DynamicAnnotationKeyRoute.stringArray2HashArray(tables), context);
}
}

public void collectInSQLAnnotationList(int schema, int sqltype, int[] tables, BufferSQLContext context, List<SQLAnnotationList> collect)throws Exception;
public default void collectInSQLAnnotationList(String schema, SQLType sqltype, int[] tables, BufferSQLContext context, List<SQLAnnotationList> collect) throws Exception {
collectInSQLAnnotationList(schema.hashCode(), sqltype.getValue(), tables, context,collect);
}
public void collect(int schema, int sqltype, int[] tables, BufferSQLContext context, List<SQLAnnotation> collect)throws Exception;
public default void collect(String schema, SQLType sqltype, int[] tables, BufferSQLContext context, List<SQLAnnotation> collect) throws Exception {
collect(schema.hashCode(), sqltype.getValue(), tables, context,collect);
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.mycat.mycat2.sqlparser.byteArrayInterface.dynamicAnnotation;

import io.mycat.mycat2.sqlannotations.SQLAnnotation;
import io.mycat.mycat2.sqlannotations.SQLAnnotationList;
import io.mycat.mycat2.sqlparser.BufferSQLContext;
import io.mycat.mycat2.sqlparser.BufferSQLParser;
import io.mycat.mycat2.sqlparser.byteArrayInterface.dynamicAnnotation.impl.*;
Expand All @@ -13,7 +15,6 @@

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
Expand All @@ -34,11 +35,11 @@ public class DynamicAnnotationManagerImpl implements DynamicAnnotationManager {
public static final String match_actions = "actions";
final DynamicAnnotationKeyRoute route;
final Map<Integer, DynamicAnnotation[]> cache;
final Map<Integer, List<Function<BufferSQLContext, BufferSQLContext>>> globalFunction = new HashMap<>();
final Map<Integer, List<SQLAnnotationList>> globalFunction = new HashMap<>();
private static final Logger logger = LoggerFactory.getLogger(DynamicAnnotationManagerImpl.class);
public DynamicAnnotationManagerImpl(String actionsPath, String annotationsPath, Map<Integer, DynamicAnnotation[]> cache) throws Exception {
try {
ActonFactory<BufferSQLContext> actonFactory = new ActonFactory<>(actionsPath);
ActonFactory actonFactory = new ActonFactory(actionsPath);
RootBean object = YamlUtil.load(annotationsPath, RootBean.class);
HashMap<DynamicAnnotationKey, DynamicAnnotation> table = new HashMap<>();
Iterator<Schema> iterator = object.getAnnotations().stream().map((s) -> s.getSchema()).iterator();
Expand Down Expand Up @@ -134,9 +135,40 @@ public static void doAnnotations(DynamicAnnotation[] res, BufferSQLContext conte
}
}

public List<Function<BufferSQLContext, BufferSQLContext>> getGlobalFunctionAnnotations(int schema, int sqltype) {
public static void collectAnnotationsListSQLAnnotationList(DynamicAnnotation[] res, BufferSQLContext context, List<SQLAnnotationList> list) {
int size = res.length;
for (int i = 0; i < size; i++) {
DynamicAnnotation annotation = res[i];
try {
annotation.match.pick(0, context);
if (annotation.match.isComplete()) {
list.add(annotation.actions);
}
} catch (Exception e) {
System.out.println(annotation.toString());
e.printStackTrace();
}
}
}
public static void collectAnnotationsListSQLAnnotation(DynamicAnnotation[] res, BufferSQLContext context, List<SQLAnnotation> list) {
int size = res.length;
for (int i = 0; i < size; i++) {
DynamicAnnotation annotation = res[i];
try {
annotation.match.pick(0, context);
if (annotation.match.isComplete()) {
list.addAll(annotation.actions.getSqlAnnotations());
}
} catch (Exception e) {
System.out.println(annotation.toString());
e.printStackTrace();
}
}
}

public List<SQLAnnotationList> getGlobalFunctionAnnotations(int schema, int sqltype) {
int hash = globalFunctionHash(schema, sqltype);
List<Function<BufferSQLContext, BufferSQLContext>> list = this.globalFunction.get(hash);
List<SQLAnnotationList> list = this.globalFunction.get(hash);
return list;
}

Expand All @@ -156,7 +188,7 @@ public Runnable process(int schema, int sqltype, int[] tables, BufferSQLContext

}
DynamicAnnotation[] res = annotations;
List<Function<BufferSQLContext, BufferSQLContext>> globalFunction = getGlobalFunctionAnnotations(schema, sqltype);
List<SQLAnnotationList> globalFunction = getGlobalFunctionAnnotations(schema, sqltype);
if (res == null && globalFunction == null) {
return () -> {
logger.debug("没有匹配的action");
Expand All @@ -176,7 +208,75 @@ public Runnable process(int schema, int sqltype, int[] tables, BufferSQLContext
};
}

private static void doList(List<Function<BufferSQLContext, BufferSQLContext>> globalFunction, BufferSQLContext args) {
public void collectInSQLAnnotationList(int schema, int sqltype, int[] tables, BufferSQLContext context, List<SQLAnnotationList> collect) throws Exception {
Arrays.sort(tables);
DynamicAnnotation[] annotations;
int hash = hash(schema, sqltype, tables);
annotations = cache.get(hash);
if (annotations == null) {
annotations = getAnnotations(schema, sqltype, tables);
if (annotations == null) {

} else {
cache.put(hash, annotations);
}

}
DynamicAnnotation[] res = annotations;
List<SQLAnnotationList> globalFunction = getGlobalFunctionAnnotations(schema, sqltype);
if (res == null && globalFunction == null) {

} else if (res != null && globalFunction == null) {
collectAnnotationsListSQLAnnotationList(res, context, collect);
} else if (res == null && globalFunction != null) {
int size = globalFunction.size();
for (int i = 0; i < size; i++) {
collect.add(globalFunction.get(i));
}
}else {
int size = globalFunction.size();
for (int i = 0; i < size; i++) {
collect.add(globalFunction.get(i));
}
collectAnnotationsListSQLAnnotationList(res, context, collect);
}

}
public void collect(int schema, int sqltype, int[] tables, BufferSQLContext context, List<SQLAnnotation> collect) throws Exception {
Arrays.sort(tables);
DynamicAnnotation[] annotations;
int hash = hash(schema, sqltype, tables);
annotations = cache.get(hash);
if (annotations == null) {
annotations = getAnnotations(schema, sqltype, tables);
if (annotations == null) {

} else {
cache.put(hash, annotations);
}

}
DynamicAnnotation[] res = annotations;
List<SQLAnnotationList> globalFunction = getGlobalFunctionAnnotations(schema, sqltype);
if (res == null && globalFunction == null) {

} else if (res != null && globalFunction == null) {
collectAnnotationsListSQLAnnotation(res, context, collect);
} else if (res == null && globalFunction != null) {
int size = globalFunction.size();
for (int i = 0; i < size; i++) {
collect.addAll(globalFunction.get(i).getSqlAnnotations());
}
}else {
int size = globalFunction.size();
for (int i = 0; i < size; i++) {
collect.addAll(globalFunction.get(i).getSqlAnnotations());
}
collectAnnotationsListSQLAnnotation(res, context, collect);
}

}
private static void doList(List<SQLAnnotationList> globalFunction, BufferSQLContext args) {
int size = globalFunction.size();
for (int i = 0; i < size; i++) {
globalFunction.get(i).apply(args);
Expand Down
Loading

0 comments on commit 86f2a41

Please sign in to comment.