Giraph源码分析(六)——Edge 分析

1.在Vertex类中,顶点的存储方式采用邻接表形式。每一个顶点有 VertexId、VertexValue、OutgoingEdges和Halt,boolean型的halt变量用于记录顶点的状态,false时表示active,true表示inactive状态。 片断代码以下。apache

Giraph源码分析(六)——Edge 分析
2.org.apache.giraph.edge.Edge 接口,用于存储顶点的边,每条边包含targetVertexId和edgeValue两个属性。类关系图以下:ide

Giraph源码分析(六)——Edge 分析

Giraph默认使用DefaultEdge类存储边,该类中有两个变量: I targetVertexId和 E value,I为顶点ID的类型,E为边的类型。注意,DefaultEdge类同时继承ReusableEdge<I,E>接口,在ReusableEdge<I,E>类的定义中,有以下说明文字:
A complete edge, the target vertex and the edge value. Can only be one edge with a destination vertex id per edge map. This edge can be reused, that is you can set it's target vertex ID and edge value. Note: this class is useful for certain optimizations, but it's not meant to be exposed to the user. Look at MutableEdge instead.源码分析

从上述说明文字可知,edge能够被重用,只须要修改targetVertexId和value的值就行。即每一个Vertex如有多条出边,只会建立一个DefaultEdge对象来存储边。
3.org.apache.giraph.edge.OutEdges 用于存储每一个顶点的out-edges。从Vertex类的定义可知,顶点的每条边都被存储在OutEdges类型的edge对象中,OutEdges接口的关系图以下:优化

Giraph源码分析(六)——Edge 分析

Giraph默认的使用ByteArrayEdges<I,E>,每一个顶点的全部边都被存储在byte[ ]中。当顶点向它的出边发送消息时,须要遍历Vertex类中的edges对象。示例代码以下:this

Giraph源码分析(六)——Edge 分析
注意:由DefaultEdge的定义可知,遍历getEdges时,返回的Edge对象时同一个对象,只是该对象中值改变了。下面继续查看代码来证实此观点。
查看ByteArrayEdges类的iterator()方法,以下:3d

Giraph源码分析(六)——Edge 分析
返回的是内部类ByteArrayEdgeIterator对象,定义以下:对象

Giraph源码分析(六)——Edge 分析

总结:当顶点的出度很大时,此优化甚好,能很好的节约内存。如UK-2005数据中,顶点的最大出度为 5213。
假设顶点1的出度顶点有<2 , 0.4>,<3 , 7.8> ,<5 , 6.4> 。以下代码:blog

Giraph源码分析(六)——Edge 分析
输出结果为:
[ 2 ]
[ 3 , 3 ]
[ 5 , 5 , 5 ]
并不是是但愿的 [ 2 , 3 , 5 ]继承

相关文章
相关标签/搜索