janusgraph源码分析7-存储结构

本文分析了JanusGraph的存储结构,详细介绍了InternalRelation、InternalRelationType、RelationCache和EdgeSerializer等关键类。JanusGraphElement的继承体系复杂,包括JanusGraphVertex、JanusGraphEdge等。EdgeSerializer用于序列化和反序列化边,writeRelation方法涉及关系的方向、可见性、schemaId等信息的写入。readRelation则负责读取关系数据。

基础类

InternalRelation 和 InternalRelationType

有关类型体系很复杂,可以使用 IDEA 的显示继承体系功能,查看类图。类图比较大,不太好看。大概描述一下:
主要是 JanusGraphElement 作为顶级类,接下来还有一个 InternalElement 作为顶级的 Internal 类。
JanusGraphElement 继承的类主要分为 JanusGraphRelation,JanusGraphVertex 两个分支,前者又分为 JanusGraphVertexProperty, JanusGraphEdge 。
InternalElement 的继承类主要分为 InternalRelation ,InternalVertex 两个分支,前者又分为 JanusGraphVertexProperty, JanusGraphEdge 。
其中 Internal 开头类总是有一个 JanusGraph 开头的类作为父类。例如 InternalRelation 继承自 JanusGraphRelation。

JanusGraphVertex 比较特殊,他除了有 InternalVertex 子类以外,还有 VertexLabel 和 RelationType 两个子类。
同理 InternalVertex 的继承体系下,除了真正的实体以外,还有一个 JanusGraphSchemaVertex ,他有 VertexLabelVertex, RelationTypeVertex 两个子类,
RelationTypeVertex 又有 EdgeLabelVertex 和 PropertyKeyVertex 两个子类。还有 BaseLabel BaseKey BaseVerteLabel 等子类。

这里就需要提到我们之前说的,janus 的 schema 也是以顶点的形式保存的,顶级类就是 JanusGraphSchemaVertex ,有 VertexLabelVertex, EdgeLabelVertex 和 PropertyKeyVertex 三个实现。
他们分别代表了 VertexLabel EdgeLabel PropertyKey 的 Vertex,同时我们想想,这些 Vertex 也是 janus 的元素 也是有属性的,我们岂不是还要新建三个类,保存他们的 Property Label 等?
然后他们的 Label 也是有属性的,这样下去就子子孙孙无穷尽也。所以才有了上面的 BaseLabel BaseKey BaseVerteLabel 作为终极的 Vertex。

然后我们看一下 InternalRelation 和 InternalRelationType 的关系,InternalRelation 代表的就是一种关系,有 JanusGraphEdge 和 JanusGraphVertexProperty 两种,

例如一个用户的性别是女,也就是给一个顶点添加一个性别 的属性:
首先有两个顶点, a: InternalVertex (JanusGraphVertex), 性别则是一个 b: PropertyKey (InternalRelationType) 也是一个 Vertex,
则是 property 的值,实际上就是在这两个不同类型的 Vertex 之间建立一条连接。再加上一个 value 这三个组合在一起就是一个 JanusGraphVertexProperty 。

再例如我们要给一个顶点的 VertexLabel 是 User:
首先有一个用户顶点,a: InternalVertex (JanusGraphVertex),然后 User 也是一个建好的 schema,也就是顶点: VertexLabelVertex 。然后给他们之间建立一条关系,这个关系也是一个顶点 BaseLabel.VertexLabelEdge。

在比如给两个用户之间添加一个 Friend 的关系。
首先有两个顶点就是用户,然后新建一个 StandardEdge,然后 这两个顶点分别和这个 StandardEdge 建立一个 EdgeLabel 为 Friend 的关系。

到这里我们大概明白,其实添加 Property 就是和 和一个 PropertyKey 建立一条边,添加 Edge 就是和一个 vertex 建立一条边,添加 VertexLabel 就是和一个 VertexLabel 建立一条边。

InternalRelation 就是添加的边,可以序列化存储起来,也可以读出来反序列化成 InternalRelation。 InternalRelationType 就是类型,类型也是一个顶点, 而 PropertyKey 这种类型对应的属性都是 Base开头的。

RelationCache

StaticArrayEntry

类似 java.nio 的 ByteBuffer。

EdgeSerializer

writeRelation

