TT#58950 RedisLocationResult improvements

* switch from Moose to Moo reduced memory consumption
    * rework RedisLocationResultSource to use AUTOLOAD
      instead of creating accessors in BUILD (that is very expensive
      considering the amount of rows, multiplied by the overall amount
      of entries). Now the object creation takes sub 0.0001 sec from
      0.017 sec as before.

Change-Id: I9917ff38266ce89297adf55d75c40dd5f16a435b
changes/75/31075/2
Kirill Solomko 6 years ago
parent 38d730ed07
commit 28d51dce15

@ -1,37 +1,54 @@
package NGCP::Panel::Utils::RedisLocationResultSet;
use Moose;
use Moo;
use TryCatch;
use NGCP::Panel::Utils::Generic qw(:all);
use NGCP::Panel::Utils::RedisLocationResultSource;
use Data::Dumper;
use Time::HiRes qw(time);
has _c => (
is => 'ro',
isa => 'NGCP::Panel',
isa => sub { die "$_[0] must be NGCP::Panel" unless $_[0] && ref $_[0] eq 'NGCP::Panel' },
);
has _redis => (
is => 'ro',
isa => 'Redis',
isa => sub { die "$_[0] must be Redis" unless $_[0] && ref $_[0] eq 'Redis' },
);
has _rows => (
is => 'rw',
isa => 'ArrayRef',
isa => sub { die "$_[0] must be ARRAY" unless $_[0] && ref $_[0] eq 'ARRAY' },
default => sub {[]}
);
has _query_done => (
is => 'rw',
isa => 'Int',
isa => sub { die "$_[0] must be int" unless defined $_[0] && is_int($_[0]) },
default => 0,
);
has result_source => (
is => 'ro',
default => sub { NGCP::Panel::Utils::RedisLocationResultSource->new },
);
has result_class => (
is => 'ro',
default => 'dummy',
);
has current_source_alias => (
is => 'ro',
default => 'me',
);
has _domain_resellers => (
is => 'rw',
isa => 'HashRef',
isa => sub { die "$_[0] must be HASHREF" unless $_[0] && ref $_[0] eq 'HASH' },
lazy => 1,
default => sub {
my $self = shift;
@ -164,7 +181,6 @@ sub _filter {
foreach my $row (@{ $self->_rows }) {
my $match = 0;
my $filter_applied = 0;
my %attr = map { $_->name => 1 } $row->meta->get_all_attributes;
foreach my $colname (keys %{ $filter }) {
my $condition = $filter->{$colname};
my $searchname = $colname;
@ -173,7 +189,7 @@ sub _filter {
next if ($colname =~ /\./); # we don't support joined table columns
$filter_applied = 1;
if (ref $condition eq "") {
if (!exists $attr{$colname} || lc($row->$colname) ne lc($condition)) {
if (lc($row->$colname) ne lc($condition)) {
$match = 0;
last;
} else {
@ -183,7 +199,7 @@ sub _filter {
my $fil = $condition->{like};
$fil =~ s/^\%//;
$fil =~ s/\%$//;
if (!exists $attr{$colname} || $row->$colname !~ /$fil/i) {
if ($row->$colname !~ /$fil/i) {
$match = 0;
last;
} else {
@ -221,17 +237,4 @@ sub _scan {
return 1;
}
sub result_class {
"dummy";
}
sub result_source {
NGCP::Panel::Utils::RedisLocationResultSource->new;
}
sub current_source_alias {
my ($self) = @_;
return 'me';
}
1;

@ -1,21 +1,20 @@
package NGCP::Panel::Utils::RedisLocationResultSource;
use Moose;
use Moo;
our $AUTOLOAD;
has _data => (
is => 'ro',
isa => 'HashRef',
isa => sub { die "$_[0] must be HASHREF" unless $_[0] && ref $_[0] eq 'HASH' },
default => sub {{}},
);
sub BUILD {
my ($self) = @_;
foreach my $k (keys %{ $self->_data }) {
$self->meta->add_attribute(
$k => (accessor => $k)
);
$self->$k($self->_data->{$k});
}
sub AUTOLOAD {
my $self = shift;
my $col = $AUTOLOAD;
$col = (split /::/, $col)[-1];
return $self->_data->{$col};
}
sub get_inflated_columns {
@ -23,9 +22,6 @@ sub get_inflated_columns {
return %{ $self->_data };
}
sub columns {
}
sub columns { }
1;

Loading…
Cancel
Save