- Timestamp:
- 2015-12-10T12:19:20+13:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gs2-extensions/tdb/trunk/perllib/DBDrivers/BaseDBDriver.pm
r30343 r30347 44 44 # Debug messages for this driver 45 45 $self->{'debug'} = $debug; # 1 to enable 46 # Keep track of all opened file handles, but only for drivers that support 47 # persistent connections 48 $self->{'handle_pool'} = {}; 46 49 # Default file extension - in this case it is an error to create a DB from 47 50 # BaseDBDriver 48 51 $self->{'default_file_extension'} = 'err'; 49 52 # Support 53 $self->{'supports_datestamp'} = 0; 54 $self->{'supports_merge'} = 0; 55 $self->{'supports_persistentconnection'} = 0; 56 $self->{'supports_rss'} = 0; 50 57 $self->{'supports_set'} = 0; 51 58 bless($self, $class); … … 68 75 if ($self->{'debug'}) { 69 76 my ($seconds, $microseconds) = gettimeofday(); 70 print STDERR '[DEBUG:' . $seconds . '.' . $microseconds . '] ' . (caller 1)[3] . $message . "\n";77 print STDERR '[DEBUG:' . $seconds . '.' . $microseconds . '] ' . (caller 1)[3] . '() ' . $message . "\n"; 71 78 } 72 79 } … … 112 119 ## errorPrint(string, integer) => void ## 113 120 121 122 ## @function registerConnectionIfPersistent(filehandle, string, string) => void 123 # 124 sub registerConnectionIfPersistent 125 { 126 my $self = shift(@_); 127 my $conn = shift(@_); 128 my $path = shift(@_); 129 my $append = shift(@_); 130 if ($self->{'supports_persistentconnection'}) { 131 $self->debugPrintFunctionHeader($conn, $path, $append); 132 my $fhid = $path; 133 if (defined $append && $append eq '-append') { 134 $fhid .= ' [APPEND]'; 135 } 136 $self->debugPrint('Registering connection: "' . $fhid . '"'); 137 $self->{'handle_pool'}->{$fhid} = $conn; 138 } 139 return; 140 } 141 ## registerConnectionIfPersistent(filehandle, string, string) => void ## 142 143 144 ## @function removeConnectionIfPersistent(filehandle, string) => integer 145 # 146 sub removeConnectionIfPersistent 147 { 148 my $self = shift(@_); 149 my $handle = shift(@_); 150 my $force_close = shift(@_); 151 my $continue_close = 1; 152 if ($self->{'supports_persistentconnection'}) { 153 $self->debugPrintFunctionHeader($handle, $force_close); 154 if (defined($force_close)) { 155 # We'll need the file path so we can locate and remove the entry 156 # in the handle pool (plus possibly the [APPEND] suffix for those 157 # connections in opened in append mode) 158 my $fhid = undef; 159 # Sometimes we can cheat, as the force_close variable will have the 160 # file_path in it thanks to the DESTROY block above. Doing a regex 161 # on force_close will treat it like a string no matter what it was, 162 # and we can search for the appropriate file extension that should 163 # be there for valid paths. 164 my $pattern = '\.' . $self->{'default_file_extension'} . '(\s\[APPEND\])?$'; 165 if ($force_close =~ /$pattern/) { 166 $fhid = $force_close; 167 } 168 # If we can't cheat then we are stuck finding which connection in 169 # the handle_pool we are about to close. Need to compare objects 170 # using refaddr() 171 else { 172 foreach my $possible_fhid (keys %{$self->{'handle_pool'}}) { 173 my $possible_handle = $self->{'handle_pool'}->{$possible_fhid}; 174 if (ref($handle) && ref($possible_handle) && refaddr($handle) == refaddr($possible_handle)) { 175 $fhid = $possible_fhid; 176 last; 177 } 178 } 179 } 180 # If we found the fhid we can proceed to close the connection 181 if (defined($fhid)) { 182 $self->debugPrint('Closing persistent connection: ' . $fhid); 183 delete($self->{'handle_pool'}->{$fhid}); 184 $continue_close = 1; 185 } 186 else { 187 print STDERR "Warning! About to close persistent database handle, but couldn't locate in open handle pool.\n"; 188 } 189 } 190 # Persistent connection don't close *unless* force close is set 191 else { 192 $continue_close = 0; 193 } 194 } 195 return $continue_close; 196 } 197 ## removeConnectionIfPersistent(filehandle, string) => integer ## 198 199 200 ## 201 # 202 sub retrieveConnectionIfPersistent 203 { 204 my $self = shift(@_); 205 my $path = shift(@_); 206 my $append = shift(@_); # -append support 207 my $conn; # This should be populated 208 if ($self->{'supports_persistentconnection'}) { 209 $self->debugPrintFunctionHeader($path, $append); 210 my $fhid = $path; 211 # special case: if the append mode has changed for a persistent 212 # connection, we need to close the old connection first or things 213 # will get wiggy. 214 if (defined $append && $append eq '-append') { 215 # see if there is a non-append mode connection already open 216 if (defined $self->{'handle_pool'}->{$path}) { 217 $self->debugPrint("Append mode added - closing existing non-append mode connection"); 218 my $old_conn = $self->{'handle_pool'}->{$path}; 219 $self->close_infodb_write_handle($old_conn, $path); 220 } 221 # Append -append so we know what happened. 222 $fhid .= ' [APPEND]'; 223 } 224 else { 225 my $fhid_append = $path . ' [APPEND]'; 226 if (defined $self->{'handle_pool'}->{$fhid_append}) { 227 $self->debugPrint("Append mode removed - closing existing append mode connection"); 228 my $old_conn = $self->{'handle_pool'}->{$fhid_append}; 229 $self->close_infodb_write_handle($old_conn, $fhid_append); 230 } 231 } 232 if (defined $self->{'handle_pool'}->{$fhid}) { 233 $self->debugPrint('Retrieving existing connection: ' . $fhid); 234 $conn = $self->{'handle_pool'}->{$fhid}; 235 } 236 } 237 return $conn; 238 } 239 ## ## 240 241 242 243 244 245 246 114 247 ############################################################################### 115 248 ## Public Functions … … 126 259 my $infodb_file_name = &util::get_dirsep_tail($collection_name) . '.' . $self->{'default_file_extension'}; 127 260 my $infodb_file_path = &FileUtils::filenameConcatenate($infodb_directory_path, $infodb_file_name); 261 # Correct the path separators to work in Cygwin 262 if ($^O eq "cygwin") { 263 $infodb_file_path = `cygpath -w "$infodb_file_path"`; 264 chomp($infodb_file_path); 265 $infodb_file_path =~ s%\\%\\\\%g; 266 } 128 267 return $infodb_file_path; 129 268 } … … 131 270 132 271 133 ## @function supportsDatestamp(void) => boolean272 ## @function supportsDatestamp(void) => integer 134 273 # 135 274 sub supportsDatestamp 136 275 { 137 276 my $self = shift(@_); 138 return 0;139 } 140 ## supportsDatestamp(void) => boolean##277 return $self->{'supports_datestamp'}; 278 } 279 ## supportsDatestamp(void) => integer ## 141 280 142 281 … … 146 285 { 147 286 my $self = shift(@_); 148 return 0; 149 } 150 ## supportsMerge(void) => boolean ## 151 152 153 ## @function supportsRSS(void) => boolean 287 return $self->{'supports_merge'}; 288 } 289 ## supportsMerge(void) => integer ## 290 291 292 ## @function supportsPersistentConnection(void) => integer 293 # 294 sub supportsPersistentConnection 295 { 296 my $self = shift(@_); 297 return $self->{'supports_persistentconnection'}; 298 } 299 ## supportsPersistentConnection(void) => integer ## 300 301 302 ## @function supportsRSS(void) => integer 154 303 # 155 304 sub supportsRSS 156 305 { 157 306 my $self = shift(@_); 158 return 0;159 } 160 ## supportsRSS(void) => boolean##307 return $self->{'supports_rss'}; 308 } 309 ## supportsRSS(void) => integer ## 161 310 162 311
Note:
See TracChangeset
for help on using the changeset viewer.