version 1.5, 2006/08/03 17:27:48
|
version 1.7, 2016/01/31 21:26:01
|
Line 38 use Apache::lonnet;
|
Line 38 use Apache::lonnet;
|
|
|
# |
# |
# Options |
# Options |
my ($help,$debug,$test,$p_is_s); |
my ($help,$debug,$test,$test_db,$p_is_s); |
GetOptions("help" => \$help, |
GetOptions("help" => \$help, |
"debug" => \$debug, |
"debug" => \$debug, |
"test" => \$test, |
"test" => \$test, |
"p_is_s" => \$p_is_s); |
"create_test_db" => \$test_db, |
|
"p_is_s" => \$p_is_s); |
|
|
if (! defined($debug)) { $debug = 0; } |
if (! defined($debug)) { $debug = 0; } |
if (! defined($test)) { $test = 0; } |
if (! defined($test)) { $test = 0; } |
Line 57 Options:
|
Line 58 Options:
|
-debug Output debugging code (not much is output yet) |
-debug Output debugging code (not much is output yet) |
-test Verify the given *.hist file will reconstruct the current db file |
-test Verify the given *.hist file will reconstruct the current db file |
Sends error messages to STDERR. |
Sends error messages to STDERR. |
|
-create_test_db |
|
when testing also create a *.db.test db of the testing info |
-p_is_s Treat 'P' lines as 'S' lines. |
-p_is_s Treat 'P' lines as 'S' lines. |
Examples: |
Examples: |
rebuild_db_from_hist.pl -t $file.hist # Perform a test rebuild |
rebuild_db_from_hist.pl -t $file.hist # Perform a test rebuild |
Line 76 while (my $fname = shift) {
|
Line 79 while (my $fname = shift) {
|
my $db_filename = $fname; |
my $db_filename = $fname; |
$db_filename =~ s/\.hist$/\.db/; |
$db_filename =~ s/\.hist$/\.db/; |
if (-e $db_filename && ! $test) { |
if (-e $db_filename && ! $test) { |
print "Aborting: The target file $db_filename exists.".$/; |
print STDERR "Aborting: The target file $db_filename exists.".$/; |
next; |
next; |
} |
} |
my ($error,$constructed_hash) = &process_file($fname,$db_filename,$debug); |
my ($error,$constructed_hash) = &process_file($fname,$db_filename,$debug); |
Line 86 while (my $fname = shift) {
|
Line 89 while (my $fname = shift) {
|
if (! defined($error) || ! $test) { |
if (! defined($error) || ! $test) { |
$error = &write_hash($db_filename,$constructed_hash); |
$error = &write_hash($db_filename,$constructed_hash); |
} |
} |
if ($test) { |
if ($test && $test_db) { |
$error = &write_hash($db_filename.'.test',$constructed_hash); |
$error = &write_hash($db_filename.'.test',$constructed_hash); |
} |
} |
if ($test) { |
if ($test) { |
my $error = &test_hash($db_filename,$constructed_hash); |
my $error = &test_hash($db_filename,$constructed_hash); |
if (defined($error)) { |
if (defined($error)) { |
print "Error processing ".$fname.$/; |
print "Error processing ".$fname.$/; |
print $error; |
print STDERR $error; |
} else { |
} else { |
print "Everything looks good for ".$fname.$/; |
print "Everything looks good for ".$fname.$/; |
} |
} |
Line 121 sub process_file {
|
Line 124 sub process_file {
|
# S:store |
# S:store |
# D:delete |
# D:delete |
# N:new put (only adds tha values if they are all new values) |
# N:new put (only adds tha values if they are all new values) |
|
# M:modify the values for a previous S |
|
# U:update the values (action could be add or del). |
my ($action,$time,$concatenated_data) = split(':',$command,3); |
my ($action,$time,$concatenated_data) = split(':',$command,3); |
if ($fname eq 'roles.hist' && $concatenated_data =~ /^.*:.*:/) { |
if ($fname eq 'roles.hist' && $concatenated_data =~ /^.*:.*:/) { |
(undef,undef,$concatenated_data) = split(':',$concatenated_data,3); |
(undef,undef,$concatenated_data) = split(':',$concatenated_data,3); |
} |
} |
next if (! defined($action)); |
next if (! defined($action)); |
if ($action eq 'P' && $p_is_s) { $action = 'S'; } |
if ($action eq 'P' && $p_is_s) { $action = 'S'; } |
my ($rid,$allkeys,$version); |
my ($rid,@allkeys,$version,$updatetype); |
if ($action eq 'S') { |
if ($action eq 'S') { |
($rid,$concatenated_data) = split(':',$concatenated_data,2); |
($rid,$concatenated_data) = split(':',$concatenated_data,2); |
$version = ++$db_to_store{"version:$rid"}; |
$version = ++$db_to_store{"version:$rid"}; |
#print $version.$/; |
#print $version.$/; |
} |
} |
|
if ($action eq 'M') { |
|
($rid,$version,$concatenated_data) = |
|
split(':',$concatenated_data,3); |
|
} |
|
if ($action eq 'U') { |
|
($updatetype,$concatenated_data) = |
|
split(':',$concatenated_data,2); |
|
} |
next if (! defined($concatenated_data)); |
next if (! defined($concatenated_data)); |
my $add_new_data = 1; |
my $add_new_data = 1; |
my @data = split('&',$concatenated_data); |
my @data = split('&',$concatenated_data); |
Line 144 sub process_file {
|
Line 157 sub process_file {
|
} else { |
} else { |
$no_action_count++; |
$no_action_count++; |
} |
} |
} elsif ($action eq 'S') { |
} elsif ($action eq 'S' || $action eq 'M') { |
# Versioning of data, so we update the old ata |
# Versioning of data, so we update the old ata |
$allkeys.=$key.':'; |
push(@allkeys,$key); |
$db_to_store{"$version:$rid:$key"}=$value; |
$db_to_store{"$version:$rid:$key"}=$value; |
} elsif ($action eq 'N') { |
} elsif ($action eq 'N') { |
if (exists($db_to_store{$key})) { |
if (exists($db_to_store{$key})) { |
Line 155 sub process_file {
|
Line 168 sub process_file {
|
} |
} |
} elsif ($action eq 'D') { |
} elsif ($action eq 'D') { |
delete($db_to_store{$key}); |
delete($db_to_store{$key}); |
|
} elsif ($action eq 'U') { |
|
if ($updatetype eq 'del') { |
|
if (exists($db_to_store{$key})) { |
|
my %current; |
|
map { $current{$_} = 1; } split(/,/,&unescape($db_to_store{$key})); |
|
map { delete($current{$_}); } split(/,/,&unescape($value)); |
|
if (keys(%current)) { |
|
$db_to_store{$key}=&escape(join(',',sort(keys(%current)))); |
|
} else { |
|
delete($db_to_store{$key}); |
|
} |
|
} |
|
} elsif ($updatetype eq 'add') { |
|
if (exists($db_to_store{$key})) { |
|
my @newvals = split(/,/,&unescape($value)); |
|
my @currvals = split(/,/,&unescape($db_to_store{$key})); |
|
my @merged = sort(keys(%{{map { $_ => 1 } (@newvals,@currvals)}})); |
|
$db_to_store{$key}=&escape(join(',',@merged)); |
|
} else { |
|
$db_to_store{$key}=$value; |
|
} |
|
} |
} else { |
} else { |
$error = "Unable to understand action '".$action."'"; |
$error = "Unable to understand action '".$action."'"; |
} |
} |
} |
} |
|
|
if ($action eq 'N' && $add_new_data) { |
if ($action eq 'N' && $add_new_data) { |
foreach my $k_v_pair (@data) { |
foreach my $k_v_pair (@data) { |
my ($key,$value) = split('=',$k_v_pair,2); |
my ($key,$value) = split('=',$k_v_pair,2); |
$db_to_store{$key}=$value; |
$db_to_store{$key}=$value; |
} |
} |
} |
} |
if ($action eq 'S') { |
if ($action eq 'S') { |
$db_to_store{"$version:$rid:timestamp"}=$time; |
$db_to_store{"$version:$rid:timestamp"}=$time; |
$allkeys.='timestamp'; |
push(@allkeys,'timestamp'); |
$db_to_store{"$version:keys:$rid"}=$allkeys; |
} |
|
if ($action eq 'S' || $action eq 'M') { |
|
$db_to_store{"$version:keys:$rid"}=join(':',@allkeys); |
} |
} |
if (defined($error)) { |
if (defined($error)) { |
return ('Error:'.$error.$/,undef); |
return ('Error:'.$error.$/,undef); |