line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Sphinx::XMLpipe2; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
14918
|
use XML::Hash::XS; |
|
2
|
|
|
|
|
1430
|
|
|
2
|
|
|
|
|
1341
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = "0.05"; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub new { |
8
|
4
|
|
|
4
|
1
|
204
|
my ($class, %args) = @_; |
9
|
4
|
100
|
|
|
|
14
|
return undef unless %args; |
10
|
|
|
|
|
|
|
|
11
|
3
|
|
|
|
|
4
|
my $self = \%args; |
12
|
3
|
|
|
|
|
5
|
$self->{'data'} = {}; |
13
|
|
|
|
|
|
|
|
14
|
3
|
|
|
|
|
78
|
$self->{'xmlprocessor'} = XML::Hash::XS->new( |
15
|
|
|
|
|
|
|
xml_decl => 0, |
16
|
|
|
|
|
|
|
indent => 4, |
17
|
|
|
|
|
|
|
use_attr => 1, |
18
|
|
|
|
|
|
|
content => 'content', |
19
|
|
|
|
|
|
|
encoding => 'utf-8', |
20
|
|
|
|
|
|
|
utf8 => 0, |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
3
|
|
|
|
|
4
|
bless $self, $class; |
24
|
3
|
|
|
|
|
7
|
return $self |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub fetch { |
28
|
1
|
|
|
1
|
1
|
2
|
my ($self, ) = @_; |
29
|
|
|
|
|
|
|
|
30
|
1
|
|
|
|
|
3
|
my $content1 = $self->_fetch_header(); |
31
|
1
|
|
|
|
|
3
|
my $content2 = $self->_fetch_data(); |
32
|
1
|
|
|
|
|
7
|
return qq~\n$content1$content2~; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub add_data { |
36
|
2
|
|
|
2
|
1
|
166
|
my ($self, $data) = @_; |
37
|
2
|
100
|
|
|
|
9
|
return undef unless exists $data->{'id'}; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
$self->{'data'}->{'sphinx:document'} = [] |
40
|
1
|
50
|
|
|
|
7
|
unless exists $self->{'data'}->{'sphinx:document'}; |
41
|
|
|
|
|
|
|
|
42
|
1
|
|
|
|
|
2
|
my %params = (); |
43
|
1
|
|
|
|
|
2
|
my @keys = (); |
44
|
|
|
|
|
|
|
|
45
|
1
|
50
|
|
|
|
4
|
if (ref($self->{'attrs'}) eq 'HASH') { |
|
|
0
|
|
|
|
|
|
46
|
1
|
|
|
|
|
1
|
@keys = (@{$self->{'fields'}}, keys %{$self->{'attrs'}}); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
3
|
|
47
|
1
|
|
|
|
|
3
|
map { $params{$_} = [$data->{$_}] } @keys; |
|
4
|
|
|
|
|
7
|
|
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
elsif (ref($self->{'attrs'}) eq 'ARRAY') { |
50
|
0
|
|
|
|
|
0
|
my @def = (); |
51
|
0
|
|
|
|
|
0
|
for my $definition (@{$self->{'attrs'}}) { |
|
0
|
|
|
|
|
0
|
|
52
|
0
|
|
|
|
|
0
|
push @def, $definition->{'name'}; |
53
|
|
|
|
|
|
|
} |
54
|
0
|
|
|
|
|
0
|
@keys = (@{$self->{'fields'}}, @def); |
|
0
|
|
|
|
|
0
|
|
55
|
|
|
|
|
|
|
} |
56
|
1
|
50
|
|
|
|
2
|
map { $params{$_} = [$data->{$_}] if exists $data->{$_}; } @keys; |
|
4
|
|
|
|
|
16
|
|
57
|
|
|
|
|
|
|
|
58
|
1
|
|
|
|
|
5
|
push @{$self->{'data'}->{'sphinx:document'}}, { |
59
|
1
|
|
|
|
|
1
|
id => $data->{'id'}, |
60
|
|
|
|
|
|
|
%params |
61
|
|
|
|
|
|
|
}; |
62
|
1
|
|
|
|
|
4
|
return $self; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub remove_data { |
66
|
2
|
|
|
2
|
1
|
3
|
my ($self, $data) = @_; |
67
|
2
|
100
|
|
|
|
10
|
return undef unless exists $data->{'id'}; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
$self->{'data'}->{'sphinx:killlist'} = {'id' => []} |
70
|
1
|
50
|
|
|
|
13
|
unless exists $self->{'data'}->{'sphinx:killlist'}->{'id'}; |
71
|
|
|
|
|
|
|
|
72
|
1
|
|
|
|
|
2
|
push @{$self->{'data'}->{'sphinx:killlist'}->{'id'}}, [$data->{'id'}]; |
|
1
|
|
|
|
|
4
|
|
73
|
1
|
|
|
|
|
3
|
return $self; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub _fetch_header { |
78
|
1
|
|
|
1
|
|
1
|
my ($self, ) = @_; |
79
|
1
|
|
|
|
|
4
|
my $header = { |
80
|
|
|
|
|
|
|
'sphinx:schema' => { |
81
|
|
|
|
|
|
|
'sphinx:field' => [], |
82
|
|
|
|
|
|
|
'sphinx:attr' => [], |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
}; |
85
|
|
|
|
|
|
|
|
86
|
1
|
|
|
|
|
2
|
for my $field (@{$self->{'fields'}}) { |
|
1
|
|
|
|
|
3
|
|
87
|
3
|
|
|
|
|
4
|
push @{$header->{'sphinx:schema'}->{'sphinx:field'}}, {'name' => $field}; |
|
3
|
|
|
|
|
5
|
|
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
1
|
50
|
|
|
|
4
|
if (ref($self->{'attrs'}) eq 'HASH') { |
|
|
0
|
|
|
|
|
|
91
|
1
|
|
|
|
|
2
|
for my $attr (keys %{$self->{'attrs'}}) { |
|
1
|
|
|
|
|
3
|
|
92
|
1
|
|
|
|
|
2
|
push @{$header->{'sphinx:schema'}->{'sphinx:attr'}}, {'name' => $attr, 'type' => $self->{'attrs'}->{$attr}}; |
|
1
|
|
|
|
|
11
|
|
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
elsif (ref($self->{'attrs'}) eq 'ARRAY') { |
96
|
0
|
|
|
|
|
0
|
for my $definition (@{$self->{'attrs'}}) { |
|
0
|
|
|
|
|
0
|
|
97
|
0
|
|
|
|
|
0
|
my $node = {}; |
98
|
0
|
0
|
|
|
|
0
|
map {$node->{$_} = $definition->{$_} if exists $definition->{$_};} qw(name type bits default); |
|
0
|
|
|
|
|
0
|
|
99
|
0
|
|
|
|
|
0
|
push @{$header->{'sphinx:schema'}->{'sphinx:attr'}}, $node; |
|
0
|
|
|
|
|
0
|
|
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
1
|
|
|
|
|
2
|
@{$header->{'sphinx:schema'}->{'sphinx:attr'}} = sort {$a->{name} cmp $b->{name}} @{$header->{'sphinx:schema'}->{'sphinx:attr'}}; |
|
1
|
|
|
|
|
3
|
|
|
0
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
3
|
|
103
|
|
|
|
|
|
|
|
104
|
1
|
|
|
|
|
50
|
return _pruning_xml($self->{'xmlprocessor'}->hash2xml($header)); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub _fetch_data { |
108
|
1
|
|
|
1
|
|
2
|
my ($self, ) = @_; |
109
|
1
|
|
|
|
|
12
|
return _pruning_xml($self->{'xmlprocessor'}->hash2xml($self->{'data'})); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub _pruning_xml { |
113
|
2
|
|
|
2
|
|
14
|
my ($xml) = @_; |
114
|
2
|
|
|
|
|
10
|
$xml =~ s/^\s*//is; |
115
|
2
|
|
|
|
|
10
|
$xml =~ s/<\/root>\s*$//is; |
116
|
2
|
|
|
|
|
5
|
return $xml; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
1; |
120
|
|
|
|
|
|
|
__END__ |