line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Metadata::DB::Indexer; |
2
|
1
|
|
|
1
|
|
186525
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
63
|
|
3
|
1
|
|
|
1
|
|
9
|
use base 'Metadata::DB::Base'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
4930
|
|
4
|
|
|
|
|
|
|
use LEOCHARRE::DEBUG; |
5
|
|
|
|
|
|
|
use LEOCHARRE::Class2; |
6
|
|
|
|
|
|
|
use Carp; |
7
|
|
|
|
|
|
|
use vars qw($VERSION); |
8
|
|
|
|
|
|
|
$VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)/g; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
__PACKAGE__->make_constructor; |
11
|
|
|
|
|
|
|
__PACKAGE__->make_accessor_setget({ |
12
|
|
|
|
|
|
|
records_to_index => [], |
13
|
|
|
|
|
|
|
records_indexed_count => 0, |
14
|
|
|
|
|
|
|
}); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub records_to_index_count { |
18
|
|
|
|
|
|
|
my $self = shift; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $records = $self->records_to_index |
21
|
|
|
|
|
|
|
or warn('records_to_index was not set yet, cant get count') |
22
|
|
|
|
|
|
|
and return; |
23
|
|
|
|
|
|
|
ref $records eq 'ARRAY' or confess('records to index must be array ref'); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $count = scalar @$records; |
26
|
|
|
|
|
|
|
return $count; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# overrite me |
31
|
|
|
|
|
|
|
sub record_identifier_to_metadata { |
32
|
|
|
|
|
|
|
warn ( __PACKAGE__."::record_to_metadata() overrite me please, see Metadata::DB::Indexer"); |
33
|
|
|
|
|
|
|
return; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub run { |
37
|
|
|
|
|
|
|
my $self = shift; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $count = $self->records_to_index_count |
40
|
|
|
|
|
|
|
or return; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
debug("will index $count records"); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my $autocommit_original_setting = $self->dbh->{AutoCommit}; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# should we localize? |
48
|
|
|
|
|
|
|
$self->dbh->{AutoCommit} = 0; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# drop/reset/create the table |
52
|
|
|
|
|
|
|
my $name = $self->table_metadata_name; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
debug("reseting '$name' table"); |
55
|
|
|
|
|
|
|
$self->table_metadata_reset; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
my $record_identifiers = $self->records_to_index; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
ITERATION: for my $identifier ( @$record_identifiers ){ |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
$self->_index_one_record($identifier); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# commit and set back the autocommit setting to what it was |
66
|
|
|
|
|
|
|
debug("Done. Committing changes to db.."); |
67
|
|
|
|
|
|
|
$self->dbh->commit; |
68
|
|
|
|
|
|
|
$self->dbh->{AutoCommit} = $autocommit_original_setting; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
debug( sprintf "Indexed %s/%s records", $self->records_indexed_count, $count ); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
return 1; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub _index_one_record { |
78
|
|
|
|
|
|
|
my($self,$identifier) = @_; |
79
|
|
|
|
|
|
|
defined $identifier or confess; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
no strict 'refs'; |
82
|
|
|
|
|
|
|
my $meta = &{__PACKAGE__.'::record_identifier_to_metadata'}($identifier); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
if( ! defined $meta ){ |
85
|
|
|
|
|
|
|
warn("record_identifier_to_metadata() returns undef for [$identifier]"); |
86
|
|
|
|
|
|
|
return; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
elsif( ! ((ref $meta) and (ref $meta eq 'HASH')) ){ |
90
|
|
|
|
|
|
|
warn("record_identifier_to_metadata() does not return hash ref"); |
91
|
|
|
|
|
|
|
return; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
elsif( ! ( keys %$meta ) ){ |
94
|
|
|
|
|
|
|
warn("record_identified_to_metadata() has ref has nothing?"); |
95
|
|
|
|
|
|
|
return; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
my $id = ( $self->records_indexed_count + 1 ); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
unless( $self->_table_metadata_insert_multiple($id,$meta) ){ |
101
|
|
|
|
|
|
|
warn("could not insert record[$id],[meta ref:$meta], ".$self->dbh->errstr); |
102
|
|
|
|
|
|
|
return; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# hack: |
106
|
|
|
|
|
|
|
$self->records_indexed_count($id); #increment, we only get here if we inserter properly |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
return 1; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
__END__ |