| 1 | package CHI; | ||||||||||||||||||||||||||||||||||
| 2 | 21 | $CHI::VERSION = '0.59'; | |||||||||||||||||||||||||||||||||
| 3 | 21 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 21 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 21 | ▶ | use CHI::Stats; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 21 | ▶ | use String::RewritePrefix; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 21 | ▶ | use Module::Runtime qw(require_module); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 21 | ▶ | use Moo::Role (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 21 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 21 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | 21 | my ( %final_class_seen, %memoized_cache_objects, %stats ); | |||||||||||||||||||||||||||||||||
| 13 | my %valid_config_keys = | ||||||||||||||||||||||||||||||||||
| 14 | 21 | map { ( $_, 1 ) } qw(defaults memoize_cache_objects namespace storage); | |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | sub logger { | ||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | warn | ||||||||||||||||||||||||||||||||
|
Subroutine
logger: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 18 | "CHI now uses Log::Any for logging - see Log::Any documentation for details"; | ||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | sub config { | ||||||||||||||||||||||||||||||||||
| 22 | 1030 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
config: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 23 | 1030 | ▶ | $class->_set_config(@_) if @_; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 24 | 1030 | return $class->_get_config(); | |||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||
| 27 | sub _set_config { | ||||||||||||||||||||||||||||||||||
| 28 | 24 | ▶ | my ( $class, $config ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_set_config: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||
| 29 | 24 | ▶ | if ( my @bad_keys = grep { !$valid_config_keys{$_} } keys(%$config) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 30 | 0 | croak "unknown keys in config hash: " . join( ", ", @bad_keys ); | |||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | # set class specific configuration | ||||||||||||||||||||||||||||||||||
| 34 | 21 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 35 | 21 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 36 | 24 | ▶ | *{"$class\::_get_config"} = sub { $config }; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | 21 | ▶ | BEGIN { __PACKAGE__->config( {} ) } | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||
| 41 | sub memoized_cache_objects { | ||||||||||||||||||||||||||||||||||
| 42 | 4 | ▶ | my ($class) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
memoized_cache_objects: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | # Each CHI root class gets its hash of memoized objects | ||||||||||||||||||||||||||||||||||
| 45 | # | ||||||||||||||||||||||||||||||||||
| 46 | 4 | ▶ | $memoized_cache_objects{$class} ||= {}; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$memoized_cache_objects{$class} ||= {}
MC/DC100%1/1
$memoized_cache_objects{$class} ||= {}
$memoized_cache_objects{$class}
Truth table100%2/2
A
A= $memoized_cache_objects{$class}
| |||||||||||||||||||||||||||||||||||
| 47 | 4 | return $memoized_cache_objects{$class}; | |||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||
| 50 | sub clear_memoized_cache_objects { | ||||||||||||||||||||||||||||||||||
| 51 | 1 | ▶ | my ($class) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
clear_memoized_cache_objects: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | 1 | $memoized_cache_objects{$class} = {}; | |||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | sub stats { | ||||||||||||||||||||||||||||||||||
| 57 | 999 | ▶ | my ($class) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
stats: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | # Each CHI root class gets its own stats object | ||||||||||||||||||||||||||||||||||
| 60 | # | ||||||||||||||||||||||||||||||||||
| 61 | 999 | ▶ | $stats{$class} ||= CHI::Stats->new( chi_root_class => $class ); | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$stats{$class} ||= "CHI::Stats"->new("chi_root_class", $class)
MC/DC0%0/2
$stats{$class} ||= "CHI::Stats"->new("chi_root_class", $class)
$stats{$class}
"CHI::Stats"->new("chi_root_class", $class)
Truth table66%2/3
A ||= B
A= $stats{$class}B= "CHI::Stats"->new("chi_root_class", $class)
| |||||||||||||||||||||||||||||||||||
| 62 | 999 | return $stats{$class}; | |||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | sub new { | ||||||||||||||||||||||||||||||||||
| 66 | 1006 | ▶ | my ( $chi_root_class, %params ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=22 SCAR=30.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||
| 68 | 1006 | my $config = $chi_root_class->config; | |||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | # Cache object memoization: See if cache object with these parameters | ||||||||||||||||||||||||||||||||||
| 71 | # has already been created, and return it if so. Only for parameters | ||||||||||||||||||||||||||||||||||
| 72 | # with 0 or 1 keys. | ||||||||||||||||||||||||||||||||||
| 73 | # | ||||||||||||||||||||||||||||||||||
| 74 | 1006 | my ( $cache_object_key, $cache_objects ); | |||||||||||||||||||||||||||||||||
| 75 | 1006 | ▶ | if ( $config->{memoize_cache_objects} && keys(%params) <= 1 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$config->{"memoize_cache_objects"} and keys %params <= 1
MC/DC100%2/2
$config->{"memoize_cache_objects"} and keys %params <= 1
$config->{"memoize_cache_objects"}
keys %params <= 1
Truth table100%3/3
A and B
A= $config->{"memoize_cache_objects"}B= keys %params <= 1
| |||||||||||||||||||||||||||||||||||
| 76 | 4 | $cache_object_key = join chr(28), %params; | |||||||||||||||||||||||||||||||||
| 77 | 4 | $cache_objects = $chi_root_class->memoized_cache_objects; | |||||||||||||||||||||||||||||||||
| 78 | 4 | ▶ | if ( my $cache_object = $cache_objects->{$cache_object_key} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 79 | 2 | return $cache_object; | |||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||
| 83 | # Gather defaults | ||||||||||||||||||||||||||||||||||
| 84 | # | ||||||||||||||||||||||||||||||||||
| 85 | 1004 | ▶ | my $core_defaults = $config->{defaults} || {}; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'defaults'} || {}
MC/DC100%1/1
$config->{'defaults'} || {}
$config->{'defaults'}
Truth table100%2/2
A
A= $config->{'defaults'}
| |||||||||||||||||||||||||||||||||||
| 86 | my $namespace_defaults = | ||||||||||||||||||||||||||||||||||
| 87 | 1004 | ▶ | $config->{namespace}->{ $params{namespace} || 'Default' } || {}; | ||||||||||||||||||||||||||||||||
|
Condition100%4/4
$config->{'namespace'}{$params{'namespace'} or 'Default'} || {}
$params{'namespace'} || 'Default'
MC/DC100%1/1
$config->{'namespace'}{$params{'namespace'} or 'Default'} || {}
$config->{'namespace'}{$params{'namespace'} or 'Default'}
MC/DC100%1/1
$params{'namespace'} || 'Default'
$params{'namespace'}
Truth table100%2/2
A
A= $config->{'namespace'}{$params{'namespace'} or 'Default'}
Truth table100%2/2
A
A= $params{'namespace'}
| |||||||||||||||||||||||||||||||||||
| 88 | my $storage = | ||||||||||||||||||||||||||||||||||
| 89 | $params{storage} | ||||||||||||||||||||||||||||||||||
| 90 | || $namespace_defaults->{storage} | ||||||||||||||||||||||||||||||||||
| 91 | 1004 | ▶ | || $core_defaults->{storage}; | ||||||||||||||||||||||||||||||||
|
Condition66%4/6
$params{'storage'} || $namespace_defaults->{'storage'} || $core_defaults->{'storage'}
$params{"storage"} or $namespace_defaults->{"storage"}
MC/DC33%1/3
$params{'storage'} || $namespace_defaults->{'storage'} || $core_defaults->{'storage'}
$params{"storage"}
$namespace_defaults->{"storage"}
$core_defaults->{'storage'}
Truth table50%2/4
A or B || C
A= $params{"storage"}B= $namespace_defaults->{"storage"}C= $core_defaults->{'storage'}
| |||||||||||||||||||||||||||||||||||
| 92 | 1004 | my $storage_defaults = {}; | |||||||||||||||||||||||||||||||||
| 93 | 1004 | ▶ | if ( defined($storage) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 94 | 16 | ▶ | $storage_defaults = $config->{storage}->{$storage} | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 95 | or croak "no config for storage type '$storage'"; | ||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | # Combine passed params with defaults | ||||||||||||||||||||||||||||||||||
| 99 | # | ||||||||||||||||||||||||||||||||||
| 100 | %params = | ||||||||||||||||||||||||||||||||||
| 101 | 1004 | ( %$core_defaults, %$storage_defaults, %$namespace_defaults, %params ); | |||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | # Get driver class from driver or driver_class parameters | ||||||||||||||||||||||||||||||||||
| 104 | # | ||||||||||||||||||||||||||||||||||
| 105 | 1004 | my $driver_class; | |||||||||||||||||||||||||||||||||
| 106 | 1004 | ▶ | if ( my $driver = delete( $params{driver} ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 107 | 910 | ($driver_class) = | |||||||||||||||||||||||||||||||||
| 108 | String::RewritePrefix->rewrite( { '' => 'CHI::Driver::', '+' => '' }, | ||||||||||||||||||||||||||||||||||
| 109 | $driver ); | ||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||
| 111 | else { | ||||||||||||||||||||||||||||||||||
| 112 | 94 | $driver_class = delete( $params{driver_class} ); | |||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||
| 114 | 1004 | ▶ | croak "missing required param 'driver' or 'driver_class'" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 115 | unless defined $driver_class; | ||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||
| 117 | # Load driver class if it hasn't been loaded or defined in-line already | ||||||||||||||||||||||||||||||||||
| 118 | # | ||||||||||||||||||||||||||||||||||
| 119 | 1004 | ▶ | unless ( $driver_class->can('fetch') ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 120 | 17 | require_module($driver_class); | |||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||
| 123 | # Select roles depending on presence of certain arguments. Everyone gets | ||||||||||||||||||||||||||||||||||
| 124 | # the Universal role. Accept both 'roles' and 'traits' for backwards | ||||||||||||||||||||||||||||||||||
| 125 | # compatibility. Add CHI::Driver::Role:: unless prefixed with '+'. | ||||||||||||||||||||||||||||||||||
| 126 | # | ||||||||||||||||||||||||||||||||||
| 127 | 1001 | my @roles = ('Universal'); | |||||||||||||||||||||||||||||||||
| 128 | 1001 | foreach my $param_name (qw(roles traits)) { | |||||||||||||||||||||||||||||||||
| 129 | 2002 | ▶ | if ( exists( $params{$param_name} ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 130 | 182 | push( @roles, @{ delete( $params{$param_name} ) } ); | |||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||
| 133 | 1001 | ▶ | if ( exists( $params{max_size} ) || exists( $params{is_size_aware} ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $params{"max_size"} or exists $params{"is_size_aware"}
MC/DC100%2/2
exists $params{"max_size"} or exists $params{"is_size_aware"}
exists $params{"max_size"}
exists $params{"is_size_aware"}
Truth table100%3/3
A or B
A= exists $params{"max_size"}B= exists $params{"is_size_aware"}
| |||||||||||||||||||||||||||||||||||
| 134 | 161 | push( @roles, 'IsSizeAware' ); | |||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||
| 136 | 1001 | ▶ | if ( exists( $params{l1_cache} ) || exists( $params{mirror_cache} ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $params{"l1_cache"} or exists $params{"mirror_cache"}
MC/DC100%2/2
exists $params{"l1_cache"} or exists $params{"mirror_cache"}
exists $params{"l1_cache"}
exists $params{"mirror_cache"}
Truth table100%3/3
A or B
A= exists $params{"l1_cache"}B= exists $params{"mirror_cache"}
| |||||||||||||||||||||||||||||||||||
| 137 | 150 | push( @roles, 'HasSubcaches' ); | |||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||
| 139 | 1001 | ▶ | if ( $params{is_subcache} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 140 | 165 | push( @roles, 'IsSubcache' ); | |||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||
| 142 | 1001 | @roles = String::RewritePrefix->rewrite( | |||||||||||||||||||||||||||||||||
| 143 | { '' => 'CHI::Driver::Role::', '+' => '' }, @roles ); | ||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||
| 145 | # Select a final class based on the driver class and roles, creating it | ||||||||||||||||||||||||||||||||||
| 146 | # if necessary - adapted from MooseX::Traits | ||||||||||||||||||||||||||||||||||
| 147 | # | ||||||||||||||||||||||||||||||||||
| 148 | 1001 | my $final_class = | |||||||||||||||||||||||||||||||||
| 149 | Moo::Role->create_class_with_roles( $driver_class, @roles ); | ||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||
| 151 | 1001 | my $cache_object = $final_class->new( | |||||||||||||||||||||||||||||||||
| 152 | chi_root_class => $chi_root_class, | ||||||||||||||||||||||||||||||||||
| 153 | driver_class => $driver_class, | ||||||||||||||||||||||||||||||||||
| 154 | %params | ||||||||||||||||||||||||||||||||||
| 155 | ); | ||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||
| 157 | # Memoize if appropriate | ||||||||||||||||||||||||||||||||||
| 158 | # | ||||||||||||||||||||||||||||||||||
| 159 | 998 | ▶ | if ($cache_object_key) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 160 | 2 | $cache_objects->{$cache_object_key} = $cache_object; | |||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||
| 163 | 998 | return $cache_object; | |||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||
| 166 | 21 | 1; | |||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||
| 168 | __END__ | ||||||||||||||||||||||||||||||||||