<commit_before>package silky.persistence.file

import java.io.FileNotFoundException
import java.nio.file.Paths

import silky.persistence.{Entry, Persistence}

import scala.reflect.io.Directory

class FilePersistence(baseDir: String, fileExtension: String = "json") extends Persistence {

  def lastRefAcross(prefix: Char, contexts: String*): String = {
    val files = contexts flatMap filesIn filter (f ⇒ f.name.head == prefix && f.extension == fileExtension) sortBy (_.name)
    if (files.isEmpty) "00000000" else files.last.stripExtension.tail
  }

  def save(entry: Entry) = {
    createIfRequired(directoryFor(entry.context))
    Filepath.save(entry.contents, pathFor(entry.context, entry.ref))
    entry
  }

  def find(context: String, ref: String) = filesIn(context)
    .find(_.name == s"$ref.$fileExtension")
    .map(f ⇒ Entry(context, ref, f.slurp()))

  def load(context: String, predicate: String ⇒ Boolean) = filesIn(context)
    .filter(f ⇒ predicate(f.name.replace(s".$fileExtension", "")))
    .map(f ⇒ Entry(context, f.name.replace(s".$fileExtension", ""), f.slurp()))
    .toSeq

  def move(ref: String, source: String, target: String) = {
    val sourcePath = pathFor(source, ref)
    if (!sourcePath.toFile.exists()) throw new FileNotFoundException(s"$sourcePath does not exist")
    Filepath.move(sourcePath, pathFor(target, ref))
  }

  private def filesIn(context: String) = directoryFor(context).files
  private def directoryFor(context: String) = Directory(s"$baseDir/$context")
  private def createIfRequired(directory: Directory) = if (directory.notExists) directory.createDirectory()
  private def pathFor(context: String, ref: String) = Paths.get(s"$baseDir/$context/$ref.$fileExtension")
}<commit_msg>Directory.nonExists is no longer available in Scala 2.11<commit_after>package silky.persistence.file

import java.io.FileNotFoundException
import java.nio.file.Paths

import silky.persistence.{Entry, Persistence}

import scala.reflect.io.Directory

class FilePersistence(baseDir: String, fileExtension: String = "json") extends Persistence {

  def lastRefAcross(prefix: Char, contexts: String*): String = {
    val files = contexts flatMap filesIn filter (f ⇒ f.name.head == prefix && f.extension == fileExtension) sortBy (_.name)
    if (files.isEmpty) "00000000" else files.last.stripExtension.tail
  }

  def save(entry: Entry) = {
    createIfRequired(directoryFor(entry.context))
    Filepath.save(entry.contents, pathFor(entry.context, entry.ref))
    entry
  }

  def find(context: String, ref: String) = filesIn(context)
    .find(_.name == s"$ref.$fileExtension")
    .map(f ⇒ Entry(context, ref, f.slurp()))

  def load(context: String, predicate: String ⇒ Boolean) = filesIn(context)
    .filter(f ⇒ predicate(f.name.replace(s".$fileExtension", "")))
    .map(f ⇒ Entry(context, f.name.replace(s".$fileExtension", ""), f.slurp()))
    .toSeq

  def move(ref: String, source: String, target: String) = {
    val sourcePath = pathFor(source, ref)
    if (!sourcePath.toFile.exists()) throw new FileNotFoundException(s"$sourcePath does not exist")
    Filepath.move(sourcePath, pathFor(target, ref))
  }

  private def filesIn(context: String) = directoryFor(context).files
  private def directoryFor(context: String) = Directory(s"$baseDir/$context")
  private def createIfRequired(directory: Directory) = if (!directory.exists) directory.createDirectory()
  private def pathFor(context: String, ref: String) = Paths.get(s"$baseDir/$context/$ref.$fileExtension")
}
{'fraction_non_alphanumeric': 0.13829483889364128, 'fraction_numerical': 0.005417735956658112, 'mean_word_length': 4.945762711864407, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '3433726', 'n_tokens_mistral': 1119, 'n_tokens_neox': 1089, 'n_words': 263}