<commit_before>// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.workspace.api.store

import com.intellij.openapi.util.text.StringUtil
import com.intellij.util.io.URLUtil
import java.util.*

fun main() {
  val store = Store()
  store.add("/private/var/folders/dw/T/unitTest_importModule/idea_test_/oyt.dmg", 20)
  store.add("/private/var/folders/dw/T/oyt.dmg", 15)
  store.add("/private/var/folders/dt/idea_test_/oyt.dmg", 7)
  store.add("/private/var/folders/dk/T/unitTest_importModule/idea_test_/oyt.dmg", 6)
  store.add("/private/var/folders/do/T/unitTest_importModule/oyt.dmg", 8)
  println("")
  println(store.findLatestFilePathNode("/private/var/folders/dk/T/unitTest_importModule/idea_test_/oyt.dmg")?.values)
  val values = LinkedHashSet<Int>(5)
  println(values.isEmpty())
}

class FilePathNode(val content: String, val parent: FilePathNode? = null) {
  val values: MutableSet<Int> = mutableSetOf()
  val children: MutableSet<FilePathNode> = mutableSetOf()
}

class Store {
  private var rootNode: FilePathNode? = null

  //fun add(path: String, entity: PEntityData<out TypedEntity>) {
  fun add(path: String, entityId: Int) {
    val names = splitNames(path)
    var latestNode: FilePathNode? = rootNode
    for (index in names.indices.reversed()) {
      val name = names[index]
      // Latest node can be NULL only if it's root node
      if (latestNode == null) {
        val newNode = FilePathNode(name)
        // If it's the latest name of folder or files, save entity Id as node value
        if (index == 0) {
          newNode.values.add(entityId)
          return
        }
        latestNode = newNode
        rootNode = newNode
        continue
      }

      if (latestNode === rootNode) {
        if (latestNode.content == name) {
          if (index == 0) {
            latestNode.values.add(entityId)
            return
          }
          continue
        }
      }

      val node = latestNode.children.find { it.content == name }
      if (node == null) {
        val newNode = FilePathNode(name, latestNode)
        latestNode.children.add(newNode)
        latestNode = newNode
        // If it's the latest name of folder or files, save entity Id as node value
        if (index == 0) {
          newNode.values.add(entityId)
          return
        }
      } else {
        // If it's the latest name of folder or files, save entity Id as node value
        if (index == 0) {
          node.values.add(entityId)
          return
        }
        latestNode = node
      }
    }
  }

  //fun remove(path: String, entity: PEntityData<out TypedEntity>) {
  fun remove(path: String, entityId: Int) {
    val names = splitNames(path)
    var latestNode: FilePathNode? = rootNode
    for (index in names.indices.reversed()) {
      val name = names[index]
      // Latest node can be NULL only if it's root node
      if (latestNode == null) {
        println("Path doesn't exist")
        return
      }

      if (latestNode === rootNode) {
        if (latestNode.content != name) {
          println("Path doesn't exist")
          return
        } else {
          if (index == 0) {
            if (!latestNode.values.remove(entityId)) {
              println("EntityId $entityId doesn't exist")
              return
            }
            if (latestNode.values.isEmpty() && latestNode.children.isEmpty()) {
              rootNode = null
              return
            }
          } else continue
        }
      }

      val node = latestNode.children.find { it.content == name }
      if (node == null) {
        println("Path doesn't exist")
      } else {
        if (index == 0) {
          if (!node.values.remove(entityId)) {
            println("EntityId $entityId doesn't exist")
            return
          }
          if (latestNode.values.isEmpty() && latestNode.children.isEmpty()) {
            rootNode = null
            return
          }
          node.values.add(entityId)
          return
        }
        latestNode = node
      }
    }
  }

  //fun update(oldPath: String, newPath: String, entity: PEntityData<out TypedEntity>) {
  fun update(oldPath: String, newPath: String, entityId: Int) {
    remove(oldPath, entityId)
    add(newPath, entityId)
  }

