EEDB/Peer.pm untested

filter: Statementstmt 0.0% 0 / 128 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 227 CC 22 SCAR 25.4
CC
22
Cov
0%
mapRow
42.0
create_self_peer_for_db
20.0
display_desc
12.0
CRAP 12.7
1 =head1 NAME - EEDB::Peer
2  
3 =head1 SYNOPSIS
4  
5 =head1 DESCRIPTION
6  
7 =head1 CONTACT
8  
9 Jessica Severin <severin@gsc.riken.jp>
10  
11 =head1 LICENSE
12  
13 * Software License Agreement (BSD License)
14 * EdgeExpressDB [eeDB] system
15 * copyright (c) 2007-2009 Jessica Severin RIKEN OSC
16 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are met:
19 * * Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * * Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * * Neither the name of Jessica Severin RIKEN OSC nor the
25 * names of its contributors may be used to endorse or promote products
26 * derived from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
29 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS BE LIABLE FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  
39 =head1 APPENDIX
40  
41 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
42  
43 =cut
44  
45 my $__riken_EEDB_peer_global_should_cache = 1;
46 my $__riken_EEDB_peer_global_id_cache = {};
47  
48 $VERSION = 0.953;
49  
50 package EEDB::Peer;
51  
52 use strict;
53 use Time::HiRes qw(time gettimeofday tv_interval);
54 use Data::UUID;
55  
56 use MQdb::MappedQuery;
57 our @ISA = qw(MQdb::MappedQuery);
58  
59 #################################################
60 # Class methods
61 #################################################
62  
63 sub class { return "Peer"; }
64  
65 sub set_cache_behaviour {
66 my $class = shift;
67 my $mode = shift;
68
69 $__riken_EEDB_peer_global_should_cache = $mode;
70
71 if(defined($mode) and ($mode eq '0')) {
72 #if turning off caching, then flush the caches
73 $__riken_EEDB_peer_global_id_cache = {};
74 }
75 }
76  
77 =head2 create_self_peer_for_db
78  
79 Description: used when creating a new instance of an eeDB database
80 Returntype : EEDB::Peer
81 Exceptions : none
82  
83 =cut
84  
85 sub create_self_peer_for_db {
86 #used as part of new EEDB instance creation process.
87 #each EEDB instance will have a peer entry for itself, this way when
88 #other EEDB databases need to externally link to features they can use copy
89 #this self-peer entry to the remote database
90
91 my $class = shift;
92 my $db = shift; #required
93 my $web_url = shift; #optional
94
95 return undef unless($db);
96
97 my $self = EEDB::Peer->fetch_by_alias($db, $db->dbname); #should return the self Peer
98 if(!$self) {
99 $self = new EEDB::Peer;
100 $self->create_uuid;
101 $self->alias($db->dbname);
102 $self->db_url(sprintf("%s://read:read@%s:%s/%s",
103 $db->driver,
104 $db->host,
105 $db->port,
106 $db->dbname));
107 if($web_url) { $self->web_url($web_url); }
108 $self->store($db);
109 $db->execute_sql("UPDATE peer SET is_self=1 WHERE uuid=?", $self->id);
110 }
111 return $self;
112 }
113  
114 #################################################
115 # Instance methods
116 #################################################
117  
118 sub init {
119 my $self = shift;
120 my @args = @_;
121 $self->SUPER::init(@args);
122 return $self;
123 }
124  
125 sub create_uuid {
126 my $self = shift;
127
128 my $ug = new Data::UUID;
129 my $uuid = $ug->create();
130 $self->primary_id($ug->to_string($uuid));
131 }
132  
133  
134 ##########################
135 #
136 # getter/setter methods of data which is stored in database
137 #
138 ##########################
139  
140 sub uuid {
141 my $self = shift;
142 return $self->primary_id;
143 }
144  
145 sub alias {
146 my $self = shift;
147 return $self->{'alias'} = shift if(@_);
148 return $self->{'alias'};
149 }
150  
151 sub db_url {
152 my $self = shift;
153 return $self->{'db_url'} = shift if(@_);
154 return $self->{'db_url'};
155 }
156  
157 sub web_url {
158 my $self = shift;
159 return $self->{'web_url'} = shift if(@_);
160 return $self->{'web_url'};
161 }
162  
163 sub display_desc {
164 my $self = shift;
165 my $str = sprintf("Peer(%s) %s ", $self->id, $self->alias);
166 $str .= sprintf(" db::%s", $self->db_url) if($self->db_url);
167 $str .= sprintf(" web::%s", $self->web_url) if($self->web_url);
168 }
169  
170 sub xml {
171 my $self = shift;
172 my $str = sprintf("<peer uuid=\"%s\" alias=\"%s\"", $self->id, $self->alias);
173 $str .= sprintf(" db_url=\"%s\"", $self->db_url) if($self->db_url);
174 $str .= sprintf(" web_url=\"%s\"", $self->web_url) if($self->web_url);
175
176 $str .= " />\n";
177 return $str;
178 }
179  
180 ##### URL follow section ##########
181  
182  
183 sub peer_database {
184 my $self = shift;
185
186 if(defined($self->db_url)) {
187 if(!defined($self->{'_peer_external_database'})) {
188 my $db = MQdb::Database->new_from_url($self->db_url);
189 $db->alias($self->alias);
190 $db->uuid($self->uuid);
191 $self->{'_peer_external_database'} = $db;
192 }
193 return $self->{'_peer_external_database'};
194 }
195 return $self->database;
196 }
197  
198  
199 #################################################
200 #
201 # DBObject override methods
202 #
203 #################################################
204  
205  
206 sub mapRow {
207 my $self = shift;
208 my $rowHash = shift;
209 my $dbc = shift;
210  
211 my $dbID = $rowHash->{'uuid'};
212 if(($__riken_EEDB_peer_global_should_cache != 0) and ($self->database())) {
213 my $cached_self = $__riken_EEDB_peer_global_id_cache->{$self->database() . $dbID};
214 if(defined($cached_self)) {
215 #printf("link already loaded in cache, reuse\n");
216 #$cached_self->display_info;
217 #printf(" db_id :: %s\n", $cached_self->db_id);
218 return $cached_self;
219 }
220 }
221  
222 $self->primary_id($rowHash->{'uuid'});
223 $self->alias($rowHash->{'alias'});
224 $self->db_url($rowHash->{'db_url'}) if($rowHash->{'db_url'});
225 $self->web_url($rowHash->{'web_url'}) if($rowHash->{'web_url'});
226
227 if(($__riken_EEDB_peer_global_should_cache != 0) and ($self->database())) {
228 $__riken_EEDB_peer_global_id_cache->{$self->database() . $self->id} = $self;
229 $__riken_EEDB_peer_global_id_cache->{$self->database() . $self->alias} = $self;
230 }
231  
232 return $self;
233 }
234  
235 ##### storage/update #####
236  
237 sub store {
238 my $self = shift;
239 my $db = shift;
240
241 unless($db) { return undef; }
242 if(!defined($self->primary_id)) { $self->create_uuid; }
243
244 $db->execute_sql("INSERT ignore INTO peer (uuid, alias, db_url, web_url) VALUES(?,?,?,?)",
245 $self->id, $self->alias, $self->db_url, $self->web_url);
246 $self->database($db);
247 return $self;
248 }
249  
250  
251  
252 ##### public class methods for fetching by utilizing DBObject framework methods #####
253  
254 sub fetch_self_from_url {
255 my $class = shift;
256 my $url = shift;
257
258 my $db = MQdb::Database->new_from_url($url);
259 unless($db) { return undef; }
260
261 my $sql = "SELECT * FROM peer WHERE is_self=1 and alias=?";
262 return $class->fetch_single($db, $sql, $db->dbname);
263 }
264  
265 sub fetch_by_uuid {
266 my $class = shift;
267 my $db = shift;
268 my $id = shift;
269  
270 my $sql = "SELECT * FROM peer WHERE uuid=?";
271 return $class->fetch_single($db, $sql, $id);
272 }
273  
274 sub fetch_all {
275 my $class = shift;
276 my $db = shift;
277  
278 my $sql = "SELECT * FROM peer";
279 return $class->fetch_multiple($db, $sql);
280 }
281  
282 sub fetch_by_name {
283 #a fuzzy method which allows either the UUID or alias to be
284 #used for access
285 my $class = shift;
286 my $db = shift;
287 my $name = shift;
288  
289 my $sql = "SELECT * FROM peer WHERE uuid=? or alias=?";
290 return $class->fetch_single($db, $sql, $name, $name);
291 }
292  
293 sub fetch_by_alias {
294 my $class = shift;
295 my $db = shift;
296 my $alias = shift;
297  
298 my $sql = "SELECT * FROM peer WHERE alias=?";
299 return $class->fetch_single($db, $sql, $alias);
300 }
301  
302  
303  
304 1;
305