--- AnyEvent-MP/MP/Global.pm 2012/03/03 13:07:19 1.50 +++ AnyEvent-MP/MP/Global.pm 2012/03/03 19:43:41 1.51 @@ -63,7 +63,7 @@ our %NODE_REQ; # only in global code -our %GLOBAL_MON; # monitors {family}{"" or key} +our %GLOBAL_MON; # monitors {family} sub other_globals() { grep $_ ne $NODE && node_is_up $_, keys %{ $GLOBAL_DB{"'g"} } @@ -75,19 +75,6 @@ for other_globals; } -sub g_mon_check { - warn "g_mon_check<@_>\n";#d# - - my %node = ( - %{ $GLOBAL_MON{$_[1]}{$_[2]} }, - %{ $GLOBAL_MON{$_[1]}{"" } }, - %{ $GLOBAL_MON{"" }{"" } }, - ); - - snd $_ => g_chg2 => $_[1], $_[2], @_ > 2 ? $_[3] : () - for keys %node; -} - # add/replace a key in the database sub g_add($$$$) { $LOCAL_DBS{$_[0]}{$_[1]}{$_[2]} = @@ -96,29 +83,48 @@ g_broadcast g_add => $_[1] => $_[2] => $_[3] if exists $GLOBAL_SLAVE{$_[0]}; - warn "g_add<@_>\n";#d# - &g_mon_check; + # tell subscribers we have added or replaced a key + snd $_ => g_chg2 => $_[1], $_[2], $_[3] + for keys %{ $GLOBAL_MON{$_[1]} }; } # delete a key from the database sub g_del($$$) { delete $LOCAL_DBS{$_[0]}{$_[1]}{$_[2]}; + delete $LOCAL_DBS{$_[0]}{$_[1]} unless %{ $LOCAL_DBS{$_[0]}{$_[1]} }; g_broadcast g_del => $_[1] => $_[2] if exists $GLOBAL_SLAVE{$_[0]}; delete $GLOBAL_DB{$_[1]}{$_[2]}; + delete $GLOBAL_DB{$_[1]} unless %{ $GLOBAL_DB{$_[1]} }; # could be moved below for # check if other node maybe still has the key, then we don't delete, but add for (values %LOCAL_DBS) { if (exists $_->{$_[1]}{$_[2]}) { - $GLOBAL_DB{$_[1]}{$_[2]} = $_->{$_[1]}{$_[2]}; + my $val = $GLOBAL_DB{$_[1]}{$_[2]} = $_->{$_[1]}{$_[2]}; + + # tell subscribers we have added or replaced a key + snd $_ => g_chg2 => $_[1], $_[2], $val + for keys %{ $GLOBAL_MON{$_[1]} }; + last; } } - warn "g_del<@_>\n";#d# - &g_mon_check; + # tell subscribers key is actually gone + snd $_ => g_chg2 => $_[1], $_[2] + for keys %{ $GLOBAL_MON{$_[1]} }; +} + +# set the whole (node-local) database - previous value must be empty +sub g_set($$) { + my ($node, $db) = @_; + + while (my ($f, $k) = each %$db) { + g_add $node, $f => $_ => delete $k->{$_} + for keys %$k; + } } # delete all keys from a database @@ -134,16 +140,6 @@ delete $LOCAL_DBS{$node}; } -# set the whole (node-local) database - previous value must be empty -sub g_set($$) { - my ($node, $db) = @_; - - while (my ($f, $k) = each %$db) { - g_add $node, $f => $_ => delete $k->{$_} - for keys %$k; - } -} - # gather node databases from slaves # other node wants to make us the master @@ -184,23 +180,27 @@ while (my ($f, $fv) = each %$mon) { delete $GLOBAL_MON{$f}{$_} for keys %$fv; + + delete $GLOBAL_MON{$f} + unless %{ $GLOBAL_MON{$f} }; } } } -# g_mon0 family key - stop monitoring +# g_mon0 family - stop monitoring $NODE_REQ{g_mon0} = sub { - delete $GLOBAL_MON{$_[0]}{$_[1]}{$SRCNODE->{id}}; - delete $GLOBAL_SLAVE{$SRCNODE->{id}}{$_[0]}{$_[1]}; + delete $GLOBAL_MON{$_[0]}{$SRCNODE->{id}}; + delete $GLOBAL_MON{$_[0]} unless %{ $GLOBAL_MON{$_[0]} }; + + delete $GLOBAL_SLAVE{$SRCNODE->{id}}{$_[0]}; }; # g_mon1 family key - start monitoring $NODE_REQ{g_mon1} = sub { - undef $GLOBAL_SLAVE{$SRCNODE->{id}}{$_[0]}{$_[1]}; - undef $GLOBAL_MON{$_[0]}{$_[1]}{$SRCNODE->{id}}; + undef $GLOBAL_SLAVE{$SRCNODE->{id}}{$_[0]}; + undef $GLOBAL_MON{$_[0]}{$SRCNODE->{id}}; - snd $SRCNODE->{id}, g_chg1 => $_[0], $_[1], - defined $_[1] ? $GLOBAL_DB{$_[0]}{$_[1]} : $GLOBAL_DB{$_[0]}; + snd $SRCNODE->{id}, g_chg1 => $_[0], $GLOBAL_DB{$_[0]}; }; #############################################################################