File Coverage

lib/Apache/Solr/Document.pm
Criterion Covered Total %
statement 12 72 16.6
branch 0 28 0.0
condition 0 4 0.0
subroutine 4 17 23.5
pod 11 12 91.6
total 27 133 20.3


line stmt bran cond sub pod time code
1             # Copyrights 2012-2022 by [Mark Overmeer].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.03.
5             # This code is part of distribution Apache-Solr. Meta-POD processed with
6             # OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package Apache::Solr::Document;
10 5     5   1339 use vars '$VERSION';
  5         9  
  5         272  
11             $VERSION = '1.08';
12              
13              
14 5     5   29 use warnings;
  5         8  
  5         142  
15 5     5   25 use strict;
  5         8  
  5         126  
16              
17 5     5   22 use Log::Report qw(solr);
  5         7  
  5         31  
18              
19              
20 0     0 1   sub new(@) { my $c = shift; (bless {}, $c)->init({@_}) }
  0            
21             sub init($)
22 0     0 0   { my ($self, $args) = @_;
23              
24 0   0       $self->{ASD_boost} = $args->{boost} || 1.0;
25 0           $self->{ASD_fields} = []; # ordered
26 0           $self->{ASD_fields_h} = {}; # grouped by name
27 0           $self->addFields($args->{fields});
28 0           $self;
29             }
30              
31              
32             sub fromResult($$)
33 0     0 1   { my ($class, $data, $rank) = @_;
34 0           my (@f, %fh);
35            
36 0           while(my($k, $v) = each %$data)
37 0 0         { my @v = map +{name => $k, content => $_}
38             , ref $v eq 'ARRAY' ? @$v : $v;
39 0           push @f, @v;
40 0           $fh{$k} = \@v;
41             }
42              
43 0           my $self = $class->new;
44 0           $self->{ASD_rank} = $rank;
45 0           $self->{ASD_fields} = \@f;
46 0           $self->{ASD_fields_h} = \%fh;
47 0           $self;
48             }
49              
50             #---------------
51              
52             sub boost(;$)
53 0     0 1   { my $self = shift;
54 0 0         @_ or return $self->{ASD_boost};
55 0 0         my $f = $self->field(shift) or return;
56 0 0         @_ ? $f->{boost} = shift : $f->{boost};
57             }
58              
59 0     0 1   sub fieldNames() { my %c; $c{$_->{name}}++ for shift->fields; sort keys %c }
  0            
  0            
60              
61              
62 0     0 1   sub uniqueId() {shift->content($Apache::Solr::uniqueKey)}
63              
64              
65 0     0 1   sub rank() {shift->{ASD_rank}}
66              
67              
68             sub fields(;$)
69 0     0 1   { my $self = shift;
70 0           my $f = $self->{ASD_fields};
71 0 0         @_ or return @$f;
72 0           my $name = shift;
73 0           my $fh = $self->{ASD_fields_h}{$name}; # grouped by name
74 0 0         $fh ? @$fh : ();
75             }
76              
77              
78             sub field($)
79 0     0 1   { my $fh = $_[0]->{ASD_fields_h}{$_[1]};
80 0 0         $fh ? $fh->[0] : undef;
81             }
82              
83              
84             sub content($)
85 0     0 1   { my $f = $_[0]->field($_[1]);
86 0 0         $f ? $f->{content} : undef;
87             }
88              
89             our $AUTOLOAD;
90             sub AUTOLOAD
91 0     0     { my $self = shift;
92 0           (my $fn = $AUTOLOAD) =~ s/.*\:\://;
93              
94 0 0         $fn =~ /^_(.*)/ ? $self->content($1)
    0          
95             : $fn eq 'DESTROY' ? undef
96             : panic "Unknown method $AUTOLOAD (hint: fields start with '_')";
97             }
98              
99              
100             sub addField($$%)
101 0     0 1   { my $self = shift;
102 0           my $name = shift;
103 0 0         defined $_[0] or return;
104              
105             my $field = # important to minimalize copying of content
106             { name => $name
107             , content => ( !ref $_[0] ? shift
108 0 0         : ref $_[0] eq 'SCALAR' ? ${shift()}
  0 0          
109             : shift
110             )
111             };
112 0           my %args = @_;
113 0   0       $field->{boost} = $args{boost} || 1.0;
114 0           $field->{update} = $args{update};
115              
116 0           push @{$self->{ASD_fields}}, $field;
  0            
117 0           push @{$self->{ASD_fields_h}{$name}}, $field;
  0            
118 0           $field;
119             }
120              
121              
122             sub addFields($%)
123 0     0 1   { my ($self, $h, @args) = @_;
124             # pass content by ref to avoid a copy of potentially huge field.
125 0 0         if(ref $h eq 'ARRAY')
126 0           { for(my $i=0; $i < @$h; $i+=2)
127 0           { $self->addField($h->[$i] => \$h->[$i+1], @args);
128             }
129             }
130             else
131 0           { $self->addField($_ => \$h->{$_}, @args) for sort keys %$h;
132             }
133 0           $self;
134             }
135              
136             #--------------------------
137              
138             1;