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);
}
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);
}
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);
}
}
Aggregations