Search in sources :

Example 26 with SimpleFSDirectory

use of org.apache.lucene.store.SimpleFSDirectory in project elasticsearch by elastic.

the class FsDirectoryServiceTests method testHasNoSleepWrapperOnNormalFS.

public void testHasNoSleepWrapperOnNormalFS() throws IOException {
    Settings build = Settings.builder().put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), "simplefs").build();
    IndexSettings settings = IndexSettingsModule.newIndexSettings("foo", build);
    IndexStore store = new IndexStore(settings);
    Path tempDir = createTempDir().resolve(settings.getUUID()).resolve("0");
    Files.createDirectories(tempDir);
    ShardPath path = new ShardPath(false, tempDir, tempDir, new ShardId(settings.getIndex(), 0));
    FsDirectoryService fsDirectoryService = new FsDirectoryService(settings, store, path);
    Directory directory = fsDirectoryService.newDirectory();
    assertFalse(directory instanceof SleepingLockWrapper);
    assertTrue(directory instanceof SimpleFSDirectory);
}
Also used : ShardPath(org.elasticsearch.index.shard.ShardPath) Path(java.nio.file.Path) ShardId(org.elasticsearch.index.shard.ShardId) ShardPath(org.elasticsearch.index.shard.ShardPath) IndexSettings(org.elasticsearch.index.IndexSettings) SleepingLockWrapper(org.apache.lucene.store.SleepingLockWrapper) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) MMapDirectory(org.apache.lucene.store.MMapDirectory) Directory(org.apache.lucene.store.Directory) FileSwitchDirectory(org.apache.lucene.store.FileSwitchDirectory)

Example 27 with SimpleFSDirectory

use of org.apache.lucene.store.SimpleFSDirectory in project elasticsearch by elastic.

the class KeyStoreWrapper method save.

/** Write the keystore to the given config directory. */
void save(Path configDir) throws Exception {
    char[] password = this.keystorePassword.get().getPassword();
    SimpleFSDirectory directory = new SimpleFSDirectory(configDir);
    // write to tmp file first, then overwrite
    String tmpFile = KEYSTORE_FILENAME + ".tmp";
    try (IndexOutput output = directory.createOutput(tmpFile, IOContext.DEFAULT)) {
        CodecUtil.writeHeader(output, KEYSTORE_FILENAME, FORMAT_VERSION);
        output.writeByte(password.length == 0 ? (byte) 0 : (byte) 1);
        output.writeString(type);
        output.writeString(secretFactory.getAlgorithm());
        ByteArrayOutputStream keystoreBytesStream = new ByteArrayOutputStream();
        keystore.get().store(keystoreBytesStream, password);
        byte[] keystoreBytes = keystoreBytesStream.toByteArray();
        output.writeInt(keystoreBytes.length);
        output.writeBytes(keystoreBytes, keystoreBytes.length);
        CodecUtil.writeFooter(output);
    }
    Path keystoreFile = keystorePath(configDir);
    Files.move(configDir.resolve(tmpFile), keystoreFile, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
    PosixFileAttributeView attrs = Files.getFileAttributeView(keystoreFile, PosixFileAttributeView.class);
    if (attrs != null) {
        // don't rely on umask: ensure the keystore has minimal permissions
        attrs.setPermissions(PosixFilePermissions.fromString("rw-------"));
    }
}
Also used : Path(java.nio.file.Path) IndexOutput(org.apache.lucene.store.IndexOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 28 with SimpleFSDirectory

use of org.apache.lucene.store.SimpleFSDirectory in project lucene-solr by apache.

the class TestIndexWriter method testWithPendingDeletions.

public void testWithPendingDeletions() throws Exception {
    // irony: currently we don't emulate windows well enough to work on windows!
    assumeFalse("windows is not supported", Constants.WINDOWS);
    Path path = createTempDir();
    // Use WindowsFS to prevent open files from being deleted:
    FileSystem fs = new WindowsFS(path.getFileSystem()).getFileSystem(URI.create("file:///"));
    Path root = new FilterPath(path, fs);
    // MMapDirectory doesn't work because it closes its file handles after mapping!
    try (FSDirectory dir = new SimpleFSDirectory(root)) {
        IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
        IndexWriter w = new IndexWriter(dir, iwc);
        w.commit();
        IndexInput in = dir.openInput("segments_1", IOContext.DEFAULT);
        w.addDocument(new Document());
        w.close();
        assertTrue(dir.checkPendingDeletions());
        // make sure we get NFSF if we try to delete and already-pending-delete file:
        expectThrows(NoSuchFileException.class, () -> {
            dir.deleteFile("segments_1");
        });
        IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
            new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())));
        });
        assertTrue(expected.getMessage().contains("still has pending deleted files; cannot initialize IndexWriter"));
        in.close();
    }
}
Also used : FilterPath(org.apache.lucene.mockfile.FilterPath) Path(java.nio.file.Path) WindowsFS(org.apache.lucene.mockfile.WindowsFS) FilterPath(org.apache.lucene.mockfile.FilterPath) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) FileSystem(java.nio.file.FileSystem) IndexInput(org.apache.lucene.store.IndexInput) FSDirectory(org.apache.lucene.store.FSDirectory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) Document(org.apache.lucene.document.Document) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory)

