Skip to content

base upon gremlin-core dependency, support gremlin traversal extension api such as mybatis-plus.

Notifications You must be signed in to change notification settings

ICoder0/gremlin-plus

Repository files navigation

Gremlin plus

AUR license GitHub Repo stars GitHub forks

主要解决以下问题

  • 释放对Vertex/Edge/Property标签MAGIC_CODE管理.
  • 简化Vertex/Edge对应实体类的Convert/Mapping逻辑.
  • 提供Vertex/Edge对应非序列化字段的对象缓存机制, 减少客户端显式声明多个缓存管理的开发成本开销, 并提供KeyGenerator/UnSerializedPropertyCache扩展插件接口.

参见相关的ogm(ferma、mybatis-plus、spring-data-xx)源码.

个人倾向mybatis-plus的api, jpa方面声明式的api对于图数据库的遍历不够灵活.

mybatis-plus的serializedLambda内部resolve方法不支持evaluate expression调试, 该问题在gremlin-plus中已提供解决方案.

@VertexLabel("<USER>")
public class User {
  @VertexProperty("[NAME]")
  private String name;
  @VertexProperty("[AGE]")
  private Long age;
}

public class Solution {

  public static void main(String[] args) {
    Graph graph = null;
    try (final GraphPlusTraversalSource g = graph.traversal(GraphPlusTraversalSource.class)) {
        g.addV(new User("bofa1ex", 23L));
        // or g.addV(User.class).property(User::getName, "bofa1ex").property(User::getAge, 23L).property(TheOther::Sth, "sth else");
        final Vertex vertex = g.V().hasLabel(User.class).has(User::getName, "bofa1ex").next();
        // or u wanna convert vertex/edge to bean directly.
        final User user = g.V().hasLabel(User.class).has(User::getName, "bofa1ex").toBean();
        // maybe need optional object which wrapper the destinition obj.
        final Optional<User> userOpt = g.V().hasLabel(User.class).has(User::getName, "bofa1ex").tryToBean();
        // maybe need beans collection/stream in normal.
        final List<User> users = g.V().hasLabel(User.class).has(User::getName, "bofa1ex").toBeanList();
        // maybe when u query the bean which is not exists, and u should try add it like that in below.
        final Optional<User> anonymousUserOpt = g.V().hasLabel(User.class).has(User::getName, "anonymous").tryToBean();
        if(!anonymousUserOpt.isPresent()){
            g.addV(User.builder().name("anonymous").build());
        }
        // but `gremlin-plus` supported `#getIfAbsent` api, u just need try it in the same situation in below.
        Pair<User,Vertex> userVertexPair = g.V().hasLabel(User.class).has(User::getName, "anonymous").getIfAbsent(
            User.builder().name("anonymous").build()
        );
        // u might be confused by this return value, why use tuple with bean and vertex, talk is cheap, see the code in below.
        g.addE(Follow.class).from(g.V().hasLabel(User.class).has(User::getName, "bofa1ex")).to(
            // when u wanna add edge from the vertex to the other vertex which maybe not exists.
            userVertexPair.getRight()
        ).tryNext();
        // i thought the update operation should follow the query operation in strictly, so that why i did not open the getIfPresent api.
    } catch (Exception ignored) {
    }
  }
}

更多samples参见example模块, 若有更多需求, 欢迎提issue/pr.

About

base upon gremlin-core dependency, support gremlin traversal extension api such as mybatis-plus.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages