Gadtry 1.4.0-rc1 发布,全新Graph图数据容器能力,异步Fork JVM 多进程支持。Gadtry是一个java工具库集合。包含:ioc, exec(fork进程), graph(图计算)等等常用工具集,并且完全零依赖。更新内容如下:
Aop:
Fix JavassistProxy thread safety issues
Fork Multiprocessing
JvmLanucher supports asynchronous
ForkJvm supports environment variable settings
Graph:
Graph supports search features
Optimize graph data structures and models
Node and edge support separate data
Graph provides powerful search capabilities and supports independent data filtering
Other:
add newStringHelper() support toString()
更新核心内容演示:
支持异步多进程:
JVMLauncher<String> launcher = JVMLaunchers.<String>newJvm() .setEnvironment("TestEnv", envValue) .addUserjars(Collections.emptyList()) .setXms("16m") .setXmx("16m") .setConsole((msg) -> System.out.println(msg)) .build(); //带来以下全新移步API VmFuture<String> vmFuture = launcher.startAsync(() -> { System.out.println("hello world! I am Fork Jvm"); return "hello world!"; }); int ForkJvmPid = vmFuture.getPid(); //获取pid vmFuture.isRunning(); //判断fork进程是否在允运行中 vmFuture.cancel(); //强制结束fork进程 String value = vmFuture.get(3, TimeUnit.SECONDS); //阻塞获取fork进程返回值 Assert.assertEquals("hello world!", value);
Graph图数据结构容器完全重构:
现在完全支持图寻迹搜索功能(图数据库特性)
节点(node)和边(edge)全部都支持独立数据(图数据库特性)
全新的图print打印功能
Graph<?, ?> graph = Graph.builder() .addNode("Throwable") .addNode("Exception") .addNode("IOException") .addNode("FileNotFoundException") .addNode("RuntimeException") .addNode("UnsupportedOperationException") .addNode("IllegalArgumentException") .addNode("Error") .addNode("OutOfMemoryError") .addNode("NoClassDefFoundError") .addEdge("Throwable", "Exception") .addEdge("Throwable", "Error") .addEdge("Exception", "IOException") .addEdge("Exception", "FileNotFoundException") .addEdge("Exception", "RuntimeException") .addEdge("RuntimeException", "UnsupportedOperationException") .addEdge("RuntimeException", "IllegalArgumentException") .addEdge("Error", "OutOfMemoryError") .addEdge("Error", "NoClassDefFoundError") .create(); graph.printShow().forEach(System.out::println);
将打印如下 效果:
/
└────Throwable
├────Error
│ ├────NoClassDefFoundError
│ └────OutOfMemoryError
└────Exception
├────RuntimeException
│ ├────IllegalArgumentException
│ └────UnsupportedOperationException
├────FileNotFoundException
└────IOException
搜索能力:
以某著名外企笔试题为例:
找出A地到B地的最短路径(注意可能有多条)
那么使用Gadtry Graph如何来解决呢?
List<Route<NodeData, EdgeData>> minRoutes = new ArrayList<>(); //存放最短路径的容器 graph.searchRuleRoute("A", route -> { //已起点A开始搜索 if ("B".equals(route.getEndNodeId())) { if (minRoutes.isEmpty()) { minRoutes.add(route); return false; } long minDistance = getRouteDistance(minRoutes.get(0)); //取出第一个轨迹计算距离即可 long distance = getRouteDistance(route); //计算出当前轨迹距离 if (distance == minDistance) { minRoutes.add(route); //注意可能有多条 } else if (distance < minDistance) { minRoutes.clear(); minRoutes.add(route); } return false; } else { //这里给出理论: 起点和终点不同时如果一个点在轨迹中出现两次那么它一定不是最短路径. return route.containsDeadRecursion(); //如果出现两次则无须继续递归查找 } }); checkState(!minRoutes.isEmpty(), "NO SUCH ROUTE A TO B"); return minRoutes;
上面略复杂,下面我们再看个简单的:
找出A到B不超过3个节点以内所有轨迹
List<Route<NodeData, EdgeData>> routes = graph.searchRuleRoute("A", "B", route -> route.size() <= 3);
完整的图计算相关demo可参考:
https://github.com/harbby/gadtry/blob/1.4.0-rc1/src/test/java/com/github/harbby/gadtry/graph/GraphDemo.java
https://github.com/harbby/gadtry/blob/1.4.0-rc1/src/test/java/com/github/harbby/gadtry/graph/GraphxTest.java