Debian Codegraph @

t/01-environment.t

raw
1 #!perl
2 use strict;
3 use warnings;
4 use Test::More tests => 174;
5 use Test::Exception;
6 use Encode;
7
8 use File::Temp;
9 use LMDB_File qw(:envflags :cursor_op);
10
11 # The tests needs 'C' LC_MESSAGES
12 eval { require POSIX; POSIX::setlocale(POSIX::LC_ALL(), 'C'); };
13
14 throws_ok {
15 LMDB::Env->new("NoSuChDiR");
16 } qr/No such|cannot find/, 'Directory must exists';
17
18 my $dir = File::Temp->newdir('mdbtXXXX', TMPDIR => 1, EXLOCK => 0);
19 ok(-d $dir, "Created $dir");
20 my $testdir = "TestDir";
21
22 throws_ok {
23 LMDB::Env->new($dir, { flags => MDB_RDONLY });
24 } qr/No such|cannot find/, 'RO must exists';
25
26 {
27 my $env = new_ok('LMDB::Env' => [ $dir ], "Create Environment")
28 or BAIL_OUT("Can't create environment, test the LMDB library first!");
29 ok(-e "$dir/data.mdb", 'Data file created exists');
30 ok(-e "$dir/lock.mdb", 'Lock file created exists');
31 $env->get_path(my $dummy);
32 is($dir, $dummy, 'get_path');
33
34 # Environment flags
35 {
36 $env->get_flags($dummy);
37 my $expflags =
38 $^O =~ /openbsd/ ? MDB_WRITEMAP : # Forced, sorry
39 0x0; # None set
40 is($dummy, $expflags, 'Flags setted'); # Using private
41 }
42 ok($env->id, 'Env ID: ' . $env->id);
43
44 # Basic Environment info
45 isa_ok(my $envinfo = $env->info, 'HASH', 'Get Info');
46 ok(exists $envinfo->{$_}, "Info has $_")
47 for qw(mapaddr mapsize last_pgno last_txnid maxreaders numreaders);
48 ok(!exists $envinfo->{SomeOther}, 'Not in info');
49
50 is($envinfo->{mapaddr}, 0, 'Not mapfixed');
51 is($envinfo->{mapsize}, 1024 * 1024, 'Stock mapsize');
52 is($envinfo->{maxreaders}, 126, 'Default maxreders');
53 is($envinfo->{numreaders}, 0, 'No readers');
54
55 isa_ok(my $stat = $env->stat, 'HASH', 'Get Stat');
56 ok(exists $stat->{$_}, "Stat has $_")
57 for qw(psize depth branch_pages leaf_pages overflow_pages entries);
58 # psize differs on various platforms
59 #is($stat->{psize}, 4096, 'Default psize');
60 is($stat->{$_}, 0, "$_ = 0, empty")
61 for qw(depth branch_pages leaf_pages overflow_pages entries);
62
63 # Check Internals
64 {
65 my @envid = keys %LMDB::Env::Envs;
66 is(scalar(@envid), 1, 'One Environment');
67 is($envid[0], $$env, 'The active one');
68 my $ed = $LMDB::Env::Envs{$$env};
69 isa_ok($ed, 'ARRAY');
70 is(scalar @{ $ed }, 4, 'Size');
71 isa_ok($ed->[0], 'ARRAY', 'Txns');
72 isa_ok($ed->[1], 'ARRAY', 'DCmps');
73 isa_ok($ed->[2], 'ARRAY', 'Cmps');
74 isa_ok(\$ed->[3], 'SCALAR', 'OFlags');
75 }
76
77 # Check Env refcounts
78 is(Internals::SvREFCNT($$env), 1, 'Env Inactive');
79
80 isa_ok(my $txn = $env->BeginTxn, 'LMDB::Txn', 'Transaction');
81 ok($txn->_id > 0, 'Expected');
82 is($txn->_id, $$txn, "Txn Id ($$txn)");
83 is(Internals::SvREFCNT($$txn), 1, 'Txn active');
84
85 is(Internals::SvREFCNT($$env), 2, 'Env Active');
86 throws_ok {
87 $txn->OpenDB('NAMED');
88 } qr/limit reached/, 'No named allowed';
89
90 SKIP: {
91 skip "Unsuported with MDB_WRITEMAP", 2 if $dummy & MDB_WRITEMAP;
92 isa_ok(my $sub = $env->BeginTxn, 'LMDB::Txn', 'Subtransaction');
93 is(Internals::SvREFCNT($$env), 3, 'Env Active');
94 }
95
96 is(Internals::SvREFCNT($$env), 2, 'Back normal');
97 {
98 isa_ok(my $eclone = $txn->env, 'LMDB::Env', 'Got Env');
99 is($env->id, $eclone->id, "The same env ID ($$env)");
100 is(Scalar::Util::refaddr($env), Scalar::Util::refaddr($eclone), 'Same refaddr');
101 is(Internals::SvREFCNT($$env), 3, 'Refcounted');
102 }
103 is(Internals::SvREFCNT($$env), 2, 'Back normal');
104 lives_ok {
105 $txn->commit;
106 is(Internals::SvREFCNT($$env), 1, 'Env free');
107 } 'Null Commit';
108 throws_ok {
109 $txn->commit;
110 } qr/Terminated/, 'Terminated';
111 is($$txn, 0, 'Nullified');
112 is($txn->_id, 0, 'The same');
113
114 ok($txn = $env->BeginTxn, 'Recreated');
115
116 # Open main DB
117 isa_ok(my $DB = $txn->OpenDB, 'LMDB_File', 'DBI created');
118 is($DB->Alive, 1, 'The first');
119 is($DB->flags, 0, 'Main DBI Flags');
120 is($env->info->{numreaders}, 0, "I'm not a reader");
121
122 is(Internals::SvREFCNT($$txn), 2, 'DB Keeps Txn');
123
124 is($txn->OpenDB->Alive, $DB->Alive, 'Just a clone');
125
126 # Put some data
127 my %data;
128 my $c;
129 foreach('A' .. 'Z') {
130 $c = ord($_) - ord('A') + 1;
131 my $k = $_ x 4;
132 my $v = sprintf('Datum #%d', $c);
133 $data{$k} = $v; # Keep a copy, for testing
134 if($c < 4) {
135 is($DB->put($k, $v), $v, "Put $k");
136 is($DB->stat->{entries}, $c, "Entry $c");
137 } else {
138 # Don't be verbose
139 $DB->put($k, $v);
140 }
141 }
142 is($c, 26, 'All in');
143
144 # Check data in random HASH order
145 $c = 5; # Don't be verbose
146 while(my($k, $v) = each %data) {
147 is($DB->get($k), $v, "Get $k") if(--$c >= 0);
148 }
149
150 { # Check UTF8 Handling
151 use utf8;
152 use Devel::Peek;
153 my $unicode = "♠♡♢♣"; # U+2660 .. U+2663
154 is(length $unicode, 4, 'Four unicode characters');
155 my $invariant = "áéíóú";
156 my $latin1 = Encode::encode('Latin1', $invariant);
157 # Without explicit help of LMDB_File we need extra care
158 is($DB->put('UNIC', $unicode), $unicode, 'Put unicode');
159 my $data = $DB->get('UNIC');
160 isnt($data, $unicode, 'Not the same!');
161 { use bytes; ok($data eq $unicode, 'Ugly!'); }
162 # Use Latin1
163 is($DB->put('UNIC2', $latin1), $latin1, 'Put latin1');
164 $data = $DB->get('UNIC2');
165 is($data, $latin1, 'By chance');
166 # But beware
167 is($data, $invariant, 'By perl magic');
168 { use bytes; ok($data ne $invariant, 'ne OK!?'); }
169
170 # Safe only if use explicit encode/decode
171 my $encoded = Encode::encode_utf8($unicode);
172 is($DB->put('ENC1', $encoded), $encoded, 'Put encoded');
173 $data = $DB->get('ENC1');
174 isnt($data, $unicode, 'Need decode');
175 is(Encode::decode_utf8($data), $unicode, 'Decode');
176 is(Encode::decode('Latin1', $DB->get('UNIC2')), $invariant, 'Should');
177
178 # Easier with explicit help
179 is($DB->UTF8(1), 0, 'Was off');
180
181 is($DB->put('UNIC', $unicode), $unicode, 'Just put in');
182 is($DB->get('UNIC'), $unicode, 'Just get out');
183 $data = do { use bytes; $DB->get('UNIC'); };
184 is($data, $encoded, 'Handy');
185 {
186 # Be permisive
187 my $warn;
188 local $SIG{__WARN__} = sub { $warn = shift };
189 $data = $DB->get('UNIC2');
190 like($warn, qr/Malformed/, 'Warning emited');
191 ok(!utf8::is_utf8($data), 'No UTF8');
192 is($data, $latin1, 'But just works');
193 $warn = '';
194
195 # Can correct that.
196 is($DB->put('UNIC2', $latin1), $latin1, 'Just put encoded');
197 ok(!utf8::is_utf8($latin1), 'Orig not touched');
198 $data = $DB->get('UNIC2');
199 is($data, $invariant, 'Must be');
200 { use bytes; ok($data eq $invariant, 'No suprises'); }
201 is($data, $latin1, 'Just get out encoded');
202 { use bytes; ok($data ne $latin1, 'Expected'); }
203 ok($warn eq '', 'No more warnings');
204 }
205
206 is($DB->get('ENC1'), $unicode, 'Safe played');
207 is($DB->get('AAAA'), $data{'AAAA'}, 'Unaltered');
208
209 $DB->del($_) for qw(UNIC UNIC2 ENC1);
210 }
211
212 # Commit
213 lives_ok { $txn->commit; } 'Commited';
214
215 # Commit terminates transaction and DB
216 ok(!$DB->Alive, "Not Alive");
217 is($DB->Txn, undef, "No Txn");
218 is($DB->dbi, 1, "Last memory of dbi");
219 throws_ok {
220 $DB->get('SOMEKEY');
221 } qr/Not an active/, 'Commit invalidates DB';
222 throws_ok {
223 $txn->OpenDB;
224 } qr/Not an alive/, 'Commit finalized txn';
225 lives_ok {
226 my $warn;
227 local $SIG{__WARN__} = sub { $warn = shift };
228 $txn->abort;
229 like($warn, qr/Terminated/, 'Warning emited');
230 is($txn->_id, 0, 'Expected 0');
231 is($$txn, 0, 'A ghost...');
232 $warn = ''; # Clean;
233 undef $txn;
234 ok(!$warn, 'No warnings');
235 } 'but blessed';
236
237 is(Internals::SvREFCNT($$env), 1, 'Env Inactive');
238 is($env->info->{numreaders}, 0, 'No readers yet');
239
240 # Test copy method
241 throws_ok {
242 $env->copy($testdir);
243 } qr/No such/, 'Copy needs a directory';
244 throws_ok {
245 $env->copy($dir);
246 } qr/file exists|error/i, 'An empty one, not myself';
247
248 SKIP: {
249 skip "Need a local directory", 2 unless(-d $testdir or mkdir $testdir);
250 is($env->copy($testdir), 0, 'Copied');
251 my $size = -s "$testdir/data.mdb";
252 ok($size, "Data file created, $size");
253 }
254 $testdir = $dir unless -s "$testdir/data.mdb";
255
256 {
257 open(my $fd, '>', "$testdir/other.mdb");
258 is($env->copyfd($fd), 0, 'Copied to HANDLE');
259 #is($env->info->{numreaders}, 1, 'A reader');
260 }
261
262 isa_ok($DB = LMDB_File->new($env->BeginTxn(MDB_RDONLY), 1),
263 'LMDB_File', 'DBI fast opened RO');
264
265 throws_ok {
266 $DB->put('0000', 'Datum #0');
267 } qr/Permission denied/, 'Read only transaction';
268
269 is($env->info->{numreaders}, 1, "I'm a reader");
270 is($DB->stat->{entries}, 26, 'Has my data');
271
272 # Read using cursors
273 isa_ok(my $cursor = $DB->Cursor, 'LMDB::Cursor', 'A cursor');
274 is($cursor->dbi, $DB->Alive, 'Get DBI');
275
276 $cursor->get(my $key, my $datum, MDB_FIRST);
277 is($key, 'AAAA', 'First key');
278 is($datum, 'Datum #1', 'First datum');
279 throws_ok {
280 $cursor->get($key, $datum, MDB_PREV);
281 } qr/NOTFOUND/, 'No previous key';
282 $cursor->get($key, $datum, MDB_NEXT);
283 is($key, 'BBBB', 'Next key');
284 is($datum, 'Datum #2', 'Next datum');
285 $cursor->get($key, $datum, MDB_LAST);
286 is($key, 'ZZZZ', 'Last key');
287 is($datum, 'Datum #26', 'Last datum');
288 $cursor->get($key, $datum, MDB_PREV);
289 is($key, 'YYYY', 'Previous key');
290 is($datum, 'Datum #25', 'Previous datum');
291 $key = $datum = '';
292 $cursor->get($key, $datum, MDB_GET_CURRENT);
293 is($key, 'YYYY', 'Current key');
294 is($datum, 'Datum #25', 'Current datum');
295
296 throws_ok {
297 # Most cursor_ops need to return the key
298 $cursor->get('CCCC', $datum, MDB_GET_CURRENT);
299 } qr/read-only value/, 'Need lvalue';
300 lives_ok {
301 # Some accept a constant
302 $cursor->get('CCCC', $datum, MDB_SET);
303 } 'Can be constant';
304 is($datum, 'Datum #3', 'lookup datum');
305
306 throws_ok {
307 $cursor->get($key = 'ZABC', $datum, MDB_SET);
308 } qr/NOTFOUND/, 'Not found';
309 $cursor->get($key, $datum, MDB_SET_RANGE);
310 is($key, 'ZZZZ', 'Got last key');
311 is($datum, 'Datum #26', 'Got last datum');
312 throws_ok {
313 $cursor->get($key, $datum, MDB_NEXT);
314 } qr/NOTFOUND/, 'No next key';
315 }
316 is(scalar keys %LMDB::Env::Envs, 0, 'No environment open');
317 {
318 # Using TIE interface
319 my $h;
320 isa_ok(
321 tie(%$h, 'LMDB_File', "$testdir/other.mdb" => {
322 flags => MDB_NOSUBDIR,
323 mapsize => 2 * 1024 * 1024,
324 }),
325 'LMDB_File', 'Tied'
326 );
327 { # Consistency checks
328 isa_ok(my $DB = tied %$h, 'LMDB_File', 'The same');
329 isa_ok(my $txn = $DB->Txn, 'LMDB::Txn', 'Has Txn');
330 isa_ok(my $env = $txn->env, 'LMDB::Env');
331 is($env->info->{mapsize}, 2 * 1024 * 1024, 'mapsize increased');
332 is($DB->dbi, 1, 'The default one');
333 }
334
335 # Check optimized scalar
336 ok(scalar %$h, 'Has data');
337 is($h->{EEEE}, 'Datum #5', 'FETCH');
338 is($h->{ABCS}, undef, 'No data');
339 my @keys = keys %{$h};
340 is(scalar @keys, 26, 'Size');
341 is_deeply(['A'..'Z'], [ map substr($_, 0, 1), @keys ], 'All in');
342
343 ok(exists $h->{ZZZZ}, 'Exists');
344 is(delete $h->{ZZZZ}, 'Datum #26', 'Deleted #26');
345 ok(!exists $h->{ZZZZ}, 'Really deleted');
346 is(scalar %$h, 25, 'Reduced');
347
348 is($h->{ZZZZ} = 'New data', 'New data', 'STORE');
349 is(scalar %$h, 26, 'Stored');
350 is($h->{ZZZZ}, 'New data', 'Really stored');
351 %$h = ();
352 ok(!scalar %$h, 'Emptied');
353 %$h = (a => 1, b => 2, c=>3);
354 is(scalar %$h, 3, 'Loaded');
355
356 # Check each
357 while(my($k, $v) = each %$h) {
358 is(ord($k)-96,$v, "Match for $k");
359 }
360
361 untie %$h;
362 }
363
364 END {
365 unless($ENV{KEEP_TMPS}) {
366 for($testdir, $dir) {
367 unlink glob("$_/*");
368 rmdir or warn "rm $_: $!\n";
369 #warn "Removed $_\n";
370 }
371 }
372 }