[30355] | 1 | ###############################################################################
|
---|
| 2 | #
|
---|
| 3 | # DBDrivers/JDBM.pm -- utility functions for writing to jdbm databases
|
---|
| 4 | #
|
---|
| 5 | # A component of the Greenstone digital library software from the New Zealand
|
---|
| 6 | # Digital Library Project at the University of Waikato, New Zealand.
|
---|
| 7 | #
|
---|
| 8 | # Copyright (C) 1999-2015 New Zealand Digital Library Project
|
---|
| 9 | #
|
---|
| 10 | # This program is free software; you can redistribute it and/or modify it under
|
---|
| 11 | # the terms of the GNU General Public License as published by the Free Software
|
---|
| 12 | # Foundation; either version 2 of the License, or (at your option) any later
|
---|
| 13 | # version.
|
---|
| 14 | #
|
---|
| 15 | # This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
| 16 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
---|
| 17 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
---|
| 18 | # more details.
|
---|
| 19 | #
|
---|
| 20 | # You should have received a copy of the GNU General Public License along with
|
---|
| 21 | # this program; if not, write to the Free Software Foundation, Inc., 675 Mass
|
---|
| 22 | # Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | #
|
---|
| 24 | ###############################################################################
|
---|
| 25 |
|
---|
| 26 | package DBDrivers::JDBM;
|
---|
| 27 |
|
---|
| 28 | # Pragma
|
---|
| 29 | use strict;
|
---|
| 30 |
|
---|
| 31 | # Libraries
|
---|
| 32 | use util;
|
---|
| 33 | use FileUtils;
|
---|
[30370] | 34 | use DBDrivers::70HyphenFormat;
|
---|
[30355] | 35 |
|
---|
| 36 | sub BEGIN
|
---|
| 37 | {
|
---|
| 38 | if (!defined $ENV{'GSDLHOME'} || !defined $ENV{'GSDLOS'}) {
|
---|
| 39 | die("Error! Environment must be prepared by sourcing setup.bash\n");
|
---|
| 40 | }
|
---|
[30370] | 41 | @DBDrivers::JDBM::ISA = ('DBDrivers::70HyphenFormat');
|
---|
[30355] | 42 | }
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | ## @function constructor
|
---|
| 46 | #
|
---|
| 47 | sub new
|
---|
| 48 | {
|
---|
| 49 | my $class = shift(@_);
|
---|
| 50 | my $self = DBDrivers::70HyphenFormat->new(@_);
|
---|
| 51 | $self->{'default_file_extension'} = 'jdb';
|
---|
[30517] | 52 | $self->{'supports_concurrent_read_and_write'} = 1;
|
---|
[30355] | 53 |
|
---|
| 54 | # Executables need a little extra work since we are using Java
|
---|
| 55 | # - we need to build up the classpath continue the Jar libraries to use
|
---|
| 56 | my $jdbmwrap_jar = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, 'bin', 'java', 'JDBMWrapper.jar');
|
---|
| 57 | my $jdbm_jar = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, 'lib', 'java', 'jdbm.jar');
|
---|
| 58 | my $classpath = &util::pathname_cat($jdbmwrap_jar,$jdbm_jar);
|
---|
| 59 | # Massage paths for Cygwin. Away to run a java program, using a binary that
|
---|
| 60 | # is native to Windows, so need Windows directory and path separators
|
---|
[30370] | 61 | # Note: this is done after the util::pathname_cat as that fuction can also
|
---|
| 62 | # (incorrectly) change file separators.
|
---|
[30355] | 63 | if ($^O eq "cygwin") {
|
---|
| 64 | $classpath = `cygpath -wp "$classpath"`;
|
---|
| 65 | chomp($classpath);
|
---|
| 66 | $classpath =~ s%\\%\\\\%g;
|
---|
| 67 | }
|
---|
| 68 | $self->{'executable_path'} = '';
|
---|
| 69 | $self->{'read_executable'} = 'java -cp "' . $classpath . '" Jdb2Txt';
|
---|
| 70 | $self->{'keyread_executable'} = 'java -cp "' . $classpath . '" JdbKeys';
|
---|
| 71 | $self->{'write_executable'} = 'java -cp "' . $classpath . '" Txt2Jdb';
|
---|
| 72 | # Support
|
---|
| 73 | $self->{'supports_set'} = 1;
|
---|
| 74 |
|
---|
| 75 | bless($self, $class);
|
---|
| 76 | return $self;
|
---|
| 77 | }
|
---|
| 78 | ## constructor() ##
|
---|
| 79 |
|
---|
| 80 |
|
---|
[31208] | 81 | # jdb also creates .lg log files, that don't get removed on delete or move operations
|
---|
| 82 | # Make sure to rename them to when performing a rename operation on the main db file.
|
---|
| 83 | # dbutil::renameTo(src,dest) already took care of renaming the main db file.
|
---|
| 84 | sub rename_db_file_to {
|
---|
| 85 | my $self = shift(@_);
|
---|
| 86 |
|
---|
| 87 | $self->SUPER::rename_db_file_to(@_);
|
---|
| 88 | my ($srcpath, $destpath) = @_;
|
---|
| 89 |
|
---|
| 90 | my ($srctailname, $srcdirname, $srcsuffix)
|
---|
| 91 | = &File::Basename::fileparse($srcpath, "\\.[^\\.]+\$");
|
---|
| 92 | my ($desttailname, $destdirname, $destsuffix)
|
---|
| 93 | = &File::Basename::fileparse($destpath, "\\.[^\\.]+\$");
|
---|
| 94 |
|
---|
| 95 | # add in the lg extension
|
---|
| 96 | my $src_log_file = &FileUtils::filenameConcatenate($srcdirname, $srctailname.".lg");
|
---|
| 97 | my $dest_log_file = &FileUtils::filenameConcatenate($destdirname, $desttailname.".lg");
|
---|
| 98 |
|
---|
| 99 | # finally, move/rename any log file belonging to the src db file
|
---|
| 100 | if(&FileUtils::fileExists($src_log_file)) {
|
---|
| 101 | &FileUtils::moveFiles($src_log_file, $dest_log_file);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | # don't want to keep the log file for any files renamed to bak (backup file) though
|
---|
| 105 | if($destsuffix =~ m/bak$/) {
|
---|
| 106 | my $assoc_log_file = &FileUtils::filenameConcatenate($destdirname, $desttailname.".lg");
|
---|
| 107 | if(&FileUtils::fileExists($assoc_log_file)) {
|
---|
| 108 | &FileUtils::removeFiles($assoc_log_file);
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | sub remove_db_file {
|
---|
| 115 | my $self = shift(@_);
|
---|
| 116 |
|
---|
| 117 | print STDERR "###### |".join(",", @_) . "|\n";
|
---|
| 118 |
|
---|
| 119 | $self->SUPER::remove_db_file(@_);
|
---|
| 120 | my ($db_filepath) = @_;
|
---|
| 121 |
|
---|
| 122 | # add in the lg extension to get the log file name
|
---|
| 123 | my ($tailname, $dirname, $suffix) = &File::Basename::fileparse($db_filepath, "\\.[^\\.]+\$");
|
---|
| 124 | my $assoc_log_file = &FileUtils::filenameConcatenate($dirname, $tailname.".lg");
|
---|
| 125 |
|
---|
| 126 | # remove any log file associated with the db file
|
---|
| 127 | if(&FileUtils::fileExists($assoc_log_file)) {
|
---|
| 128 | &FileUtils::removeFiles($assoc_log_file);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 |
|
---|
[30355] | 135 | # -----------------------------------------------------------------------------
|
---|
| 136 | # JDBM IMPLEMENTATION
|
---|
| 137 | # -----------------------------------------------------------------------------
|
---|
| 138 |
|
---|
[31188] | 139 | # When DBUtil::* is properly structured with inheritance, then
|
---|
[30355] | 140 | # much of this code (along with GDBM and GDBM-TXT-GZ) can be grouped into
|
---|
| 141 | # a shared base class. Really it is only the the command that needs to
|
---|
| 142 | # be constructed that changes between much of the code that is used
|
---|
| 143 |
|
---|
| 144 | # Handled by BaseDBDriver
|
---|
| 145 | # sub get_infodb_file_path {}
|
---|
| 146 |
|
---|
[31188] | 147 | # Handled by 70HyphenFormat
|
---|
[30355] | 148 | # sub open_infodb_write_handle(string, string?) => filehandle
|
---|
| 149 | # sub close_infodb_write_handle(filehandle) => void
|
---|
| 150 | # sub read_infodb_file(string, hashmap) => void
|
---|
| 151 | # sub read_infodb_keys(string, hashmap) => void
|
---|
| 152 | # sub write_infodb_entry(filehandle, string, hashmap) => void
|
---|
| 153 | # sub write_infodb_rawentry(filehandle, string, string) => void
|
---|
| 154 | # sub set_infodb_entry(filehandle, string, string) => void
|
---|
| 155 | # sub delete_infodb_entry(filehandle, string) => void
|
---|
| 156 |
|
---|
| 157 | 1;
|
---|