summaryrefslogtreecommitdiff
path: root/fml/lib/Tie
diff options
context:
space:
mode:
authorfukachan <fukachan>2004-01-24 09:03:55 +0000
committerfukachan <fukachan>2004-01-24 09:03:55 +0000
commitff05d890ce1e70329fff09e207acd4d11740090f (patch)
treea7d4024ed2867e73e5f94b0b696fa4b1633785fa /fml/lib/Tie
parent22441065efdc239bc5533152f7fc2a61623916c7 (diff)
downloadfml8-ff05d890ce1e70329fff09e207acd4d11740090f.tar.gz
fml8-ff05d890ce1e70329fff09e207acd4d11740090f.tar.bz2
fml8-ff05d890ce1e70329fff09e207acd4d11740090f.zip
overhaul from FNF point view.
o fix comments. o fix loop label name. o more correct initialization if could. o use //o;
Diffstat (limited to 'fml/lib/Tie')
-rw-r--r--fml/lib/Tie/JournaledDir.pm38
-rw-r--r--fml/lib/Tie/JournaledFile.pm44
2 files changed, 45 insertions, 37 deletions
diff --git a/fml/lib/Tie/JournaledDir.pm b/fml/lib/Tie/JournaledDir.pm
index 86645d44..e41ff4e1 100644
--- a/fml/lib/Tie/JournaledDir.pm
+++ b/fml/lib/Tie/JournaledDir.pm
@@ -1,10 +1,10 @@
#-*- perl -*-
#
-# Copyright (C) 2001,2002,2003 Ken'ichi Fukamachi
+# Copyright (C) 2001,2002,2003,2004 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: JournaledDir.pm,v 1.20 2003/07/21 09:40:31 fukachan Exp $
+# $FML: JournaledDir.pm,v 1.21 2003/08/23 04:35:49 fukachan Exp $
#
package Tie::JournaledDir;
@@ -94,7 +94,7 @@ use Tie::JournaledFile;
my $debug = 0;
-# Descriptions: constructor
+# Descriptions: constructor.
# Arguments: OBJ($self) HASH_REF($args)
# $args = {
# dir => directory path,
@@ -109,10 +109,13 @@ sub new
my ($type) = ref($self) || $self;
my $me = {};
- my $dir = $args->{ 'dir' };
+ my $dir = $args->{ 'dir' } || '';
my $unit = $args->{ 'unit' } || 'day'; # 1 day
my $limit = $args->{ 'limit' } || 90;
+ # sanity.
+ unless ($dir) { croak("dir unspecified");}
+
# reverse order file list to search
my @filelist = ();
for (my $i = 0; $i < $limit; $i++) {
@@ -135,7 +138,7 @@ sub _file_name
my ($unit, $dir, $i) = @_;
my $fn = '';
- if ($unit =~ /^\d+$/) {
+ if ($unit =~ /^\d+$/o) {
$fn = $unit * int(time / $unit) - ($i * $unit);
}
elsif ($unit eq 'day') {
@@ -152,7 +155,7 @@ sub _file_name
}
-# Descriptions: call new()
+# Descriptions: call new().
# Arguments: OBJ($self) HASH_REF($args)
# Side Effects: same as new()
# Return Value: OBJ
@@ -175,18 +178,18 @@ sub FETCH
my $files = $self->{ '_files' } || [];
my $x = '';
- FILES_LOOP:
+ FILE:
for my $f (@$files) {
if (-f $f) {
# XXX reverse order: firstly, try last match in the latest file.
my $obj = new Tie::JournaledFile {
'match_condition' => 'last',
- 'file' => $f,
+ 'file' => $f,
};
if (defined $obj) {
$x = $obj->FETCH($key);
- last FILES_LOOP if defined $x;
+ last FILE if defined $x;
}
}
}
@@ -207,14 +210,14 @@ sub STORE
my $obj = new Tie::JournaledFile {
'match_condition' => 'last',
- 'file' => $f,
+ 'file' => $f,
};
return $obj->STORE($key, $value);
}
-# Descriptions: generate and return HASH_REF on memory over all data
+# Descriptions: generate and return HASH_REF on memory over all data.
# Arguments: OBJ($self)
# Side Effects: generate hash on memory
# Return Value: HASH_REF
@@ -233,7 +236,7 @@ sub __gen_hash
tie %db, 'Tie::JournaledFile', {
'match_condition' => 'last',
- 'file' => $f,
+ 'file' => $f,
};
# XXX overwrite { key => value } for normal time order.
@@ -256,7 +259,7 @@ sub __gen_hash
sub FIRSTKEY
{
my ($self) = @_;
- my $hash = $self->__gen_hash();
+ my $hash = $self->__gen_hash();
if (defined $hash) {
$self->{ _hash } = $hash;
@@ -300,13 +303,14 @@ sub EXISTS
}
-# Descriptions: delete $key
+# Descriptions: delete $key.
# Arguments: OBJ($self) STR($key)
# Side Effects: update cache.
# Return Value: none
sub DELETE
{
my ($self, $key) = @_;
+
$self->STORE($key, '');
}
@@ -354,11 +358,13 @@ sub get_all_values_as_hash_ref
my $files = $self->{ '_files' } || [];
my $result = {};
+ # read files in reverse order to overwrite values by the latest one
+ # from old to new.
use FileHandle;
for my $f (reverse @$files) {
my $obj = new Tie::JournaledFile {
'match_condition' => 'last',
- 'file' => $f,
+ 'file' => $f,
};
my $hash = $obj->get_all_values_as_hash_ref();
@@ -393,7 +399,7 @@ Ken'ichi Fukamachi
=head1 COPYRIGHT
-Copyright (C) 2001,2002,2003 Ken'ichi Fukamachi
+Copyright (C) 2001,2002,2003,2004 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.
diff --git a/fml/lib/Tie/JournaledFile.pm b/fml/lib/Tie/JournaledFile.pm
index 8690b919..57bf5896 100644
--- a/fml/lib/Tie/JournaledFile.pm
+++ b/fml/lib/Tie/JournaledFile.pm
@@ -1,10 +1,10 @@
#-*- perl -*-
#
-# Copyright (C) 2001,2002,2003 Ken'ichi Fukamachi
+# Copyright (C) 2001,2002,2003,2004 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: JournaledFile.pm,v 1.29 2003/07/21 09:40:31 fukachan Exp $
+# $FML: JournaledFile.pm,v 1.30 2003/10/15 01:03:41 fukachan Exp $
#
package Tie::JournaledFile;
@@ -82,7 +82,7 @@ standard hash functions.
my $debug = 0;
-# Descriptions: constructor
+# Descriptions: constructor.
# Arguments: OBJ($self) HASH_REF($args)
# Side Effects: import _match_condition into $self
# Return Value: OBJ
@@ -108,7 +108,7 @@ sub new
}
-# Descriptions: tie() operation stars
+# Descriptions: tie() operation stars.
# Arguments: OBJ($self) HASH_REF($args)
# Side Effects: initialize object
# Return Value: OBJ
@@ -121,7 +121,7 @@ sub TIEHASH
}
-# Descriptions: tie() fetch op
+# Descriptions: tie() fetch op.
# Arguments: OBJ($self) STR($key)
# Side Effects: none
# Return Value: STR
@@ -133,7 +133,7 @@ sub FETCH
}
-# Descriptions: tie() store op
+# Descriptions: tie() store op.
# Arguments: OBJ($self) STR($key) STR($value)
# Side Effects: none
# Return Value: STR
@@ -145,15 +145,15 @@ sub STORE
}
-# Descriptions: op for keys() and each()
+# Descriptions: op for keys() and each().
# Arguments: OBJ($self)
# Side Effects: initialize $self->{ _hash }.
# Return Value: ARRAY(STR, STR)
sub FIRSTKEY
{
my ($self) = @_;
- my $file = $self->{ '_file' };
- my $hash = {};
+ my $file = $self->{ '_file' };
+ my $hash = {};
use IO::File;
my $fh = new IO::File $file;
@@ -176,14 +176,14 @@ sub FIRSTKEY
}
-# Descriptions: tie() keys op (next op)
+# Descriptions: tie() keys op (next op).
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: STR
sub NEXTKEY
{
my ($self) = @_;
- my $hash = $self->{ _hash };
+ my $hash = $self->{ _hash };
if (defined $hash) {
return each %$hash;
@@ -216,7 +216,7 @@ which is by default.
# Descriptions: return all key and the values as HASH_REF
-# { key => [ values ] }
+# { key => [ values ] }.
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: HASH_REF
@@ -283,6 +283,7 @@ method. C<last match> by default.
sub find
{
my ($self, $key, $mode) = @_;
+
return $self->_fetch($key, $mode || 'array');
}
@@ -317,12 +318,12 @@ sub _fetch
my ($xkey, $xvalue, $value, @values) = ();
my $buf;
- SEARCH:
+ LINE:
while ($buf = <$fh>) {
- next SEARCH if $buf =~ /^\#*$/o;
- next SEARCH if $buf =~ /^\s*$/o;
- next SEARCH unless $buf =~ /^$prekey/i;
- next SEARCH unless $buf =~ /^$keytrap/i;
+ next LINE if $buf =~ /^\#*$/o;
+ next LINE if $buf =~ /^\s*$/o;
+ next LINE unless $buf =~ /^$prekey/i;
+ next LINE unless $buf =~ /^$keytrap/i;
chomp $buf;
@@ -336,7 +337,7 @@ sub _fetch
if ($mode eq 'scalar') {
# firstmatch: exit loop ASAP if the $key is found.
if ($self->{ '_match_condition' } eq 'first') {
- last SEARCH;
+ last LINE;
}
}
}
@@ -367,11 +368,12 @@ sub _fetch
sub _store
{
my ($self, $key, $value) = @_;
+
$self->_puts(sprintf("%-20s %s", $key, $value));
}
-# Descriptions: append given string to cache file
+# Descriptions: append given string to cache file.
# Arguments: OBJ($self) STR($string)
# Side Effects: update cache file
# Return Value: 1 or throw exception by croak()
@@ -387,7 +389,7 @@ sub _puts
if (defined $string) {
$fh->print($string);
- $fh->print("\n") unless $string =~ /\n$/;
+ $fh->print("\n") unless $string =~ /\n$/o;
}
$fh->close;
@@ -408,7 +410,7 @@ Ken'ichi Fukamachi
=head1 COPYRIGHT
-Copyright (C) 2001,2002,2003 Ken'ichi Fukamachi
+Copyright (C) 2001,2002,2003,2004 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.