Search in sources :

Example 1 with IndexMetadata

use of org.apache.stanbol.commons.solr.managed.IndexMetadata in project stanbol by apache.

the class ManagedIndexMetadata method updateIndexProperties.

/**
 * Adds, update and deletes index metadata
 * @param name the name of the index (can be <code>null</code> of properties are parsed)
 * @param properties the properties or <code>null</code> to remove
 * @param clone If <code>true</code> the parsed properties are cloned. Clones
 * are required for parsed properties to prevent external changes of properties
 * stored in the internal lists. Only parse <code>false</code> in case the
 * parsed properties are already a clone
 * @return The old {@link IndexMetadata} instance (especially usefull in case of
 * remove operations)
 */
private IndexMetadata updateIndexProperties(String name, IndexMetadata properties, boolean clone) {
    if (name == null && properties == null) {
        return null;
    }
    if (name != null && properties != null && !name.equals(properties.getIndexName())) {
        throw new IllegalArgumentException("The value of the Index-Name property '" + properties.getIndexName() + "' is not the same as the parsed name '" + name + "'!");
    }
    if (name == null) {
        name = properties.getIndexName();
    }
    // first persist
    if (properties != null) {
        try {
            saveIndexConfig(name, properties);
        } catch (IOException e) {
            log.error("Unable to store Properties (see Exception below): {}", properties.toString());
            throw new IllegalStateException("Unable to save Index metadata for index '" + name + "'!", e);
        }
    } else {
        removeIndexConfig(name);
    }
    // clone is meaningless if properties are NULL
    if (clone && properties != null) {
        IndexMetadata tmp = properties;
        properties = new IndexMetadata();
        properties.putAll(tmp);
    }
    Map<String, IndexMetadata> toAdd, toRemove;
    IndexMetadata oldMetadata = null;
    synchronized (inMemoryModelLock) {
        ManagedIndexState currentState = getState(name);
        if (currentState != null) {
            toRemove = managed.get(currentState);
        } else {
            toRemove = null;
        }
        if (properties == null) {
            // remove
            toAdd = null;
        } else {
            ManagedIndexState newState = properties.getState();
            toAdd = managed.get(newState);
        }
        // now update in-memory state
        if (toRemove != null) {
            oldMetadata = toRemove.remove(name);
        }
        if (toAdd != null) {
            toAdd.put(name, properties);
        }
        // now update the archive name to core name mappings
        if (oldMetadata != null) {
            for (String indexArchive : oldMetadata.getIndexArchives()) {
                Collection<String> indexes = archiveName2CoreName.get(indexArchive);
                if (indexes.remove(name) && indexes.isEmpty()) {
                    archiveName2CoreName.remove(indexArchive);
                }
            }
        }
        if (properties != null) {
            for (String indexArchive : properties.getIndexArchives()) {
                Collection<String> indexes = archiveName2CoreName.get(indexArchive);
                if (indexes == null) {
                    indexes = new HashSet<String>();
                    archiveName2CoreName.put(indexArchive, indexes);
                }
                indexes.add(name);
            }
        }
    }
    return oldMetadata;
}
Also used : IOException(java.io.IOException) IndexMetadata(org.apache.stanbol.commons.solr.managed.IndexMetadata) ManagedIndexState(org.apache.stanbol.commons.solr.managed.ManagedIndexState)

Example 2 with IndexMetadata

use of org.apache.stanbol.commons.solr.managed.IndexMetadata in project stanbol by apache.

the class ManagedIndexMetadata method getIndexMetadata.

/**
 * Getter for the metadata of all indexes in a given state. Changing the
 * returned {@link Collection} or the Entries does not affect the state
 * of this class.
 * @param state the state
 * @return the metadata of all the indexes in that state (empty if none,
 * <code>null</code> if <code>null</code> was parsed as state)
 */
public Collection<IndexMetadata> getIndexMetadata(ManagedIndexState state) {
    if (state == null) {
        return null;
    }
    Collection<IndexMetadata> clones = new HashSet<IndexMetadata>();
    synchronized (inMemoryModelLock) {
        for (IndexMetadata metadata : managed.get(state).values()) {
            IndexMetadata clone = new IndexMetadata();
            clone.putAll(metadata);
            clones.add(clone);
        }
    }
    return clones;
}
Also used : IndexMetadata(org.apache.stanbol.commons.solr.managed.IndexMetadata) HashSet(java.util.HashSet)

Example 3 with IndexMetadata

use of org.apache.stanbol.commons.solr.managed.IndexMetadata in project stanbol by apache.

the class ManagedIndexMetadata method loadIndexConfigs.

/**
 * Loads the configurations of uninitialised Solr Indexes
 *
 * @return the map with the index name as key and the properties as values
 * @throws IOException
 *             on any error while loading the configurations
 */
