def graphToFile()

in cassovary-core/src/main/scala/com/twitter/cassovary/graph/MemoryMappedDirectedGraph.scala [95:128]


  def graphToFile(graph: DirectedGraph[Node], file: File): Unit = {
    val n = graph.maxNodeId + 1 // includes both 0 and maxNodeId as ids
    val out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)))
    out.writeLong(0)
    out.writeLong(n)
    //The outneighbor data starts after the initial 8 bytes, n+1 Longs for outneighbors, and n+1
    // Longs for in-neighbors
    var outboundOffset = 16L + 8L * (n + 1) * 2
    for (i <- 0 until n) {
      out.writeLong(outboundOffset)
      outboundOffset += 4 * (graph.getNodeById(i) map (_.outboundCount)).getOrElse(0)
    }
    out.writeLong(outboundOffset) // Needed to compute outdegree of node n-1

    // The inbound data starts immediately after the outbound data
    var inboundOffset = outboundOffset
    for (i <- 0 until n) {
      out.writeLong(inboundOffset)
      inboundOffset += 4 * (graph.getNodeById(i) map (_.inboundCount)).getOrElse(0)
    }
    out.writeLong(inboundOffset) // Needed to compute indegree of node n-1

    for (i <- 0 until n) {
      for (v <- (graph.getNodeById(i) map (_.outboundNodes())).getOrElse(CSeq.empty[Int])) {
        out.writeInt(v)
      }
    }
    for (i <- 0 until n) {
      for (v <- (graph.getNodeById(i) map (_.inboundNodes())).getOrElse(CSeq.empty[Int])) {
        out.writeInt(v)
      }
    }
    out.close()
  }