summaryrefslogtreecommitdiffabout
path: root/lib/App/Glacier/DB/GDBM.pm
blob: 0e14a655b207dc4e8d7375b20a78554f246403a9 (plain)
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
package App::Glacier::DB::GDBM;
use strict;
use warnings;
use GDBM_File;
use Carp;
use File::Basename;
use File::Path qw(make_path);

sub new {
    my $class = shift;
    local %_ = @_;
    my $file = delete $_{file} // croak "filename is required";
    unless (-f $file) {
	if (defined(my $create = delete $_{create})) {
	    if (ref($create) eq 'CODE') {
		$create = &{$create}();
	    }
	    return undef unless $create;
	}
	my $dir = dirname($file);
	unless (-d $dir) {
	    make_path($dir, {error=>\my $err});
	    if (@$err) {
		for my $diag (@$err) {
		    my ($filename, $message) = %$diag;
                    $filename = $dir if ($filename eq '');
                    carp("error creating $filename: $message");
		}
		croak("failed to create $dir");
	    }
	}
    }
    my $self = bless {}, $class;
    $self->{_filename} = $file;
    $self->{_mode} = delete $_{mode} || 0644;
    $self->{_retries} = delete $_{retries} || 10;
    $self->{_nref} = 0;
    $self->{_deleted} = [];
    return $self;
}

my %lexicon = (
	backend => 1,
	file => { mandatory => 1 },
	mode => { default => 0644 },
	ttl => { default => 72000, check => \&App::Glacier::Command::ck_number },
	encoding => { default => 'json' }
);

sub configtest {
    my ($class, $cfg, @path) = @_;
    $cfg->lint(\%lexicon, @path);
}

# We can't tie the DB to $self->{_map} at once, in the new method, because
# this will cause coredumps in threaded code (see 
# https://rt.perl.org/Public/Bug/Display.html?id=61912).  So, the following
# auxiliary method is used, which calls &$code with $self->{_map} tied
# to the DB.
sub _tied {
    my ($self, $code) = @_;
    croak "argument must be a CODE ref" unless ref($code) eq 'CODE';
    if ($self->{_nref}++ == 0) {
	my $n = 0;
	while (! tie %{$self->{_map}}, 'GDBM_File', $self->{_filename},
	             GDBM_WRCREAT, $self->{_mode}) {
	    if ($n++ > $self->{_retries}) {
		croak "can't open file $self->{_filename}: $!";
	    }
	    sleep(1);
	}
    }
    my $ret = wantarray ? [ &{$code}() ] : &{$code}();
    if (--$self->{_nref} == 0) {
	untie %{$self->{_map}};
    }
    return wantarray ? @$ret : $ret;
}

sub drop {
    my ($self) = @_;
    my $filename = $self->{_filename};
    unlink $filename or carp "can't unlink $filename: $!";
}

sub has {
    my ($self, $key) = @_;
    return $self->_tied(sub { exists($self->{_map}{$key}) });
}

sub retrieve {
    my ($self, $key) = @_;
    return $self->_tied(sub {
	return undef unless exists $self->{_map}{$key};
	return $self->{_map}{$key};
    });
}

sub store {
    my ($self, $key, $val) = @_;
    return $self->_tied(sub { $self->{_map}{$key} = $val });
}

sub delete {
    my ($self, $key) = @_;
    if (@{$self->{_deleted}}) {
	push @{$self->{_deleted}[-1]}, $key;
    } else {
	$self->_tied(sub { delete $self->{_map}{$key} });
    }
}

sub foreach {
    my ($self, $code) = @_;
    croak "argument must be a CODE" unless ref($code) eq 'CODE';
    $self->_tied(sub {
	push @{$self->{_deleted}}, [];
	while (my ($key, $val) = each %{$self->{_map}}) {
	    &{$code}($key, $val);
	}

	foreach my $key (@{pop @{$self->{_deleted}}}) {
	    $self->delete($key);
	}
    });
}

1;

Return to:

Send suggestions and report system problems to the System administrator.