001/*
002 * #%L
003 * Netarchivesuite - archive
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023package dk.netarkivet.archive.tools;
024
025import java.io.BufferedReader;
026import java.io.File;
027import java.io.FileReader;
028import java.io.IOException;
029import java.util.Date;
030
031import com.sleepycat.je.DatabaseException;
032
033import dk.netarkivet.archive.ArchiveSettings;
034import dk.netarkivet.archive.checksum.DatabaseChecksumArchive;
035import dk.netarkivet.common.exceptions.IOFailure;
036import dk.netarkivet.common.utils.KeyValuePair;
037import dk.netarkivet.common.utils.Settings;
038import dk.netarkivet.common.utils.batch.ChecksumJob;
039
040/**
041 * Program for uploading data from the filebased FileChecksumArchive to a DatabaseChecksumArchive. The two arguments are
042 * /full/path/to/databaseBaseDirectory and /full/path/to/checksum_CS.md5
043 */
044public class LoadDatabaseChecksumArchive {
045    /**
046     * Main program for the LoadDatabaseChecksumArchive class
047     *
048     * @param args two arguments /full/path/to/databaseBaseDirectory and /full/path/to/checksum_CS.md5
049     * @throws IOFailure
050     * @throws DatabaseException
051     */
052    public static void main(String[] args) throws IOFailure, DatabaseException {
053        if (args.length != 2) {
054            System.err.println("Missing args. Required args:  "
055                    + " /full/path/to/databaseBaseDirectory /full/path/to/checksum_CS.md5");
056            System.exit(1);
057        }
058        File databaseBasedir = new File(args[0]);
059        File checksumCSFile = new File(args[1]);
060
061        if (!databaseBasedir.isDirectory()) {
062            String errMsg = "databaseBaseDirectory '" + databaseBasedir.getAbsolutePath()
063                    + "' does not exist or is a file instead";
064            throw new IOFailure(errMsg);
065        }
066        System.out.println("Started loading database at: " + new Date());
067        Settings.set(ArchiveSettings.CHECKSUM_BASEDIR, databaseBasedir.getAbsolutePath());
068        DatabaseChecksumArchive dca = new DatabaseChecksumArchive();
069
070        BufferedReader in = null;
071        int loginterval = 10000;
072        int currentLine = 0;
073        try {
074            try {
075                in = new BufferedReader(new FileReader(checksumCSFile));
076                String line;
077                while ((line = in.readLine()) != null) {
078                    currentLine++;
079                    if (currentLine % loginterval == 0) {
080                        System.out.println("Processing line " + currentLine);
081                    }
082                    KeyValuePair<String, String> entry = ChecksumJob.parseLine(line);
083                    dca.put(entry.getKey(), entry.getValue());
084                }
085            } finally {
086                if (in != null) {
087                    in.close();
088                }
089            }
090        } catch (IOException e) {
091            String msg = "Could not read data from " + checksumCSFile.getAbsolutePath();
092            throw new IOFailure(msg, e);
093        }
094
095        System.out.println("Finished importing " + currentLine + " lines into the database at " + new Date());
096    }
097}