-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyValueTree.pm
161 lines (120 loc) · 3.55 KB
/
KeyValueTree.pm
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
package KeyValueTree;
use strict;
use Exporter;
use File::Path qw(make_path);
use Tie::File;;
use Fcntl qw(O_RDONLY O_RDWR O_CREAT);
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
$VERSION = '0.00';
@ISA = qw(Exporter);
@EXPORT = ();
@EXPORT_OK = qw(new);
sub new {
my ($class_name) = shift;
my %params = @_;
if (!$params{'PATH'}) { die 'No PATH given'; }
if (!-e $params{'PATH'} or !-x $params{'PATH'}) { die $params{'PATH'}.' does not exist or not accessible'; }
my $self = {};
bless($self, $class_name);
$self->{'created'} = 1;
$self->{'PATH'} = $params{'PATH'};
$self->{'PATH'} =~ s/\/+$//;
$self->{'PATH'}.= '/';
if (!-e $self->{'PATH'}.'/kvt.path') { die $self->{'PATH'}.'/kvt.path does not exist'; }
return $self;
}
sub get {
my ($self, $key) = @_;
$key = $self->_valid_key($key);
if ($key eq '') { die('invalid key ('.$key.') in get'); }
my $path = $self->_path_for_key($key);
my $data_path = $self->{'PATH'}.$path;
if (-r $data_path) {
eval {
tie my @lines, 'Tie::File', $data_path, mode => O_RDONLY;
my @list = grep { /^$key\t([0-9.]{7,15})$/ } @lines;
my $value;
if (@list > 0) {
@list = split /\t/, $list[0];
$value = $list[1];
}
untie @lines;
return $value;
};
}
}
sub set {
my ($self, $key, $value) = @_;
$key = $self->_valid_key($key);
if ($key eq '') { die('invalid key ('.$key.') in set'); }
my $path = $self->_path_for_key($key);
# create path unless exists
if (!-e $self->{'PATH'}.$path) { $self->_mkdir_recursive($path); }
eval {
tie my @lines, 'Tie::File', $self->{'PATH'}.$path, mode => O_RDWR | O_CREAT;;
my @list = grep { /^$key\t([0-9.]{7,15})$/ } @lines;
my $old_value = '';
if (@list > 0) {
@list = split /\t/, $list[0];
$old_value = $list[1];
}
if ($old_value && $old_value ne "") {
map { s/^($key)\t([0-9.]{7,15})$/$1\t$value/; } @lines;
} else {
push @lines, "$key\t$value";
}
untie @lines;
};
}
sub _mkdir_recursive {
my ($self, $path) = @_;
# separate file from dir path
my $dir_path = $path;
if ($dir_path =~ /^[a-z0-9_]{1,3}\.stor$/) { return; }
$dir_path =~ s/\/[a-z0-9_]{1,3}\.stor$//;
if ($dir_path ne "") {
make_path($self->{'PATH'}.'/'.$dir_path, { 'verbose' => 0 });
}
}
sub _valid_key {
my ($self, $key) = @_;
$key = lc($key);
$key =~ s/[^a-z0-9.-]/-/g;
if ($key !~ /^[a-z0-9.-]{4,100}$/) {
return '';
}
$key =~ s/\./_/g;
return $key;
}
sub _path_for_key {
my ($self, $key) = @_;
$key =~ s/[^a-z0-9]//g;
my @key = split //, $key;
my $path = '';
for (my $i = 0; $i < 2 && $#key > 0; $i++) {
$path .= shift(@key);
if ($#key >= 0) { $path.= shift(@key); }
$path .= '/';
}
$path =~ s/\/$/\.stor/;
return $path;
}
__END__
=head1 NAME
KeyValueTree.pm - a quick module to store data in multiple files across a directory tree
=head1 SYNOPSIS
Setup:
use KeyValueTree;
my $kvt = KeyValueTree->new( PATH => '/tmp/kvt/' );
(make sure that /tmp/kvt includes a file "kvt.path" and that it is otherwise empty)
Basic usage:
$kvt->set("key", "value");
my $value = $kvt->get("key");
=head1 DESCRIPTION
KeyValueTree is a quick solution to store a lot of small Key-Value pairs in a directory tree. The idea was to get small files and directories that are still usable (not filled with thousands of files).
=head1 AUTHOR
Mario Witte, <[email protected]>
=head1 COPYRIGHT
Copyright 2013 by Mario Witte
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.
=cut