Debian Codegraph @

t/03-fastmode.t

raw
1 #!perl
2 use strict;
3 use warnings;
4 use utf8;
5 use Test::More tests => 53;
6 use Test::Exception;
7 #use Test::ZeroCopy;
8 use B;
9 use Benchmark qw(:hireswallclock);
10 use Config;
11 #use Devel::Peek;
12 #$Devel::Peek::pv_limit = 20;
13
14 use File::Temp;
15 use LMDB_File qw(:flags :cursor_op);
16
17 my $dir = File::Temp->newdir('mdbtXXXX', TMPDIR => 1, EXLOCK => 0);
18 ok(-d $dir, "Created test dir $dir");
19 my $large1 = '0123456789' x 100_000;
20 my $val;
21 {
22 my $env = LMDB::Env->new($dir, {
23 mapsize => 100 * 1024 * 1024
24 });
25 ok(my $DB = $env->BeginTxn->OpenDB, 'Open unamed');
26 is($DB->dbi, 1, 'Opened');
27 is($DB->put('A' => $large1), $large1, 'Put large value');
28 $DB->Txn->commit;
29
30 $DB = LMDB_File->new($env->BeginTxn(MDB_RDONLY), 1);
31 is($DB->ReadMode, 0, 'In normal read mode');
32 $DB->get('A', $val);
33 is(length($val), 1_000_000, '1MB');
34 is($val, $large1, 'The value is there');
35 ok(my $inspec = B::svref_2object(\$val), 'Inspected');
36 ok($inspec->isa('B::PV'), 'Is a PV');
37 ok($inspec->LEN > 0, 'Perl owned');
38 #Dump($val);
39
40 my $t = timeit(50, sub { $DB->get('A', $val) for (1..1000); });
41 diag("Normal mode in ", timestr($t));
42
43 is($DB->ReadMode(1), 0, 'Previous mode returned');
44 is($DB->ReadMode, 1, 'In fast read mode');
45 $DB->get('A', my $fval);
46 #Dump($fval);
47 is(length($fval), 1_000_000, '1MB');
48 is($fval, $large1, 'The value is there');
49 ok($inspec = B::svref_2object(\$fval), 'Inspected');
50 ok($inspec->isa('B::PVMG'), 'Is a PVMG');
51 is($inspec->LEN, 0, 'Not perl owned');
52 is($fval, $val, 'Same value');
53 #isnt_zerocopy($fval, $val, 'Diferent buffer');
54 throws_ok {
55 $fval = 'Hola';
56 } qr/read-only/, 'Is ReadOnly';
57
58 $t = timeit(50, sub { $DB->get('A', $val) for (1..1000); });
59 diag("Fast mode in ", timestr($t));
60 is($fval, $val, 'Same value');
61 #is_zerocopy($fval, $val, 'Same buffer');
62 my $oval = $DB->Rget('A');
63 is($$oval, $val, 'Same value by ref');
64 #is_zerocopy($$oval, $val, 'Same buffer by ref');
65
66 $DB = undef;
67 $DB = LMDB_File->new($env->BeginTxn(MDB_RDONLY), 1);
68 is($DB->ReadMode, 1, 'Preserved fast read mode');
69 $DB->get('A', $fval);
70 #Dump($fval);
71 TODO: {
72 local $TODO = 'End of Txn should invalidate fastmode magic vars';
73 ok(!defined($val), 'Was invalidated');
74 # Commented until fixed ZeroCopy
75 #isnt_zerocopy($fval, $val, 'New Txn, so different buffer');
76 }
77 my $count = $fval =~ tr/5/5/;
78 is($count, 100_000, 'fives');
79 throws_ok {
80 $count = $fval =~ tr/5/E/;
81 } qr/read-only/, 'Is RO';
82 }
83 {
84 # Change environment to MDB_WRITEMAP.
85 my $env = LMDB::Env->new($dir, { flags => MDB_WRITEMAP });
86 ok(my $DB = $env->BeginTxn->OpenDB, 'Open unamed in WM');
87 is($DB->ReadMode(1), 0, 'No fast read mode preserved');
88 $DB->get('A', my $fval);
89 #Dump($fval);
90 # According to cpantesters on Mac OS X and Debian's libc-2.3.6 the
91 # same address for mmap is used!!
92 #isnt_zerocopy($fval, $val, 'New Env, so diferent buffer');
93 $DB->get('A', $val);
94 for($fval) { # A twist
95 #Dump($_);
96 is($_, $large1, 'The value is there');
97 my $count;
98 lives_ok {
99 $count = tr/5/E/;
100 } 'Now R/W!';
101 is($count, 100_000, 'fives replaced');
102 is(tr/5/5/, 0, 'No more fives');
103 #is_zerocopy($_, $val, 'Same buffer yet');
104 isnt($_, $large1, 'The value has changed');
105 { # Test for warning when indirect writes
106 my $warn;
107 local $SIG{__WARN__} = sub { $warn = shift };
108 $_ = 'Z' x 9;
109 like($warn, qr/not recommended/, 'Warn emited');
110 is(length, 1_000_000, 'Length unchanged');
111 $warn = '';
112 is(s/123/ABC/g, 99_999, 'Lots changed');
113 is($warn, '', 'No warn emited');
114 # Try to extend/grow
115 $_ .= 'hi';
116 like($warn, qr/Truncating/, 'Truncating warning emited');
117 is(length, 1_000_000, 'Length unchanged');
118 is(substr($_, -1, 1), '9', 'Last byte is 9');
119 }
120 #is_zerocopy($_, $val, 'Same buffer yet');
121 }
122 SKIP: {
123 skip 'Need ASCII platform', 1 unless 65 == ord 'A';
124 for(substr($fval, 20, 9)) {
125 $_ |= ('p' x 9);
126 is(uc, 'PQRSTUVWX', 'Bits changed');
127 }
128 }
129 lives_ok {
130 $DB->Txn->commit();
131 } 'Changes commited';
132 isnt($DB->Alive, 'Because Txn terminated');
133
134 ($DB->Txn = $env->BeginTxn)->AutoCommit(1); #Nice hack
135 is($DB->Alive, 1, 'Alive again');
136 is($DB->ReadMode, 1, 'Preserved');
137 $DB->get('A', $fval);
138 is($fval =~ tr/A/A/, 99_998, 'Changes preserved');
139 $fval =~ s/9/\n/g;
140 {
141 local $LMDB_File::DEBUG = 1;
142 my $warn; local $SIG{__WARN__} = sub { $warn .= shift };
143 $DB->Txn = undef; # Commit, another hack
144 like($warn, qr/commiting/, 'In AutoCommit');
145 like($warn, qr/commited/, 'Commited');
146 }
147 $DB->Txn = $env->BeginTxn(MDB_RDONLY);
148 SKIP: {
149 skip 'Need perlio and PerlIO::scalar', 6
150 unless PerlIO::Layer->find('perlio') &&
151 $Config{'extensions'} =~ m|PerlIO/scalar|;
152
153 # Try a nice trick
154 require IO::File;
155 my $io = IO::File->new($DB->Rget('A'), 'r');
156 isa_ok($io, 'IO::File');
157 is($io->getline, 'Z' x 9 . "\n");
158 is($io->getline, "0ABC4E678\n");
159 is($io->getline, "pqrstuvwx\n");
160 my($c, $l) = (3, 30); #So far
161 while(<$io>) { $c++; $l += length; }
162 is($c, 100_000, 'All read');
163 is($l, 1_000_000, 'Complete');
164 }
165 }
166
167
168 END {
169 unless($ENV{KEEP_TMPS}) {
170 for($dir) {
171 unlink glob("$_/*");
172 rmdir $_;
173 #warn "Removed $_\n";
174 }
175 }
176 }