| Next | Lightweight Databases | 63 |
You can use an in-memory hash as an index into a Tie::File file
Here, %index maps usernames to record numbers:
my %index;
my $NEXT_UNREAD = 0;
sub find_user {
my ($file, $user) = @_;
my $rec;
until (exists $index{$user}) {
$rec = $file->[$NEXT_UNREAD];
return unless defined $rec;
my ($u) = unpack "A8", $rec;
$index{$u} = $NEXT_UNREAD;
$NEXT_UNREAD++;
}
return unpack "A8 A5 A13 A20 A18", $file->[$index{$user}];
}
Each time this is called, it checks the index for the user you asked for
If the user is there, it uses Tie::File to retrieve the data quickly
If not, it scans the file until it finds what you wanted
| Next | ![]() |
Copyright © 2003 M. J. Dominus |