@Override public Vertex<I, V, E> getVertex(I vertexIndex) { byte[] vertexData = vertexMap.get(vertexIndex); if (vertexData == null) { return null; } WritableUtils.reinitializeVertexFromByteArray( vertexData, representativeVertex, useUnsafeSerialization, getConf()); return representativeVertex; }
@Override public Vertex<I, V, E> removeVertex(I vertexIndex) { byte[] vertexBytes = vertexMap.remove(vertexIndex); if (vertexBytes == null) { return null; } WritableUtils.reinitializeVertexFromByteArray(vertexBytes, representativeVertex, useUnsafeSerialization, getConf()); return representativeVertex; }
@Override public long getEdgeCount() { long edges = 0; for (byte[] vertexBytes : vertexMap.values()) { WritableUtils.reinitializeVertexFromByteArray(vertexBytes, representativeVertex, useUnsafeSerialization, getConf()); edges += representativeVertex.getNumEdges(); } return edges; }
@Override public void addPartition(Partition<I, V, E> partition) { // Only work with other ByteArrayPartition instances if (!(partition instanceof ByteArrayPartition)) { throw new IllegalStateException("addPartition: Cannot add partition " + "of type " + partition.getClass()); } ByteArrayPartition<I, V, E> byteArrayPartition = (ByteArrayPartition<I, V, E>) partition; for (Map.Entry<I, byte[]> entry : byteArrayPartition.vertexMap.entrySet()) { byte[] oldVertexBytes = vertexMap.putIfAbsent(entry.getKey(), entry.getValue()); if (oldVertexBytes == null) { continue; } // Note that vertex combining is going to be expensive compared to // SimplePartition since here we have to deserialize the vertices, // combine them, and then reserialize them. If the vertex doesn't exist, // just add the new vertex as a byte[] synchronized (this) { // Combine the vertex values WritableUtils.reinitializeVertexFromByteArray(oldVertexBytes, representativeVertex, useUnsafeSerialization, getConf()); WritableUtils.reinitializeVertexFromByteArray(entry.getValue(), representativeCombinerVertex, useUnsafeSerialization, getConf()); combine(representativeVertex, representativeCombinerVertex); } } }
@Override public Vertex<I, V, E> putVertex(Vertex<I, V, E> vertex) { byte[] vertexData = WritableUtils.writeVertexToByteArray( vertex, useUnsafeSerialization, getConf()); byte[] oldVertexBytes = vertexMap.put(vertex.getId(), vertexData); if (oldVertexBytes == null) { return null; } else { WritableUtils.reinitializeVertexFromByteArray(oldVertexBytes, representativeVertex, useUnsafeSerialization, getConf()); return representativeVertex; } }
@Override public synchronized boolean putOrCombine(Vertex<I, V, E> vertex) { // Optimistically try to first put and then combine if this fails byte[] vertexData = WritableUtils.writeVertexToByteArray( vertex, useUnsafeSerialization, getConf()); byte[] oldVertexBytes = vertexMap.putIfAbsent(vertex.getId(), vertexData); if (oldVertexBytes == null) { return true; } WritableUtils.reinitializeVertexFromByteArray(oldVertexBytes, representativeVertex, useUnsafeSerialization, getConf()); combine(representativeVertex, vertex); return false; }