1 package IndexData::Utils::PersistentCounter;
12 IndexData::Utils::PersistentCounter - Perl extension imnlementing persistent counters
16 use IndexData::Utils::PersistentCounter;
17 $counter = new IndexData::Utils::PersistentCounter($file, 1);
18 $n = $counter->next();
19 $n = $counter->next();
21 $n = $counter->delete();
25 This library provides a simple persistent counter class for
26 maintaining a counter on disk across multiple runs of a program. It is
27 safe against multiple concurrent accesses (i.e. will not issue the
28 same value twice to different processes). It can be used for
29 applications such as generating unique record IDs.
35 $old = new IndexData::Utils::PersistentCounter($file1);
36 $new = new IndexData::Utils::PersistentCounter($file2, 1);
38 Creates a new counter object associated with a file which contains the
39 persistent state of the counter. The purpose of the counter is to
40 return consecutive integers on consecutive calls, even if those calls
41 are made from multiple concurrent processes. The file stores the state
44 In the usual case (no second argument), the file must already exist;
45 if it does not, it is not created, but an undefined value is returned.
47 If a second argument is provided and its value is true, then a new
48 counter file is created with initial value 1. Note that B<this will
49 overwrite any existing file>, so use with caution.
55 my($file, $create) = @_;
58 return undef if !$create;
59 # ### There is a bit of a race condition here, but it's not
60 # something that's going to crop up in real life.
61 my $fh = new IO::File(">$file") || return undef;
63 $fh->close() or return undef;
76 $n = $counter->next();
78 Returns the next available integer from the specified counter, and
79 increments the counter ready for the next invocation (whether that
80 invocation is in this process or a different one).
82 The first call of C<next()> on a newly created counter returns 1, not
83 0. Each subsequent call returns a value one higher than the previous
91 my $fh = new IO::File('+<' . $this->{file}) || return undef;
92 flock($fh, 2) || die "can't lock file";
95 $fh->print($n+1, "\n");
96 $fh->close() or return undef;
103 $ok = $counter->delete();
105 Permanently deletes a counter file. Returns true if the deletion was
106 successful, false otherwise.
113 unlink $this->{file} or return 0;
124 Mike Taylor, E<lt>mike@indexdata.comE<gt>
126 =head1 COPYRIGHT AND LICENSE
128 Copyright (C) 2014 by Index Data.
130 This library is free software; you can redistribute it and/or modify
131 it under the same terms as Perl itself, either Perl version 5.8.4 or,
132 at your option, any later version of Perl 5 you may have available.