Example usage for org.apache.lucene.store Directory deleteFile

List of usage examples for org.apache.lucene.store Directory deleteFile

Introduction

In this page you can find the example usage for org.apache.lucene.store Directory deleteFile.

Prototype

public abstract void deleteFile(String name) throws IOException;

Source Link

Document

Removes an existing file in the directory.

Usage

From source file:axiom.objectmodel.dom.LuceneManager.java

License:Open Source License

public static void commitSegments(String segmentsNew, Connection conn, Application app, Directory dir) {
    byte[] segmentContents = null;
    if (segmentsNew == null) {
        segmentsNew = TransFSDirectory.SEGMENTS_NEW;//TODO:IndexFileNames.getSegmentsNewFileName();
    }//from  w  w  w .  ja  v  a 2  s  .  co  m
    IndexInput input = null;
    try {
        input = dir.openInput(segmentsNew);
        int length = (int) input.length();
        segmentContents = new byte[length];
        try {
            input.readBytes(segmentContents, 0, length);
        } catch (IOException ioe) {
            segmentContents = null;
        }
    } catch (Exception ex) {
        app.logError(ErrorReporter.errorMsg(LuceneManager.class, "commitSegments"), ex);
        throw new TransactionException("LuceneTransaction.executeSubTransaction(): " + ex.getMessage());
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (Exception ignore) {
            }
            input = null;
        }
    }

    if (segmentContents == null || segmentContents.length == 0) {
        throw new TransactionException("LuceneTransaction.executeSubTransaction(): "
                + "The segments.new file does not contain any data to save.");
    }

    PreparedStatement pstmt = null;
    ByteArrayInputStream bais = null;
    boolean exceptionOccured = false;

    try {
        String sql = "UPDATE Lucene SET valid = ?, version = ? " + "WHERE valid = ? AND db_home = ?";
        pstmt = conn.prepareStatement(sql);
        int count = 1;
        pstmt.setBoolean(count++, false);
        pstmt.setInt(count++, getLuceneVersion());
        pstmt.setBoolean(count++, true);
        pstmt.setString(count++, app.getDbDir().getName());
        pstmt.executeUpdate();
        pstmt.close();
        pstmt = null;

        sql = "INSERT INTO Lucene (valid, db_home, segments, version) " + "VALUES (?,?,?,?)";
        pstmt = conn.prepareStatement(sql);
        count = 1;
        pstmt.setBoolean(count++, true);
        pstmt.setString(count++, app.getDbDir().getName());
        bais = new ByteArrayInputStream(segmentContents);
        pstmt.setBinaryStream(count++, bais, segmentContents.length);
        pstmt.setInt(count++, getLuceneVersion());
        int rows = pstmt.executeUpdate();
        if (rows < 1) {
            throw new Exception(
                    "LuceneTransactionManager.executeTransaction(): update didn't affect any rows in the database");
        }
    } catch (Exception ex) {
        exceptionOccured = true;
        throw new TransactionException(ex.getMessage());
    } finally {
        try {
            dir.deleteFile(segmentsNew);
        } catch (IOException ioex) {
            // i guess its okay if a random segments.new file is lying around, itll 
            // get overwritten on the next lucene write operation anyway
            app.logEvent(ErrorReporter.warningMsg(LuceneManager.class, "commitSegments") + "Could not delete "
                    + segmentsNew);
        }

        if (bais != null) {
            try {
                bais.close();
            } catch (Exception ignoreit) {
            }
            bais = null;
        }
        segmentContents = null;

        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException sqle) {
                if (!exceptionOccured) {
                    throw new TransactionException(sqle.getMessage());
                }
            }
            pstmt = null;
        }
    }
}

From source file:axiom.objectmodel.dom.LuceneManager.java

License:Open Source License

