| 1 | # Tie::ShadowHash -- Merge multiple data sources into a hash. | ||||||||||||||||||||
| 2 | # | ||||||||||||||||||||
| 3 | # This module combines multiple sources of data into a single tied hash, so | ||||||||||||||||||||
| 4 | # that they can all be queried simultaneously, the source of any given | ||||||||||||||||||||
| 5 | # key-value pair irrelevant to the client script. Data sources are searched | ||||||||||||||||||||
| 6 | # in the order that they're added to the shadow hash. Changes to the hashed | ||||||||||||||||||||
| 7 | # data aren't propagated back to the actual data files; instead, they're saved | ||||||||||||||||||||
| 8 | # within the tied hash and override any data obtained from the data sources. | ||||||||||||||||||||
| 9 | # | ||||||||||||||||||||
| 10 | # SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | ############################################################################## | ||||||||||||||||||||
| 13 | # Modules and declarations | ||||||||||||||||||||
| 14 | ############################################################################## | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | package Tie::ShadowHash 2.01; | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 2 | ▶ | use 5.024; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 2 | ▶ | use autodie; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | ############################################################################## | ||||||||||||||||||||
| 25 | # Regular methods | ||||||||||||||||||||
| 26 | ############################################################################## | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | # Given a file name and optionally a split regex, builds a hash out of the | ||||||||||||||||||||
| 29 | # contents of the file. | ||||||||||||||||||||
| 30 | # | ||||||||||||||||||||
| 31 | # If the split sub exists, use it to split each line into an array; if the | ||||||||||||||||||||
| 32 | # array has two elements, those are taken as the key and value. If there are | ||||||||||||||||||||
| 33 | # more, the value is an anonymous array containing everything but the first. | ||||||||||||||||||||
| 34 | # | ||||||||||||||||||||
| 35 | # If there's no split sub, take the entire line modulo the line terminator as | ||||||||||||||||||||
| 36 | # the key and the value the number of times it occurs in the file. | ||||||||||||||||||||
| 37 | # | ||||||||||||||||||||
| 38 | # $file - File containing the data | ||||||||||||||||||||
| 39 | # $split - Optional anonymous sub that splits a line into key and value | ||||||||||||||||||||
| 40 | # | ||||||||||||||||||||
| 41 | # Returns: Hash created by loading the file | ||||||||||||||||||||
| 42 | 4 | ▶ | sub _text_source { | ||||||||||||||||||
|
Subroutine
_text_source: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 43 | 4 | my ($self, $file, $split) = @_; | |||||||||||||||||||
| 44 | 4 | my %hash; | |||||||||||||||||||
| 45 | 4 | open(my $fh, '<', $file); | |||||||||||||||||||
| 46 | 4 | ▶ | while (defined(my $line = <$fh>)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | 48 | chomp($line); | |||||||||||||||||||
| 48 | 48 | ▶ | if (defined($split)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 6 | my ($key, @rest) = $split->($line); | |||||||||||||||||||
| 50 | 6 | ▶ | $hash{$key} = (@rest == 1) ? $rest[0] : [@rest]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | } else { | ||||||||||||||||||||
| 52 | 42 | $hash{$line}++; | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | 4 | close($fh); | |||||||||||||||||||
| 56 | 4 | return \%hash; | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | # Add data sources to the shadow hash. | ||||||||||||||||||||
| 60 | # | ||||||||||||||||||||
| 61 | # Each data source is one of the following: | ||||||||||||||||||||
| 62 | # | ||||||||||||||||||||
| 63 | # - An anonymous array, in which case the first element is the type of source | ||||||||||||||||||||
| 64 | # and the rest are arguments. Currently, "text" is the only supported type. | ||||||||||||||||||||
| 65 | # | ||||||||||||||||||||
| 66 | # - A file name, which is taken to be a text file with each line as a key and | ||||||||||||||||||||
| 67 | # a value equal to the number of times that line appears. | ||||||||||||||||||||
| 68 | # | ||||||||||||||||||||
| 69 | # - A hash reference, possibly to a tied hash. | ||||||||||||||||||||
| 70 | # | ||||||||||||||||||||
| 71 | # @sources - Data sources to add | ||||||||||||||||||||
| 72 | # | ||||||||||||||||||||
| 73 | # Returns: True | ||||||||||||||||||||
| 74 | 8 | ▶ | sub add { | ||||||||||||||||||
|
Subroutine
add: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||
| 75 | 8 | my ($self, @sources) = @_; | |||||||||||||||||||
| 76 | 8 | for my $source (@sources) { | |||||||||||||||||||
| 77 | 9 | ▶ | if (ref($source) eq 'ARRAY') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 78 | 2 | my ($type, @args) = $source->@*; | |||||||||||||||||||
| 79 | 2 | ▶ | if ($type eq 'text') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | 2 | $source = $self->_text_source(@args); | |||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||
| 82 | 0 | croak("invalid source type $type"); | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | } elsif (!ref($source)) { | ||||||||||||||||||||
| 85 | 2 | $source = $self->_text_source($source); | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | 9 | push($self->{SOURCES}->@*, $source); | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | 8 | return 1; | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | ############################################################################## | ||||||||||||||||||||
| 93 | # Tie methods | ||||||||||||||||||||
| 94 | ############################################################################## | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | # Create a new tied hash. | ||||||||||||||||||||
| 97 | # | ||||||||||||||||||||
| 98 | # @sources - Sources to add to the new hash | ||||||||||||||||||||
| 99 | # | ||||||||||||||||||||
| 100 | # Returns: Newly created tied hash | ||||||||||||||||||||
| 101 | 2 | ▶ | sub TIEHASH { | ||||||||||||||||||
|
Subroutine
TIEHASH: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 102 | 2 | my ($class, @sources) = @_; | |||||||||||||||||||
| 103 | 2 | ▶ | $class = ref($class) || $class; | ||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 104 | #<<< | ||||||||||||||||||||
| 105 | 2 | my $self = { | |||||||||||||||||||
| 106 | DELETED => {}, # All keys that have been deleted | ||||||||||||||||||||
| 107 | EACH => -1, # Index of source currently being traversed | ||||||||||||||||||||
| 108 | OVERRIDE => {}, # Values set directly by the user | ||||||||||||||||||||
| 109 | SOURCES => [], # Array of all of the underlying hashes | ||||||||||||||||||||
| 110 | }; | ||||||||||||||||||||
| 111 | #>>> | ||||||||||||||||||||
| 112 | 2 | bless($self, $class); | |||||||||||||||||||
| 113 | 2 | $self->add(@sources); | |||||||||||||||||||
| 114 | 2 | return $self; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | # Retrieve a value. | ||||||||||||||||||||
| 118 | # | ||||||||||||||||||||
| 119 | # This doesn't work quite right in the case of keys with undefined values, but | ||||||||||||||||||||
| 120 | # we can't make it work right since that would require using exists and a lot | ||||||||||||||||||||
| 121 | # of common data sources (such as NDBM_File tied hashes) don't implement | ||||||||||||||||||||
| 122 | # exists. | ||||||||||||||||||||
| 123 | # | ||||||||||||||||||||
| 124 | # $key - Key to look up | ||||||||||||||||||||
| 125 | # | ||||||||||||||||||||
| 126 | # Returns: Value for that key, undef if it is not present | ||||||||||||||||||||
| 127 | 23 | ▶ | sub FETCH { | ||||||||||||||||||
|
Subroutine
FETCH: called CC=5 SCAR=16.1
| |||||||||||||||||||||
| 128 | 23 | my ($self, $key) = @_; | |||||||||||||||||||
| 129 | 23 | ▶ | if ($self->{DELETED}{$key}) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 130 | 1 | return; | |||||||||||||||||||
| 131 | } elsif (exists($self->{OVERRIDE}{$key})) { | ||||||||||||||||||||
| 132 | 6 | return $self->{OVERRIDE}{$key}; | |||||||||||||||||||
| 133 | } else { | ||||||||||||||||||||
| 134 | 16 | for my $source ($self->{SOURCES}->@*) { | |||||||||||||||||||
| 135 | 18 | ▶ | if (defined($source->{$key})) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | 13 | return $source->{$key}; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | 3 | return; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | # Store a value. This goes into the override hash, which is checked before | ||||||||||||||||||||
| 144 | # any of the underlying data sources. | ||||||||||||||||||||
| 145 | # | ||||||||||||||||||||
| 146 | # $key - Key to store a value for | ||||||||||||||||||||
| 147 | # $value - Value to store | ||||||||||||||||||||
| 148 | 8 | ▶ | sub STORE { | ||||||||||||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 149 | 8 | my ($self, $key, $value) = @_; | |||||||||||||||||||
| 150 | 8 | delete $self->{DELETED}{$key}; | |||||||||||||||||||
| 151 | 8 | $self->{OVERRIDE}{$key} = $value; | |||||||||||||||||||
| 152 | 8 | return; | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | # Delete a key. The key is flagged in the deleted hash, which ensures that | ||||||||||||||||||||
| 156 | # undef will be returned for any future retrieval. Dropping the override | ||||||||||||||||||||
| 157 | # value isn't required for currect future FETCH behavior, but it drops the | ||||||||||||||||||||
| 158 | # reference so that memory can be released. | ||||||||||||||||||||
| 159 | # | ||||||||||||||||||||
| 160 | # $key - Key to delete | ||||||||||||||||||||
| 161 | 5 | ▶ | sub DELETE { | ||||||||||||||||||
|
Subroutine
DELETE: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 162 | 5 | my ($self, $key) = @_; | |||||||||||||||||||
| 163 | 5 | delete $self->{OVERRIDE}{$key}; | |||||||||||||||||||
| 164 | 5 | $self->{DELETED}{$key} = 1; | |||||||||||||||||||
| 165 | 5 | return; | |||||||||||||||||||
| 166 | } | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | # Clear the hash. Removes all sources and all overrides and resets any | ||||||||||||||||||||
| 169 | # iteration. | ||||||||||||||||||||
| 170 | 5 | ▶ | sub CLEAR { | ||||||||||||||||||
|
Subroutine
CLEAR: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 171 | 5 | my ($self) = @_; | |||||||||||||||||||
| 172 | 5 | $self->{DELETED} = {}; | |||||||||||||||||||
| 173 | 5 | $self->{OVERRIDE} = {}; | |||||||||||||||||||
| 174 | 5 | $self->{SOURCES} = []; | |||||||||||||||||||
| 175 | 5 | $self->{EACH} = -1; | |||||||||||||||||||
| 176 | 5 | return; | |||||||||||||||||||
| 177 | } | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | # Return whether a key exists. | ||||||||||||||||||||
| 180 | # | ||||||||||||||||||||
| 181 | # This could throw an exception if any underlying source doesn't support | ||||||||||||||||||||
| 182 | # exists (like NDBM_File). | ||||||||||||||||||||
| 183 | # | ||||||||||||||||||||
| 184 | # $key - Key to query for existence | ||||||||||||||||||||
| 185 | # | ||||||||||||||||||||
| 186 | # Returns: True if the key exists, false otherwise | ||||||||||||||||||||
| 187 | 5 | ▶ | sub EXISTS { | ||||||||||||||||||
|
Subroutine
EXISTS: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 188 | 5 | my ($self, $key) = @_; | |||||||||||||||||||
| 189 | 5 | ▶ | return if exists($self->{DELETED}{$key}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 190 | 4 | for my $source ($self->{OVERRIDE}, $self->{SOURCES}->@*) { | |||||||||||||||||||
| 191 | 8 | ▶ | return 1 if exists($source->{$key}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 192 | } | ||||||||||||||||||||
| 193 | 1 | return; | |||||||||||||||||||
| 194 | } | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | # Start an iteration. | ||||||||||||||||||||
| 197 | # | ||||||||||||||||||||
| 198 | # We have to reset the each counter on all hashes. For tied hashes, we call | ||||||||||||||||||||
| 199 | # FIRSTKEY directly because it's potentially more efficient than calling keys | ||||||||||||||||||||
| 200 | # on the hash. | ||||||||||||||||||||
| 201 | 12 | ▶ | sub FIRSTKEY { | ||||||||||||||||||
|
Subroutine
FIRSTKEY: called CC=3 SCAR=11.2
| |||||||||||||||||||||
| 202 | 12 | my ($self) = @_; | |||||||||||||||||||
| 203 | 12 | keys($self->{OVERRIDE}->%*); | |||||||||||||||||||
| 204 | 12 | for my $source ($self->{SOURCES}->@*) { | |||||||||||||||||||
| 205 | 21 | my $tie = tied($source); | |||||||||||||||||||
| 206 | 21 | ▶ | if ($tie) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 207 | 0 | $tie->FIRSTKEY(); | |||||||||||||||||||
| 208 | } else { | ||||||||||||||||||||
| 209 | 21 | keys($source->%*); | |||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | } | ||||||||||||||||||||
| 212 | 12 | $self->{EACH} = -1; | |||||||||||||||||||
| 213 | 12 | return $self->NEXTKEY(); | |||||||||||||||||||
| 214 | } | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | # Iterate through the hashes. | ||||||||||||||||||||
| 217 | # | ||||||||||||||||||||
| 218 | # Walk the sources by calling each on each one in turn, skipping deleted keys | ||||||||||||||||||||
| 219 | # and keys shadowed by earlier hashes and using $self->{EACH} to store the | ||||||||||||||||||||
| 220 | # number of source we're at. | ||||||||||||||||||||
| 221 | # | ||||||||||||||||||||
| 222 | # Returns: Next key in iteration, or undef if sources are exhausted | ||||||||||||||||||||
| 223 | 278 | ▶ | sub NEXTKEY { | ||||||||||||||||||
|
Subroutine
NEXTKEY: called CC=10 SCAR=23.0
| |||||||||||||||||||||
| 224 | 278 | my ($self) = @_; | |||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | # EACH is the numeric index in the SOURCES list for the source we're | ||||||||||||||||||||
| 227 | # currently calling each on, or -1 if we're just starting and thus | ||||||||||||||||||||
| 228 | # operating on the OVERRIDE hash. | ||||||||||||||||||||
| 229 | # | ||||||||||||||||||||
| 230 | # We have to loop until we find the next value, which may take several | ||||||||||||||||||||
| 231 | # iterations since keys could have been overridden by an earlier hash or | ||||||||||||||||||||
| 232 | # deleted. | ||||||||||||||||||||
| 233 | SOURCE: | ||||||||||||||||||||
| 234 | 278 | ▶ | while ($self->{EACH} < $self->{SOURCES}->@*) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 235 | 312 | my $key; | |||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | ## no critic (Each) | ||||||||||||||||||||
| 238 | 312 | ▶ | if ($self->{EACH} == -1) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 239 | 18 | $key = each($self->{OVERRIDE}->%*); | |||||||||||||||||||
| 240 | } else { | ||||||||||||||||||||
| 241 | 294 | $key = each($self->{SOURCES}[$self->{EACH}]->%*); | |||||||||||||||||||
| 242 | } | ||||||||||||||||||||
| 243 | ## use critic | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | # If we got a valid result, we have to check against DELETED, | ||||||||||||||||||||
| 246 | # OVERRIDE, and all earlier sources before returning it. | ||||||||||||||||||||
| 247 | 312 | ▶ | if (defined($key)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 248 | 280 | ▶ | if ($self->{DELETED}{$key}) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 249 | 4 | next; | |||||||||||||||||||
| 250 | ▶ | } elsif ($self->{EACH} >= 0 && exists($self->{OVERRIDE}{$key})) { | |||||||||||||||||||
|
Condition100%3/3
$self->{"EACH"} >= 0 and exists $self->{"OVERRIDE"}{$key}
MC/DC100%2/2
$self->{"EACH"} >= 0 and exists $self->{"OVERRIDE"}{$key}
$self->{"EACH"} >= 0
exists $self->{"OVERRIDE"}{$key}
Truth table100%3/3
A and B
A= $self->{"EACH"} >= 0B= exists $self->{"OVERRIDE"}{$key}
| |||||||||||||||||||||
| 251 | 7 | next; | |||||||||||||||||||
| 252 | } elsif ($self->{EACH} > 0) { | ||||||||||||||||||||
| 253 | 126 | for my $index (reverse(0 .. $self->{EACH} - 1)) { | |||||||||||||||||||
| 254 | 126 | ▶ | if (defined($self->{SOURCES}[$index]{$key})) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 255 | 2 | next SOURCE; | |||||||||||||||||||
| 256 | } | ||||||||||||||||||||
| 257 | } | ||||||||||||||||||||
| 258 | } | ||||||||||||||||||||
| 259 | 267 | return $key; | |||||||||||||||||||
| 260 | } | ||||||||||||||||||||
| 261 | 32 | $self->{EACH}++; | |||||||||||||||||||
| 262 | } | ||||||||||||||||||||
| 263 | |||||||||||||||||||||
| 264 | # We have exhausted all of the sources. | ||||||||||||||||||||
| 265 | 11 | return; | |||||||||||||||||||
| 266 | } | ||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | ############################################################################## | ||||||||||||||||||||
| 269 | # Module return value and documentation | ||||||||||||||||||||
| 270 | ############################################################################## | ||||||||||||||||||||
| 271 | |||||||||||||||||||||
| 272 | # Make sure the module returns true. | ||||||||||||||||||||
| 273 | 2 | 1; | |||||||||||||||||||
| 274 | |||||||||||||||||||||
| 275 | __DATA__ | ||||||||||||||||||||