Search in sources :

Example 1 with IndexRepository

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

the class RawIndexRepositoryFactory method computeIndexRepository.

@Override
public IndexRepository computeIndexRepository(final Integer bucketId, LuceneSerializer serializer, LuceneIndexImpl index, PartitionedRegion userRegion, IndexRepository oldRepository) throws IOException {
    final IndexRepository repo;
    if (oldRepository != null) {
        oldRepository.cleanup();
    }
    LuceneRawIndex indexForRaw = (LuceneRawIndex) index;
    BucketRegion dataBucket = getMatchingBucket(userRegion, bucketId);
    Directory dir = null;
    if (indexForRaw.withPersistence()) {
        String bucketLocation = LuceneServiceImpl.getUniqueIndexName(index.getName(), index.getRegionPath() + "_" + bucketId);
        File location = new File(index.getName(), bucketLocation);
        if (!location.exists()) {
            location.mkdirs();
        }
        dir = new NIOFSDirectory(location.toPath());
    } else {
        dir = new RAMDirectory();
    }
    IndexWriterConfig config = new IndexWriterConfig(indexForRaw.getAnalyzer());
    IndexWriter writer = new IndexWriter(dir, config);
    return new IndexRepositoryImpl(null, writer, serializer, indexForRaw.getIndexStats(), dataBucket, null, "");
}
Also used : NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) BucketRegion(org.apache.geode.internal.cache.BucketRegion) IndexWriter(org.apache.lucene.index.IndexWriter) File(java.io.File) RAMDirectory(org.apache.lucene.store.RAMDirectory) RegionDirectory(org.apache.geode.cache.lucene.internal.directory.RegionDirectory) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) IndexRepositoryImpl(org.apache.geode.cache.lucene.internal.repository.IndexRepositoryImpl)

Example 2 with IndexRepository

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

the class LuceneQueryFunctionJUnitTest method createMocksAndCommonObjects.

@Before
public void createMocksAndCommonObjects() throws Exception {
    mockContext = mock(InternalRegionFunctionContext.class);
    mockResultSender = mock(ResultSender.class);
    mockRegion = mock(Region.class);
    mockRepoManager = mock(RepositoryManager.class);
    mockRepository1 = mock(IndexRepository.class, "repo1");
    mockRepository2 = mock(IndexRepository.class, "repo2");
    mockCollector = mock(IndexResultCollector.class);
    mockStats = mock(LuceneIndexStats.class);
    repos = new ArrayList<IndexRepository>();
    repos.add(mockRepository1);
    repos.add(mockRepository2);
    mockIndex = mock(LuceneIndexImpl.class);
    mockService = mock(InternalLuceneService.class);
    mockCache = mock(InternalCache.class);
    Analyzer analyzer = new StandardAnalyzer();
    Mockito.doReturn(analyzer).when(mockIndex).getAnalyzer();
    queryProvider = new StringQueryProvider("gemfire:lucene", DEFAULT_FIELD);
    searchArgs = new LuceneFunctionContext<IndexResultCollector>(queryProvider, "indexName");
    when(mockRegion.getCache()).thenReturn(mockCache);
    when(mockRegion.getFullPath()).thenReturn(regionPath);
    when(mockCache.getService(any())).thenReturn(mockService);
    when(mockService.getIndex(eq("indexName"), eq(regionPath))).thenReturn(mockIndex);
    when(mockIndex.getRepositoryManager()).thenReturn(mockRepoManager);
    when(mockIndex.getFieldNames()).thenReturn(new String[] { "gemfire" });
    when(mockIndex.getIndexStats()).thenReturn(mockStats);
    query = queryProvider.getQuery(mockIndex);
}
Also used : LuceneIndexStats(org.apache.geode.cache.lucene.internal.LuceneIndexStats) StringQueryProvider(org.apache.geode.cache.lucene.internal.StringQueryProvider) InternalCache(org.apache.geode.internal.cache.InternalCache) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ResultSender(org.apache.geode.cache.execute.ResultSender) IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) IndexResultCollector(org.apache.geode.cache.lucene.internal.repository.IndexResultCollector) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) InternalRegionFunctionContext(org.apache.geode.internal.cache.execute.InternalRegionFunctionContext) Region(org.apache.geode.cache.Region) RepositoryManager(org.apache.geode.cache.lucene.internal.repository.RepositoryManager) InternalLuceneService(org.apache.geode.cache.lucene.internal.InternalLuceneService) LuceneIndexImpl(org.apache.geode.cache.lucene.internal.LuceneIndexImpl) Before(org.junit.Before)

Example 3 with IndexRepository

use of org.apache.geode.cache.lucene.internal.repository.IndexRepository 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 4 with IndexRepository

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

the class LuceneEventListenerJUnitTest method testProcessBatch.

