List of usage examples for org.apache.lucene.store Directory copyFrom
public void copyFrom(Directory from, String src, String dest, IOContext context) throws IOException
From source file:com.impetus.kundera.index.LuceneIndexer.java
License:Apache License
private void copy(Directory src, Directory to) throws IOException { for (String file : src.listAll()) { src.copyFrom(src, file, to.toString(), IOContext.DEFAULT); }// w w w . jav a 2 s. c om }
From source file:com.vmware.xenon.services.common.LuceneDocumentIndexBackupService.java
License:Open Source License
private void restoreFromLocal(Operation op, Path restoreFrom, Long timeSnapshotBoundaryMicros, InternalDocumentIndexInfo indexInfo) { LuceneDocumentIndexService luceneIndexService = indexInfo.luceneIndexService; IndexWriter w = indexInfo.indexWriter; if (w == null) { op.fail(new CancellationException("writer is null")); return;/*from w ww. j av a2 s . c o m*/ } boolean isInMemoryIndex = indexInfo.indexDirectory == null; boolean restoreFromZipFile = Files.isRegularFile(restoreFrom); // resolve index directory path for filesystem based index Path restoreTo = null; if (!isInMemoryIndex) { restoreTo = Paths.get(getHost().getStorageSandbox()).resolve(indexInfo.indexDirectory); } Set<Path> pathToDeleteAtFinally = new HashSet<>(); // We already have a slot in the semaphore. Acquire the rest. final int semaphoreCount = QUERY_THREAD_COUNT + UPDATE_THREAD_COUNT - 1; try { indexInfo.writerSync.acquire(semaphoreCount); luceneIndexService.close(w); // extract zip file to temp directory if (restoreFromZipFile && isInMemoryIndex) { Path tempDir = Files.createTempDirectory("restore-" + Utils.getSystemNowMicrosUtc()); pathToDeleteAtFinally.add(tempDir); logInfo("extracting zip file to temporal directory %s", tempDir); FileUtils.extractZipArchive(restoreFrom.toFile(), tempDir); // now behave as if it was restoring from directory restoreFromZipFile = false; restoreFrom = tempDir; } IndexWriter newWriter; if (restoreFromZipFile) { // index service is always on filesystem since zip with in-memory is already checked above // perform restore from zip file (original behavior) logInfo("restoring index %s from %s md5sum(%s)", restoreTo, restoreFrom, FileUtils.md5sum(restoreFrom.toFile())); FileUtils.extractZipArchive(restoreFrom.toFile(), restoreTo); newWriter = luceneIndexService.createWriter(restoreTo.toFile(), true); } else { // perform restore from directory if (isInMemoryIndex) { logInfo("restoring in-memory index from directory %s", restoreFrom); // copy to lucene ram directory Directory from = MMapDirectory.open(restoreFrom); Directory to = new RAMDirectory(); for (String filename : from.listAll()) { to.copyFrom(from, filename, filename, IOContext.DEFAULT); } newWriter = luceneIndexService.createWriterWithLuceneDirectory(to, true); } else { logInfo("restoring index %s from directory %s", restoreTo, restoreFrom); // Copy whatever was there out just in case. if (Files.list(restoreTo).count() > 0) { logInfo("archiving existing index %s", restoreTo); luceneIndexService.archiveCorruptIndexFiles(restoreTo.toFile()); } FileUtils.copyFiles(restoreFrom.toFile(), restoreTo.toFile()); newWriter = luceneIndexService.createWriter(restoreTo.toFile(), true); } } // perform time snapshot recovery if (timeSnapshotBoundaryMicros != null) { performTimeSnapshotRecovery(timeSnapshotBoundaryMicros, newWriter); } luceneIndexService.setWriterUpdateTimeMicros(Utils.getNowMicrosUtc()); op.complete(); logInfo("restore complete"); } catch (Exception e) { logSevere(e); op.fail(e); } finally { luceneIndexService.writerSync.release(semaphoreCount); for (Path path : pathToDeleteAtFinally) { try { Files.deleteIfExists(path); } catch (IOException e) { } } } }
From source file:org.apache.maven.index.context.IndexUtils.java
License:Apache License
public static void copyDirectory(Directory source, Directory target) throws IOException { //FIXME: check if this copies too much, Lucene 4 has no filter for lucene files //Directory.copy( source, target, false ); for (String file : source.listAll()) { target.copyFrom(source, file, file, IOContext.DEFAULT); }/*from ww w. j av a 2 s .co m*/ copyFile(source, target, IndexingContext.INDEX_UPDATER_PROPERTIES_FILE); copyFile(source, target, IndexingContext.INDEX_PACKER_PROPERTIES_FILE); Date ts = getTimestamp(source); updateTimestamp(target, ts); }
From source file:org.apache.maven.index.context.IndexUtils.java
License:Apache License
public static boolean copyFile(Directory source, Directory target, String srcName, String targetName) throws IOException { try {/* w ww . j a v a 2s . c o m*/ source.fileLength(srcName); // instead of fileExists } catch (FileNotFoundException | NoSuchFileException e) { return false; } target.copyFrom(source, srcName, targetName, IOContext.DEFAULT); return true; }
From source file:org.apache.solr.core.backup.repository.HdfsBackupRepository.java
License:Apache License
@Override public void copyFileTo(URI sourceRepo, String fileName, Directory dest) throws IOException { try (HdfsDirectory dir = new HdfsDirectory(new Path(sourceRepo), NoLockFactory.INSTANCE, hdfsConfig, HdfsDirectory.DEFAULT_BUFFER_SIZE)) { dest.copyFrom(dir, fileName, fileName, DirectoryFactory.IOCONTEXT_NO_CACHE); }/* www. ja va2 s.co m*/ }
From source file:org.apache.solr.core.backup.repository.LocalFileSystemRepository.java
License:Apache License
@Override public void copyFileTo(URI sourceDir, String fileName, Directory dest) throws IOException { try (FSDirectory dir = new SimpleFSDirectory(Paths.get(sourceDir), NoLockFactory.INSTANCE)) { dest.copyFrom(dir, fileName, fileName, DirectoryFactory.IOCONTEXT_NO_CACHE); }/* www . j a v a 2 s . com*/ }
From source file:org.apache.solr.handler.RestoreCore.java
License:Apache License
public boolean doRestore() throws Exception { URI backupPath = backupRepo.resolve(backupLocation, backupName); SimpleDateFormat dateFormat = new SimpleDateFormat(SnapShooter.DATE_FMT, Locale.ROOT); String restoreIndexName = "restore." + dateFormat.format(new Date()); String restoreIndexPath = core.getDataDir() + restoreIndexName; String indexDirPath = core.getIndexDir(); Directory restoreIndexDir = null; Directory indexDir = null;// www . j a v a2 s . c om try { restoreIndexDir = core.getDirectoryFactory().get(restoreIndexPath, DirectoryFactory.DirContext.DEFAULT, core.getSolrConfig().indexConfig.lockType); //Prefer local copy. indexDir = core.getDirectoryFactory().get(indexDirPath, DirectoryFactory.DirContext.DEFAULT, core.getSolrConfig().indexConfig.lockType); //Move all files from backupDir to restoreIndexDir for (String filename : backupRepo.listAll(backupPath)) { checkInterrupted(); log.info("Copying file {} to restore directory ", filename); try (IndexInput indexInput = backupRepo.openInput(backupPath, filename, IOContext.READONCE)) { Long checksum = null; try { checksum = CodecUtil.retrieveChecksum(indexInput); } catch (Exception e) { log.warn("Could not read checksum from index file: " + filename, e); } long length = indexInput.length(); IndexFetcher.CompareResult compareResult = IndexFetcher.compareFile(indexDir, filename, length, checksum); if (!compareResult.equal || (IndexFetcher.filesToAlwaysDownloadIfNoChecksums(filename, length, compareResult))) { backupRepo.copyFileTo(backupPath, filename, restoreIndexDir); } else { //prefer local copy restoreIndexDir.copyFrom(indexDir, filename, filename, IOContext.READONCE); } } catch (Exception e) { log.warn("Exception while restoring the backup index ", e); throw new SolrException(SolrException.ErrorCode.UNKNOWN, "Exception while restoring the backup index", e); } } log.debug("Switching directories"); core.modifyIndexProps(restoreIndexName); boolean success; try { core.getUpdateHandler().newIndexWriter(false); openNewSearcher(); success = true; log.info("Successfully restored to the backup index"); } catch (Exception e) { //Rollback to the old index directory. Delete the restore index directory and mark the restore as failed. log.warn("Could not switch to restored index. Rolling back to the current index", e); Directory dir = null; try { dir = core.getDirectoryFactory().get(core.getDataDir(), DirectoryFactory.DirContext.META_DATA, core.getSolrConfig().indexConfig.lockType); dir.deleteFile(IndexFetcher.INDEX_PROPERTIES); } finally { if (dir != null) { core.getDirectoryFactory().release(dir); } } core.getDirectoryFactory().doneWithDirectory(restoreIndexDir); core.getDirectoryFactory().remove(restoreIndexDir); core.getUpdateHandler().newIndexWriter(false); openNewSearcher(); throw new SolrException(SolrException.ErrorCode.UNKNOWN, "Exception while restoring the backup index", e); } if (success) { core.getDirectoryFactory().doneWithDirectory(indexDir); // Cleanup all index files not associated with any *named* snapshot. core.deleteNonSnapshotIndexFiles(indexDirPath); } return true; } finally { if (restoreIndexDir != null) { core.getDirectoryFactory().release(restoreIndexDir); } if (indexDir != null) { core.getDirectoryFactory().release(indexDir); } } }