summaryrefslogtreecommitdiff
path: root/fml/lib/FML/Cache/Ring.pm
blob: 0764373dc00b906e42525bd8399a383eb4e77eff (plain)
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#-*- perl -*-
#
#  Copyright (C) 2004,2005,2006 Ken'ichi Fukamachi
#   All rights reserved. This program is free software; you can
#   redistribute it and/or modify it under the same terms as Perl itself.
#
# $FML: Ring.pm,v 1.6 2005/08/17 10:51:01 fukachan Exp $
#

package FML::Cache::Ring;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;
use IO::File;
use File::Spec;


=head1 NAME

FML::Cache::Ring - IO operations to ring buffer which consists of files.

=head1 SYNOPSIS

   ... lock e.g. by flock(2) ...

   use FML::Cache::Ring;
   $obj = new FML::Cache::Ring {
       directory          => '/some/where' # mandatory
       sequence_file_name => '.seq',       # optional
       modulus            => 128,          # optional
   };
   $fh = $obj->open;
   print $fh "some message";
   $fh->close;

   ... unlock ...

The buffer directory has files with the numeric name C<0>, C<1>, ...
You can specify C<file_name> parameter.

   $obj = new FML::Cache::Ring {
       directory => '/some/where',
       file_name => '_smtplog',
   };

If specified, the file names become _smtplog.0, _smtplog.1, ...

The cache data is limited by the number of files, so approximately
size by default. 

Instead of number fo files, you can limit FML::Cache::Ring based on
time. It is time based expiretion. To specify based expiration, use
new() like this:

   $obj = new FML::Cache::Ring {
       directory  => '/some/where',
       cache_type => 'temporal',
       expires_in => 90,             # 90 days
   };

where C<cache_type> is C<cyclic> by default.


=head1 DESCRIPTION

To log messages up to some limit,
it may be useful to use filenames in cyclic way.
The file to write is chosen among a set of files allocated as a buffer.

Consider several files under a directory C<ring/> as a ring buffer
where the unit of the ring is 5 here.
C<ring/> may have 5 files in it.

   0 1 2 3 4

To log a message is to write it to one of them.
At the first time the message is logged to the file C<0>,
and next time to C<1> and so on.
If all 5 files are used,
this module reuses and overwrites the oldest one C<0>.
So we use a file in cyclic way as follows:

   0 -> 1 -> 2 -> 3 -> 4 -> 0 -> 1 -> ...

We expire the old data.
A file name is a number for simplicity.
The latest number is holded in C<ring/.seq> file (C<.seq> in that
direcotry by default) and truncated to 0 by the modulo C<5>.

=head1 METHODS

=head2 new(args)

$args hash can take the following arguments:

	variable		default value
	--------------------------------
	directory 		.
	file_name 		""
	sequence_file_name 	.seq
	modulus 		128
	cache_type 		cyclic
	dir_mode 		0755

=cut


@ISA = qw(IO::File);

BEGIN {}
END   {}


# Descriptions: constructor.
#               forward new() request to superclass (IO::File).
#    Arguments: OBJ($self) HASH_REF($args)
# Side Effects: none
# Return Value: OBJ (blessed as a file handle).
sub new
{
    my ($self, $args) = @_;
    my ($type) = ref($self) || $self;
    my $me     = {};

    _take_file_name($me, $args);

    return bless $me, $type;
}


