--- loncom/debugging_tools/db_copy.pl 2006/08/24 22:46:30 1.1 +++ loncom/debugging_tools/db_copy.pl 2007/01/25 18:05:40 1.6 @@ -8,77 +8,88 @@ use GDBM_File; use File::Find; use LONCAPA; use LONCAPA::Configuration; -use Cwd; +use Fcntl qw(:flock); -my $dump_db = './dump_db'; -my $dir = './test'; +my $dump_db = '/home/httpd/perl/debug/dump_db_static_32'; +my $create_db = '/home/httpd/perl/debug/create_db_dynamic_64'; +if (!-x $dump_db || !-x $create_db) { + print("Unable to run needed helper programs\n $dump_db\n $create_db\n."); + exit(-1); +} my %perlvar=%{&LONCAPA::Configuration::read_conf('loncapa.conf')}; +my $do_locks = 1; { - my $straight; sub lock_db { - my ($fname) = @_; - my $dbref; - $fname = &Cwd::abs_path($fname); - if ($fname =~ m/^\Q$perlvar{'lonUsersDir'}\E/) { - $dbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER()); - $straight=0; - } else { - if (tie(my %db,'GDBM_File',$fname,&GDBM_READER(),0640)) { - $dbref = \%db; - } - $straight=1; - } - return $dbref; + my ($fname) = @_; + my $sym; + if ($do_locks) { + open($sym,">>$fname.lock"); + flock($sym,(LOCK_EX)); + } + return $sym; } sub unlock_db { - my ($dbref) = @_; - if ($straight) { - untie($dbref); - } else { - &LONCAPA::locking_hash_untie($dbref); - } + my ($sym) = @_; + if (ref($sym)) { + flock($sym,(LOCK_UN)); + } } } +my $count=0; +sub count_db { + return if ($_!~m/\.db$/); + $count++; +} + +my $done=0; +my $skip=0; +my $starttime; +my $last_100_start_time; sub process_db { return if ($_!~m/\.db$/); - my $file = $_; - my $dbref =&lock_db($file); - print("attempting $dbref\n"); - my %newdb; - my $new_file = $file.'.new'; - if (!tie(%newdb,'GDBM_File',$new_file,&GDBM_WRCREAT(),0640)) { - die("unable to create new db $new_file $! $@"); + if (!-e "$_.old") { + my $file = $_; + my $dbref =&lock_db($file); + #print("attempting $file\n"); + system("$dump_db -f $file|$create_db -f $file.new"); +# print("finishing $dbref\n"); + rename($file,"$file.old"); + rename("$file.new","$file"); + &unlock_db($dbref); + } else { + #print("skip $_\n"); + $skip++; } - open(my $fh,"$dump_db -f $file|"); - while (my $entry = <$fh>) { - chomp($entry); - if ($entry =~/^ERROR:/) { - die($entry); - } - my ($key,$value) = split(' -> ',$entry,2); - $newdb{&unescape($key)} = &unescape($value); + $done++; + if (!($done %100)) { + print("$_\n"); + my $took = time()-$starttime; + my $togo = int(($took/$done) * ($count-$done)); + my $total = $togo+$took; + my $per = ($done-$skip)?$took/($done-$skip):0; + my $last_per = (time()-$last_100_start_time)/100; + printf("%6d (%6d) in %6d, togo %6d, overall %6d, %.3f (each), %.3f\n", + $done,$skip,$took,$togo,$total,$per,$last_per); + $last_100_start_time = time(); } - print("finishing $dbref\n"); - untie(%newdb); - untie($dbref); - system("/bin/mv $file $file.old"); - system("/bin/mv $file.new $file"); - &unlock_db($dbref); } sub main { - find( - { - no_chdir => 1, - wanted => \&process_db, - }, - $dir - #$perlvar->{'lonUsersDir'} - ); + my $dir = $perlvar{'lonUsersDir'}; + print("Doing $dir\n"); + &find({ no_chdir => 1, + wanted => \&count_db, }, + $dir); + print("Found $count db to do\n"); + $last_100_start_time = $starttime = time(); + &find({ no_chdir => 1, + wanted => \&process_db, }, + $dir); + } &main();