Search in sources :

Example 6 with IndexRepository

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

the class AbstractPartitionedRepositoryManager method getRepository.

/**
   * Return the repository for a given user bucket
   */
protected IndexRepository getRepository(Integer bucketId) throws BucketNotFoundException {
    IndexRepository repo = indexRepositories.get(bucketId);
    if (repo != null && !repo.isClosed()) {
        return repo;
    }
    repo = computeRepository(bucketId);
    if (repo == null) {
        throw new BucketNotFoundException("Unable to find lucene index because no longer primary for bucket " + bucketId);
    }
    return repo;
}
Also used : IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) BucketNotFoundException(org.apache.geode.internal.cache.BucketNotFoundException)

Example 7 with IndexRepository

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

the class AbstractPartitionedRepositoryManager method getRepositories.

@Override
public Collection<IndexRepository> getRepositories(RegionFunctionContext ctx) throws BucketNotFoundException {
    Region<Object, Object> region = ctx.getDataSet();
    Set<Integer> buckets = ((InternalRegionFunctionContext) ctx).getLocalBucketSet(region);
    ArrayList<IndexRepository> repos = new ArrayList<IndexRepository>(buckets.size());
    for (Integer bucketId : buckets) {
        BucketRegion userBucket = userRegion.getDataStore().getLocalBucketById(bucketId);
        if (userBucket == null) {
            throw new BucketNotFoundException("User bucket was not found for region " + region + "bucket id " + bucketId);
        } else {
            repos.add(getRepository(userBucket.getId()));
        }
    }
    return repos;
}
Also used : IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) BucketRegion(org.apache.geode.internal.cache.BucketRegion) ArrayList(java.util.ArrayList) InternalRegionFunctionContext(org.apache.geode.internal.cache.execute.InternalRegionFunctionContext) BucketNotFoundException(org.apache.geode.internal.cache.BucketNotFoundException)

Example 8 with IndexRepository

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

the class IndexRepositoryFactory method computeIndexRepository.

public IndexRepository computeIndexRepository(final Integer bucketId, LuceneSerializer serializer, LuceneIndexImpl index, PartitionedRegion userRegion, final IndexRepository oldRepository) throws IOException {
    LuceneIndexForPartitionedRegion indexForPR = (LuceneIndexForPartitionedRegion) index;
    final PartitionedRegion fileRegion = indexForPR.getFileAndChunkRegion();
    BucketRegion fileAndChunkBucket = getMatchingBucket(fileRegion, bucketId);
    BucketRegion dataBucket = getMatchingBucket(userRegion, bucketId);
    boolean success = false;
    if (fileAndChunkBucket == null) {
        if (oldRepository != null) {
            oldRepository.cleanup();
        }
        return null;
    }
    if (!fileAndChunkBucket.getBucketAdvisor().isPrimary()) {
        if (oldRepository != null) {
            oldRepository.cleanup();
        }
        return null;
    }
    if (oldRepository != null && !oldRepository.isClosed()) {
        return oldRepository;
    }
    if (oldRepository != null) {
        oldRepository.cleanup();
    }
    DistributedLockService lockService = getLockService();
    String lockName = getLockName(fileAndChunkBucket);
    while (!lockService.lock(lockName, 100, -1)) {
        if (!fileAndChunkBucket.getBucketAdvisor().isPrimary()) {
            return null;
        }
    }
    final IndexRepository repo;
    try {
        RegionDirectory dir = new RegionDirectory(getBucketTargetingMap(fileAndChunkBucket, bucketId), indexForPR.getFileSystemStats());
        IndexWriterConfig config = new IndexWriterConfig(indexForPR.getAnalyzer());
        IndexWriter writer = new IndexWriter(dir, config);
        repo = new IndexRepositoryImpl(fileAndChunkBucket, writer, serializer, indexForPR.getIndexStats(), dataBucket, lockService, lockName);
        success = true;
        return repo;
    } catch (IOException e) {
        logger.info("Exception thrown while constructing Lucene Index for bucket:" + bucketId + " for file region:" + fileAndChunkBucket.getFullPath());
        throw e;
    } finally {
        if (!success) {
            lockService.unlock(lockName);
        }
    }
}
Also used : IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) BucketRegion(org.apache.geode.internal.cache.BucketRegion) IndexWriter(org.apache.lucene.index.IndexWriter) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) DistributedLockService(org.apache.geode.distributed.DistributedLockService) IOException(java.io.IOException) RegionDirectory(org.apache.geode.cache.lucene.internal.directory.RegionDirectory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) IndexRepositoryImpl(org.apache.geode.cache.lucene.internal.repository.IndexRepositoryImpl)

Example 9 with IndexRepository

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

the class LuceneEventListener method process.

