Changeset 17907 for greenstone3/trunk


Ignore:
Timestamp:
2008-11-24T15:43:01+13:00 (15 years ago)
Author:
ak19
Message:

Inserted code to first look for whether the db file is already there. If not, it looks for the txtgz file. If that exists, it unzips the txtgz file and converts it to the ldb (bdb?) database file. The same process was followed in the original Greenstone2 code (gdbmclass.cpp in common-src/src/lib).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/util/GDBMWrapper.java

    r16869 r17907  
    2626
    2727import java.io.UnsupportedEncodingException;
     28import java.io.File;
    2829
    2930/** java wrapper class for gdbm - uses Java-GDBM written by Martin Pool
     
    4243     NEWDB - read/write access, db should be replaced if exists
    4344  */
    44  
    45 
     45   
     46   
    4647    protected GdbmFile db_=null;
    47 
     48   
    4849  /** open the database filename, with mode mode - uses the constants
    4950      above, eg GdbmFile.WRITER */
     
    5859      mode = GdbmFile.READER;
    5960    }
    60      
    6161    try {
    62       if (db_!=null) {
    63     db_.close();
    64       }
    65       db_ = new GdbmFile(filename, mode);
     62    if (db_!=null) {
     63        db_.close();
     64    }
     65
     66    // The java version of the C++ code in common-src/src/lib/gdbmclass.cpp
     67    if(mode == GdbmFile.READER) {
     68        // Looking to read in the database
     69        // => check to see if .ldb/.bdb file already there
     70        // if not (first time) then generate using txt2db
     71        if (!new File(filename).exists()) {
     72        logger.warn("Database file " + filename + " does not exist. Looking for txtgz version of db file.");
     73       
     74        // need to generate architecture native GDBM file using txt2db
     75       
     76        // replace sought after gdbm filename ext with ".txt.gz"
     77       
     78        int extension = filename.lastIndexOf('.');
     79        String txtgzFilename = filename.substring(0, extension) + ".txt.gz";
     80        if(new File(txtgzFilename).exists()) {         
     81            // Test to make sure Perl is on the path
     82            // On Linux, the output of the test goes to STDOUT so redirect it to STDERR
     83            String cmdTest = "perl -v 2>&1";           
     84            //String cmdTest = "echo %PATH%";
     85            int returnValue = Processing.runProcess(cmdTest);
     86            if (returnValue != 0) {
     87            logger.error("Tried to find Perl. Return exit value of running "
     88                     + cmdTest + ": " +  returnValue + ", (expected this to be 0)");
     89            logger.error("Check that Perl is set in your PATH environment variable.");
     90            //log.error("At present, PATH=" + System.getenv("PATH"));
     91            }
     92           
     93            String cmd = "perl -S txtgz-to-gdbm.pl \"" + txtgzFilename + "\" \"" + filename + "\"";
     94            returnValue = Processing.runProcess(cmd);
     95            // For some reason, launching this command with gsdl_system() still returns 1
     96            // even when it returns 0 when run from the command-line. We can check whether
     97            // we succeeded by looking at whether the output database file was created.
     98            if (returnValue != 0) {
     99            logger.warn("Warning, non-zero return value on running command \"" + cmd + "\": " + returnValue);
     100            if (!new File(filename).exists()) {
     101                logger.error("Tried to run command \"" + cmd + "\", but it failed");
     102            }
     103            }           
     104        }
     105        }
     106    }
     107
     108    db_ = new GdbmFile(filename, mode);
    66109    } catch ( GdbmException e) { // the database wasn't opened or created
    67       logger.error("couldn't open database "+filename);
    68       return false;
     110    logger.error("couldn't open database "+filename);
     111    return false;
    69112    }
    70113    db_.setKeyPacking(new StringPacking());
Note: See TracChangeset for help on using the changeset viewer.