# Descriptions: determine the file name to write into.
#    Arguments: OBJ($self) HASH_REF($args)
# Side Effects: increment $sequence_file_name
#               set the file name at ${*$self}{ _file }
# Return Value: none
sub _take_file_name
{
    my ($self, $args)      = @_;
    my $sequence_file_name = $args->{ sequence_file_name } || '.seq';
    my $directory          = $args->{ directory }  || '.';
    my $filename_prefix    = $args->{ file_name }  || '';
    my $modulus            = $args->{ modulus }    || 128;
    my $cache_type         = $args->{ cache_type } || 'cyclic';
    my $dir_mode           = $args->{ dir_mode }   || 0755;
    my $file;

    unless (-d $directory) {
	use File::Path;
	mkpath( [ $directory ], 0, $dir_mode);
    }

    if ($cache_type eq 'temporal') {
	my ($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime(time);
	my $filename = sprintf("%04d%02d%02d", 1900+$year, $mon+1, $mday);
	$file = File::Spec->catfile($directory, $filename);
    }
    elsif ($cache_type eq 'cyclic') {
	my $seq_file = File::Spec->catfile($directory, $sequence_file_name);

	use IO::Adapter;
	my $io = new IO::Adapter $seq_file;
	my $id = $io->sequence_increment();

	# the sequence $id is already incremetd.
	my $saved_id = $id;

	# check if $id is rolled over or not.
	$id = $id % $modulus;
	if ($saved_id != $id) {
	    $io->sequence_replace($id);
	}

	# file.
	$file = File::Spec->catfile($directory,
				    sprintf("%s%s", $filename_prefix, $id));
    }

    $self->{ _cache_type } = $cache_type || 'cyclic';
    $self->{ _cache_data } = {};
    $self->{ _directory }  = $directory  || '';
    $self->{ _file }       = $file       || '';
}


# Descriptions: return the path of file to be written.
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: STR
sub cache_file_path
{
    my ($self) = @_;

    return( $self->{ _opened_file } || '');
}


=head2 open(file, mode)

open file in the buffer.
The target file is already determined when our constructor runs.

=cut


# Descriptions: open() cache file in the buffer.
#    Arguments: OBJ($self) STR($file) STR($mode)
#               XXX $self is blessed file handle.
# Side Effects: create ${ *$self } hash to save status information.
# Return Value: HANDLE(write file handle for $file.new.$$)
sub open
{
    my ($self, $file, $mode) = @_;
    $file = defined $file ? $file : $self->{ _file };
    $mode = defined $mode ? $mode :
	($self->{ _cache_type } eq 'temporal' ? "a+" : "w+");

    # error
    return undef unless $file;

    # If the cache is limited by "time", we only add values to the file.
    # If limited by space, we ovewrite the file, so open it by the mode "w".
    my $fh = new IO::File;

    # real open with $mode
    if (defined $fh) {
	$self->{ _opened_file } = $file;
	$fh->open($file, $mode);
	$fh->autoflush(1);
	$self->{ _fh } = $fh;
	return $fh;
    }
    else {
	return undef;
    }
}


=head2 close()

no argument.

=cut


# Descriptions: forward close() to SUPER class
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: same as SUPER::close() or UNDEF
sub close
{
    my ($self) = @_;
    my $fh = $self->{ _fh };
    defined $fh ? $fh->close() : undef;
}


=head2 add($args)

import data into cache from file.
Actually, link(2) $src to cache file.

    KEY        VALUE
    ---------------------------
    file       STR
    try_link   STR ( yes | no )

=cut


# Descriptions: import data from file.
#    Arguments: OBJ($self) HASH_REF($args)
# Side Effects: create $dst file (linked).
# Return Value: NUM(1(success) or 0(fail))
sub add
{
    my ($self, $args) = @_;
    my $link = $args->{ try_link }      || "yes";
    my $src  = $args->{ file }          || '';
    my $dst  = $self->cache_file_path() || '';

    unless ($src) { return 0;}
    unless ($dst) {
	$self->open();
	$dst = $self->cache_file_path() || '';
    }

    if ($dst && $src) {
	return 0 unless -f $src;
	unlink $dst  if -f $dst;
	if (link($src, $dst)) {
	    return 1;
	}
	else {
	    return 0;
	}
    }

    return 0;
}


=head2 get(key)

get value (latest value in the ring buffer) for the key.

=cut


# Descriptions: get value (latest value in the ring buffer) for the key.
#    Arguments: OBJ($self) STR($key)
# Side Effects: none
# Return Value: STR
sub get
{
    my ($self, $key) = @_;
    $self->get_latest_value($key);
}


# Descriptions: get value (latest value in the ring buffer) for the
#               specified key.
#    Arguments: OBJ($self) STR($key)
# Side Effects: none
# Return Value: STR
sub get_latest_value
{
    my ($self, $key) = @_;

    # cheap sanity.
    return '' unless defined $key;

    # 1. return matched value if found in the latest cache.
    my $file = $self->{ _file };
    my $buf  = $self->_search($file, $key);
    return $buf if $buf;

    # 2. if not found in the latest cache, search cacheses in the
    #    cache directory in reverse temporal order.
    my $dir = $self->{ _directory };
    return '' unless $dir;

    my $dh = new IO::Handle;
    opendir($dh, $dir);

    my @dh = ();
    for my $entry (readdir($dh)) { push(@dh, $entry) if $entry =~ /^\d+/o;}
    @dh = sort { $b <=> $a } @dh;

  DIR_ENTRY:
    for my $_dirent (@dh) {
	next DIR_ENTRY if $_dirent =~ /^\./o;
	next DIR_ENTRY if $_dirent !~ /^\d/o;

	# XXX-TODO: rule ignoring /^\d{1,2}$/ is correct ?
	next DIR_ENTRY if $_dirent =~ /^\d{1,2}$/;

	$file = File::Spec->catfile($dir, $_dirent);
	$buf  = $self->_search($file, $key);

	last DIR_ENTRY if $buf;
    }
    closedir($dh) if defined $dh;

    return $buf;
}


# Descriptions: search value for $key in the $file.
#    Arguments: OBJ($self) STR($file) STR($key)
# Side Effects: none
# Return Value: STR
sub _search
{
    my ($self, $file, $key) = @_;
    my $hash = $self->{ _cache_data };
    my $pkey = quotemeta( substr($key, 0, 1) );
    my $buf  = '';

    # simple check
    return '' unless defined $file;
    return '' unless $file;

    # XXX-TODO: negative cache is needed ?
    # XXX-TODO: when negative cache is expired ? this code is correct ?
    return '' if defined $hash->{ $file };
    $hash->{ $file } = 1;

    my $fh = $self->open($file, "r");

    if (defined $fh) {
	my $x;

      ENTRY:
	while ($x = $fh->getline) {
	    next ENTRY unless $x =~ /^$pkey/;

	    if ($x =~ /^$key\s+/ || $x =~ /^$key$/) {
		chomp $x;
		my ($k, $v) = split(/\s+/, $x, 2);
		$buf = $v;
	    }
	}
	$self->close;
    }

    return $buf;
}


=head2 find(key)

get value (latest value in the ring buffer) for key.
same as get() now.

=cut


# Descriptions: get value (latest value in the ring buffer) for key.
#    Arguments: OBJ($self) STR($key)
# Side Effects: none
# Return Value: STR
sub find
{
    my ($self, $key) = @_;
    $self->get($key);
}


=head2 set(key, value)

set value for key.

=cut


# Descriptions: set value for key.
#    Arguments: OBJ($self) STR($key) STR($value)
# Side Effects: none
# Return Value: same as close()
sub set
{
    my ($self, $key, $value) = @_;
    my $fh = $self->open;

    if (defined $fh) {
	printf $fh "%s\t%s\n", $key, $value;
	$self->close;
    }
}


#
# debug
#
if ($0 eq __FILE__) {
    my $tmp_dir = "/tmp/cachedir";

    unless (-d $tmp_dir) {
	eval q{
	    use File::Path;
	    mkpath( [ $tmp_dir ], 0, 0755);
	};
    }
    my $cache = new FML::Cache::Ring { directory => $tmp_dir };
    $cache->set(time, time);

    print STDERR "see $tmp_dir\n";
}


=head1 TODO

Export core parts of this module to another putlic class e.g.
File::SOMETHING outside FML::* classes, again.

=head1 CODING STYLE

See C<http://www.fml.org/software/FNF/> on fml coding style guide.

=head1 AUTHOR

Ken'ichi Fukamachi

=head1 COPYRIGHT

Copyright (C) 2004,2005,2006 Ken'ichi Fukamachi

All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.

=head1 HISTORY

FML::Cache::Ring first appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.

FML::Cache::Ring is renamed from File::CacheDir in 2004.

=cut


1;