public static void commitSegments(String segmentsNew, Connection conn, File dbhome, Directory dir) {
    byte[] segmentContents = null;
    if (segmentsNew == null) {
        segmentsNew = TransFSDirectory.SEGMENTS_NEW;//TODO:IndexFileNames.getSegmentsNewFileName();
    }// w  w w . jav  a 2s  .c o  m
    IndexInput input = null;
    try {
        input = dir.openInput(segmentsNew);
        int length = (int) input.length();
        segmentContents = new byte[length];
        try {
            input.readBytes(segmentContents, 0, length);
        } catch (IOException ioe) {
            segmentContents = null;
        }
    } catch (Exception ex) {
        throw new TransactionException("LuceneTransaction.executeSubTransaction(): " + ex.getMessage());
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (Exception ignore) {
            }
            input = null;
        }
    }

    if (segmentContents == null || segmentContents.length == 0) {
        throw new TransactionException("LuceneTransaction.executeSubTransaction(): "
                + "The segments.new file does not contain any data to save.");
    }

    PreparedStatement pstmt = null;
    ByteArrayInputStream bais = null;
    boolean exceptionOccured = false;

    try {
        String sql = "UPDATE Lucene SET valid = ?, version = ? " + "WHERE valid = ? AND db_home = ?";
        pstmt = conn.prepareStatement(sql);
        int count = 1;
        pstmt.setBoolean(count++, false);
        pstmt.setInt(count++, getLuceneVersion());
        pstmt.setBoolean(count++, true);
        pstmt.setString(count++, dbhome.getName());
        pstmt.executeUpdate();
        pstmt.close();
        pstmt = null;

        sql = "INSERT INTO Lucene (valid, db_home, segments, version) " + "VALUES (?,?,?,?)";
        pstmt = conn.prepareStatement(sql);
        count = 1;
        pstmt.setBoolean(count++, true);
        pstmt.setString(count++, dbhome.getName());
        bais = new ByteArrayInputStream(segmentContents);
        pstmt.setBinaryStream(count++, bais, segmentContents.length);
        pstmt.setInt(count++, getLuceneVersion());
        int rows = pstmt.executeUpdate();
        System.out.println("EXECUTE update was a SUCCESS!!");
        if (rows < 1) {
            throw new Exception(
                    "LuceneTransactionManager.executeTransaction(): update didn't affect any rows in the database");
        }
    } catch (Exception ex) {
        exceptionOccured = true;
        throw new TransactionException(ex.getMessage());
    } finally {
        try {
            dir.deleteFile(segmentsNew);
        } catch (IOException ioex) {
            // i guess its okay if a random segments.new file is lying around, itll 
            // get overwritten on the next lucene write operation anyway
        }

        if (bais != null) {
            try {
                bais.close();
            } catch (Exception ignoreit) {
            }
            bais = null;
        }
        segmentContents = null;

        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException sqle) {
                if (!exceptionOccured) {
                    throw new TransactionException(sqle.getMessage());
                }
            }
            pstmt = null;
        }
    }
}

From source file:com.amalto.core.storage.hibernate.HibernateStorage.java

License:Open Source License

private void cleanFullTextIndex(List<ComplexTypeMetadata> sortedTypesToDrop) {
    if (dataSource.supportFullText()) {
        try {//from   w w  w  .  j  a v  a 2 s .c o m
            for (ComplexTypeMetadata typeMetadata : sortedTypesToDrop) {
                try {
                    Class<?> clazz = storageClassLoader
                            .loadClass(ClassCreator.getClassName(typeMetadata.getName()));
                    File directoryFile = new File(
                            dataSource.getIndexDirectory() + '/' + getName() + '/' + clazz.getName());
                    if (directoryFile.exists()) {
                        final Directory directory = FSDirectory.open(directoryFile);
                        final String lockName = "delete." + typeMetadata.getName(); //$NON-NLS-1$
                        // Default 5 sec timeout for lock
                        Lock.With lock = new Lock.With(directory.makeLock(lockName), 5000) {

                            @Override
                            protected Object doBody() throws IOException {
                                String[] files = directory.listAll();
                                for (String file : files) {
                                    if (!file.endsWith(lockName)) { // Don't delete our own lock
                                        directory.deleteFile(file);
                                    }
                                }
                                return null;
                            }
                        };
                        lock.run();
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("Removed full text directory for entity '" + typeMetadata.getName() //$NON-NLS-1$
                                    + "' at '" //$NON-NLS-1$
                                    + directoryFile.getAbsolutePath() + "'"); //$NON-NLS-1$
                        }
                    } else {
                        LOGGER.warn("Full text index directory for entity '" + typeMetadata.getName() //$NON-NLS-1$
                                + "' no longer exists. No need to delete it."); //$NON-NLS-1$
                    }
                } catch (Exception e) {
                    LOGGER.warn("Could not remove full text directory for '" + typeMetadata.getName() + "'.", //$NON-NLS-1$//$NON-NLS-2$
                            e);
                }
            }
        } catch (Exception e) {
            LOGGER.warn("Could not correctly clean full text directory.", e); //$NON-NLS-1$
        }
    }
}