  fun findLatestFilePathNode(path: String): FilePathNode? {<commit_msg>[workspace model] Add remove method for file path storage<commit_after>// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.workspace.api.store

import com.intellij.openapi.util.text.StringUtil
import com.intellij.util.io.URLUtil
import java.util.*

fun main() {
  val store = Store()
  store.add("/private/var/folders/dw/T/unitTest_importModule/idea_test_/oyt.dmg", 20)
  store.add("/private/var/folders/dw/T/oyt.dmg", 15)
  store.add("/private/var/folders/dt/idea_test_/oyt.dmg", 7)
  store.add("/private/var/folders/dk/T/unitTest_importModule/idea_test_/oyt.dmg", 6)
  store.add("/private/var/folders/do/T/unitTest_importModule/oyt.dmg", 8)
  println("")
  println(store.findLatestFilePathNode("/private/var/folders/dk/T/unitTest_importModule/idea_test_/oyt.dmg")?.values)
  val values = LinkedHashSet<Int>(5)
  println(values.isEmpty())
}

class FilePathNode(val content: String, val parent: FilePathNode? = null) {
  val values: MutableSet<Int> = mutableSetOf()
  val children: MutableSet<FilePathNode> = mutableSetOf()
}

class Store {
  private var rootNode: FilePathNode? = null

  //fun add(path: String, entity: PEntityData<out TypedEntity>) {
  fun add(path: String, entityId: Int) {
    val names = splitNames(path)
    var latestNode: FilePathNode? = rootNode
    for (index in names.indices.reversed()) {
      val name = names[index]
      // Latest node can be NULL only if it's root node
      if (latestNode == null) {
        val newNode = FilePathNode(name)
        // If it's the latest name of folder or files, save entity Id as node value
        if (index == 0) {
          newNode.values.add(entityId)
          return
        }
        latestNode = newNode
        rootNode = newNode
        continue
      }

      if (latestNode === rootNode) {
        if (latestNode.content == name) {
          if (index == 0) {
            latestNode.values.add(entityId)
            return
          }
          continue
        }
      }

      val node = latestNode.children.find { it.content == name }
      if (node == null) {
        val newNode = FilePathNode(name, latestNode)
        latestNode.children.add(newNode)
        latestNode = newNode
        // If it's the latest name of folder or files, save entity Id as node value
        if (index == 0) {
          newNode.values.add(entityId)
          return
        }
      } else {
        // If it's the latest name of folder or files, save entity Id as node value
        if (index == 0) {
          node.values.add(entityId)
          return
        }
        latestNode = node
      }
    }
  }

  //fun remove(path: String, entity: PEntityData<out TypedEntity>) {
  fun remove(path: String, entityId: Int) {
    val node = findLatestFilePathNode(path)
    if (node == null || node.values.remove(entityId)) {
      println("File not found")
      return
    }
    if (node.values.isNotEmpty() || node.children.isNotEmpty()) return

    var currentNode: FilePathNode = node
    do {
      val parent = currentNode.parent
      if (parent == null) {
        if (parent === rootNode) rootNode = null
        return
      }

      parent.children.remove(node)
      currentNode = parent
    } while (currentNode.values.isEmpty() && currentNode.children.isEmpty())
  }

  //fun update(oldPath: String, newPath: String, entity: PEntityData<out TypedEntity>) {
  fun update(oldPath: String, newPath: String, entityId: Int) {
    remove(oldPath, entityId)
    add(newPath, entityId)
  }

  fun findLatestFilePathNode(path: String): FilePathNode? {
{'fraction_non_alphanumeric': 0.11451331839681354, 'fraction_numerical': 0.005725665919840677, 'mean_word_length': 2.373215785054576, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '3393193', 'n_tokens_mistral': 2538, 'n_tokens_neox': 2451, 'n_words': 632}