001/*
002 * #%L
003 * Netarchivesuite - common
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 */
023
024package dk.netarkivet.common.distribute.indexserver;
025
026import java.io.File;
027import java.util.Set;
028
029import dk.netarkivet.common.CommonSettings;
030import dk.netarkivet.common.exceptions.ArgumentNotValid;
031import dk.netarkivet.common.exceptions.IOFailure;
032import dk.netarkivet.common.exceptions.NotImplementedException;
033import dk.netarkivet.common.utils.FileUtils;
034import dk.netarkivet.common.utils.Settings;
035
036/**
037 * A trivial JobIndexCache implementation that just assumes somebody places the indexes in the right place (in
038 * TrivialJobIndexCache under the cache dir).
039 */
040public class TrivialJobIndexCache implements JobIndexCache {
041
042    private static final String CACHE_SUBDIR = "TrivialJobIndexCache";
043
044    private final File dir = new File(Settings.get(CommonSettings.CACHE_DIR), CACHE_SUBDIR);
045
046    private final RequestType requestType;
047
048    /**
049     * Construct a trivial cache that requires manual setup of files.
050     * <p>
051     * The directory that the files are to be put into will be created by this method.
052     *
053     * @param t The type of requests handled
054     */
055    public TrivialJobIndexCache(RequestType t) {
056        ArgumentNotValid.checkNotNull(t, "RequestType t");
057        requestType = t;
058        FileUtils.createDir(dir);
059    }
060
061    /**
062     * Get an index for the given list of job IDs. The resulting file contains a suitably sorted list. This method
063     * should always be safe for asynchronous calling. This method may use a cached version of the file.
064     *
065     * @param jobIDs Set of job IDs to generate index for.
066     * @return A file containing the index, and always the full set. The file must not be modified or deleted, since it
067     * is part of the cache of data.
068     * @throws IOFailure if there is no cache file for the set.
069     */
070    @Override
071    public Index<Set<Long>> getIndex(Set<Long> jobIDs) {
072        ArgumentNotValid.checkNotNull(jobIDs, "Set<Long> jobIDs");
073
074        File cacheFile = new File(dir, FileUtils.generateFileNameFromSet(jobIDs, "-" + requestType + "-cache"));
075
076        if (!cacheFile.exists()) {
077            throw new IOFailure("The cache does not contain '" + cacheFile + "' for " + jobIDs);
078        }
079        return new Index<Set<Long>>(cacheFile, jobIDs);
080    }
081
082    @Override
083    public void requestIndex(Set<Long> jobSet, Long harvestId) {
084        throw new NotImplementedException("This feature is not implemented for this type of cache");
085    }
086
087}