Search in sources :

Example 1 with InternalLuceneIndex

use of org.apache.geode.cache.lucene.internal.InternalLuceneIndex in project geode by apache.

the class DumpDirectoryFilesJUnitTest method createMocks.

@Before
public void createMocks() throws BucketNotFoundException {
    GemFireCacheImpl cache = Fakes.cache();
    context = mock(RegionFunctionContext.class);
    ResultSender sender = mock(ResultSender.class);
    Region region = mock(Region.class);
    InternalLuceneService service = mock(InternalLuceneService.class);
    InternalLuceneIndex index = mock(InternalLuceneIndex.class);
    RepositoryManager repoManager = mock(RepositoryManager.class);
    IndexRepository repo = mock(IndexRepository.class);
    IndexWriter writer = mock(IndexWriter.class);
    RegionDirectory directory = mock(RegionDirectory.class);
    fileSystem = mock(FileSystem.class);
    Region bucket = mock(Region.class);
    when(bucket.getFullPath()).thenReturn(bucketName);
    when(context.getArguments()).thenReturn(new String[] { directoryName, indexName });
    when(context.getResultSender()).thenReturn(sender);
    when(context.getDataSet()).thenReturn(region);
    when(region.getCache()).thenReturn(cache);
    when(cache.getService(any())).thenReturn(service);
    when(repoManager.getRepositories(eq(context))).thenReturn(Collections.singleton(repo));
    when(index.getRepositoryManager()).thenReturn(repoManager);
    when(index.getName()).thenReturn(indexName);
    when(service.getIndex(eq(indexName), any())).thenReturn(index);
    when(directory.getFileSystem()).thenReturn(fileSystem);
    when(writer.getDirectory()).thenReturn(directory);
    when(repo.getWriter()).thenReturn(writer);
    when(repo.getRegion()).thenReturn(bucket);
}
Also used : IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) IndexWriter(org.apache.lucene.index.IndexWriter) InternalLuceneIndex(org.apache.geode.cache.lucene.internal.InternalLuceneIndex) FileSystem(org.apache.geode.cache.lucene.internal.filesystem.FileSystem) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) Region(org.apache.geode.cache.Region) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) RepositoryManager(org.apache.geode.cache.lucene.internal.repository.RepositoryManager) InternalLuceneService(org.apache.geode.cache.lucene.internal.InternalLuceneService) ResultSender(org.apache.geode.cache.execute.ResultSender) Before(org.junit.Before)

Example 2 with InternalLuceneIndex

use of org.apache.geode.cache.lucene.internal.InternalLuceneIndex in project geode by apache.

the class DumpDirectoryFilesIntegrationTest method shouldDumpReadableLuceneIndexFile.

@Test
public void shouldDumpReadableLuceneIndexFile() throws Exception {
    luceneService.createIndexFactory().setFields("title", "description").create(INDEX_NAME, REGION_NAME);
    Region region = createRegion(REGION_NAME, RegionShortcut.PARTITION);
    region.put(0, new TestObject("title 1", "hello world"));
    region.put(1 * 113, new TestObject("title 2", "this will not match"));
    region.put(2 * 113, new TestObject("title 3", "hello world"));
    region.put(3 * 113, new TestObject("hello world", "hello world"));
    InternalLuceneIndex index = (InternalLuceneIndex) luceneService.getIndex(INDEX_NAME, REGION_NAME);
    luceneService.waitUntilFlushed(INDEX_NAME, REGION_NAME, 60000, TimeUnit.MILLISECONDS);
    index.dumpFiles(diskDirRule.get().getAbsolutePath());
    // Find the directory for the first bucket
    File bucket0 = diskDirRule.get().listFiles(file -> file.getName().endsWith("_0"))[0];
    // Test that we can read the lucene index from the dump
    final FSDirectory directory = FSDirectory.open(bucket0.toPath());
    IndexReader reader = DirectoryReader.open(directory);
    IndexSearcher searcher = new IndexSearcher(reader);
    final TopDocs results = searcher.search(new MatchAllDocsQuery(), 1000);
    assertEquals(4, results.totalHits);
}
Also used : InternalLuceneIndex(org.apache.geode.cache.lucene.internal.InternalLuceneIndex) TopDocs(org.apache.lucene.search.TopDocs) DirectoryReader(org.apache.lucene.index.DirectoryReader) RegionShortcut(org.apache.geode.cache.RegionShortcut) Test(org.junit.Test) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Category(org.junit.experimental.categories.Category) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) LuceneTestUtilities(org.apache.geode.cache.lucene.test.LuceneTestUtilities) Region(org.apache.geode.cache.Region) LuceneIntegrationTest(org.apache.geode.cache.lucene.LuceneIntegrationTest) DiskDirRule(org.apache.geode.test.junit.rules.DiskDirRule) TestObject(org.apache.geode.cache.lucene.test.TestObject) Assert(org.junit.Assert) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest) FSDirectory(org.apache.lucene.store.FSDirectory) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) InternalLuceneIndex(org.apache.geode.cache.lucene.internal.InternalLuceneIndex) IndexReader(org.apache.lucene.index.IndexReader) Region(org.apache.geode.cache.Region) TestObject(org.apache.geode.cache.lucene.test.TestObject) FSDirectory(org.apache.lucene.store.FSDirectory) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) File(java.io.File) Test(org.junit.Test) LuceneIntegrationTest(org.apache.geode.cache.lucene.LuceneIntegrationTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 3 with InternalLuceneIndex

use of org.apache.geode.cache.lucene.internal.InternalLuceneIndex in project geode by apache.

the class DumpDirectoryFiles method execute.

@Override
public void execute(FunctionContext context) {
    RegionFunctionContext ctx = (RegionFunctionContext) context;
    if (!(context.getArguments() instanceof String[])) {
        throw new IllegalArgumentException("Arguments should be a string array");
    }
    String[] args = (String[]) context.getArguments();
    if (args.length != 2) {
        throw new IllegalArgumentException("Expected 2 arguments: exportLocation, indexName");
    }
    String exportLocation = args[0];
    String indexName = args[1];
    final Region<Object, Object> region = ctx.getDataSet();
    LuceneService service = LuceneServiceProvider.get(ctx.getDataSet().getCache());
    InternalLuceneIndex index = (InternalLuceneIndex) service.getIndex(indexName, region.getFullPath());
    if (index == null) {
        throw new IllegalStateException("Index not found for region " + region + " index " + indexName);
    }
    final RepositoryManager repoManager = index.getRepositoryManager();
    try {
        final Collection<IndexRepository> repositories = repoManager.getRepositories(ctx);
        repositories.stream().forEach(repo -> {
            final IndexWriter writer = repo.getWriter();
            RegionDirectory directory = (RegionDirectory) writer.getDirectory();
            FileSystem fs = directory.getFileSystem();
            String bucketName = index.getName() + "_" + repo.getRegion().getFullPath();
            bucketName = bucketName.replace("/", "_");
            File bucketDirectory = new File(exportLocation, bucketName);
            bucketDirectory.mkdirs();
            fs.export(bucketDirectory);
        });
        context.getResultSender().lastResult(null);
    } catch (BucketNotFoundException e) {
        throw new FunctionException(e);
    }
}
Also used : FunctionException(org.apache.geode.cache.execute.FunctionException) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) LuceneService(org.apache.geode.cache.lucene.LuceneService) IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) IndexWriter(org.apache.lucene.index.IndexWriter) InternalLuceneIndex(org.apache.geode.cache.lucene.internal.InternalLuceneIndex) FileSystem(org.apache.geode.cache.lucene.internal.filesystem.FileSystem) BucketNotFoundException(org.apache.geode.internal.cache.BucketNotFoundException) RepositoryManager(org.apache.geode.cache.lucene.internal.repository.RepositoryManager) File(java.io.File)

Aggregations

InternalLuceneIndex (org.apache.geode.cache.lucene.internal.InternalLuceneIndex)3 File (java.io.File)2 Region (org.apache.geode.cache.Region)2 RegionFunctionContext (org.apache.geode.cache.execute.RegionFunctionContext)2 FileSystem (org.apache.geode.cache.lucene.internal.filesystem.FileSystem)2 IndexRepository (org.apache.geode.cache.lucene.internal.repository.IndexRepository)2 RepositoryManager (org.apache.geode.cache.lucene.internal.repository.RepositoryManager)2 IndexWriter (org.apache.lucene.index.IndexWriter)2 TimeUnit (java.util.concurrent.TimeUnit)1 RegionShortcut (org.apache.geode.cache.RegionShortcut)1 FunctionException (org.apache.geode.cache.execute.FunctionException)1 ResultSender (org.apache.geode.cache.execute.ResultSender)1 LuceneIntegrationTest (org.apache.geode.cache.lucene.LuceneIntegrationTest)1 LuceneService (org.apache.geode.cache.lucene.LuceneService)1 InternalLuceneService (org.apache.geode.cache.lucene.internal.InternalLuceneService)1 LuceneTestUtilities (org.apache.geode.cache.lucene.test.LuceneTestUtilities)1 TestObject (org.apache.geode.cache.lucene.test.TestObject)1 BucketNotFoundException (org.apache.geode.internal.cache.BucketNotFoundException)1 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)1 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)1