This file is indexed.

/usr/share/perl5/Cache/BDB.pm is in libcache-bdb-perl 0.04-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package Cache::BDB;

use strict;
use warnings;

use BerkeleyDB;
use Storable;
use File::Path qw(mkpath);

our $VERSION = '0.04';

use constant DEFAULT_DB_TYPE => 'Btree';

#############################
# Construction/Destruction. #
#############################
 
sub new {
    my ($proto, %params) = @_;
    my $class = ref($proto) || $proto;

    die "$class requires Berkeley DB version 3 or greater"
      unless $BerkeleyDB::db_version >= 3;
    
    # can't do anything without at least these params
    die "$class: cache_root not specified" unless($params{cache_root});
    die "$class: namespace not specified" unless($params{namespace});

    my $cache_root = $params{cache_root};
    unless(-d $cache_root) {
      eval {
	mkpath($cache_root, 0, 0777);
      };
      if($@) {
	die "$class: cache_root '$cache_root' unavailable: $@";
      }
    }
 
    my $env = BerkeleyDB::Env->new(
				   -Home => $cache_root,
				   -Flags => 
				   (DB_INIT_CDB | DB_CREATE | DB_INIT_MPOOL),
				   -ErrPrefix => $class,
				   -ErrFile => *STDERR,
				   -SetFlags => 
				   $params{env_lock} ? DB_CDB_ALLDB : 0,
				   -Verbose => 1,
				  ) 
      or die "$class: Unable to create env: $BerkeleyDB::Error";

    my $type = join('::', 'BerkeleyDB', ($params{type} &&
					 ($params{type} eq 'Btree' ||
					  $params{type} eq 'Hash'  ||
					  $params{type} eq 'Recno')) ?
		    $params{type} : DEFAULT_DB_TYPE);

    my $fname =  $params{cache_file} || join('.', $params{namespace}, "db");

    my $db = $type->new(
		      -Env => $env,
		      -Subname => $params{namespace},
		      -Filename => $fname,
		      -Flags => DB_CREATE,
		      # -Pagesize => 8192,
		     );

    # make a second attempt to connect to the db. this should handle
    # the case where a cache was created with one type and connected
    # to again with a different type. should probably just be an
    # error, but just in case ...
    
    unless(defined $db ) {
      $db = BerkeleyDB::Unknown->new(
				     -Env => $env,
				     -Subname => $params{namespace},
				     -Filename => $fname,
				     #-Pagesize => 8192,
				    );
    }
  
    die "$class: Unable to open db: $BerkeleyDB::Error" unless defined $db;
  
    # eventually these should support user defined subs and/or
    # options as well.
    $db->filter_store_value( sub { $_ = Storable::freeze($_) });
    $db->filter_fetch_value( sub { $_ = Storable::thaw($_) });
  
    # sync the db for good measure.
    $db->db_sync();      
        
    my $self = {
		# private stuff
		__env => $env,
		__last_purge_time => time(),
		__type => $type, 
		__db => $db,

		# expiry/purge
		default_expires_in => $params{default_expires_in} || 0,
		auto_purge_interval => $params{auto_purge_interval} || 0,
		auto_purge_on_set => $params{auto_purge_on_set} || 0,
		auto_purge_on_get => $params{auto_purge_on_get} || 0,

		purge_on_init => $params{purge_on_init} || 0,
		purge_on_destroy => $params{purge_on_destroy} || 0,

		clear_on_init => $params{clear_on_init} || 0,
		clear_on_destroy => $params{clear_on_destroy} || 0,

		disable_auto_purge => $params{disable_auto_purge} || 0,

		# file/namespace
		namespace => $params{namespace},
		cache_root => $params{cache_root},

		# options
		disable_compact => $params{disable_compact},

	       };

    bless $self, $class;
    
    $self->clear() if $self->{clear_on_init};
    $self->purge() if $self->{purge_on_init};

    return $self;
}

sub DESTROY {
    my $self = shift;

    $self->clear() if $self->{clear_on_destroy};
    $self->purge() if $self->{purge_on_destroy};

    undef $self->{__db};
    undef $self->{__env};
}

*close = \&DESTROY;

##############################################
# Instance options and expiry configuration. #  
##############################################

sub namespace {
    my $self = shift;
    warn "namespace is read only" if shift;
    return $self->{namespace};
}

sub auto_purge_interval {
    my ($self, $interval) = @_;

    if(defined($interval)) {
	return undef unless $interval =~ /^\d+$/;
	$self->{auto_purge_interval} = $interval;
    }
    return $self->{auto_purge_interval};
}

sub auto_purge_on_set {
    my ($self, $v) = @_;
    if(defined($v)) {
	$self->{auto_purge_on_set} = $v;
    }
    return $self->{auto_purge_on_set};
}

sub auto_purge_on_get {
    my ($self, $v) = @_;
    if(defined($v)) {
	$self->{auto_purge_on_get} = $v;
    }
    return $self->{auto_purge_on_get};
}

#################################################
# Methods for setting and getting cached items. # 
#################################################