Example 29 with SimpleFSDirectory

use of org.apache.lucene.store.SimpleFSDirectory in project lucene-solr by apache.

the class TestReplicationHandlerBackup method verify.

private void verify(Path backup, int nDocs) throws IOException {
    try (Directory dir = new SimpleFSDirectory(backup);
        IndexReader reader = DirectoryReader.open(dir)) {
        IndexSearcher searcher = new IndexSearcher(reader);
        TopDocs hits = searcher.search(new MatchAllDocsQuery(), 1);
        assertEquals(nDocs, hits.totalHits);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) IndexReader(org.apache.lucene.index.IndexReader) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) Directory(org.apache.lucene.store.Directory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory)

Example 30 with SimpleFSDirectory

use of org.apache.lucene.store.SimpleFSDirectory in project tika by apache.

the class RecentFiles method generateRSS.

public String generateRSS(File indexFile) throws CorruptIndexException, IOException {
    StringBuffer output = new StringBuffer();
    output.append(getRSSHeaders());
    IndexSearcher searcher = null;
    try {
        reader = IndexReader.open(new SimpleFSDirectory(indexFile));
        searcher = new IndexSearcher(reader);
        GregorianCalendar gc = new java.util.GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
        gc.setTime(new Date());
        String nowDateTime = ISO8601.format(gc);
        gc.add(java.util.GregorianCalendar.MINUTE, -5);
        String fiveMinsAgo = ISO8601.format(gc);
        TermRangeQuery query = new TermRangeQuery(Metadata.DATE.toString(), fiveMinsAgo, nowDateTime, true, true);
        TopScoreDocCollector collector = TopScoreDocCollector.create(20, true);
        searcher.search(query, collector);
        ScoreDoc[] hits = collector.topDocs().scoreDocs;
        for (int i = 0; i < hits.length; i++) {
            Document doc = searcher.doc(hits[i].doc);
            output.append(getRSSItem(doc));
        }
    } finally {
        if (reader != null)
            reader.close();
        if (searcher != null)
            searcher.close();
    }
    output.append(getRSSFooters());
    return output.toString();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopScoreDocCollector(org.apache.lucene.search.TopScoreDocCollector) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) GregorianCalendar(java.util.GregorianCalendar) Document(org.apache.lucene.document.Document) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) Date(java.util.Date) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Aggregations

SimpleFSDirectory (org.apache.lucene.store.SimpleFSDirectory)37 Directory (org.apache.lucene.store.Directory)23 Path (java.nio.file.Path)15 IOException (java.io.IOException)13 File (java.io.File)9 IndexWriter (org.apache.lucene.index.IndexWriter)9 FSDirectory (org.apache.lucene.store.FSDirectory)7 Settings (org.elasticsearch.common.settings.Settings)7 LockObtainFailedException (org.apache.lucene.store.LockObtainFailedException)6 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)5 IndexSearcher (org.apache.lucene.search.IndexSearcher)5 FilterDirectory (org.apache.lucene.store.FilterDirectory)5 IndexInput (org.apache.lucene.store.IndexInput)5 InputStream (java.io.InputStream)4 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)4 Dictionary (org.apache.lucene.analysis.hunspell.Dictionary)4 IndexReader (org.apache.lucene.index.IndexReader)4 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)4 MMapDirectory (org.apache.lucene.store.MMapDirectory)4 NIOFSDirectory (org.apache.lucene.store.NIOFSDirectory)4