@Test
public void testProcessBatch() throws Exception {
    RepositoryManager manager = Mockito.mock(RepositoryManager.class);
    IndexRepository repo1 = Mockito.mock(IndexRepository.class);
    IndexRepository repo2 = Mockito.mock(IndexRepository.class);
    Region region1 = Mockito.mock(Region.class);
    Region region2 = Mockito.mock(Region.class);
    Object callback1 = new Object();
    Mockito.when(manager.getRepository(eq(region1), any(), eq(callback1))).thenReturn(repo1);
    Mockito.when(manager.getRepository(eq(region2), any(), eq(null))).thenReturn(repo2);
    LuceneEventListener listener = new LuceneEventListener(manager);
    List<AsyncEvent> events = new ArrayList<AsyncEvent>();
    int numEntries = 100;
    for (int i = 0; i < numEntries; i++) {
        AsyncEvent event = Mockito.mock(AsyncEvent.class);
        Region region = i % 2 == 0 ? region1 : region2;
        Object callback = i % 2 == 0 ? callback1 : null;
        Mockito.when(event.getRegion()).thenReturn(region);
        Mockito.when(event.getKey()).thenReturn(i);
        Mockito.when(event.getCallbackArgument()).thenReturn(callback);
        switch(i % 4) {
            case 0:
            case 1:
                final EntrySnapshot entry = mock(EntrySnapshot.class);
                when(entry.getRawValue(true)).thenReturn(i);
                when(region.getEntry(eq(i))).thenReturn(entry);
                break;
            case 2:
            case 3:
                // Do nothing, get value will return a destroy
                break;
        }
        events.add(event);
    }
    listener.processEvents(events);
    verify(repo1, atLeast(numEntries / 4)).delete(any());
    verify(repo1, atLeast(numEntries / 4)).update(any(), any());
    verify(repo2, atLeast(numEntries / 4)).delete(any());
    verify(repo2, atLeast(numEntries / 4)).update(any(), any());
    verify(repo1, times(1)).commit();
    verify(repo2, times(1)).commit();
}
Also used : IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) ArrayList(java.util.ArrayList) Region(org.apache.geode.cache.Region) RepositoryManager(org.apache.geode.cache.lucene.internal.repository.RepositoryManager) AsyncEvent(org.apache.geode.cache.asyncqueue.AsyncEvent) EntrySnapshot(org.apache.geode.internal.cache.EntrySnapshot) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 5 with IndexRepository

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

the class IndexRepositorySpy method computeIndexRepository.

@Override
public IndexRepository computeIndexRepository(final Integer bucketId, LuceneSerializer serializer, LuceneIndexImpl index, PartitionedRegion userRegion, IndexRepository oldRepository) throws IOException {
    LuceneIndexForPartitionedRegion indexForPR = (LuceneIndexForPartitionedRegion) index;
    final IndexRepository indexRepo = super.computeIndexRepository(bucketId, serializer, index, userRegion, oldRepository);
    if (indexRepo == null) {
        return null;
    }
    if (mockingDetails(indexRepo).isSpy()) {
        return indexRepo;
    }
    final IndexRepository spy = Mockito.spy(indexRepo);
    Answer invokeBeforeWrite = invocation -> {
        beforeWrite.accept(invocation.getArgumentAt(0, Object.class));
        return invocation.callRealMethod();
    };
    doAnswer(invokeBeforeWrite).when(spy).update(any(), any());
    doAnswer(invokeBeforeWrite).when(spy).create(any(), any());
    doAnswer(invokeBeforeWrite).when(spy).delete(any());
    return spy;
}
Also used : Matchers.any(org.mockito.Matchers.any) Consumer(java.util.function.Consumer) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) IndexRepositoryFactory(org.apache.geode.cache.lucene.internal.IndexRepositoryFactory) LuceneIndexForPartitionedRegion(org.apache.geode.cache.lucene.internal.LuceneIndexForPartitionedRegion) IOException(java.io.IOException) LuceneSerializer(org.apache.geode.cache.lucene.internal.repository.serializer.LuceneSerializer) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) LuceneIndexImpl(org.apache.geode.cache.lucene.internal.LuceneIndexImpl) PartitionedRepositoryManager(org.apache.geode.cache.lucene.internal.PartitionedRepositoryManager) IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) Answer(org.mockito.stubbing.Answer) IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) LuceneIndexForPartitionedRegion(org.apache.geode.cache.lucene.internal.LuceneIndexForPartitionedRegion)

Aggregations

IndexRepository (org.apache.geode.cache.lucene.internal.repository.IndexRepository)13 RepositoryManager (org.apache.geode.cache.lucene.internal.repository.RepositoryManager)6 Region (org.apache.geode.cache.Region)5 BucketNotFoundException (org.apache.geode.internal.cache.BucketNotFoundException)5 IOException (java.io.IOException)4 IndexWriter (org.apache.lucene.index.IndexWriter)4 ArrayList (java.util.ArrayList)3 RegionFunctionContext (org.apache.geode.cache.execute.RegionFunctionContext)3 LuceneIndexImpl (org.apache.geode.cache.lucene.internal.LuceneIndexImpl)3 IndexRepositoryImpl (org.apache.geode.cache.lucene.internal.repository.IndexRepositoryImpl)3 BucketRegion (org.apache.geode.internal.cache.BucketRegion)3 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)3 InternalRegionFunctionContext (org.apache.geode.internal.cache.execute.InternalRegionFunctionContext)3 File (java.io.File)2 CacheClosedException (org.apache.geode.cache.CacheClosedException)2 AsyncEvent (org.apache.geode.cache.asyncqueue.AsyncEvent)2 ResultSender (org.apache.geode.cache.execute.ResultSender)2 InternalLuceneIndex (org.apache.geode.cache.lucene.internal.InternalLuceneIndex)2 InternalLuceneService (org.apache.geode.cache.lucene.internal.InternalLuceneService)2 LuceneIndexStats (org.apache.geode.cache.lucene.internal.LuceneIndexStats)2