Class: Entitlements::Data::Groups::Cached

Inherits:
Object
  • Object
show all
Includes:
Contracts::Core
Defined in:
lib/entitlements.rb,
lib/entitlements/data/groups/cached.rb

Constant Summary collapse

C =
::Contracts

Class Method Summary collapse

Methods included from Contracts::Core

common, extended, included

Class Method Details

.invalidate(dn) ⇒ Object



68
69
70
71
72
# File 'lib/entitlements/data/groups/cached.rb', line 68

def self.invalidate(dn)
  return unless Entitlements.cache[:predictive_state]
  Entitlements.cache[:predictive_state][:invalid].add(dn)
  nil
end

.load_caches(dir) ⇒ Object



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
# File 'lib/entitlements/data/groups/cached.rb', line 24

def self.load_caches(dir)
  unless File.directory?(dir)
    raise Errno::ENOENT, "Predictive state directory #{dir.inspect} does not exist!"
  end

  Entitlements.logger.debug "Loading predictive update caches from #{dir}"

  Entitlements.cache[:predictive_state] = { by_ou: {}, by_dn: {}, invalid: Set.new }

  Dir.glob(File.join(dir, "*")).each do |filename|
    dn = File.basename(filename)
    identifier, ou = dn.split(",", 2)

    file_lines = File.readlines(filename).map(&:strip).map(&:downcase).compact

    members = file_lines.dup
    members.reject! { |line| line.start_with?("#") }
    members.reject! { |line| line.start_with?("metadata_") }
    member_set = Set.new(members)

     = file_lines.dup
    unless .empty?
      .select! { |line| line.start_with?("metadata_") }
       = .map { || .split "=" }.to_h
      .transform_keys! { |key| key.delete_prefix("metadata_") }
    end

    Entitlements.cache[:predictive_state][:by_ou][ou] ||= {}
    Entitlements.cache[:predictive_state][:by_ou][ou][identifier] = member_set
    Entitlements.cache[:predictive_state][:by_dn][dn] = { members: member_set, metadata:  }
  end

  Entitlements.logger.debug "Loaded #{Entitlements.cache[:predictive_state][:by_ou].keys.size} OU(s) from cache"
  Entitlements.logger.debug "Loaded #{Entitlements.cache[:predictive_state][:by_dn].keys.size} DN(s) from cache"

  nil
end

.members(dn) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/entitlements/data/groups/cached.rb', line 80

def self.members(dn)
  return unless Entitlements.cache[:predictive_state]

  if Entitlements.cache[:predictive_state][:invalid].member?(dn)
    Entitlements.logger.debug "members(#{dn}): DN has been marked invalid in cache"
    return
  end

  unless Entitlements.cache[:predictive_state][:by_dn].key?(dn)
    Entitlements.logger.debug "members(#{dn}): DN does not exist in cache"
    return
  end

  Entitlements.cache[:predictive_state][:by_dn][dn][:members]
end

.metadata(dn) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/entitlements/data/groups/cached.rb', line 102

def self.(dn)
  return unless Entitlements.cache[:predictive_state]

  if Entitlements.cache[:predictive_state][:invalid].member?(dn)
    Entitlements.logger.debug "metadata(#{dn}): DN has been marked invalid in cache"
    return
  end

  unless Entitlements.cache[:predictive_state][:by_dn].key?(dn)
    Entitlements.logger.debug "metadata(#{dn}): DN does not exist in cache"
    return
  end

  Entitlements.cache[:predictive_state][:by_dn][dn][:metadata]
end