File: Hierarchy.pm

package info (click to toggle)
libdata-hierarchy-perl 0.21-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 56 kB
  • ctags: 21
  • sloc: perl: 230; makefile: 42
file content (257 lines) | stat: -rw-r--r-- 6,189 bytes parent folder | download
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package Data::Hierarchy;
$VERSION = '0.21';
use strict;
use Clone qw(clone);

=head1 NAME

Data::Hierarchy - Handle data in a hierarchical structure

=head1 SYNOPSIS

    my $tree = Data::Hierarchy->new();
    $tree->store ('/', {access => 'all'});
    $tree->store ('/private', {access => 'auth',
                               '.sticky' => 'this is private});

    $info = $tree->get ('/private/somewhere/deep');

    # return actual data points in list context
    ($info, @fromwhere) = $tree->get ('/private/somewhere/deep');

    my @items = $tree->find ('/', {access => qr/.*/});

    # override all children
    $tree->store_recursively ('/', {access => 'all'});

    my $hashref = $tree->dump;

=head1 DESCRIPTION

Data::Hierarchy provides a simple interface for manipulating
inheritable data attached to a hierarchical environment (like
filesystem).

=cut

sub new {
    my $class = shift;
    # allow shorthand of ->new({...}) to mean ->new(hash => {...})
    unshift @_, 'hash' if @_ % 2;

    my $self = bless {@_}, $class;
    $self->{sep} ||= '/';
    $self->{hash} ||= {};
    $self->{sticky} ||= {};
    return $self;
}

sub key_safe {
    if (length ($_[1]) > 1 and rindex($_[1], $_[0]->{sep}) == length ($_[1])) {
	require Carp;
	Carp::confess('key unsafe');
    }
    $_[1] =~ s/\Q$_[0]->{sep}\E+$//;
}

sub store_single {
    my ($self, $key, $value) = @_;
    $self->key_safe ($key);
    $self->{hash}{$key} = $value;
}

sub _store {
    my ($self, $key, $value) = @_;
    $self->key_safe ($key);

    my $oldvalue = $self->{hash}{$key} if exists $self->{hash}{$key};
    my $hash = {%{$oldvalue||{}}, %$value};
    for (keys %$hash) {
	if (index($_, '.') == 0) {
	    defined $hash->{$_} ?
		$self->{sticky}{$key}{$_} = $hash->{$_} :
		delete $self->{sticky}{$key}{$_};
	    delete $hash->{$_};
	}
	else {
	    delete $hash->{$_}
		unless defined $hash->{$_};
	}
    }

    $self->{hash}{$key} = $hash;
    delete $self->{hash}{$key} unless %{$self->{hash}{$key}};
    delete $self->{sticky}{$key} unless keys %{$self->{sticky}{$key}};
}

sub merge {
    my ($self, $other, $path) = @_;
    my %datapoints = map {$_ => 1} ($self->descendents ($path),
				    $other->descendents ($path));
    for my $key (reverse sort keys %datapoints) {
	my $value = $self->get ($key);
	my $nvalue = $other->get ($key);
	for (keys %$value) {
	    $nvalue->{$_} = undef
		unless defined $nvalue->{$_};
	}
	$self->store ($key, $nvalue);
    }
}

sub _descendents {
    my ($self, $hash, $key) = @_;

    # If finding for everything, don't bother grepping
    return sort keys %$hash unless length($key);

    return sort grep {index($_.$self->{sep}, $key.$self->{sep}) == 0}
	keys %$hash;
}

sub descendents {
    my ($self, $key) = @_;
    my $both = {%{$self->{hash}}, %{$self->{sticky} || {}}};

    # If finding for everything, don't bother grepping
    return sort keys %$both unless length($key);

    return sort grep {index($_.$self->{sep}, $key.$self->{sep}) == 0}
	keys %$both;
}

# empty the overridden values on descendents for hash
sub _store_recursively {
    my ($self, $key, $value, $hash) = @_;

    $self->key_safe ($key);
    my @datapoints = $self->_descendents ($hash, $key);

    for (@datapoints) {
	my $vhash = $hash->{$_};
	delete $vhash->{$_} for keys %$value;
	delete $hash->{$_} unless %{$hash->{$_}};
    }
}

# use store_fast to avoid trimming duplicated value with ancestors
sub store_fast {
    my $self = shift;
    $self->store (@_, 1);
}

sub store {
    my ($self, $key, $value, $fast) = @_;

    unless ($fast) {
	my $ovalue = $self->get ($key);
	for (keys %$value) {
	    next unless defined $value->{$_};
	    delete $value->{$_}
		if exists $ovalue->{$_} && $ovalue->{$_} eq $value->{$_};
	}
    }
    return unless keys %$value;
    $self->_store_recursively ($key, $value, $self->{hash})
	unless $fast;
    $self->_store ($key, $value);
}

sub store_override {
    my ($self, $key, $value) = @_;

    my ($ovalue, @datapoints) = $self->get ($key);
    for (keys %$value) {
	next unless defined $value->{$_};
	if (exists $ovalue->{$_} && $ovalue->{$_} eq $value->{$_}) {
	    # if the parent has the property already
	    if ($#datapoints > 0 && exists $self->{hash}{$datapoints[-1]}{$_}) {
		$value->{$_} = undef;
	    }
	    else {
		delete $value->{$_};
	    }
	}
    }
    return unless keys %$value;
    $self->_store ($key, $value);
}

# Useful for removing sticky properties.
sub store_recursively {
    my ($self, $key, $value) = @_;

    $self->_store_recursively ($key, $value, $self->{hash});
    $self->_store_recursively ($key, $value, $self->{sticky});
    $self->_store ($key, $value);
}

sub find {
    my ($self, $key, $value) = @_;
    $self->key_safe ($key);
    my @items;
    my @datapoints = $self->descendents($key);

    for my $entry (@datapoints) {
	my $matched = 1;
	for (keys %$value) {
	    my $lookat = (index($_, '.') == 0) ?
		$self->{sticky}{$entry} : $self->{hash}{$entry};
	    $matched = 0
		unless exists $lookat->{$_}
			&& $lookat->{$_} =~ m/$value->{$_}/;
	    last unless $matched;
	}
	push @items, $entry
	    if $matched;
    }
    return @items;
}

sub get_single {
    my ($self, $key) = @_;
    return clone ($self->{hash}{$key} || {});
}

sub get {
    my ($self, $key, $rdonly) = @_;
    $self->key_safe ($key);
    my $value = {};
    # XXX: could build cached pointer for fast traversal
    my @datapoints = sort grep {index($key.$self->{sep}, $_.$self->{sep}) == 0}
	 keys %{$self->{hash}};

    for (@datapoints) {
	my $newv = $self->{hash}{$_};
	$newv = clone $newv unless $rdonly;
	$value = {%$value, %$newv};
    }
    if (exists $self->{sticky}{$key}) {
	my $newv = $self->{sticky}{$key};
	$newv = clone $newv unless $rdonly;
	$value = {%$value, %$newv}
    }
    return wantarray ? ($value, @datapoints) : $value;
}

sub dump {
    my ($self) = @_;
    return $self->{hash};
}

1;

=head1 AUTHORS

Chia-liang Kao E<lt>clkao@clkao.orgE<gt>

=head1 COPYRIGHT

Copyright 2003 by Chia-liang Kao E<lt>clkao@clkao.orgE<gt>.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

See L<http://www.perl.com/perl/misc/Artistic.html>

=cut