EdgeSerializer 类主要用来写 edgestore 库,这个库序列化方式相对比较简单,但代码还是比较多。

从代码调用开始看:

for (Long vertexId : mutations.keySet()) {
   
   
       Preconditions.checkArgument(vertexId > 0, "Vertex has no id: %s", vertexId);
       final List<InternalRelation> edges = mutations.get(vertexId);
       final List<Entry> additions = new ArrayList<>(edges.size());
       final List<Entry> deletions = new ArrayList<>(Math.max(10, edges.size() / 10));
       for (final InternalRelation edge : edges) {
   
   
           final InternalRelationType baseType = (InternalRelationType) edge.getType();
           assert baseType.getBaseType()==null;

           for (InternalRelationType type : baseType.getRelationIndexes()) {
   
   
               if (type.getStatus()== SchemaStatus.DISABLED) continue;
               for (int pos = 0; pos < edge.getArity(); pos++) {
   
   
                   if (!type.isUnidirected(Direction.BOTH) && !type.isUnidirected(EdgeDirection.fromPosition(pos)))
                       continue; //Directionality is not covered
                   if (edge.getVertex(pos).longId()==vertexId) {
   
   
                       StaticArrayEntry entry = edgeSerializer.writeRelation(edge, type, pos, tx);
                       if (edge.isRemoved()) {
   
   
                           deletions.add(entry);
                       } else {
   
   
                           Preconditions.checkArgument(edge.isNew());
                           int ttl = getTTL(edge);
                           if (ttl > 0) {
   
   
                               entry.setMetaData(EntryMetaData.TTL, ttl);
                           }
                           additions.add(entry);
                       }
                   }
               }
           }
       }

       StaticBuffer vertexKey = idManager.getKey(vertexId);
       mutator.mutateEdges(vertexKey, additions, deletions);
   }

这是java类 StandardJanusGraph 写数据 的代码。可以看出写数据之前是需要调用 StaticArrayEntry entry = edgeSerializer.writeRelation(edge, type, pos, tx);
所以接下来我么的任务就是看看这个方法,我们先看看这几个参数的意义:

InternalRelation relation, 代表一条关系,可以是 edge,也可以是 Property。
如果是edge,edge的两个顶点都会保存这条边,如果是 Property,只会有节点保存,PropertyKey 不会保存。

InternalRelationType type, 可以是 Property 和 Edge

int position, 通过调用部分代码,可以看出表示顶点在这个关系中的位置。例如 v1 -[e1]-> v2, 对于e1来讲,v1的pos是0,v2的pos是1。
TypeInspector tx 用来检测类型.


public StaticArrayEntry writeRelation(InternalRelation relation, 
										InternalRelationType type, 
										int position,
										TypeInspector tx) 
										
