1 |
package Cornea::RecallTable; |
---|
2 |
use strict; |
---|
3 |
use Cornea::Config; |
---|
4 |
use Cornea::Utils; |
---|
5 |
use DBI; |
---|
6 |
|
---|
7 |
sub __connect { |
---|
8 |
my $self = shift; |
---|
9 |
my $config = Cornea::Config->new(); |
---|
10 |
my $dbh; |
---|
11 |
my $dsns = $config->get_list("DB::dsn"); |
---|
12 |
Cornea::Utils::shuffle($dsns); |
---|
13 |
foreach my $dsn (@$dsns) { |
---|
14 |
eval { |
---|
15 |
$dbh = DBI->connect($dsn, |
---|
16 |
$config->get("DB::user"), |
---|
17 |
$config->get("DB::pass"), |
---|
18 |
{ PrintError => 0, RaiseError => 1 }, |
---|
19 |
); |
---|
20 |
}; |
---|
21 |
last unless ($@); |
---|
22 |
} |
---|
23 |
$self->{dbh} = shift; |
---|
24 |
} |
---|
25 |
sub __reconnect { |
---|
26 |
my $self = shift; |
---|
27 |
$self->{dbh} = undef; |
---|
28 |
$self->__connect(); |
---|
29 |
} |
---|
30 |
sub new { |
---|
31 |
my $class = shift; |
---|
32 |
my $self = bless { }, $class; |
---|
33 |
$self->__connect; |
---|
34 |
} |
---|
35 |
|
---|
36 |
sub insert { |
---|
37 |
my $self = shift; |
---|
38 |
my ($serviceId, $assetId, $repId, $snl) = @_; |
---|
39 |
my $tried = 0; |
---|
40 |
die 'bad parameters' unless UNIVERSAL::ISA($snl, 'Cornea::StorageNodeList'); |
---|
41 |
again: |
---|
42 |
eval { |
---|
43 |
my $sth = $self->{dbh}->prepare("select storeAsset(?,?,?,?)"); |
---|
44 |
$sth->execute($serviceId, $assetId, $repId, $snl); |
---|
45 |
$sth->finish(); |
---|
46 |
}; |
---|
47 |
if ($@) { |
---|
48 |
unless ($tried++) { $self->{dbh}->__reconnect(); goto again; } |
---|
49 |
die $@ if $@; |
---|
50 |
} |
---|
51 |
return 1; |
---|
52 |
} |
---|
53 |
|
---|
54 |
sub find { |
---|
55 |
my $self = shift; |
---|
56 |
my ($serviceId, $assetId, $repId) = @_; |
---|
57 |
my $sth = $self->{dbh}->prepare("select findAsset(?,?,?)"); |
---|
58 |
my $tried = 0; |
---|
59 |
my $C; |
---|
60 |
again: |
---|
61 |
eval { |
---|
62 |
$C = Cornea::StorageNodeList->new(); |
---|
63 |
$sth->execute($serviceId, $assetId, $repId); |
---|
64 |
while(my $node = $sth->fetchrow_hashref()) { |
---|
65 |
$C->add(Cornea::StorageNode->new_from_row($node)); |
---|
66 |
} |
---|
67 |
$sth->finish(); |
---|
68 |
}; |
---|
69 |
if ($@) { |
---|
70 |
unless ($tried++) { $self->{dbh}->__reconnect(); goto again; } |
---|
71 |
die $@ if $@; |
---|
72 |
} |
---|
73 |
return $C; |
---|
74 |
} |
---|
75 |
|
---|
76 |
sub getNodes { |
---|
77 |
my $self = shift; |
---|
78 |
my $type = shift; |
---|
79 |
my $tried = 0; |
---|
80 |
my $snl; |
---|
81 |
again: |
---|
82 |
eval { |
---|
83 |
$snl = Cornea::StorageNodeList->new(); |
---|
84 |
my $sth = $self->{dbh}->prepare("select * from getCorneaNodes(?)"); |
---|
85 |
$sth->execute($type); |
---|
86 |
while(my $row = $sth->fetchrow_hashref()) { |
---|
87 |
$snl->add(Cornea::StorageNode->new_from_row($row)); |
---|
88 |
} |
---|
89 |
$sth->finish(); |
---|
90 |
}; |
---|
91 |
if ($@) { |
---|
92 |
unless ($tried++) { $self->{dbh}->__reconnect(); goto again; } |
---|
93 |
die $@ if $@; |
---|
94 |
} |
---|
95 |
return $snl; |
---|
96 |
} |
---|
97 |
|
---|
98 |
sub repInfo { |
---|
99 |
my $self = shift; |
---|
100 |
my ($serviceId, $repId) = @_; |
---|
101 |
my $tried = 0; |
---|
102 |
my $row; |
---|
103 |
again: |
---|
104 |
eval { |
---|
105 |
my $sth = $self->{dbh}->prepare("select * from getRepInfo(?,?)"); |
---|
106 |
$sth->execute($serviceId, $repId); |
---|
107 |
$row = $sth->fetchrow_hashref(); |
---|
108 |
$sth->finish(); |
---|
109 |
}; |
---|
110 |
if ($@) { |
---|
111 |
unless ($tried++) { $self->{dbh}->__reconnect(); goto again; } |
---|
112 |
die $@ if $@; |
---|
113 |
} |
---|
114 |
return Cornea::RepresentationInfo->new_from_row($row); |
---|
115 |
} |
---|
116 |
|
---|
117 |
sub repInfoDependents { |
---|
118 |
my $self = shift; |
---|
119 |
my ($serviceId, $repId) = @_; |
---|
120 |
my $tried = 0; |
---|
121 |
my @deps; |
---|
122 |
|
---|
123 |
eval { |
---|
124 |
@deps = (); |
---|
125 |
my $sth = $self->{dbh}->prepare("select * from getRepInfoDependents(?,?)"); |
---|
126 |
$sth->execute($serviceId, $repId); |
---|
127 |
while(my $row = $sth->fetchrow_hashref()) { |
---|
128 |
push @deps, Cornea::RepresentationInfo->new_from_row($row); |
---|
129 |
} |
---|
130 |
$sth->finish(); |
---|
131 |
}; |
---|
132 |
if ($@) { |
---|
133 |
unless ($tried++) { $self->{dbh}->__reconnect(); goto again; } |
---|
134 |
die $@ if $@; |
---|
135 |
} |
---|
136 |
return @deps; |
---|
137 |
} |
---|
138 |
|
---|
139 |
1; |
---|