All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jparams.store.index.IndexManager Maven / Gradle / Ivy

There is a newer version: 3.1.4
Show newest version
package com.jparams.store.index;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.jparams.store.reference.Reference;

public abstract class IndexManager
{
    private final Map> indexMap;

    public IndexManager(final Collection> indexes)
    {
        this.indexMap = new HashMap<>();
        indexes.forEach(index -> indexMap.put(index.getName(), index));
    }

    public  ReferenceIndex createIndex(final String indexName, final IndexDefinition indexDefinition, final Collection> references)
    {
        if (indexMap.containsKey(indexName))
        {
            throw new IllegalArgumentException("An index already exists with this name");
        }

        final ReferenceIndex newIndex = createIndex(indexName, indexDefinition);
        indexMap.put(indexName, newIndex);
        indexReferences(Collections.singleton(newIndex), references);
        return newIndex;
    }

    public ReferenceIndex getIndex(final String indexName)
    {
        return indexMap.get(indexName);
    }

    public void reindex(final Collection> references)
    {
        indexReferences(indexMap.values(), references);
    }

    public boolean removeIndex(final String indexName)
    {
        return indexMap.remove(indexName) != null;
    }

    public boolean removeIndex(final Index index)
    {
        if (index.equals(indexMap.get(index.getName())))
        {
            indexMap.remove(index.getName());
            return true;
        }

        return false;
    }

    public void removeReference(final Reference reference)
    {
        indexMap.values().forEach(index -> index.removeIndex(reference));
    }

    public void clear()
    {
        indexMap.values().forEach(ReferenceIndex::clear);
    }

    public Collection> getIndexes()
    {
        return Collections.unmodifiableCollection(indexMap.values());
    }

    public IndexManager copy()
    {
        final Set> copyOfIndexes = indexMap.values().stream().map(ReferenceIndex::copy).collect(Collectors.toSet());
        return createCopy(copyOfIndexes);
    }

    protected abstract IndexManager createCopy(Set> copyOfIndexes);

    protected abstract  ReferenceIndex createIndex(String indexName, IndexDefinition indexDefinition);

    private static  void indexReferences(final Collection> indexes, final Collection> references)
    {
        final List exceptions = new ArrayList<>();

        for (final Reference reference : references)
        {
            for (final ReferenceIndex index : indexes)
            {
                try
                {
                    index.index(reference);
                }
                catch (final IndexCreationException e)
                {
                    exceptions.add(e);
                }
            }
        }

        if (!exceptions.isEmpty())
        {
            final String message = (exceptions.size() == 1 ? "1 exception" : exceptions.size() + " exceptions") + " occurred during indexing";
            throw new IndexException(message, exceptions);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy