From 941b3327c6fa984e630dea54a7fd1412ece98ffb Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Sat, 29 Feb 2020 01:46:29 -0800 Subject: [PATCH] Factoids: Optimize export_factoids() with get_each() and get_next() --- PBot/Factoids.pm | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/PBot/Factoids.pm b/PBot/Factoids.pm index 8775c689..74bd5df2 100644 --- a/PBot/Factoids.pm +++ b/PBot/Factoids.pm @@ -192,19 +192,20 @@ sub export_factoids { print FILE "\n\n\n"; $table_id++; - foreach my $trigger (sort $self->{factoids}->get_keys($channel)) { - my $trigger_name = $self->{factoids}->get_data($channel, $trigger, '_name'); - if ($self->{factoids}->get_data($channel, $trigger, 'type') eq 'text') { + my $iter = $self->{factoids}->get_each(index1 => $channel, _everything => 1, _sort => 'index1'); + while ((my $factoid = $self->{factoids}->get_next($iter)) != undef) { + my $trigger_name = $self->{factoids}->get_data($factoid->{index1}, $factoid->{index2}, '_name'); + if ($factoid->{type} eq 'text') { $i++; if ($i % 2) { print FILE "\n"; } else { print FILE "\n"; } - print FILE "" . encode_entities($self->{factoids}->get_data($channel, $trigger, 'owner')) . "\n"; - print FILE "" . encode_entities(strftime "%Y/%m/%d %H:%M:%S", localtime $self->{factoids}->get_data($channel, $trigger, 'created_on')) . "\n"; + print FILE "" . encode_entities($factoid->{'owner'}) . "\n"; + print FILE "" . encode_entities(strftime "%Y/%m/%d %H:%M:%S", localtime $factoid->{'created_on'}) . "\n"; - print FILE "" . $self->{factoids}->get_data($channel, $trigger, 'ref_count') . "\n"; + print FILE "" . $factoid->{'ref_count'} . "\n"; - my $action = $self->{factoids}->get_data($channel, $trigger, 'action'); + my $action = $factoid->{'action'}; if ($action =~ m/https?:\/\/[^ ]+/) { $action =~ s/(.*?)http(s?:\/\/[^ ]+)/encode_entities($1) . "http" . encode_entities($2) . "<\/a>"/ge; @@ -213,8 +214,8 @@ sub export_factoids { $action = encode_entities($action); } - if ($self->{factoids}->exists($channel, $trigger, 'action_with_args')) { - my $with_args = $self->{factoids}->get_data($channel, $trigger, 'action_with_args'); + if (defined $factoid->{'action_with_args'}) { + my $with_args = $factoid->{'action_with_args'}; $with_args =~ s/(.*?)http(s?:\/\/[^ ]+)/encode_entities($1) . "http" . encode_entities($2) . "<\/a>"/ge; $with_args =~ s/(.*)<\/a>(.*$)/"$1<\/a>" . encode_entities($2)/e; print FILE "" . encode_entities($trigger_name) . " is $action

with_args: " . encode_entities($with_args) . "\n"; @@ -222,18 +223,18 @@ sub export_factoids { print FILE "" . encode_entities($trigger_name) . " is $action\n"; } - if ($self->{factoids}->exists($channel, $trigger, 'edited_by')) { - print FILE "" . $self->{factoids}->get_data($channel, $trigger, 'edited_by') . "\n"; - print FILE "" . encode_entities(strftime "%Y/%m/%d %H:%M:%S", localtime $self->{factoids}->get_data($channel, $trigger, 'edited_on')) . "\n"; + if (defined $factoid->{'edited_by'}) { + print FILE "" . $factoid->{'edited_by'} . "\n"; + print FILE "" . encode_entities(strftime "%Y/%m/%d %H:%M:%S", localtime $factoid->{'edited_on'}) . "\n"; } else { print FILE "\n"; print FILE "\n"; } - print FILE "" . encode_entities($self->{factoids}->get_data($channel, $trigger, 'ref_user')) . "\n"; + print FILE "" . encode_entities($factoid->{'ref_user'}) . "\n"; - if ($self->{factoids}->exists($channel, $trigger, 'last_referenced_on')) { - print FILE "" . encode_entities(strftime "%Y/%m/%d %H:%M:%S", localtime $self->{factoids}->get_data($channel, $trigger, 'last_referenced_on')) . "\n"; + if (defined $factoid->{'last_referenced_on'}) { + print FILE "" . encode_entities(strftime "%Y/%m/%d %H:%M:%S", localtime $factoid->{'last_referenced_on'}) . "\n"; } else { print FILE "\n"; } @@ -329,7 +330,7 @@ sub find_factoid { } if (not $opts{exact_channel}) { - foreach my $factoid ($self->{factoids}->get(index2 => $keyword, index1 => undef, action => undef)) { + foreach my $factoid ($self->{factoids}->get_all(index2 => $keyword, index1 => undef, action => undef)) { $channel = $factoid->{index1}; $trigger = $keyword; @@ -370,10 +371,10 @@ sub find_factoid { my @factoids; if ($opts{exact_channel}) { - @factoids = $self->{factoids}->get(type => 'regex', index1 => $channel, index2 => undef, action => undef); - push @factoids, $self->{factoids}->get(type => 'regex', index1 => '.*', index2 => undef, action => undef); + @factoids = $self->{factoids}->get_all(type => 'regex', index1 => $channel, index2 => undef, action => undef); + push @factoids, $self->{factoids}->get_all(type => 'regex', index1 => '.*', index2 => undef, action => undef); } else { - @factoids = $self->{factoids}->get(type => 'regex', index1 => undef, index2 => undef, action => undef); + @factoids = $self->{factoids}->get_all(type => 'regex', index1 => undef, index2 => undef, action => undef); } foreach my $factoid (@factoids) { @@ -782,7 +783,7 @@ sub interpreter { my ($fwd_chan, $fwd_trig); # build list of which channels contain the keyword, keeping track of the last one and count - foreach my $factoid ($self->{factoids}->get(index2 => $original_keyword, index1 => undef, type => undef)) { + foreach my $factoid ($self->{factoids}->get_all(index2 => $original_keyword, index1 => undef, type => undef)) { next if $factoid->{type} ne 'text' and $factoid->{type} ne 'module'; push @chanlist, $self->{factoids}->get_data($factoid->{index1}, '_name'); $fwd_chan = $factoid->{index1};