File: BerkeleyDB.pm

package info (click to toggle)
libdata-session-perl 1.18-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: perl: 2,916; makefile: 7
file content (260 lines) | stat: -rw-r--r-- 5,782 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
258
259
260
package Data::Session::Driver::BerkeleyDB;

use parent 'Data::Session::Base';
no autovivification;
use strict;
use warnings;

use BerkeleyDB;

use Hash::FieldHash ':all';

use Try::Tiny;

our $VERSION = '1.18';

# -----------------------------------------------

sub init
{
	my($self, $arg) = @_;
	$$arg{cache}    ||= '';
	$$arg{verbose}  ||= 0;

} # End of init.

# -----------------------------------------------

sub new
{
	my($class, %arg) = @_;

	$class -> init(\%arg);

	(! $arg{cache}) && die __PACKAGE__ . '. No cache supplied to new(...)';

	return from_hash(bless({}, $class), \%arg);

} # End of new.

# -----------------------------------------------

sub remove
{
	my($self, $id) = @_;
	my($lock)      = $self -> cache -> cds_lock;
	my($status)    = $self -> cache -> db_del($id);

	$lock -> cds_unlock;

	# Return '' for failure.

	return $status ? '' : 1;

} # End of remove.

# -----------------------------------------------

sub retrieve
{
	my($self, $id) = @_;
	my($lock)      = $self -> cache -> cds_lock;
	my($data)      = '';
	my($status)    = $self -> cache -> db_get($id => $data);

	$lock -> cds_unlock;

	# Return '' for failure.

	return $status ? '' : $data;

} # End of retrieve.

# -----------------------------------------------

sub store
{
	my($self, $id, $data) = @_;
	my($lock)   = $self -> cache -> cds_lock;
	my($status) = $self -> cache -> db_put($id => $data);

	$lock -> cds_unlock;

	return $status ? '' : 1;

} # End of store.

# -----------------------------------------------

sub traverse
{
	my($self, $sub) = @_;
	my($id, $data)  = ('', '');
	my($cursor)     = $self -> cache -> db_cursor;

	while ($cursor -> c_get($id, $data, DB_NEXT) == 0)
	{
		$sub -> ($id);
	}

	undef $cursor;

	return 1;

} # End of traverse.

# -----------------------------------------------

1;

=pod

=head1 NAME

L<Data::Session::Driver::BerkeleyDB> - A persistent session manager

=head1 Synopsis

See L<Data::Session> for details.

=head1 Description

L<Data::Session::Driver::BerkeleyDB> allows L<Data::Session> to manipulate sessions via
L<BerkeleyDB>.

To use this module do both of these:

=over 4

=item o Specify a driver of type BerkeleyDB, as
Data::Session -> new(type => 'driver:BerkeleyDB ...')

=item o Specify a cache object of type L<BerkeleyDB> as Data::Session -> new(cache => $object)

Also, $object must have been created with a Env parameter of type L<BerkeleyDB::Env>. See below.

=back

See scripts/berkeleydb.pl.

=head1 Case-sensitive Options

See L<Data::Session/Case-sensitive Options> for important information.

=head1 Method: new()

Creates a new object of type L<Data::Session::Driver::BerkeleyDB>.

C<new()> takes a hash of key/value pairs, some of which might mandatory. Further, some combinations
might be mandatory.

The keys are listed here in alphabetical order.

They are lower-case because they are (also) method names, meaning they can be called to set or get
the value at any time.

=over 4

=item o cache => $object

Specifies the object of type L<BerkeleyDB> to use for session storage.

This key is normally passed in as Data::Session -> new(cache => $object).

Warning: This cache object must have been set up both as an object of type L<BerkeleyDB>, and with
that object having an Env parameter of type L<Berkeley::Env>, because this module -
L<Data::Session::Driver::BerkeleyDB> - uses the L<BerkeleyDB> method cds_lock().

This key is mandatory.

=item o verbose => $integer

Print to STDERR more or less information.

Typical values are 0, 1 and 2.

This key is normally passed in as Data::Session -> new(verbose => $integer).

This key is optional.

=back

=head1 Method: remove($id)

Deletes from storage the session identified by $id.

Returns the result of calling the L<BerkeleyDB> method delete($id).

This result is a Boolean value indicating 1 => success or 0 => failure.

=head1 Method: retrieve($id)

Retrieve from storage the session identified by $id.

Returns the result of calling the L<BerkeleyDB> method get($id).

This result is a frozen session. This value must be thawed by calling the appropriate serialization
driver's thaw() method.

L<Data::Session> calls the right thaw() automatically.

=head1 Method: store($id => $data)

Writes to storage the session identified by $id, together with its data $data.

Returns the result of calling the L<BerkeleyDB> method set($id => $data).

This result is a Boolean value indicating 1 => success or 0 => failure.

=head1 Method: traverse()

Retrieves all ids via a cursor, and for each id calls the supplied subroutine with the id as the
only parameter.

The database is not locked during this process.

Returns 1.

=head1 Installing BerkeleyDB

	Get Oracle's BerkeleyDB from
	http://www.oracle.com/technetwork/database/berkeleydb/overview/index.html
	I used V 5.1.19
	tar xvzf db-5.1.19.tar.gz
	cd db-5.1.19/build_unix
	../dist/configure
	make
	sudo make install
	It installs into /usr/local/BerkeleyDB.5.1

	Get Perl's BerkeleyDB from http://search.cpan.org
	I used V 0.43
	tar xvzf BerkeleyDB-0.43.tar.gz
	cd BerkeleyDB-0.43
	Edit 2 lines in config.in:
	INCLUDE = /usr/local/BerkeleyDB.5.1/include
	LIB     = /usr/local/BerkeleyDB.5.1/lib
	perl Makefile.PL
	make && make test
	sudo make install

=head1 Support

Log a bug on RT: L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Session>.

=head1 Author

L<Data::Session> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2010.

Home page: L<http://savage.net.au/index.html>.

=head1 Copyright

Australian copyright (c) 2010, Ron Savage.

	All Programs of mine are 'OSI Certified Open Source Software';
	you can redistribute them and/or modify them under the terms of
	The Artistic License, a copy of which is available at:
	http://www.opensource.org/licenses/index.html

=cut