{
   
   
    // 判断类型
    assert type==relation.getType() || (type.getBaseType() != null
            && type.getBaseType().equals(relation.getType()));
    // 得到方向,可以是 只有 OUT 和 IN 两种结果
    Direction dir = EdgeDirection.fromPosition(position);
    
    // isUnidi
<think>好的,我需要解决用户提到的在阿里云Maven仓库中找不到org.janusgraph:janusgraph-io:pom:1.1.0的问题。首先,我应该回忆一下常见的Maven依赖问题解决方法,并结合用户提供的引用内容中的类似案例进行分析。 用户提供的引用[1]中,问题是由于本地仓库被清空后,父POM无法找到,导致构建失败。解决方案可能涉及重新安装依赖或检查父项目的相对路径。引用[2]和[3]则是关于特定依赖版本在远程仓库中不存在,解决方法包括添加其他仓库或更换版本。这些案例提示我需要考虑仓库配置和依赖版本的问题。 接下来,针对JanusGraph的依赖问题,可能的步骤包括: 1. **确认依赖配置是否正确**:用户是否在pom.xml中正确声明了依赖项,特别是groupId、artifactId和版本号。需要检查是否有拼写错误或版本号错误。 2. **检查阿里云仓库是否包含该构件**:访问阿里云Maven仓库的网页,手动搜索org.janusgraph:janusgraph-io:1.1.0是否存在。如果不存在,可能需要添加其他仓库,比如JanusGraph的官方仓库或Maven中央仓库。 3. **添加其他仓库配置**:如果阿里云仓库没有该构件,可以在pom.xml或settings.xml中添加其他仓库地址,例如Maven Central或JanusGraph自己的仓库。 4. **使用正确版本号**:确认1.1.0是否为JanusGraph的有效版本。有时候版本号可能不正确,或者该版本可能已被移动到其他仓库。 5. **本地安装依赖**:如果构件确实无法从远程仓库获取,可能需要手动下载jar和pom文件,然后使用mvn install命令安装到本地仓库。 6. **检查网络和代理设置**:确保Maven能够正常访问远程仓库,没有网络问题或代理配置错误。 现在,我需要验证这些步骤是否适用。首先,访问阿里云仓库的URL:https://maven.aliyun.com/mvn/search,搜索janusgraph-io,查看是否存在1.1.0版本。假设搜索后发现不存在,那么需要添加其他仓库。JanusGraph的官方仓库可能需要查阅其文档,例如可能托管在Maven Central或自己的仓库。例如,Maven Central的仓库URL是https://repo1.maven.org/maven2/,可以检查是否存在该构件。 如果Maven Central有该构件,那么用户需要在pom.xml中添加Maven Central仓库的配置,或者确保阿里云镜像正确代理了该仓库。如果阿里云镜像没有同步该构件,可能需要直接使用中央仓库。 此外,用户可能错误地使用了不存在的版本号,比如1.1.0是否存在。根据JanusGraph的发布历史,确认是否有这个版本。例如,JanusGraph的最新版本可能是0.6.x或1.0.0,而1.1.0可能不存在,导致无法找到依赖。 如果版本号正确,但仓库中没有,可能需要手动下载并安装。用户需要从官方来源获取jar和pom文件,然后使用mvn install:install-file命令安装到本地仓库。 最后,确保用户的Maven settings.xml中没有错误的镜像配置,例如将所有请求重定向到阿里云,而阿里云缺少该构件,这时候需要排除某些仓库不被镜像覆盖。 总结,可能的解决方案包括添加正确的仓库、检查依赖配置、使用正确版本、手动安装依赖或调整镜像设置。</think>### 解决阿里云仓库找不到`org.janusgraph:janusgraph-io:pom:1.1.0`的方法 #### 1. 确认依赖配置是否正确 检查`pom.xml`中是否正确定义了依赖项: ```xml <dependency> <groupId>org.janusgraph</groupId> <artifactId>janusgraph-io</artifactId> <version>1.1.0</version> </dependency> ``` - **常见问题**:版本号错误或拼写错误(如`janusgraph-io`写成`janusgraph-io-core`)[^1]。 #### 2. 检查阿里云仓库是否包含该构件 访问阿里云Maven仓库搜索页面(https://maven.aliyun.com/mvn/search),输入`org.janusgraph:janusgraph-io`,确认是否存在`1.1.0`版本。 若阿里云仓库无此版本,需添加其他仓库(如Maven Central或JanusGraph官方仓库)。 #### 3. 添加其他仓库配置 在`pom.xml`中添加Maven Central仓库: ```xml <repositories> <repository> <id>central</id> <url>https://repo1.maven.org/maven2/</url> </repository> </repositories> ``` 或添加JanusGraph官方仓库(需确认其地址,例如GitHub Packages)[^2]。 #### 4. 验证版本号合法性 通过JanusGraph官方文档或Maven Central(https://search.maven.org/)确认`1.1.0`是否为有效版本。 若版本号错误,调整为可用版本(如`0.6.2`): ```xml <version>0.6.2</version> ``` #### 5. 手动安装依赖到本地仓库 若远程仓库无此构件,需手动下载并安装: 1. 从官方来源获取`janusgraph-io-1.1.0.jar`和`janusgraph-io-1.1.0.pom`。 2. 执行命令: ```bash mvn install:install-file \ -Dfile=janusgraph-io-1.1.0.jar \ -DpomFile=janusgraph-io-1.1.0.pom ``` #### 6. 检查镜像配置 确认`settings.xml`中未强制覆盖仓库镜像。若阿里云镜像未代理Maven Central,需调整配置: ```xml <mirror> <id>aliyunmaven</id> <mirrorOf>central</mirrorOf> <!-- 仅镜像Central,避免覆盖其他仓库 --> <url>https://maven.aliyun.com/repository/public</url> </mirror> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值