ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/YAMLDB.pm
Revision: 1.1
Committed: Mon Jul 24 19:51:43 2006 UTC (17 years, 9 months ago) by elmex
Branch: MAIN
Log Message:
refactored ipo database

File Contents

# Content
1 package YAMLDB;
2 use YAML qw/LoadFile DumpFile Dump/;
3 use POSIX qw/strftime/;
4
5 sub new {
6 my ($class, %arg) = @_;
7
8 my $self = bless {
9 %arg,
10 }, $class;
11
12 $self->{last_check} = (-M $self->{db_file}) + 1;
13 unless (-x $self->{db_file}) {
14 DumpFile ($self->{db_file}, {});
15 }
16 $self->check_maildb ();
17
18 $self;
19 }
20
21 sub check_maildb {
22 my ($self) = @_;
23
24 if ($self->{last_check} > (-M $self->{db_file})) {
25 $self->{last_check} = -M $self->{db_file};
26
27 my $maildb = eval { my $m = LoadFile ($self->{db_file}); return $m };
28 if ($@) {
29 warn "ERROR when reading mail database $self->{db_file}: $@\n";
30 $self->{db_file} = $self->{db_file} . ".after_failure";
31 } else {
32 $self->{maildb} = $maildb;
33 }
34 }
35 }
36
37 # This is not thread or multiprocess safe!
38 # XXX: A second process will (of course) terribly damage the maildatabase
39 # but i assume there wont be any second process.
40 sub sync_maildb {
41 my ($self) = @_;
42
43 eval {
44 DumpFile ("$self->{db_file}.tmp", $self->{maildb});
45 };
46 if ($@) {
47 warn "ERROR when writing mail database $self->{db_file}.tmp: $@\n";
48 } else {
49 rename "$self->{db_file}.tmp", $self->{db_file};
50 $self->{last_check} = -M $self->{db_file};
51 }
52 }
53
54 sub get {
55 my ($self, $key) = @_;
56 $self->check_maildb;
57 $self->{maildb}->{$key};
58 }
59
60 sub clear {
61 my ($self, $key) = @_;
62 $self->check_maildb;
63 delete $self->{maildb}->{$key};
64 $self->sync_maildb;
65 }
66
67 sub set {
68 my ($self, $key, $value) = @_;
69 $self->check_maildb;
70 $self->{maildb}->{$key} = $value;
71 $self->sync_maildb;
72 }
73 1