<commit_before>package ingraph.ire

import hu.bme.mit.ire.util.BufferMultimap

import org.neo4j.driver.v1.Value
import org.neo4j.driver.v1.types.{Node, Relationship}

import scala.collection.JavaConversions._
import scala.collection.mutable


class IngraphVertex(node: Node, indexer: Indexer) {
  val id: Long = node.id()
  val properties: Map[String, AnyRef]  = node.asMap().toMap
  val edges: mutable.ListMap[String, IngraphEdge] = mutable.ListMap[String, IngraphEdge]()
  val reverseEdges: mutable.ListMap[String, IngraphEdge] = mutable.ListMap[String, IngraphEdge]()
  val labels = node.labels().toSet
  for (label <- labels)
    indexer.vertexLabelLookup.addBinding(label, this)
  indexer.nodeLookup(id) = this

  override def toString: String = s"Vertex($id, $properties)"
}

class IngraphEdge(relation: Relationship, indexer: Indexer) {
  val id: Long = relation.id()
  val properties: Map[String, AnyRef] = relation.asMap().toMap
  val sourceVertex: IngraphVertex = indexer.nodeLookup(relation.startNodeId())
  sourceVertex.edges(relation.`type`()) = this
  val targetVertex: IngraphVertex = indexer.nodeLookup(relation.endNodeId())
  targetVertex.reverseEdges(relation.`type`()) = this
  indexer.edgeLabelLookup.addBinding(relation.`type`(), this)
  val label: String = relation.`type`()
  override def toString: String = s"Edge(${sourceVertex.id} -[$id]-> ${targetVertex.id}, $properties)"
}

class Indexer(tupleMapper: EntityToTupleMapper) {

  val nodeLookup = mutable.HashMap[Long, IngraphVertex]()
  val vertexLabelLookup = new BufferMultimap[String, IngraphVertex]()
  val edgeLabelLookup = new BufferMultimap[String, IngraphEdge]()

  def addVertex(node: Node) = {
    val res = new IngraphVertex(node, this)
    tupleMapper.addVertex(res)
    res
  }
  def addEdge(relation: Relationship) = {
    val res = new IngraphEdge(relation, this)
    tupleMapper.addEdge(res)
    res
  }

  def vertexById(id: Long): IngraphVertex = nodeLookup(id)
  def verticesByLabel(label: String): Seq[IngraphVertex] = vertexLabelLookup(label)
  def edgesByLabel(label: String): Seq[IngraphEdge] = edgeLabelLookup(label)
}<commit_msg>Decouple indexing from edge/tuple creation<commit_after>package ingraph.ire

import hu.bme.mit.ire.util.BufferMultimap

import org.neo4j.driver.v1.Value
import org.neo4j.driver.v1.types.{Node, Relationship}

import scala.collection.JavaConversions._
import scala.collection.mutable


case class IngraphVertex(id: Long, properties: Map[String, AnyRef], labels: Set[String]) {
  val edges: mutable.ListMap[String, IngraphEdge] = mutable.ListMap[String, IngraphEdge]()
  val reverseEdges: mutable.ListMap[String, IngraphEdge] = mutable.ListMap[String, IngraphEdge]()
  override def toString: String = s"Vertex($id, $properties)"
}

case class IngraphEdge(id: Long, properties: Map[String, AnyRef],
                       sourceVertex: IngraphVertex,
                       targetVertex: IngraphVertex,
                      label: String) {
  override def toString: String = s"Edge(${sourceVertex.id} -[$id]-> ${targetVertex.id}, $properties)"
}

class Indexer(tupleMapper: EntityToTupleMapper) {

  val nodeLookup = mutable.HashMap[Long, IngraphVertex]()
  val vertexLabelLookup = new BufferMultimap[String, IngraphVertex]()
  val edgeLabelLookup = new BufferMultimap[String, IngraphEdge]()

  def addVertex(node: Node): IngraphVertex = {
    val id: Long = node.id()
    val properties: Map[String, AnyRef]  = node.asMap().toMap
    val labels = node.labels().toSet
    val vertex = IngraphVertex(id, properties, labels)
    for (label <- labels)
      vertexLabelLookup.addBinding(label, vertex)
    nodeLookup(id) = vertex
    tupleMapper.addVertex(vertex)
    vertex
  }
  def addEdge(relation: Relationship): IngraphEdge = {
    val id: Long = relation.id()
    val properties: Map[String, AnyRef] = relation.asMap().toMap
    val sourceVertex: IngraphVertex = nodeLookup(relation.startNodeId())
    val targetVertex: IngraphVertex = nodeLookup(relation.endNodeId())
    val label: String = relation.`type`()
    val edge = IngraphEdge(id, properties, sourceVertex, targetVertex, label)
    sourceVertex.edges(relation.`type`()) = edge
    targetVertex.reverseEdges(relation.`type`()) = edge
    edgeLabelLookup.addBinding(relation.`type`(), edge)
    tupleMapper.addEdge(edge)
    edge
  }

  def vertexById(id: Long): IngraphVertex = nodeLookup(id)
  def verticesByLabel(label: String): Seq[IngraphVertex] = vertexLabelLookup(label)
  def edgesByLabel(label: String): Seq[IngraphEdge] = edgeLabelLookup(label)
}
{'fraction_non_alphanumeric': 0.12139872443369254, 'fraction_numerical': 0.0017594018033868484, 'mean_word_length': 4.875968992248062, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7482262', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1422, 'n_words': 314}