protected boolean process(final List<AsyncEvent> events) {
    // Try to get a PDX instance if possible, rather than a deserialized object
    DefaultQuery.setPdxReadSerialized(true);
    Set<IndexRepository> affectedRepos = new HashSet<IndexRepository>();
    try {
        for (AsyncEvent event : events) {
            Region region = event.getRegion();
            Object key = event.getKey();
            Object callbackArgument = event.getCallbackArgument();
            IndexRepository repository = repositoryManager.getRepository(region, key, callbackArgument);
            Object value = getValue(region.getEntry(key));
            if (value != null) {
                repository.update(key, value);
            } else {
                repository.delete(key);
            }
            affectedRepos.add(repository);
        }
        for (IndexRepository repo : affectedRepos) {
            repo.commit();
        }
        return true;
    } catch (BucketNotFoundException | RegionDestroyedException | PrimaryBucketException e) {
        logger.debug("Bucket not found while saving to lucene index: " + e.getMessage(), e);
        return false;
    } catch (CacheClosedException e) {
        logger.debug("Unable to save to lucene index, cache has been closed", e);
        return false;
    } catch (AlreadyClosedException e) {
        logger.debug("Unable to commit, the lucene index is already closed", e);
        return false;
    } catch (IOException e) {
        throw new InternalGemFireError("Unable to save to lucene index", e);
    } finally {
        DefaultQuery.setPdxReadSerialized(false);
    }
}
Also used : RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) CacheClosedException(org.apache.geode.cache.CacheClosedException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException) AsyncEvent(org.apache.geode.cache.asyncqueue.AsyncEvent) PrimaryBucketException(org.apache.geode.internal.cache.PrimaryBucketException) IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) Region(org.apache.geode.cache.Region) BucketNotFoundException(org.apache.geode.internal.cache.BucketNotFoundException) HashSet(java.util.HashSet) InternalGemFireError(org.apache.geode.InternalGemFireError)

Example 10 with IndexRepository

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

the class LuceneQueryFunction method execute.

@Override
public void execute(FunctionContext context) {
    RegionFunctionContext ctx = (RegionFunctionContext) context;
    ResultSender<TopEntriesCollector> resultSender = ctx.getResultSender();
    Region region = ctx.getDataSet();
    LuceneFunctionContext<IndexResultCollector> searchContext = (LuceneFunctionContext) ctx.getArguments();
    if (searchContext == null) {
        throw new IllegalArgumentException("Missing search context");
    }
    LuceneQueryProvider queryProvider = searchContext.getQueryProvider();
    if (queryProvider == null) {
        throw new IllegalArgumentException("Missing query provider");
    }
    LuceneIndexImpl index = getLuceneIndex(region, searchContext);
    if (index == null) {
        throw new LuceneIndexNotFoundException(searchContext.getIndexName(), region.getFullPath());
    }
    RepositoryManager repoManager = index.getRepositoryManager();
    LuceneIndexStats stats = index.getIndexStats();
    Query query = getQuery(queryProvider, index);
    if (logger.isDebugEnabled()) {
        logger.debug("Executing lucene query: {}, on region {}", query, region.getFullPath());
    }
    int resultLimit = searchContext.getLimit();
    CollectorManager manager = (searchContext == null) ? null : searchContext.getCollectorManager();
    if (manager == null) {
        manager = new TopEntriesCollectorManager(null, resultLimit);
    }
    Collection<IndexResultCollector> results = new ArrayList<>();
    TopEntriesCollector mergedResult = null;
    try {
        long start = stats.startQuery();
        Collection<IndexRepository> repositories = null;
        try {
            repositories = repoManager.getRepositories(ctx);
            for (IndexRepository repo : repositories) {
                IndexResultCollector collector = manager.newCollector(repo.toString());
                if (logger.isDebugEnabled()) {
                    logger.debug("Executing search on repo: " + repo.toString());
                }
                repo.query(query, resultLimit, collector);
                results.add(collector);
            }
            mergedResult = (TopEntriesCollector) manager.reduce(results);
        } finally {
            stats.endQuery(start, mergedResult == null ? 0 : mergedResult.size());
        }
        stats.incNumberOfQueryExecuted();
        resultSender.lastResult(mergedResult);
    } catch (IOException | BucketNotFoundException | CacheClosedException | PrimaryBucketException e) {
        logger.debug("Exception during lucene query function", e);
        throw new InternalFunctionInvocationTargetException(e);
    }
}
Also used : LuceneIndexStats(org.apache.geode.cache.lucene.internal.LuceneIndexStats) Query(org.apache.lucene.search.Query) LuceneQueryProvider(org.apache.geode.cache.lucene.LuceneQueryProvider) ArrayList(java.util.ArrayList) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) PrimaryBucketException(org.apache.geode.internal.cache.PrimaryBucketException) IndexResultCollector(org.apache.geode.cache.lucene.internal.repository.IndexResultCollector) RepositoryManager(org.apache.geode.cache.lucene.internal.repository.RepositoryManager) IOException(java.io.IOException) CacheClosedException(org.apache.geode.cache.CacheClosedException) IndexRepository(org.apache.geode.cache.lucene.internal.repository.IndexRepository) InternalFunctionInvocationTargetException(org.apache.geode.internal.cache.execute.InternalFunctionInvocationTargetException) LuceneIndexNotFoundException(org.apache.geode.cache.lucene.LuceneIndexNotFoundException) Region(org.apache.geode.cache.Region) BucketNotFoundException(org.apache.geode.internal.cache.BucketNotFoundException) LuceneIndexImpl(org.apache.geode.cache.lucene.internal.LuceneIndexImpl)

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