Show
Ignore:
Timestamp:
10.12.2015 12:19:20 (4 years ago)
Author:
jmt12
Message:

Continuing to refactor driver code to move shared code up to parent classes. Have all the basic drivers done...

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • gs2-extensions/tdb/trunk/perllib/DBDrivers/BaseDBDriver.pm

    r30343 r30347  
    4444    # Debug messages for this driver 
    4545    $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'} = {}; 
    4649    # Default file extension - in this case it is an error to create a DB from 
    4750    # BaseDBDriver 
    4851    $self->{'default_file_extension'} = 'err'; 
    4952    # Support 
     53    $self->{'supports_datestamp'} = 0; 
     54    $self->{'supports_merge'} = 0; 
     55    $self->{'supports_persistentconnection'} = 0; 
     56    $self->{'supports_rss'} = 0; 
    5057    $self->{'supports_set'} = 0; 
    5158    bless($self, $class); 
     
    6875    if ($self->{'debug'}) { 
    6976    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"; 
    7178    } 
    7279} 
     
    112119## errorPrint(string, integer) => void ## 
    113120 
     121 
     122## @function registerConnectionIfPersistent(filehandle, string, string) => void 
     123# 
     124sub 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# 
     146sub 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# 
     202sub 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 
    114247############################################################################### 
    115248## Public Functions 
     
    126259    my $infodb_file_name = &util::get_dirsep_tail($collection_name) . '.' . $self->{'default_file_extension'}; 
    127260    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    } 
    128267    return $infodb_file_path; 
    129268} 
     
    131270 
    132271 
    133 ## @function supportsDatestamp(void) => boolean 
     272## @function supportsDatestamp(void) => integer 
    134273# 
    135274sub supportsDatestamp 
    136275{ 
    137276    my $self = shift(@_); 
    138     return 0; 
    139 } 
    140 ## supportsDatestamp(void) => boolean ## 
     277    return $self->{'supports_datestamp'}; 
     278} 
     279## supportsDatestamp(void) => integer ## 
    141280 
    142281 
     
    146285{ 
    147286    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# 
     294sub supportsPersistentConnection 
     295{ 
     296    my $self = shift(@_); 
     297    return $self->{'supports_persistentconnection'}; 
     298} 
     299## supportsPersistentConnection(void) => integer ## 
     300 
     301 
     302## @function supportsRSS(void) => integer 
    154303# 
    155304sub supportsRSS 
    156305{ 
    157306    my $self = shift(@_); 
    158     return 0; 
    159 } 
    160 ## supportsRSS(void) => boolean ## 
     307    return $self->{'supports_rss'}; 
     308} 
     309## supportsRSS(void) => integer ## 
    161310 
    162311