=back
+There is also alternative way which can generate C<read> and C<update>
+queries on the fly:
+
+ my $mnt = Fuse::DBI->mount({
+ 'filenames' => 'select id,filename,size,writable from files',
+ 'read' => sub {
+ my ($path,$file) = @_;
+ return( 'select content from files where id = ?', $file->{row}->{id} );
+ },
+ 'update' => sub {
+ my ($path,$file) = @_;
+ return( 'update files set content = ? where id = ?', $file->{row}->{id} );
+ },
+ 'dsn' => 'DBI:Pg:dbname=test_db',
+ 'user' => 'database_user',
+ 'password' => 'database_password',
+ 'invalidate' => sub { ... },
+ });
+
=cut
my $dbh;
# be a problem.
my $fuse_self;
+my $debug = 0;
+
sub mount {
my $class = shift;
my $self = {};
$sth->{'filenames'} = $dbh->prepare($arg->{'filenames'}) || die $dbh->errstr();
- $sth->{'read'} = $dbh->prepare($arg->{'read'}) || die $dbh->errstr();
- $sth->{'update'} = $dbh->prepare($arg->{'update'}) || die $dbh->errstr();
-
-
$self->{'sth'} = $sth;
+ $self->{'dbh'} = $dbh;
$self->{'read_filenames'} = sub { $self->read_filenames };
$self->read_filenames;
- $fuse_self = \$self;
+ foreach my $op (qw/read update/) {
+ if (ref($arg->{ $op }) ne 'CODE') {
+ $self->{ $op . '_ref' } = sub {
+ my $row = shift;
+ return ($arg->{ $op }, $row->{'id'});
+ }
+ } else {
+ $self->{ $op . '_ref' } = $arg->{ $op };
+ }
+ }
+
+ $fuse_self = $self;
Fuse::main(
mountpoint=>$arg->{'mount'},
truncate=>\&e_truncate,
unlink=>\&e_unlink,
rmdir=>\&e_unlink,
- debug=>0,
+ debug=>$debug,
);
exit(0) if ($arg->{'fork'});
if ($self->{'mount'} && $self->is_mounted) {
system "( fusermount -u ".$self->{'mount'}." 2>&1 ) >/dev/null";
+ sleep 1;
if ($self->is_mounted) {
system "sudo umount ".$self->{'mount'} ||
return 0;
}
$SIG{'INT'} = sub {
- if ($fuse_self && $$fuse_self->umount) {
+ if ($fuse_self && $fuse_self->can('umount')) {
print STDERR "umount called by SIG INT\n";
}
};
$SIG{'QUIT'} = sub {
- if ($fuse_self && $$fuse_self->umount) {
+ if ($fuse_self && $fuse_self->can('umount')) {
print STDERR "umount called by SIG QUIT\n";
}
};
$files->{$row->{'filename'}} = {
size => $row->{'size'},
mode => $row->{'writable'} ? 0644 : 0444,
- id => $row->{'id'} || 99,
+ id => $row->{'id'} || undef,
+ row => $row,
};
}
sub read_content {
- my ($file,$id) = @_;
+ my $file = shift || die "need file";
+
+ warn "# read_content($file)\n" if ($debug);
- die "read_content needs file and id" unless ($file && $id);
+ my @args = $fuse_self->{'read_ref'}->($files->{$file});
+ my $sql = shift @args || die "need SQL for $file";
- $sth->{'read'}->execute($id) || die $sth->{'read'}->errstr;
- $files->{$file}->{cont} = $sth->{'read'}->fetchrow_array;
+ $fuse_self->{'read_sth'}->{$sql} ||= $fuse_self->{dbh}->prepare($sql) || die $dbh->errstr();
+ my $sth = $fuse_self->{'read_sth'}->{$sql} || die;
+
+ $sth->execute(@args) || die $sth->errstr;
+ $files->{$file}->{cont} = $sth->fetchrow_array;
# I should modify ctime only if content in database changed
#$files->{$file}->{ctime} = time() unless ($files->{$file}->{ctime});
print "file '$file' content [",length($files->{$file}->{cont})," bytes] read in cache\n";
sub update_db {
- my $file = shift || die;
+ my $file = shift || die "need file";
$files->{$file}->{ctime} = time();
$files->{$file}->{id}
);
- if (!$sth->{'update'}->execute($cont,$id)) {
- print "update problem: ",$sth->{'update'}->errstr;
+ my @args = $fuse_self->{'update_ref'}->($files->{$file});
+
+ my $sql = shift @args || die "need SQL for $file";
+
+ unshift @args, $files->{$file}->{cont} if ($#args == 0);
+
+ warn "## SQL: $sql\n# files->{$file} = ", Dumper($files->{$file}), $/ if ($debug);
+
+ my $sth = $fuse_self->{'update_sth'}->{$sql}
+ ||= $fuse_self->{dbh}->prepare($sql)
+ || die $dbh->errstr();
+
+ if (!$sth->execute(@args)) {
+ print "update problem: ",$sth->errstr;
clear_cont;
return 0;
} else {
if (! $dbh->commit) {
- print "ERROR: commit problem: ",$sth->{'update'}->errstr;
+ print "ERROR: commit problem: ",$sth->errstr;
clear_cont;
return 0;
}
print "updated '$file' [",$files->{$file}->{id},"]\n";
- $$fuse_self->{'invalidate'}->() if (ref $$fuse_self->{'invalidate'});
+ $fuse_self->{'invalidate'}->() if ($fuse_self->can('invalidate'));
}
return 1;
}
# if (exists( $dirs{$file} )) {
# print "unlink '$file' will re-read template names\n";
# print Dumper($fuse_self);
-# $$fuse_self->{'read_filenames'}->();
+# $fuse_self->{'read_filenames'}->();
# return 0;
if (exists( $files->{$file} )) {
print "unlink '$file' will invalidate cache\n";