001/*
002 * #%L
003 * Netarchivesuite - harvester
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.harvester.indexserver;
025
026import java.io.File;
027import java.util.Set;
028
029import dk.netarkivet.common.utils.FileUtils;
030
031/**
032 * Implementation of file based cache, that works with the assumption we are working on a set if ids, of which we might
033 * only get a subset correct.
034 * <p>
035 * Implements generating a filename from this.
036 *
037 * @param <T> The cache type, must extend java.lang.Comparable.
038 */
039public abstract class MultiFileBasedCache<T extends Comparable<T>> extends FileBasedCache<Set<T>> {
040
041    /**
042     * Creates a new FileBasedCache object. This creates a directory under the main cache directory holding cached
043     * files.
044     *
045     * @param cacheName Name of this cache (enabling sharing among processes). The directoriy creating in the cachedir
046     * will have this name.
047     */
048    public MultiFileBasedCache(String cacheName) {
049        super(cacheName);
050    }
051
052    /**
053     * Get the filename for the file containing the combined data for a set of IDs.
054     *
055     * @param ids A set of IDs to generate a filename for
056     * @return A filename that uniquely identifies this set of IDs within the cache. It is considered acceptable to have
057     * collisions at a likelihood the order of 1/2^128 (i.e. use MD5 to abbreviate long lists).
058     */
059    public File getCacheFile(Set<T> ids) {
060        String fileName = FileUtils.generateFileNameFromSet(ids, "-cache");
061        return new File(getCacheDir(), fileName);
062    }
063
064}