Search in sources :

Example 1 with PropertiesInputStream

use of org.apache.solr.util.PropertiesInputStream in project lucene-solr by apache.

the class SolrCore method writeNewIndexProps.

/**
   * Write the index.properties file with the new index sub directory name
   * @param dir a data directory (containing an index.properties file)
   * @param tmpFileName the file name to write the new index.properties to
   * @param tmpIdxDirName new index directory name
   */
private static void writeNewIndexProps(Directory dir, String tmpFileName, String tmpIdxDirName) {
    if (tmpFileName == null) {
        tmpFileName = IndexFetcher.INDEX_PROPERTIES;
    }
    final Properties p = new Properties();
    // Read existing properties
    try {
        final IndexInput input = dir.openInput(IndexFetcher.INDEX_PROPERTIES, DirectoryFactory.IOCONTEXT_NO_CACHE);
        final InputStream is = new PropertiesInputStream(input);
        try {
            p.load(new InputStreamReader(is, StandardCharsets.UTF_8));
        } catch (Exception e) {
            log.error("Unable to load " + IndexFetcher.INDEX_PROPERTIES, e);
        } finally {
            IOUtils.closeQuietly(is);
        }
    } catch (IOException e) {
    // ignore; file does not exist
    }
    p.put("index", tmpIdxDirName);
    // Write new properties
    Writer os = null;
    try {
        IndexOutput out = dir.createOutput(tmpFileName, DirectoryFactory.IOCONTEXT_NO_CACHE);
        os = new OutputStreamWriter(new PropertiesOutputStream(out), StandardCharsets.UTF_8);
        p.store(os, IndexFetcher.INDEX_PROPERTIES);
        dir.sync(Collections.singleton(tmpFileName));
    } catch (Exception e) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unable to write " + IndexFetcher.INDEX_PROPERTIES, e);
    } finally {
        IOUtils.closeQuietly(os);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) PropertiesInputStream(org.apache.solr.util.PropertiesInputStream) InputStream(java.io.InputStream) IndexInput(org.apache.lucene.store.IndexInput) IndexOutput(org.apache.lucene.store.IndexOutput) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Properties(java.util.Properties) PropertiesInputStream(org.apache.solr.util.PropertiesInputStream) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) IOException(java.io.IOException) NoSuchFileException(java.nio.file.NoSuchFileException) SolrException(org.apache.solr.common.SolrException) FileNotFoundException(java.io.FileNotFoundException) KeeperException(org.apache.zookeeper.KeeperException) PHPSerializedResponseWriter(org.apache.solr.response.PHPSerializedResponseWriter) XMLResponseWriter(org.apache.solr.response.XMLResponseWriter) SolrIndexWriter(org.apache.solr.update.SolrIndexWriter) SmileResponseWriter(org.apache.solr.response.SmileResponseWriter) GeoJSONResponseWriter(org.apache.solr.response.GeoJSONResponseWriter) BinaryResponseWriter(org.apache.solr.response.BinaryResponseWriter) PythonResponseWriter(org.apache.solr.response.PythonResponseWriter) JSONResponseWriter(org.apache.solr.response.JSONResponseWriter) SchemaXmlResponseWriter(org.apache.solr.response.SchemaXmlResponseWriter) IndexWriter(org.apache.lucene.index.IndexWriter) Writer(java.io.Writer) PHPResponseWriter(org.apache.solr.response.PHPResponseWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) GraphMLResponseWriter(org.apache.solr.response.GraphMLResponseWriter) RubyResponseWriter(org.apache.solr.response.RubyResponseWriter) CSVResponseWriter(org.apache.solr.response.CSVResponseWriter) OutputStreamWriter(java.io.OutputStreamWriter) RawResponseWriter(org.apache.solr.response.RawResponseWriter) SolrException(org.apache.solr.common.SolrException) PropertiesOutputStream(org.apache.solr.util.PropertiesOutputStream)

Example 2 with PropertiesInputStream

use of org.apache.solr.util.PropertiesInputStream in project lucene-solr by apache.

the class SolrCore method getNewIndexDir.

/**
   * Returns the indexdir as given in index.properties. If index.properties exists in dataDir and
   * there is a property <i>index</i> available and it points to a valid directory
   * in dataDir that is returned Else dataDir/index is returned. Only called for creating new indexSearchers
   * and indexwriters. Use the getIndexDir() method to know the active index directory
   *
   * @return the indexdir as given in index.properties
   */