From source file:com.bah.lucene.BaseDirectoryTestSuite.java

License:Apache License

private Directory getControlDir(final Directory control, final Directory test) {
    return new Directory() {

        @Override//from   w ww. j  av a  2 s  .co m
        public Lock makeLock(String name) {
            return control.makeLock(name);
        }

        @Override
        public void clearLock(String name) throws IOException {
            control.clearLock(name);
        }

        @Override
        public void setLockFactory(LockFactory lockFactory) throws IOException {
            control.setLockFactory(lockFactory);
        }

        @Override
        public LockFactory getLockFactory() {
            return control.getLockFactory();
        }

        @Override
        public String getLockID() {
            return control.getLockID();
        }

        @Override
        public void copy(Directory to, String src, String dest, IOContext context) throws IOException {
            control.copy(to, src, dest, context);
        }

        @Override
        public IndexInputSlicer createSlicer(String name, IOContext context) throws IOException {
            return control.createSlicer(name, context);
        }

        @Override
        public IndexOutput createOutput(final String name, IOContext context) throws IOException {
            final IndexOutput testOutput = test.createOutput(name, context);
            final IndexOutput controlOutput = control.createOutput(name, context);
            return new IndexOutput() {

                @Override
                public void flush() throws IOException {
                    testOutput.flush();
                    controlOutput.flush();
                }

                @Override
                public void close() throws IOException {
                    testOutput.close();
                    controlOutput.close();
                }

                @Override
                public long getFilePointer() {
                    long filePointer = testOutput.getFilePointer();
                    long controlFilePointer = controlOutput.getFilePointer();
                    if (controlFilePointer != filePointer) {
                        System.err.println("Output Name [" + name + "] with filePointer [" + filePointer
                                + "] and control filePointer [" + controlFilePointer + "] does not match");
                    }
                    return filePointer;
                }

                @SuppressWarnings("deprecation")
                @Override
                public void seek(long pos) throws IOException {
                    testOutput.seek(pos);
                    controlOutput.seek(pos);
                }

                @Override
                public long length() throws IOException {
                    long length = testOutput.length();
                    long controlLength = controlOutput.length();
                    if (controlLength != length) {
                        System.err.println("Ouput Name [" + name + "] with length [" + length
                                + "] and control length [" + controlLength + "] does not match");
                    }
                    return length;
                }

                @Override
                public void writeByte(byte b) throws IOException {
                    testOutput.writeByte(b);
                    controlOutput.writeByte(b);
                }

                @Override
                public void writeBytes(byte[] b, int offset, int length) throws IOException {
                    testOutput.writeBytes(b, offset, length);
                    controlOutput.writeBytes(b, offset, length);
                }

            };
        }

        @Override
        public IndexInput openInput(final String name, IOContext context) throws IOException {
            final IndexInput testInput = test.openInput(name, context);
            final IndexInput controlInput = control.openInput(name, context);
            return new IndexInputCompare(name, testInput, controlInput);
        }

        @Override
        public String[] listAll() throws IOException {
            return test.listAll();
        }

        @Override
        public boolean fileExists(String name) throws IOException {
            return test.fileExists(name);
        }

        @Override
        public void deleteFile(String name) throws IOException {
            test.deleteFile(name);
            control.deleteFile(name);
        }

        @Override
        public long fileLength(String name) throws IOException {
            long fileLength = test.fileLength(name);
            long controlFileLength = control.fileLength(name);
            if (controlFileLength != fileLength) {
                System.err.println("Input Name [" + name + "] with length [" + fileLength
                        + "] and control length [" + controlFileLength + "] does not match");
            }
            return fileLength;
        }

        @Override
        public void sync(Collection<String> names) throws IOException {
            test.sync(names);
            test.sync(names);
        }

        @Override
        public void close() throws IOException {
            test.close();
            control.close();
        }
    };
}

From source file:com.bdaum.zoom.lal.internal.LireActivator.java

License:Open Source License

public void releaseIndexWriter(File indexPath, boolean force) throws CorruptIndexException, IOException {
    synchronized (indexWriterMap) {
        IndexWriterEntry entry = indexWriterMap.get(indexPath);
        if (entry != null) {
            if (--entry.count <= 0 || force) {
                try {
                    IndexWriter writer = entry.writer;
                    @SuppressWarnings("resource")
                    Directory directory = writer.getDirectory();
                    writer.close();/*from   w  ww.  j ava 2  s  . com*/
                    directory.deleteFile("write.lock"); //$NON-NLS-1$
                    directory.close();
                } finally {
                    indexWriterMap.remove(indexPath);
                }
            }
        }
    }
}

