/*
 * This file is part of ***  M y C o R e  ***
 * See http://www.mycore.de/ for details.
 *
 * MyCoRe is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MyCoRe is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.mycore.datamodel.ifs2;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Iterator;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

/**
 * Stores file collections containing files and directories.
 * 
 * For each store, properties must be defined, for example
 * 
 * MCR.IFS2.Store.ID.Class=org.mycore.datamodel.ifs2.MCRFileStore
 * MCR.IFS2.Store.ID.BaseDir=/foo/bar MCR.IFS2.Store.ID.SlotLayout=4-2-2
 * 
 * @author Frank Lützenkirchen
 */
public class MCRFileStore extends MCRStore {

    private final MCRFileStore thisInstance;

    //MCR-1868: prevents parallel threads to read and write mcrmeta.xml concurrently on instantiation
    LoadingCache<Integer, MCRFileCollection> collectionLoadingCache = CacheBuilder.newBuilder()
        .weakValues()
        .build(new CacheLoader<>() {
            @Override
            public MCRFileCollection load(Integer key) throws Exception {
                return new MCRFileCollection(thisInstance, key);
            }
        });

    public MCRFileStore() {
        super();
        thisInstance = this;
    }

    /**
     * Creates and stores a new, empty file collection using the next free ID in
     * the store.
     * 
     * @return a newly created file collection
     */
    public MCRFileCollection create() throws IOException {
        int id = getNextFreeID();
        return create(id);
    }

    /**
     * Creates and stores a new, empty file collection with the given ID
     * 
     * @param id
     *            the ID of the file collection
     * @return a newly created file collection
     * @throws IOException
     *             when a file collection with the given ID already exists
     */
    public MCRFileCollection create(int id) throws IOException {
        Path path = getSlot(id);
        if (Files.exists(path)) {
            String msg = "FileCollection with ID " + id + " already exists";
            throw new IOException(msg);
        }
        return collectionLoadingCache.getUnchecked(id);
    }

    @Override
    public void delete(int id) throws IOException {
        Path path = getSlot(id);
        if (Files.exists(path)) {
            super.delete(path);
        }
        collectionLoadingCache.invalidate(id);
    }

    /**
     * Returns the file collection stored under the given ID, or null when no
     * collection is stored for the given ID.
     * 
     * @param id
     *            the file collection's ID
     * @return the file collection with the given ID, or null
     */
    public MCRFileCollection retrieve(int id) {
        Path path = getSlot(id);
        if (!Files.exists(path)) {
            return null;
        } else {
            return collectionLoadingCache.getUnchecked(id);
        }
    }

    /**
     * Repairs metadata of all file collections stored here
     * 
     */
    public void repairAllMetadata() throws IOException {
        for (Iterator<Integer> e = listIDs(MCRStore.ASCENDING); e.hasNext();) {
            retrieve(e.next()).repairMetadata();
        }
    }
}