public String getNewIndexDir() {
    String result = dataDir + "index/";
    Properties p = new Properties();
    Directory dir = null;
    try {
        dir = getDirectoryFactory().get(getDataDir(), DirContext.META_DATA, getSolrConfig().indexConfig.lockType);
        IndexInput input;
        try {
            input = dir.openInput(IndexFetcher.INDEX_PROPERTIES, IOContext.DEFAULT);
        } catch (FileNotFoundException | NoSuchFileException e) {
            input = null;
        }
        if (input != null) {
            final InputStream is = new PropertiesInputStream(input);
            try {
                p.load(new InputStreamReader(is, StandardCharsets.UTF_8));
                String s = p.getProperty("index");
                if (s != null && s.trim().length() > 0) {
                    result = dataDir + s;
                }
            } catch (Exception e) {
                log.error("Unable to load " + IndexFetcher.INDEX_PROPERTIES, e);
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    } catch (IOException e) {
        SolrException.log(log, "", e);
    } finally {
        if (dir != null) {
            try {
                getDirectoryFactory().release(dir);
            } catch (IOException e) {
                SolrException.log(log, "", e);
            }
        }
    }
    if (!result.equals(lastNewIndexDir)) {
        log.debug("New index directory detected: old=" + lastNewIndexDir + " new=" + result);
    }
    lastNewIndexDir = result;
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) PropertiesInputStream(org.apache.solr.util.PropertiesInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) IndexInput(org.apache.lucene.store.IndexInput) IOException(java.io.IOException) Properties(java.util.Properties) PropertiesInputStream(org.apache.solr.util.PropertiesInputStream) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) IOException(java.io.IOException) NoSuchFileException(java.nio.file.NoSuchFileException) SolrException(org.apache.solr.common.SolrException) FileNotFoundException(java.io.FileNotFoundException) KeeperException(org.apache.zookeeper.KeeperException) Directory(org.apache.lucene.store.Directory)

Example 3 with PropertiesInputStream

use of org.apache.solr.util.PropertiesInputStream in project lucene-solr by apache.

the class BackupManager method readBackupProperties.

/**
   * This method returns the configuration parameters for the specified backup.
   *
   * @param backupLoc The base path used to store the backup data.
   * @param backupId  The unique name for the backup whose configuration params are required.
   * @return the configuration parameters for the specified backup.
   * @throws IOException In case of errors.
   */
public Properties readBackupProperties(URI backupLoc, String backupId) throws IOException {
    Objects.requireNonNull(backupLoc);
    Objects.requireNonNull(backupId);
    // Backup location
    URI backupPath = repository.resolve(backupLoc, backupId);
    if (!repository.exists(backupPath)) {
        throw new SolrException(ErrorCode.SERVER_ERROR, "Couldn't restore since doesn't exist: " + backupPath);
    }
    Properties props = new Properties();
    try (Reader is = new InputStreamReader(new PropertiesInputStream(repository.openInput(backupPath, BACKUP_PROPS_FILE, IOContext.DEFAULT)), StandardCharsets.UTF_8)) {
        props.load(is);
        return props;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Properties(java.util.Properties) URI(java.net.URI) PropertiesInputStream(org.apache.solr.util.PropertiesInputStream) SolrException(org.apache.solr.common.SolrException)

Example 4 with PropertiesInputStream

use of org.apache.solr.util.PropertiesInputStream in project lucene-solr by apache.

the class ReplicationHandler method loadReplicationProperties.

Properties loadReplicationProperties() {
    Directory dir = null;
    try {
        try {
            dir = core.getDirectoryFactory().get(core.getDataDir(), DirContext.META_DATA, core.getSolrConfig().indexConfig.lockType);
            IndexInput input;
            try {
                input = dir.openInput(IndexFetcher.REPLICATION_PROPERTIES, IOContext.DEFAULT);
            } catch (FileNotFoundException | NoSuchFileException e) {
                return new Properties();
            }
            try {
                final InputStream is = new PropertiesInputStream(input);
                Properties props = new Properties();
                props.load(new InputStreamReader(is, StandardCharsets.UTF_8));
                return props;
            } finally {
                input.close();
            }
        } finally {
            if (dir != null) {
                core.getDirectoryFactory().release(dir);
            }
        }
    } catch (IOException e) {
        throw new SolrException(ErrorCode.SERVER_ERROR, e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) PropertiesInputStream(org.apache.solr.util.PropertiesInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) IndexInput(org.apache.lucene.store.IndexInput) IOException(java.io.IOException) Properties(java.util.Properties) PropertiesInputStream(org.apache.solr.util.PropertiesInputStream) SolrException(org.apache.solr.common.SolrException) Directory(org.apache.lucene.store.Directory)

Aggregations

InputStreamReader (java.io.InputStreamReader)4 Properties (java.util.Properties)4 SolrException (org.apache.solr.common.SolrException)4 PropertiesInputStream (org.apache.solr.util.PropertiesInputStream)4 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 NoSuchFileException (java.nio.file.NoSuchFileException)3 IndexInput (org.apache.lucene.store.IndexInput)3 Directory (org.apache.lucene.store.Directory)2 LockObtainFailedException (org.apache.lucene.store.LockObtainFailedException)2 KeeperException (org.apache.zookeeper.KeeperException)2 FileInputStream (java.io.FileInputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1 URI (java.net.URI)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexOutput (org.apache.lucene.store.IndexOutput)1 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)1