This file is indexed.

/usr/lib/ruby/vendor_ruby/sequel/plugins/static_cache.rb is in ruby-sequel 3.36.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
module Sequel
  module Plugins
    # The static_cache plugin is designed for models that are not modified at all
    # in production use cases, or at least where modifications to them would usually
    # coincide with an application restart.  When loaded into a model class, it 
    # retrieves all rows in the database and staticly caches a ruby array and hash
    # keyed on primary key containing all of the model instances.  All of these instances
    # are frozen so they won't be modified unexpectedly.
    #
    # The caches this plugin creates are used for the following things:
    #
    # * Primary key lookups (e.g. Model[1])
    # * Model.all calls
    # * Model.each calls
    # * Model.map calls without an argument
    # * Model.to_hash calls without an argument
    #
    # Usage:
    #
    #   # Cache the AlbumType class staticly
    #   AlbumType.plugin :static_cache
    module StaticCache
      # Populate the static caches when loading the plugin.
      def self.configure(model)
        model.send(:load_cache)
      end

      module ClassMethods
        # A frozen ruby hash holding all of the model's frozen instances, keyed by frozen primary key.
        attr_reader :cache

        # An array of all of the model's frozen instances, without issuing a database
        # query.
        def all
          @all.dup
        end

        # Return the frozen object with the given pk, or nil if no such object exists
        # in the cache, without issuing a database query.
        def cache_get_pk(pk)
          cache[pk]
        end

        # Yield each of the model's frozen instances to the block, without issuing a database
        # query.
        def each(&block)
          @all.each(&block)
        end

        # If no arguments are given, yield each of the model's frozen instances to the block.
        # and return a new array, without issuing a database query.  If any arguments are
        # given, use the default Sequel behavior.
        def map(*a)
          if a.empty?
            @all.map(&(Proc.new if block_given?))
          else
            super
          end
        end

        # Reload the cache when the dataset changes.
        def set_dataset(*)
          s = super
          load_cache
          s
        end

        # If no arguments are given, yield an identity map for the model with frozen primary keys
        # and instances, without issuing a database query. If any arguments are
        # given, use the default Sequel behavior.
        def to_hash(*a)
          if a.empty?
            cache.dup
          else
            super
          end
        end

        private

        # Return the frozen object with the given pk, or nil if no such object exists
        # in the cache, without issuing a database query.
        def primary_key_lookup(pk)
          cache[pk]
        end

        # Reload the cache for this model by retrieving all of the instances in the dataset
        # freezing them, and populating the cached array and hash.
        def load_cache
          a = dataset.all
          h = {}
          a.each{|o| h[o.pk.freeze] = o.freeze}
          @all = a.freeze
          @cache = h.freeze
        end
      end
    end
  end
end