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 ]繼承

相關文章
相關標籤/搜索