--- loncom/debugging_tools/rebuild_db_from_hist.pl 2006/08/03 17:53:47 1.6 +++ loncom/debugging_tools/rebuild_db_from_hist.pl 2016/01/31 21:26:01 1.7 @@ -4,7 +4,7 @@ # # rebuild_db_from_hist.pl Rebuild a *.db file from a *.hist file # -# $Id: rebuild_db_from_hist.pl,v 1.6 2006/08/03 17:53:47 albertel Exp $ +# $Id: rebuild_db_from_hist.pl,v 1.7 2016/01/31 21:26:01 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -125,13 +125,14 @@ sub process_file { # D:delete # 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); if ($fname eq 'roles.hist' && $concatenated_data =~ /^.*:.*:/) { (undef,undef,$concatenated_data) = split(':',$concatenated_data,3); } next if (! defined($action)); if ($action eq 'P' && $p_is_s) { $action = 'S'; } - my ($rid,@allkeys,$version); + my ($rid,@allkeys,$version,$updatetype); if ($action eq 'S') { ($rid,$concatenated_data) = split(':',$concatenated_data,2); $version = ++$db_to_store{"version:$rid"}; @@ -141,6 +142,10 @@ sub process_file { ($rid,$version,$concatenated_data) = split(':',$concatenated_data,3); } + if ($action eq 'U') { + ($updatetype,$concatenated_data) = + split(':',$concatenated_data,2); + } next if (! defined($concatenated_data)); my $add_new_data = 1; my @data = split('&',$concatenated_data); @@ -163,6 +168,28 @@ sub process_file { } } elsif ($action eq 'D') { 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 { $error = "Unable to understand action '".$action."'"; }