private String getMonitorKey(LuceneIndex index) { return index.getRegionPath() + "_" + index.getName(); }
@Override public Collection<Document> toDocuments(LuceneIndex index, Object value) { if (value == null) { return Collections.emptyList(); } LuceneSerializer mapper = getFieldMapper(value, index.getFieldNames()); Collection<Document> docs = mapper.toDocuments(index, value); if (logger.isDebugEnabled()) { logger.debug("HeterogeneousLuceneSerializer.toDocuments:" + docs); } return docs; }
when(mockLuceneIndex.getFieldAnalyzers()).thenReturn(Collections.unmodifiableMap(fieldAnalyzers)); when(mockLuceneIndex.getFieldNames()).thenAnswer(in -> fields.toArray(new String[fields.size()])); when(mockLuceneIndex.getLuceneSerializer()).thenAnswer(in -> luceneSerializerReference.get()); when(mockLuceneIndex.getName()).thenReturn(indexName); when(mockLuceneIndex.getRegionPath()).thenReturn(regionPath);
public void beforeRegionDestroyed(Region region) { List<LuceneIndex> indexes = getIndexes(region.getFullPath()); if (!indexes.isEmpty()) { String indexNames = indexes.stream().map(i -> i.getName()).collect(Collectors.joining(",")); throw new IllegalStateException( String.format( "Region %s cannot be destroyed because it defines Lucene index(es) [%s]. Destroy all Lucene indexes before destroying the region.", region.getFullPath(), indexNames)); } }
public List<LuceneIndex> getIndexes(String regionPath) { List<LuceneIndex> indexes = new ArrayList(); for (LuceneIndex index : getAllIndexes()) { if (index.getRegionPath().equals(regionPath)) { indexes.add(index); } } return Collections.unmodifiableList(indexes); }
@Override public void execute(final FunctionContext context) { final Set<LuceneIndexDetails> indexDetailsSet = new HashSet<>(); final Cache cache = context.getCache(); final String serverName = cache.getDistributedSystem().getDistributedMember().getName(); LuceneServiceImpl service = (LuceneServiceImpl) LuceneServiceProvider.get(cache); for (LuceneIndexCreationProfile profile : service.getAllDefinedIndexes()) { indexDetailsSet .add(new LuceneIndexDetails(profile, serverName, LuceneIndexStatus.NOT_INITIALIZED)); } for (LuceneIndex index : service.getAllIndexes()) { LuceneIndexStatus initialized; if (index.isIndexingInProgress()) { initialized = LuceneIndexStatus.INDEXING_IN_PROGRESS; } else { initialized = LuceneIndexStatus.INITIALIZED; } indexDetailsSet.add(new LuceneIndexDetails((LuceneIndexImpl) index, serverName, initialized)); } context.getResultSender().lastResult(indexDetailsSet); } }
/** * Resolves the name of the {@link LuceneIndex} required in the Lucene data access, query operations * when a {@link LuceneIndex} is not specifically provided. * * @return the name of the resolve {@link LuceneIndex}. * @throws IllegalStateException if the name of the {@link LuceneIndex} cannot be resolved. * @see org.apache.geode.cache.lucene.LuceneIndex#getName() * @see #getIndexName() * @see #getLuceneIndex() */ protected String resolveIndexName() { String resolvedIndexName = Optional.ofNullable(getIndexName()) .orElseGet(() -> safeGetValue(() -> getLuceneIndex().getName())); Assert.state(StringUtils.hasText(resolvedIndexName), "The name of the Lucene Index could not be resolved"); return resolvedIndexName; }
public LuceneIndexMetrics[] listIndexMetrics(String regionPath) { if (!regionPath.startsWith(Region.SEPARATOR)) { regionPath = Region.SEPARATOR + regionPath; } List<LuceneIndexMetrics> indexMetrics = new ArrayList(); for (LuceneIndex index : this.service.getAllIndexes()) { if (index.getRegionPath().equals(regionPath)) { indexMetrics.add(getIndexMetrics(index)); } } return indexMetrics.toArray(new LuceneIndexMetrics[indexMetrics.size()]); }
@Override public void execute(FunctionContext<Object> context) { RegionFunctionContext ctx = (RegionFunctionContext) context; ResultSender<Boolean> resultSender = ctx.getResultSender(); Region region = ctx.getDataSet(); Cache cache = region.getCache(); String indexName = (String) ctx.getArguments(); if (indexName == null) { throw new IllegalArgumentException("Missing index name"); } LuceneService luceneService = LuceneServiceProvider.get(cache); LuceneIndex luceneIndex = luceneService.getIndex(indexName, region.getFullPath()); if (luceneIndex == null) { resultSender.lastResult(false); } else { resultSender.lastResult(luceneIndex.isIndexingInProgress()); } }
private void removeFromIndexMap(LuceneIndex index) { indexMap.remove(getUniqueIndexName(index.getName(), index.getRegionPath())); }
/** * Resolves the name of the {@link LuceneIndex} required in the Lucene data access, query operations * when a {@link LuceneIndex} is not specifically provided. * * @return the name of the resolve {@link LuceneIndex}. * @throws IllegalStateException if the name of the {@link LuceneIndex} cannot be resolved. * @see org.apache.geode.cache.lucene.LuceneIndex#getName() * @see #getIndexName() * @see #getLuceneIndex() */ protected String resolveIndexName() { String resolvedIndexName = Optional.ofNullable(getIndexName()) .orElseGet(() -> safeGetValue(() -> getLuceneIndex().getName())); Assert.state(StringUtils.hasText(resolvedIndexName), "The name of the Lucene Index could not be resolved"); return resolvedIndexName; }
/** * Recursively serialize each indexed field's value into a field of lucene document. The field * name will be in the same format as its indexed, such as contacts.homepage.content * * @param index lucene index * @param value user object to be serialized into index */ @Override public Collection<Document> toDocuments(LuceneIndex index, Object value) { String[] fields = index.getFieldNames(); Document doc = new Document(); for (String indexedFieldName : fields) { List<String> tokenizedFields = tokenizeField(indexedFieldName); addFieldValue(doc, indexedFieldName, value, tokenizedFields); } if (logger.isDebugEnabled()) { logger.debug("FlatFormatSerializer.toDocuments: " + doc); } return Collections.singleton(doc); }
private void registerIndex(LuceneIndex index) { String regionAndIndex = getUniqueIndexName(index.getName(), index.getRegionPath()); if (hasIndexBeenDestroyed(regionAndIndex)) { ((InternalLuceneIndex) index).destroy(true); throw new LuceneIndexDestroyedException(index.getName(), index.getRegionPath()); } else { if (!indexMap.containsKey(regionAndIndex)) { indexMap.put(regionAndIndex, index); } definedIndexMap.remove(regionAndIndex); } }
@Override public Collection<Document> toDocuments(LuceneIndex index, Object value) { Document doc = new Document(); PdxInstance pdx = (PdxInstance) value; for (String field : index.getFieldNames()) { if (pdx.hasField(field)) { Object fieldValue = pdx.getField(field); if (fieldValue == null) { continue; } SerializerUtil.addField(doc, field, fieldValue); } } if (logger.isDebugEnabled()) { logger.debug("PdxLuceneSerializer.toDocument:" + doc); } return Collections.singleton(doc); } }
protected void destroyIndexes(String regionPath, boolean initiator) { if (!regionPath.startsWith("/")) { regionPath = "/" + regionPath; } List<LuceneIndexImpl> indexesToDestroy = new ArrayList<>(); for (LuceneIndex index : getAllIndexes()) { if (index.getRegionPath().equals(regionPath)) { LuceneIndexImpl indexImpl = (LuceneIndexImpl) index; indexImpl.destroy(initiator); indexesToDestroy.add(indexImpl); } } // If list is empty throw an exception; otherwise iterate and destroy the defined index if (indexesToDestroy.isEmpty()) { throw new IllegalArgumentException( String.format("No Lucene indexes were found in region %s", regionPath)); } else { for (LuceneIndex index : indexesToDestroy) { removeFromIndexMap(index); logger.info(String.format("Destroyed Lucene index %s from %s region %s", index.getName(), "initialized", regionPath)); } } }
public static Document invokeSerializer(LuceneSerializer mapper, Object object, String[] fields) { LuceneIndex index = Mockito.mock(LuceneIndex.class); Mockito.when(index.getFieldNames()).thenReturn(fields); Collection<Document> docs = mapper.toDocuments(index, object); assertEquals(1, docs.size()); return docs.iterator().next(); } }
public LuceneIndexStatsMonitor(LuceneIndex index) { super(LUCENE_SERVICE_MXBEAN_MONITOR_PREFIX + index.getRegionPath() + "_" + index.getName()); addStatisticsToMonitor(((LuceneIndexImpl) index).getIndexStats().getStats()); configureMetrics(); }
private IndexRepositoryImpl createIndexRepo() throws IOException { ConcurrentHashMap fileAndChunkRegion = new ConcurrentHashMap(); RegionDirectory dir = new RegionDirectory(fileAndChunkRegion, fileSystemStats); IndexWriterConfig config = new IndexWriterConfig(analyzer); IndexWriter writer = new IndexWriter(dir, config); LuceneIndex index = Mockito.mock(LuceneIndex.class); Mockito.when(index.getFieldNames()).thenReturn(new String[] {"txt"}); return new IndexRepositoryImpl(region, writer, mapper, indexStats, null, null, "", index); }
protected LuceneIndexMetrics getIndexMetrics(LuceneIndex index) { int queryExecutions = getStatistic(StatsKey.QUERIES).intValue(); long queryExecutionTime = getStatistic(StatsKey.QUERY_TIME).longValue(); long queryRateAverageLatencyValue = this.queryRateAverageLatency.getAverageLatency(); int queryExecutionsInProgress = getStatistic(StatsKey.QUERIES_IN_PROGRESS).intValue(); long queryExecutionTotalHits = getStatistic(StatsKey.QUERIES_TOTAL_HITS).longValue(); int updates = getStatistic(StatsKey.UPDATES).intValue(); long updateTime = getStatistic(StatsKey.UPDATE_TIME).longValue(); long updateRateAverageLatencyValue = this.updateRateAverageLatency.getAverageLatency(); int updatesInProgress = getStatistic(StatsKey.UPDATES_IN_PROGRESS).intValue(); int commits = getStatistic(StatsKey.COMMITS).intValue(); long commitTime = getStatistic(StatsKey.COMMIT_TIME).longValue(); long commitRateAverageLatencyValue = this.commitRateAverageLatency.getAverageLatency(); int commitsInProgress = getStatistic(StatsKey.COMMITS_IN_PROGRESS).intValue(); int documents = getStatistic(StatsKey.DOCUMENTS).intValue(); return new LuceneIndexMetrics(index.getRegionPath(), index.getName(), queryExecutions, queryExecutionTime, queryRateAverageLatencyValue, queryExecutionsInProgress, queryExecutionTotalHits, updates, updateTime, updateRateAverageLatencyValue, updatesInProgress, commits, commitTime, commitRateAverageLatencyValue, commitsInProgress, documents); } }
/** * @inheritDoc */ @Override @SuppressWarnings("all") public void destroy() throws Exception { LuceneIndex luceneIndex = getObject(); if (isLuceneIndexDestroyable(luceneIndex)) { resolveLuceneService().destroyIndex(luceneIndex.getName(), luceneIndex.getRegionPath()); } }