sub set {
    my ($self, $key, $value, $ttl) = @_;

    return 0 unless ($key && $value);

    my $db = $self->{__db};
    my $rv;

    my $now = time();

    if($self->{auto_purge_on_set}) {
       my $interval = $self->{auto_purge_interval};    
       if($now > ($self->{__last_purge_time} + $interval)) {
	 $self->purge();
	 $self->{__last_purge_time} = $now;
       }
     }
    
    $ttl ||= $self->{default_expires_in};
    my $expires = ($ttl) ? $now + $ttl : 0;
    
    my $data = {__expires => $expires,
		__set_time => $now, 
		__last_access_time => $now,
		__version => $Cache::BDB::VERSION,
		__data => $value};

    $rv = $db->db_put($key, $data);

    return $rv ? 0 : 1;
}

sub add {
  my ($self, $key, $value, $ttl) = @_;

  return $self->get($key) ? 0 : $self->set($key, $value, $ttl);
}

sub replace {
  my ($self, $key, $value, $ttl) = @_;

  return $self->get($key) ? $self->set($key, $value, $ttl) : 0;
}

sub get {
    my ($self, $key) = @_;

    return undef unless $key;
    my $db = $self->{__db};
    my $t = time();

    my $data;

    if($self->{auto_purge_on_get}) {
      my $interval = $self->{auto_purge_interval};
      if($t > ($self->{__last_purge_time} + $interval)) {
	$self->purge();
	$self->{__last_purge_time} = $t;
      }
    }
    
    my $rv = $db->db_get($key, $data);
    return undef if $rv == DB_NOTFOUND;
    return undef unless $data->{__data};

    if($self->__is_expired($data, $t)) {
      $self->remove($key) unless $self->{disable_auto_purge};
      return undef;
    } 
    # this is pretty slow, leaving it out for now. if i start supporting
    # access time related stuff i'll need to work on it.
    #      $self->_update_access_time($key, $data, $t); 
    
    return $data->{__data};
}

sub get_bulk {
    my $self = shift;
    my $t = time();
    my $count = 0;
    
    my $db = $self->{__db};
    my $cursor = $db->db_cursor();
    
    my %ret;
    my ($k, $v) = ('','');

    while($cursor->c_get($k, $v, DB_NEXT) == 0) {
      my $d = $self->get($k);
      $ret{$k} = $d if $d;
    }
    $cursor->c_close();

    return \%ret;
}

sub _update_access_time {
    my ($self, $key, $data, $t)  = @_;
    
    my $db = $self->{__db};
    $t ||= time();
    $data->{__last_access_time} = $t;

    my $rv = $db->db_put($key, $data);
    
    return $rv;
}

###########################
# Cache meta information. #
###########################

sub count {
    my $self = shift;
    my $total = 0;

    my $db = $self->{__db};
    my $stats = $db->db_stat;
    my $type = $db->type;

    $total =  ($type == DB_HASH) ? 
      $stats->{hash_ndata} : $stats->{bt_ndata};

    return $total;
}

sub size {
    my $self = shift;
    
    my $db = $self->{__db};

    eval { require Devel::Size };
    if($@) {
      warn "size() currently requires Devel::Size";
      return 0;
    }
    else {
      import Devel::Size qw(total_size);
    }
    
    my ($k, $v) = ('','');
    my $size = 0;

    my $cursor = $self->{__db}->db_cursor();
    while($cursor->c_get($k, $v, DB_NEXT) == 0) {
	$size += total_size($v->{__data});
    }

    $cursor->c_close();
    return $size;
}

##############################################
# Methods for removing items from the cache. #
##############################################

sub remove {
    my ($self, $key) = @_;

    my $rv;
    my $v = '';
    my $db = $self->{__db};
    $rv = $db->db_del($key);

    warn "compaction failed!" if $self->_compact();

    return $rv ? 0 : 1;
}

*delete = \&remove;  

sub clear {
    my $self = shift;
    my $rv;

    my $count = 0;
    my $db = $self->{__db};
    $rv = $db->truncate($count);

    warn "compaction failed!" if $self->_compact();

    return $count;
}

sub purge {
    my $self = shift;
    my $t = time();
    my $count = 0;
    
    my $db = $self->{__db};
    my $cursor = $db->db_cursor(DB_WRITECURSOR);

    my ($k, $v) = ('','');
    while($cursor->c_get($k, $v, DB_NEXT) == 0) {
      if($self->__is_expired($v, $t)) {
	$cursor->c_del();
	$count++;
      }
    }
    $cursor->c_close();

    warn "compaction failed!" if $self->_compact();

    return $count;
}

sub __is_expired {
    my ($self, $data, $t) = @_;
    $t ||= time();

    return 1 if($data->{__expires} && $data->{__expires} < $t);
    return 0;
}

sub is_expired {
    my ($self, $key) = @_;

    my $data;
    my $t = time();
    return 0 unless $key;
    my $db = $self->{__db};
    my $rv = $db->db_get($key, $data);

    return 0 unless $data;
    return $self->__is_expired($data, $t);
}

sub _compact {
  my $self = shift;

  my $rv = 0; # assume success, if compact isn't available pretend its cool
  my $db = $self->{__db};
  if($db->can('compact') && 
     $db->type == DB_BTREE && 
     !$self->{disable_compact}) {
    $rv = $db->compact(undef, undef, undef, DB_FREE_SPACE, undef);
  }
  return $rv;
}

1;