<commit_before># FuzzyRecord.rb
# FuzzyWindow
#
# Created by <NAME> on 3/16/10.
# Copyright 2010 Topfunky Corporation. All rights reserved.

require 'Constants'
include Constants

class FuzzyRecord

  attr_accessor *[:projectRoot, :filePath,
                  :scmStatus, :scmName,
                  :codeObjectName, :codeObjectNames,
                  :matchedRanges, :matchesOnFilenameScore, :longestMatch]

  MAX_SCORE = 10_000

  class ProjectRootNotFoundError < StandardError; end

  def self.discoverProjectRootForDirectoryOrFile(directoryOrFile)
    normalizedPath = directoryOrFile.gsub(/\/$/, '') # Normalize: remove trailing slash

    if File.directory?(normalizedPath)
      return normalizedPath
    end

    fileManager = NSFileManager.defaultManager
    pathComponents = normalizedPath.pathComponents
    (pathComponents.length - 1).downto(0) do |index|
      path = NSString.pathWithComponents(pathComponents[0..index])
      next if File.file?(path)
      fileManager.contentsOfDirectoryAtPath(path,
                                            error:nil).map {|f|
        projectRootRegex = Regexp.new(NSUserDefaults.standardUserDefaults.stringForKey("projectRootRegex"))
        if f.match(projectRootRegex)
          return path.to_s
        end
      }
    end
    raise ProjectRootNotFoundError, "Couldn't find a project root for #{directoryOrFile}"
  end

  # Cache is a dictionary of project roots with arrays of recent files
  # and records.
  #
  #   {
  #     "/var/www/project" => {
  #       :recentlyOpenedRecords => [],
  #       :records => []
  #     }
  #   }
  @@cache = {}

  def self.recordsForProjectRoot(theProjectRoot, withFuzzyTableViewController:fuzzyTableViewController)
    cacheScmStatus(theProjectRoot)

    if records = cachedRecordsForProjectRoot(theProjectRoot)
      # Get fresh scmStatus every time
      records.each { |r| r.scmStatus = nil }
      NSNotificationCenter.defaultCenter.postNotificationName(TFAllRecordsCreatedNotification, object:records)
      return
    end

    loadRecordsWithProjectRoot(theProjectRoot, withFuzzyTableViewController:fuzzyTableViewController)
  end

  def self.cacheForProjectRoot(theProjectRoot)
    return nil if @@cache.nil?
    @@cache[theProjectRoot]
  end

  ##
  # Returns only the cached records for a project, or nil.

  def self.cachedRecordsForProjectRoot(theProjectRoot)
    if cacheHash = cacheForProjectRoot(theProjectRoot)
      if records = cacheHash[:records]
        return records
      end
    end
    return nil
  end

  ##
  # Store records for faster launch.

  def self.setCacheRecords(theRecords, forProjectRoot:theProjectRoot)
    @@cache ||= {}
    if @@cache[theProjectRoot].nil?
      @@cache[theProjectRoot] = {}
    end
    @@cache[theProjectRoot][:records] = theRecords
  end

  ##
  # Add a single record to the existing cache for a project.
  
  def self.addRecord(theRecord, toCacheForProjectRoot:theProjectRoot)
    @@cache ||= {}
    if @@cache[theProjectRoot].nil?
      @@cache[theProjectRoot] = {}
    end
    @@cache[theProjectRoot][:records] << theRecord<commit_msg>Initialize :record as Array in cache.<commit_after># FuzzyRecord.rb
# FuzzyWindow
#
# Created by <NAME> on 3/16/10.
# Copyright 2010 Topfunky Corporation. All rights reserved.

require 'Constants'
include Constants

class FuzzyRecord

  attr_accessor *[:projectRoot, :filePath,
                  :scmStatus, :scmName,
                  :codeObjectName, :codeObjectNames,
                  :matchedRanges, :matchesOnFilenameScore, :longestMatch]

  MAX_SCORE = 10_000

  class ProjectRootNotFoundError < StandardError; end

  def self.discoverProjectRootForDirectoryOrFile(directoryOrFile)
    normalizedPath = directoryOrFile.gsub(/\/$/, '') # Normalize: remove trailing slash

    if File.directory?(normalizedPath)
      return normalizedPath
    end

    fileManager = NSFileManager.defaultManager
    pathComponents = normalizedPath.pathComponents
    (pathComponents.length - 1).downto(0) do |index|
      path = NSString.pathWithComponents(pathComponents[0..index])
      next if File.file?(path)
      fileManager.contentsOfDirectoryAtPath(path,
                                            error:nil).map {|f|
        projectRootRegex = Regexp.new(NSUserDefaults.standardUserDefaults.stringForKey("projectRootRegex"))
        if f.match(projectRootRegex)
          return path.to_s
        end
      }
    end
    raise ProjectRootNotFoundError, "Couldn't find a project root for #{directoryOrFile}"
  end

  # Cache is a dictionary of project roots with arrays of recent files
  # and records.
  #
  #   {
  #     "/var/www/project" => {
  #       :recentlyOpenedRecords => [],
  #       :records => []
  #     }
  #   }
  @@cache = {}

  def self.recordsForProjectRoot(theProjectRoot, withFuzzyTableViewController:fuzzyTableViewController)
    cacheScmStatus(theProjectRoot)

    if records = cachedRecordsForProjectRoot(theProjectRoot)
      # Get fresh scmStatus every time
      records.each { |r| r.scmStatus = nil }
      NSNotificationCenter.defaultCenter.postNotificationName(TFAllRecordsCreatedNotification, object:records)
      return
    end

    loadRecordsWithProjectRoot(theProjectRoot, withFuzzyTableViewController:fuzzyTableViewController)
  end

  def self.cacheForProjectRoot(theProjectRoot)
    return nil if @@cache.nil?
    @@cache[theProjectRoot]
  end

  ##
  # Returns only the cached records for a project, or nil.

  def self.cachedRecordsForProjectRoot(theProjectRoot)
    if cacheHash = cacheForProjectRoot(theProjectRoot)
      if records = cacheHash[:records]
        return records
      end
    end
    return nil
  end

  ##
  # Store records for faster launch.

  def self.setCacheRecords(theRecords, forProjectRoot:theProjectRoot)
    @@cache ||= {}
    if @@cache[theProjectRoot].nil?
      @@cache[theProjectRoot] = {}
    end
    @@cache[theProjectRoot][:records] = theRecords
  end

  ##
  # Add a single record to the existing cache for a project.
  
  def self.addRecord(theRecord, toCacheForProjectRoot:theProjectRoot)
    @@cache ||= {}
    if @@cache[theProjectRoot].nil?
      @@cache[theProjectRoot] = {
        :records => []  
      }
    end
    @@cache[theProjectRoot][:records] << theRecord
{'fraction_non_alphanumeric': 0.09208840486867392, 'fraction_numerical': 0.005445227418321589, 'mean_word_length': 3.239646978954515, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5154499', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1759, 'n_words': 395}