Sha256: c9bdccc759dc3f33edf27460976503af11fb0cdb86a58ff492742433ce3890d6

Contents?: true

Size: 1.73 KB

Versions: 9

Compression:

Stored size: 1.73 KB

Contents

# Author: Stephen Sykes

module SlimScrooge
  # Contains the complete list of callsites
  #
  class Callsites
    CallsitesMutex = Mutex.new
    @@callsites = {}
    ScroogeCallsiteSample = 1..16

    class << self
      # Whether we have encountered a callsite before
      #
      def has_key?(callsite_key)
        @@callsites.has_key?(callsite_key)
      end

      # Return the callsite for this key
      #
      def [](callsite_key)
        @@callsites[callsite_key]
      end

      # Fallback if you can't compile extension for some reason
      #
      begin
        callsite_hash
      rescue NameError
        def callsite_hash
          caller[ScroogeCallsiteSample].hash
        end
      end

      # Generate a key string - uses the portion of the query before the WHERE 
      # together with the callsite_hash generated by callsite_hash.c
      #
      def callsite_key(sql)
        callsite_hash + sql.gsub(/\sWHERE.*/i, "").hash
      end

      # Create a new callsite
      # 
      def create(sql, callsite_key, name)
        begin
          model_class = name.split.first.constantize
        rescue NameError, NoMethodError
          add_callsite(callsite_key, nil)
        else
          add_callsite(callsite_key, Callsite.make_callsite(model_class, sql))
        end
      end

      # Add a new callsite, wrap in a mutex for safety
      #
      def add_callsite(callsite_key, callsite)
        CallsitesMutex.synchronize do
          @@callsites[callsite_key] = callsite
        end
      end

      # Record that a column was accessed, wrap in a mutex for safety
      #
      def add_seen_column(callsite, seen_column)
        CallsitesMutex.synchronize do
          callsite.seen_columns << seen_column
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
slim_scrooge-1.0.14 lib/slim_scrooge/callsites.rb
slim_scrooge-1.0.12 lib/slim_scrooge/callsites.rb
slim_scrooge-1.0.11 lib/slim_scrooge/callsites.rb
slim_scrooge-1.0.10 lib/slim_scrooge/callsites.rb
slim_scrooge-1.0.9 lib/slim_scrooge/callsites.rb
slim_scrooge-1.0.7 lib/slim_scrooge/callsites.rb
slim_scrooge-1.0.5 lib/slim_scrooge/callsites.rb
slim_scrooge-1.0.4 lib/slim_scrooge/callsites.rb
slim_scrooge-1.0.3 lib/slim_scrooge/callsites.rb