private Map<String, IndexMetadata> loadIndexConfigs() throws IOException {
    File uninstalledConfigDir = getIndexConfigDirectory(false);
    Map<String, IndexMetadata> configs = new HashMap<String, IndexMetadata>();
    synchronized (pid) {
        if (uninstalledConfigDir.exists()) {
            for (String file : uninstalledConfigDir.list(new SuffixFileFilter(ConfigUtils.SOLR_INDEX_ARCHIVE_EXTENSION + ".ref"))) {
                String indexName = file.substring(0, file.indexOf('.'));
                File configFile = new File(uninstalledConfigDir, file);
                IndexMetadata props = new IndexMetadata();
                InputStream is = null;
                try {
                    is = new FileInputStream(configFile);
                    props.load(is);
                    // validate Index-Name and Server-Name properties!
                    if (!indexName.equals(props.getIndexName())) {
                        throw new IOException("The IndexName '" + props.getIndexName() + "within the IndexConfig file does not correspond to the file name '" + file + "'!");
                    }
                    if (!serverName.equals(props.getServerName())) {
                        throw new IOException("The Name of the Referenced Solr server '" + serverName + " does not correspond with the Server-Name value '" + props.getServerName() + "' within the property file '" + file + "'!");
                    }
                    configs.put(indexName, props);
                } finally {
                    IOUtils.closeQuietly(is);
                }
            }
        }
    }
    return configs;
}
Also used : HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SuffixFileFilter(org.apache.commons.io.filefilter.SuffixFileFilter) IOException(java.io.IOException) IndexMetadata(org.apache.stanbol.commons.solr.managed.IndexMetadata) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 4 with IndexMetadata

use of org.apache.stanbol.commons.solr.managed.IndexMetadata in project stanbol by apache.

the class ManagedSolrServerImpl method deactivateIndex.

@Override
public IndexMetadata deactivateIndex(String indexName) {
    IndexMetadata metadata = managedCores.getIndexMetadata(indexName);
    if (metadata != null && metadata.getState() == ManagedIndexState.ACTIVE) {
        try {
            deactivateCore(indexName, server);
            metadata.setState(ManagedIndexState.INACTIVE);
        } catch (RuntimeException e) {
            metadata.setError(e);
        } finally {
            managedCores.store(metadata);
        }
    }
    return metadata;
}
Also used : IndexMetadata(org.apache.stanbol.commons.solr.managed.IndexMetadata)

Example 5 with IndexMetadata

use of org.apache.stanbol.commons.solr.managed.IndexMetadata in project stanbol by apache.

the class ManagedSolrServerImpl method updateIndex.

@Override
public IndexMetadata updateIndex(String indexName, String resourceName, Properties properties) throws IOException {
    // NOTE: this does not deactivate the current index version, but only updates
    // the metadata and re-registers the DataFileTracking
    IndexMetadata oldMetadata = managedCores.getIndexMetadata(indexName);
    IndexMetadata metadata = new IndexMetadata();
    if (properties != null) {
        metadata.putAll(properties);
    }
    metadata.setServerName(serverName);
    metadata.setIndexName(indexName);
    metadata.setIndexArchives(Collections.singletonList(resourceName));
    if (oldMetadata != null) {
        // we need to
        // same as for the old version
        metadata.setState(oldMetadata.getState());
        metadata.setDirectory(oldMetadata.getDirectory());
    } else {
        metadata.setState(ManagedIndexState.UNINITIALISED);
    }
    // TODO: we need to deal with the synchronised property!
    // now add the index to the list of uninitialised
    managedCores.store(metadata);
    indexArchiveTracker.updateTracking(oldMetadata, metadata);
    return metadata;
}
Also used : IndexMetadata(org.apache.stanbol.commons.solr.managed.IndexMetadata)

Aggregations

IndexMetadata (org.apache.stanbol.commons.solr.managed.IndexMetadata)21 IOException (java.io.IOException)7 File (java.io.File)3 HashMap (java.util.HashMap)3 ManagedIndexState (org.apache.stanbol.commons.solr.managed.ManagedIndexState)3 SAXException (org.xml.sax.SAXException)3 InputStream (java.io.InputStream)2 HashSet (java.util.HashSet)2 SolrCore (org.apache.solr.core.SolrCore)2 IndexReference (org.apache.stanbol.commons.solr.IndexReference)2 FileInputStream (java.io.FileInputStream)1 EnumMap (java.util.EnumMap)1 Map (java.util.Map)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)1 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)1 SuffixFileFilter (org.apache.commons.io.filefilter.SuffixFileFilter)1 Activate (org.apache.felix.scr.annotations.Activate)1 SolrServer (org.apache.solr.client.solrj.SolrServer)1 HttpSolrServer (org.apache.solr.client.solrj.impl.HttpSolrServer)1