summaryrefslogtreecommitdiff
path: root/fml/lib
diff options
context:
space:
mode:
authorfukachan <fukachan>2003-07-21 09:40:31 +0000
committerfukachan <fukachan>2003-07-21 09:40:31 +0000
commita85de35fc6dcd6c0ade02d04f688483c7c5b9c0b (patch)
treeed4e3547fb16ef7d907c60cc82872467807659a9 /fml/lib
parent7806330320b817d1218283af343de2ec577dfa31 (diff)
downloadfml8-a85de35fc6dcd6c0ade02d04f688483c7c5b9c0b.tar.gz
fml8-a85de35fc6dcd6c0ade02d04f688483c7c5b9c0b.tar.bz2
fml8-a85de35fc6dcd6c0ade02d04f688483c7c5b9c0b.zip
clean up.
change the match conditoin parameter name to "match_condition". You should specify "first" or "last" as match_condition in Tie::JournaledFile::new() method. Tie::JournaledFile: change default match condition to "last match".
Diffstat (limited to 'fml/lib')
-rw-r--r--fml/lib/Tie/JournaledDir.pm10
-rw-r--r--fml/lib/Tie/JournaledFile.pm65
2 files changed, 39 insertions, 36 deletions
diff --git a/fml/lib/Tie/JournaledDir.pm b/fml/lib/Tie/JournaledDir.pm
index c7d7beab..2b778635 100644
--- a/fml/lib/Tie/JournaledDir.pm
+++ b/fml/lib/Tie/JournaledDir.pm
@@ -4,7 +4,7 @@
# 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.18 2003/01/11 15:14:27 fukachan Exp $
+# $FML: JournaledDir.pm,v 1.19 2003/01/11 15:16:37 fukachan Exp $
#
package Tie::JournaledDir;
@@ -180,7 +180,7 @@ sub FETCH
if (-f $f) {
# XXX reverse order: firstly, try last match in the latest file.
my $obj = new Tie::JournaledFile {
- 'last_match' => 1,
+ 'match_condition' => 'last',
'file' => $f,
};
@@ -206,7 +206,7 @@ sub STORE
my $f = $self->{ '_files' }->[ 0 ]; # XXX [0] is the latest file.
my $obj = new Tie::JournaledFile {
- 'last_match' => 1,
+ 'match_condition' => 'last',
'file' => $f,
};
@@ -232,7 +232,7 @@ sub __gen_hash
next FILE unless -f $f;
tie %db, 'Tie::JournaledFile', {
- 'last_match' => 1,
+ 'match_condition' => 'last',
'file' => $f,
};
@@ -357,7 +357,7 @@ sub get_all_values_as_hash_ref
use FileHandle;
for my $f (reverse @$files) {
my $obj = new Tie::JournaledFile {
- 'last_match' => 1,
+ 'match_condition' => 'last',
'file' => $f,
};
my $hash = $obj->get_all_values_as_hash_ref();
diff --git a/fml/lib/Tie/JournaledFile.pm b/fml/lib/Tie/JournaledFile.pm
index ab90bc00..5bc9475e 100644
--- a/fml/lib/Tie/JournaledFile.pm
+++ b/fml/lib/Tie/JournaledFile.pm
@@ -4,7 +4,7 @@
# 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.27 2003/01/11 15:14:27 fukachan Exp $
+# $FML: JournaledFile.pm,v 1.28 2003/02/01 08:48:09 fukachan Exp $
#
package Tie::JournaledFile;
@@ -45,16 +45,25 @@ By default, FETCH() returns the first value with the key.
It meas first match.
use Tie::JournaledFile;
- tie %db, 'Tie::JournaledFile', { first_match => 1, file => 'cache.txt' };
+ tie %db, 'Tie::JournaledFile', {
+ match_condition => "first",
+ file => 'cache.txt',
+ };
print $db{ rudo }, "\n";
-If you print out the latest value for C<$key>, specify C<last_match>.
+If you print out the latest value for C<$key>, specify C<last> by
+match_condition. The value returned is the last matched line with the
+key in the file.
- use Tie::JournaledFile;
- tie %db, 'Tie::JournaledFile', { last_match => 1, file => 'cache.txt' };
- print $db{ rudo }, "\n";
+=head2 WHEN YOU USE "FIRST MATCH" ?
+
+From the view of journalized data, the last written data is valid, so
+"last match" condition is fundamental. When "first match" condition
+can be used ?
+
+The first match is meaningfull only when you need to check the
+existence of the primary key regardless of the vlaue.
-It is the value at the latest line with the key in the file.
=head2 KNOWN BUG
@@ -75,7 +84,7 @@ my $debug = 0;
# Descriptions: constructor
# Arguments: OBJ($self) HASH_REF($args)
-# Side Effects: import _match_style into $self
+# Side Effects: import _match_condition into $self
# Return Value: OBJ
sub new
{
@@ -83,22 +92,16 @@ sub new
my ($type) = ref($self) || $self;
my $me = {};
- $me->{ _file } = $args->{ file };
+ $me->{ _file } = $args->{ file };
+ $me->{ _match_condition } = 'last'; # "last match" by default.
- # define search strategy: first or last match
- if (defined $args->{ 'first_match' } && defined $args->{ 'last_match' }) {
- croak "both first_match and last_match specified\n";
- }
- elsif (defined $args->{ 'first_match' }) {
- $me->{ '_match_style' } = 'first';
- }
- elsif (defined $args->{ 'last_match' }) {
- $me->{ '_match_style' } = 'last';
- }
- else {
- # XXX-TODO: why first match by default?
- # first match by default.
- $me->{ '_match_style' } = 'first';
+ # define search strategy: first or last match.
+ if (defined $args->{ 'match_condition' }) {
+ my $condition = $args->{ 'match_condition' } || 'last';
+
+ if ($condition eq 'first' || $condition eq 'last') {
+ $me->{ '_match_condition' } = $condition;
+ }
}
return bless $me, $type;
@@ -112,7 +115,7 @@ sub new
sub TIEHASH
{
my ($self, $args) = @_;
- $args->{ 'last_match' } = 1;
+ $args->{ 'match_condition' } = 'last';
new($self, $args);
}
@@ -156,9 +159,9 @@ sub FIRSTKEY
my $fh = new IO::File $file;
if (defined $fh) {
my ($k, $v, $buf);
+
while ($buf = <$fh>) {
- # XXX overwritten. it means last match.
- # XXX-TODO: hmm, what imply first match ?
+ # XXX always overwritten. it means "last match" condition.
($k, $v) = split(/\s+/, $buf, 2);
$hash->{ $k } = $v if $k;
}
@@ -266,9 +269,9 @@ sub get_all_values_as_hash_ref
return the array of line(s) with the specified C<key>.
-The line is either first or last mached line.
-The maching strategy is determined by C<last_match> or C<first_match>
-parameter at C<new()> method. C<first_match> by default.
+The line is either first or last mached line. The maching strategy is
+determined by C<match_condition> parameter at C<new()>
+method. C<last match> by default.
=cut
@@ -287,7 +290,7 @@ sub find
# Descriptions: real function to search $key.
# This routine is used at find() and FETCH() methods.
# return the value with the $key
-# $self->{ '_match_style' } conrolls the matching algorithm
+# $self->{ '_match_condition' } conrolls the matching algorithm
# is either of the fist or last match.
# Arguments: OBJ($self) STR($key) STR($mode)
# $key is the string to search.
@@ -332,7 +335,7 @@ sub _fetch
}
if ($mode eq 'scalar') {
# firstmatch: exit loop ASAP if the $key is found.
- if ($self->{ '_match_style' } eq 'first') {
+ if ($self->{ '_match_condition' } eq 'first') {
last SEARCH;
}
}