From source file:com.google.gerrit.pgm.Reindex.java

License:Apache License

private void deleteAll() throws IOException {
    for (String index : SCHEMA_VERSIONS.keySet()) {
        File file = new File(sitePaths.index_dir, index);
        if (file.exists()) {
            Directory dir = FSDirectory.open(file);
            try {
                for (String name : dir.listAll()) {
                    dir.deleteFile(name);
                }//  w  w  w. j a v a 2 s . c om
            } finally {
                dir.close();
            }
        }
    }
}

From source file:com.ibm.watson.developer_cloud.professor_languo.ingestion.indexing.LuceneIndexer.java

License:Open Source License

/**
 * clear all the index files in the Lucence directory
 * //from   ww w .j  av  a2  s .c om
 * @param indexDir - Lucene directory to store index file
 * @throws IngestionException
 */
private void clearIndexDirectory(Directory indexDir) throws IngestionException {
    if (indexDir != null) {
        try {
            String[] files = indexDir.listAll();
            for (String file : files)
                indexDir.deleteFile(file);
        } catch (IOException e) {
            throw new IngestionException(e);
        }
    }
}

From source file:it.unibz.instasearch.indexing.StorageIndexer.java

License:Open Source License

/**
 * Delethe the whole index//from w w w .j  a  va  2  s  .  co  m
 * @throws Exception
 */
public void deleteIndex() throws Exception {

    RetryingRunnable runnable = new RetryingRunnable() {
        public void run() throws Exception {
            IndexWriter w = createIndexWriter(true); // open for writing and close (make empty)
            w.deleteAll();
            w.commit();
            w.close(true);

            Directory dir = getIndexDir();
            for (String file : dir.listAll()) {
                if (dir.fileExists(file)) // still exits
                {
                    dir.sync(file);
                    dir.deleteFile(file);
                }
            }
            dir.close();
        }

        public boolean handleException(Throwable e) {
            return true;
        }
    };

    changeListener.onIndexReset(); // close searcher because index is deleted

    runRetryingRunnable(runnable); // delete index with retry
}

From source file:org.apache.jackrabbit.core.query.lucene.IndexInfos.java

License:Apache License

/**
 * Creates a new IndexInfos using <code>baseName</code> and reads the
 * current generation./*from   w  w w  . j a v  a 2 s  .  com*/
 *
 * @param dir the directory where the index infos are stored.
 * @param baseName the name of the file where infos are stored.
 * @throws IOException if an error occurs while reading the index infos
 * file.
 */
IndexInfos(Directory dir, String baseName) throws IOException {
    this.directory = dir;
    this.name = baseName;
    long gens[] = getGenerations(getFileNames(dir, baseName), baseName);
    if (gens.length == 0) {
        // write initial infos
        write();
    } else {
        // read most recent generation
        for (int i = gens.length - 1; i >= 0; i--) {
            try {
                this.generation = gens[i];
                read();
                break;
            } catch (EOFException e) {
                String fileName = getFileName(gens[i]);
                log.warn("deleting invalid index infos file: " + fileName);
                dir.deleteFile(fileName);
                // reset generation
                this.generation = 0;
            }
        }
    }
}

From source file:org.apache.jackrabbit.oak.plugins.index.lucene.IndexCopier.java

License:Apache License

private boolean deleteFile(Directory dir, String fileName, boolean copiedFromRemote) {
    LocalIndexFile file = new LocalIndexFile(dir, fileName, getFileLength(dir, fileName), copiedFromRemote);
    boolean successFullyDeleted = false;
    try {//from ww  w.  j  a v a2 s.  c  om
        boolean fileExisted = false;
        if (dir.fileExists(fileName)) {
            fileExisted = true;
            dir.deleteFile(fileName);
        }
        successfullyDeleted(file, fileExisted);
        successFullyDeleted = true;
    } catch (IOException e) {
        failedToDelete(file);
        log.debug("Error occurred while removing deleted file {} from Local {}. "
                + "Attempt would be made to delete it on next run ", fileName, dir, e);
    }
    return successFullyDeleted;
}