Ignore:
Timestamp:
2015-12-03T15:42:48+13:00 (8 years ago)
Author:
jmt12
Message:

The base class of all database drivers, providing the 'interface' for all drivers as well as some common functionality (debug printing etc)

File:
1 edited

Legend:

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

    r30318 r30336  
    2323###############################################################################
    2424
    25 package BaseDBDriver;
     25package DBDrivers::BaseDBDriver;
    2626
    2727# Pragma
    2828use strict;
     29no strict 'subs';
     30no strict 'refs'; # allow filehandles to be variables and viceversa
    2931
    3032# Libaries
     33use Time::HiRes qw( gettimeofday );
    3134use gsprintf 'gsprintf';
    3235
    33 ###############################################################################
    34 ## Private Functions
    35 ###############################################################################
    36 
    37 
    38 ## @function _convert_infodb_hash_to_string(hashmap)
    39 #
    40 sub _convert_infodb_hash_to_string
    41 {
    42     my $infodb_map = shift(@_);
    43     my $infodb_entry_value = "";
    44     foreach my $infodb_value_key (keys(%$infodb_map)) {
    45         foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}}) {
    46             $infodb_entry_value .= "<$infodb_value_key>" . $infodb_value . "\n";
    47         }
     36
     37## @function constructor
     38#
     39sub new
     40{
     41    my $class = shift(@_);
     42    my $debug = shift(@_);
     43    my $self = {};
     44    # Debug messages for this driver
     45    $self->{'debug'} = $debug; # 1 to enable
     46    # Default file extension - in this case it is an error to create a DB from
     47    # BaseDBDriver
     48    $self->{'default_file_extension'} = 'err';
     49    bless($self, $class);
     50    return $self;
     51}
     52## new(void) => BaseDBDriver ##
     53
     54
     55###############################################################################
     56## Protected Functions
     57###############################################################################
     58
     59
     60## @function debugPrint(string) => void
     61#
     62sub debugPrint
     63{
     64    my $self = shift(@_);
     65    my $message = shift(@_);
     66    if ($self->{'debug'}) {
     67    my ($seconds, $microseconds) = gettimeofday();
     68    print STDERR '[DEBUG:' . $seconds . '.' . $microseconds . '] ' . (caller 1)[3] . $message . "\n";
    4869    }
    49     return $infodb_entry_value;
    50 }
    51 ## _convert_infodb_hash_to_string(hashmap) => string ##
    52 
    53 
    54 ## @function _convert_infodb_string_to_hash(string)
    55 #
    56 sub _convert_infodb_string_to_hash
    57 {
    58     my $infodb_entry_value = shift(@_);
    59     my $infodb_map = ();
    60 
    61     if (!defined $infodb_entry_value) {
    62     print STDERR "Warning: No value to convert into a infodb hashtable\n";
     70}
     71## debugPrint(string) => void ##
     72
     73
     74## @function debugPrintFunctionHeader(*) => void
     75#
     76sub debugPrintFunctionHeader
     77{
     78    my $self = shift(@_);
     79    if ($self->{'debug'}) {
     80    my @arguments;
     81    foreach my $argument (@_) {
     82        if ($argument !~ /^-?\d+(\.?\d+)?$/) {
     83        push(@arguments, '"' . $argument . '"');
     84        }
     85        else {
     86        push(@arguments, $argument);
     87        }
     88    }
     89    my $message = '(' . join(', ', @arguments) . ')';
     90    # Would love to just call debugPrint() here, but then caller would be wrong
     91    my ($seconds, $microseconds) = gettimeofday();
     92    print STDERR '[DEBUG:' . $seconds . '.' . $microseconds . '] ' . (caller 1)[3] . $message . "\n";
    6393    }
    64     else {
    65         while ($infodb_entry_value =~ /^<(.*?)>(.*)$/mg) {
    66             my $infodb_value_key = $1;
    67             my $infodb_value = $2;
    68 
    69             if (!defined($infodb_map->{$infodb_value_key})) {
    70                 $infodb_map->{$infodb_value_key} = [ $infodb_value ];
    71             }
    72             else {
    73                 push(@{$infodb_map->{$infodb_value_key}}, $infodb_value);
    74             }
    75     }
     94}
     95## debugPrintFunctionHeader(*) => void
     96
     97
     98## @function errorPrint(string, integer) => void
     99#
     100sub errorPrint
     101{
     102    my $self = shift(@_);
     103    my $message = shift(@_);
     104    my $is_fatal = shift(@_);
     105    print STDERR 'Error in ' . (caller 1)[3] . '! ' . $message . "\n";
     106    if ($is_fatal) {
     107    exit();
    76108    }
    77 
    78     return $infodb_map;
    79 }
    80 ## _convert_infodb_string_to_hash(string) => hashmap ##
    81 
     109}
     110## errorPrint(string, integer) => void ##
    82111
    83112###############################################################################
     
    86115
    87116
    88 ## @function supports_datestamp()
    89 #
    90 sub supports_datestamp
    91 {
    92     return 0;
    93 }
    94 ## supports_datestamp() => integer ##
    95 
    96 
    97 ## @function supports_merge()
    98 #
    99 sub supports_merge
    100 {
    101     return 0;
    102 }
    103 ## supports_merge() => integer ##
    104 
    105 
    106 ## @function supports_rss()
    107 #
    108 sub supports_rss
    109 {
    110     return 0;
    111 }
    112 ## supports_rss() => integer ##
     117## @function canInstantiate(void) => integer
     118#
     119#  Called to determine if this driver implementation can actually be created
     120#  given its purpose and the current system (i.e. Windows only drivers on
     121#  Linux systems can't be instantiated...)
     122#
     123sub canInstantiate
     124{
     125    # Can't ever instantiate the base driver
     126    return 0;
     127}
     128## canInstantiate(void) => integer  ##
     129
     130
     131## @function get_infodb_file_path(string, string) => string
     132#
     133sub get_infodb_file_path
     134{
     135    my $self = shift(@_);
     136    my $collection_name = shift(@_);
     137    my $infodb_directory_path = shift(@_);
     138    my $infodb_file_name = &util::get_dirsep_tail($collection_name) . '.' . $self->{'default_file_extension'};
     139    my $infodb_file_path = &FileUtils::filenameConcatenate($infodb_directory_path, $infodb_file_name);
     140    return $infodb_file_path;
     141}
     142## get_infodb_file_path(string, string) => string ##
     143
     144
     145## @function supportsDatestamp(void) => boolean
     146#
     147sub supportsDatestamp
     148{
     149    my $self = shift(@_);
     150    return 0;
     151}
     152## supportsDatestamp(void) => boolean ##
     153
     154
     155## @function supportsMerge(void) => boolean
     156#
     157sub supportsMerge
     158{
     159    my $self = shift(@_);
     160    return 0;
     161}
     162## supportsMerge(void) => boolean ##
     163
     164
     165## @function supportsRSS(void) => boolean
     166#
     167sub supportsRSS
     168{
     169    my $self = shift(@_);
     170    return 0;
     171}
     172## supportsRSS(void) => boolean ##
    113173
    114174
     
    118178
    119179
    120 ## @function close_infodb_write_handle
     180## @function close_infodb_write_handle(*) => void
    121181#
    122182sub close_infodb_write_handle
    123183{
    124     gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
    125     die("\n");
    126 }
    127 ## close_infodb_write_handle ##
    128 
    129 
    130 ## @function delete_infodb_entry
     184    my $self = shift(@_);
     185    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
     186    die("\n");
     187}
     188## close_infodb_write_handle(*) => void ##
     189
     190
     191## @function delete_infodb_entry(*) => void
    131192#
    132193sub delete_infodb_entry
    133194{
    134     gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
    135     die("\n");
    136 }
    137 ## delete_infodb_entry ##
    138 
    139 
    140 ## @function get_infodb_file_path
    141 #
    142 sub get_infodb_file_path
    143 {
    144     gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
    145     die("\n");
    146 }
    147 ## get_infodb_file_path ##
    148 
    149 
    150 ## @function merge_databases
    151 #
    152 sub merge_databases
    153 {
    154     gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
    155     die("\n");
    156 }
    157 ## merge_databases ##
    158 
    159 
    160 ## @function open_infodb_write_handle
     195    my $self = shift(@_);
     196    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
     197    die("\n");
     198}
     199## delete_infodb_entry(*) => void ##
     200
     201
     202## @function mergeDatabases(*) => void
     203#
     204sub mergeDatabases
     205{
     206    my $self = shift(@_);
     207    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
     208    die("\n");
     209}
     210## mergeDatabases(*) => void ##
     211
     212
     213## @function open_infodb_write_handle(*) => void
    161214#
    162215sub open_infodb_write_handle
    163216{
    164     gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
    165     die("\n");
    166 }
    167 ## open_infodb_write_handle ##
    168 
    169 
    170 ## @function set_infodb_entry()
     217    my $self = shift(@_);
     218    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
     219    die("\n");
     220}
     221## open_infodb_write_handle(*) => void ##
     222
     223
     224## @function set_infodb_entry(*) => void
    171225#
    172226sub set_infodb_entry
    173227{
    174     gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
    175     die("\n");
    176 }
    177 ## set_infodb_entry() => void ##
    178 
    179 
    180 ## @function read_infodb_file
     228    my $self = shift(@_);
     229    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
     230    die("\n");
     231}
     232## set_infodb_entry(*) => void ##
     233
     234
     235## @function read_infodb_rawentry(*) => string
     236#
     237sub read_infodb_rawentry
     238{
     239    my $self = shift(@_);
     240    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
     241    die("\n");
     242}
     243## read_infodb_rawentry(*) => string ##
     244
     245
     246## @function read_infodb_file(*) => void
    181247#
    182248sub read_infodb_file
    183249{
    184     gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
    185     die("\n");
    186 }
    187 ## read_infodb_file ##
    188 
    189 
    190 ## @function read_infodb_keys
     250    my $self = shift(@_);
     251    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
     252    die("\n");
     253}
     254## read_infodb_file(*) => void ##
     255
     256
     257## @function read_infodb_keys(*) => void
    191258#
    192259sub read_infodb_keys
    193260{
    194     gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
    195     die("\n");
    196 }
    197 ## read_infodb_keys ##
    198 
    199 
    200 ## @function write_infodb_entry
     261    my $self = shift(@_);
     262    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
     263    die("\n");
     264}
     265## read_infodb_keys(*) => void ##
     266
     267
     268## @function write_infodb_entry(*) => void
    201269#
    202270sub write_infodb_entry
    203271{
    204     gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
    205     die("\n");
    206 }
    207 ## write_infodb_entry ##
    208 
    209 
    210 ## @function write_infodb_rawentry
     272    my $self = shift(@_);
     273    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
     274    die("\n");
     275}
     276## write_infodb_entry(*) => void ##
     277
     278
     279## @function write_infodb_rawentry(*) => void
    211280#
    212281sub write_infodb_rawentry
    213282{
    214     gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
    215     die("\n");
    216 }
    217 ## write_infodb_rawentry ##
     283    my $self = shift(@_);
     284    gsprintf(STDERR, (caller(0))[3] . " {common.must_be_implemented}\n");
     285    die("\n");
     286}
     287## write_infodb_rawentry(*) => void ##
     288
    218289
    2192901;
Note: See TracChangeset for help on using the changeset viewer.