http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/05e6d966/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneIndexStats.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneIndexStats.java b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneIndexStats.java deleted file mode 100644 index 220ae0e..0000000 --- a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneIndexStats.java +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package com.gemstone.gemfire.cache.lucene.internal; - -import static com.gemstone.gemfire.distributed.internal.DistributionStats.getStatTime; - -import java.util.function.IntSupplier; - -import com.gemstone.gemfire.StatisticDescriptor; -import com.gemstone.gemfire.Statistics; -import com.gemstone.gemfire.StatisticsFactory; -import com.gemstone.gemfire.StatisticsType; -import com.gemstone.gemfire.StatisticsTypeFactory; -import com.gemstone.gemfire.internal.CopyOnWriteHashSet; -import com.gemstone.gemfire.internal.statistics.StatisticsTypeFactoryImpl; - -public class LuceneIndexStats { - // statistics type - private static final StatisticsType statsType; - private static final String statsTypeName = "LuceneIndexStats"; - private static final String statsTypeDescription = "Statistics about lucene indexes"; - - private static final int queryExecutionsId; - private static final int queryExecutionTimeId; - private static final int queryExecutionsInProgressId; - private static final int queryExecutionTotalHitsId; - private static final int updatesId; - private static final int updateTimeId; - private static final int updatesInProgressId; - private static final int commitsId; - private static final int commitTimeId; - private static final int commitsInProgressId; - private static final int documentsId; - - private final Statistics stats; - private final CopyOnWriteHashSet<IntSupplier> documentsSuppliers = new CopyOnWriteHashSet<>(); - - static { - final StatisticsTypeFactory f = StatisticsTypeFactoryImpl.singleton(); - statsType = f.createType( - statsTypeName, - statsTypeDescription, - new StatisticDescriptor[] { - f.createIntCounter("queryExecutions", "Number of lucene queries executed on this member", "operations"), - f.createLongCounter("queryExecutionTime", "Amount of time spent executing lucene queries", "nanoseconds"), - f.createIntGauge("queryExecutionsInProgress", "Number of query executions currently in progress", "operations"), - f.createLongCounter("queryExecutionTotalHits", "Total number of documents returned by query executions", "entries"), - f.createIntCounter("updates", "Number of lucene index documents added/removed on this member", "operations"), - f.createLongCounter("updateTime", "Amount of time spent adding or removing documents from the index", "nanoseconds"), - f.createIntGauge("updatesInProgress", "Number of index updates in progress", "operations"), - f.createIntCounter("commits", "Number of lucene index commits on this member", "operations"), - f.createLongCounter("commitTime", "Amount of time spent in lucene index commits", "nanoseconds"), - f.createIntGauge("commitsInProgress", "Number of lucene index commits in progress", "operations"), - f.createIntGauge("documents", "Number of documents in the index", "documents"), - } - ); - - queryExecutionsId = statsType.nameToId("queryExecutions"); - queryExecutionTimeId = statsType.nameToId("queryExecutionTime"); - queryExecutionsInProgressId = statsType.nameToId("queryExecutionsInProgress"); - queryExecutionTotalHitsId = statsType.nameToId("queryExecutionTotalHits"); - updatesId = statsType.nameToId("updates"); - updateTimeId = statsType.nameToId("updateTime"); - updatesInProgressId = statsType.nameToId("updatesInProgress"); - commitsId = statsType.nameToId("commits"); - commitTimeId = statsType.nameToId("commitTime"); - commitsInProgressId = statsType.nameToId("commitsInProgress"); - documentsId = statsType.nameToId("documents"); - } - - public LuceneIndexStats(StatisticsFactory f, String name) { - this.stats = f.createAtomicStatistics(statsType, name); - stats.setIntSupplier(documentsId, this::computeDocumentCount); - } - - /** - * @return the timestamp that marks the start of the operation - */ - public long startQuery() { - stats.incInt(queryExecutionsInProgressId, 1); - return getStatTime(); - } - /** - * @param start the timestamp taken when the operation started - */ - public void endQuery(long start, final int totalHits) { - stats.incLong(queryExecutionTimeId, getStatTime()-start); - stats.incInt(queryExecutionsInProgressId, -1); - stats.incInt(queryExecutionsId, 1); - stats.incLong(queryExecutionTotalHitsId, totalHits); - } - - /** - * @return the timestamp that marks the start of the operation - */ - public long startUpdate() { - stats.incInt(updatesInProgressId, 1); - return getStatTime(); - } - /** - * @param start the timestamp taken when the operation started - */ - public void endUpdate(long start) { - stats.incLong(updateTimeId, getStatTime()-start); - stats.incInt(updatesInProgressId, -1); - stats.incInt(updatesId, 1); - } - - /** - * @return the timestamp that marks the start of the operation - */ - public long startCommit() { - stats.incInt(commitsInProgressId, 1); - return getStatTime(); - } - /** - * @param start the timestamp taken when the operation started - */ - public void endCommit(long start) { - stats.incLong(commitTimeId, getStatTime()-start); - stats.incInt(commitsInProgressId, -1); - stats.incInt(commitsId, 1); - } - - public void addDocumentsSupplier(IntSupplier supplier) { - this.documentsSuppliers.add(supplier); - } - - public void removeDocumentsSupplier(IntSupplier supplier) { - this.documentsSuppliers.remove(supplier); - } - - public int getDocuments() { - return this.stats.getInt(documentsId); - } - - private int computeDocumentCount() { - return this.documentsSuppliers.stream() - .mapToInt(IntSupplier::getAsInt) - .sum(); - } - - public int getQueryExecutions() { - return stats.getInt(queryExecutionsId); - } - - public long getQueryExecutionTime() { - return stats.getLong(queryExecutionTimeId); - } - - public int getQueryExecutionsInProgress() { - return stats.getInt(queryExecutionsInProgressId); - } - - public long getQueryExecutionTotalHits() { - return stats.getLong(queryExecutionTotalHitsId); - } - - public int getUpdates() { - return stats.getInt(updatesId); - } - - public long getUpdateTime() { - return stats.getLong(updateTimeId); - } - - public int getUpdatesInProgress() { - return stats.getInt(updatesInProgressId); - } - - public int getCommits() { - return stats.getInt(commitsId); - } - - public long getCommitTime() { - return stats.getLong(commitTimeId); - } - - public int getCommitsInProgress() { - return stats.getInt(commitsInProgressId); - } - - public Statistics getStats() { - return this.stats; - } -}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/05e6d966/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryFactoryImpl.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryFactoryImpl.java b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryFactoryImpl.java deleted file mode 100644 index 21c019b..0000000 --- a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryFactoryImpl.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package com.gemstone.gemfire.cache.lucene.internal; - -import com.gemstone.gemfire.cache.Cache; -import com.gemstone.gemfire.cache.Region; -import com.gemstone.gemfire.cache.lucene.LuceneQuery; -import com.gemstone.gemfire.cache.lucene.LuceneQueryFactory; -import com.gemstone.gemfire.cache.lucene.LuceneQueryProvider; - -public class LuceneQueryFactoryImpl implements LuceneQueryFactory { - private int limit = DEFAULT_LIMIT; - private int pageSize = DEFAULT_PAGESIZE; - private String[] projectionFields = null; - private Cache cache; - - LuceneQueryFactoryImpl(Cache cache) { - this.cache = cache; - } - - @Override - public LuceneQueryFactory setPageSize(int pageSize) { - this.pageSize = pageSize; - return this; - } - - @Override - public LuceneQueryFactory setResultLimit(int limit) { - this.limit = limit; - return this; - } - - @Override - public <K, V> LuceneQuery<K, V> create(String indexName, String regionName, String queryString, String defaultField) { - return create(indexName, regionName, new StringQueryProvider(queryString, defaultField)); - } - - @Override - public <K, V> LuceneQuery<K, V> create(String indexName, String regionName, LuceneQueryProvider provider) { - Region<K, V> region = cache.getRegion(regionName); - if(region == null) { - throw new IllegalArgumentException("Region not found: " + regionName); - } - LuceneQueryImpl<K, V> luceneQuery = new LuceneQueryImpl<K, V>(indexName, region, provider, projectionFields, limit, pageSize); - return luceneQuery; - } - - @Override - public LuceneQueryFactory setProjectionFields(String... fieldNames) { - projectionFields = fieldNames.clone(); - return this; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/05e6d966/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImpl.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImpl.java b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImpl.java deleted file mode 100644 index ef2f4ee..0000000 --- a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneQueryImpl.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package com.gemstone.gemfire.cache.lucene.internal; - -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; - -import com.gemstone.gemfire.cache.Region; -import com.gemstone.gemfire.cache.execute.Execution; -import com.gemstone.gemfire.cache.execute.FunctionException; -import com.gemstone.gemfire.cache.execute.FunctionService; -import com.gemstone.gemfire.cache.execute.ResultCollector; -import com.gemstone.gemfire.cache.lucene.LuceneQuery; -import com.gemstone.gemfire.cache.lucene.LuceneQueryException; -import com.gemstone.gemfire.cache.lucene.LuceneQueryFactory; -import com.gemstone.gemfire.cache.lucene.LuceneQueryProvider; -import com.gemstone.gemfire.cache.lucene.LuceneResultStruct; -import com.gemstone.gemfire.cache.lucene.PageableLuceneQueryResults; -import com.gemstone.gemfire.cache.lucene.internal.distributed.EntryScore; -import com.gemstone.gemfire.cache.lucene.internal.distributed.LuceneFunction; -import com.gemstone.gemfire.cache.lucene.internal.distributed.LuceneFunctionContext; -import com.gemstone.gemfire.cache.lucene.internal.distributed.TopEntries; -import com.gemstone.gemfire.cache.lucene.internal.distributed.TopEntriesCollector; -import com.gemstone.gemfire.cache.lucene.internal.distributed.TopEntriesCollectorManager; -import com.gemstone.gemfire.cache.lucene.internal.distributed.TopEntriesFunctionCollector; - -public class LuceneQueryImpl<K, V> implements LuceneQuery<K, V> { - private int limit = LuceneQueryFactory.DEFAULT_LIMIT; - private int pageSize = LuceneQueryFactory.DEFAULT_PAGESIZE; - private String indexName; - // The projected fields are local to a specific index per Query object. - private String[] projectedFieldNames; - /* the lucene Query object to be wrapped here */ - private LuceneQueryProvider query; - private Region<K, V> region; - private String defaultField; - - public LuceneQueryImpl(String indexName, Region<K, V> region, LuceneQueryProvider provider, String[] projectionFields, - int limit, int pageSize) { - this.indexName = indexName; - this.region = region; - this.limit = limit; - this.pageSize = pageSize; - this.projectedFieldNames = projectionFields; - this.query = provider; - } - - @Override - public Collection<K> findKeys() throws LuceneQueryException { - TopEntries<K> entries = findTopEntries(); - final List<EntryScore<K>> hits = entries.getHits(); - - return hits.stream() - .map(hit -> hit.getKey()) - .collect(Collectors.toList()); - } - - @Override - public Collection<V> findValues() throws LuceneQueryException { - final List<LuceneResultStruct<K, V>> page = findResults(); - - return page.stream() - .map(entry -> entry.getValue()) - .collect(Collectors.toList()); - } - - @Override - public List<LuceneResultStruct<K, V>> findResults() throws LuceneQueryException { - PageableLuceneQueryResults<K, V> pages = findPages(0); - if(!pages.hasNext()) { - return Collections.emptyList(); - } - - return pages.next(); - } - - @Override - public PageableLuceneQueryResults<K, V> findPages() throws LuceneQueryException { - return findPages(pageSize); - } - - private PageableLuceneQueryResults<K, V> findPages(int pageSize) throws LuceneQueryException { - TopEntries<K> entries = findTopEntries(); - return new PageableLuceneQueryResultsImpl<K, V>(entries.getHits(), region, pageSize); - } - - private TopEntries<K> findTopEntries() throws LuceneQueryException { - TopEntriesCollectorManager manager = new TopEntriesCollectorManager(null, limit); - LuceneFunctionContext<TopEntriesCollector> context = new LuceneFunctionContext<>(query, indexName, manager, limit); - TopEntriesFunctionCollector collector = new TopEntriesFunctionCollector(context); - - ResultCollector<TopEntriesCollector, TopEntries<K>> rc = (ResultCollector<TopEntriesCollector, TopEntries<K>>) onRegion() - .withArgs(context) - .withCollector(collector) - .execute(LuceneFunction.ID); - - //TODO provide a timeout to the user? - TopEntries<K> entries; - try { - entries = rc.getResult(); - } catch(FunctionException e) { - if(e.getCause() instanceof LuceneQueryException) { - throw new LuceneQueryException(e); - } else { - throw e; - } - } - return entries; - } - - protected Execution onRegion() { - return FunctionService.onRegion(region); - } - - @Override - public int getPageSize() { - return this.pageSize; - } - - @Override - public int getLimit() { - return this.limit; - } - - @Override - public String[] getProjectedFieldNames() { - return this.projectedFieldNames; - } -} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/05e6d966/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneRawIndex.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneRawIndex.java b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneRawIndex.java deleted file mode 100755 index e708691..0000000 --- a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneRawIndex.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package com.gemstone.gemfire.cache.lucene.internal; - -import com.gemstone.gemfire.cache.Cache; -import com.gemstone.gemfire.cache.lucene.internal.repository.RepositoryManager; -import com.gemstone.gemfire.cache.lucene.internal.repository.serializer.HeterogeneousLuceneSerializer; -import com.gemstone.gemfire.internal.cache.PartitionedRegion; - -public class LuceneRawIndex extends LuceneIndexImpl { - - protected LuceneRawIndex(String indexName, String regionPath, Cache cache) { - super(indexName, regionPath, cache); - } - - @Override - protected RepositoryManager createRepositoryManager() { - HeterogeneousLuceneSerializer mapper = new HeterogeneousLuceneSerializer(getFieldNames()); - return new RawLuceneRepositoryManager(this, mapper); - } - - @Override - public void dumpFiles(String directory) { - return; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/05e6d966/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneRawIndexFactory.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneRawIndexFactory.java b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneRawIndexFactory.java deleted file mode 100755 index 6c3bad6..0000000 --- a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneRawIndexFactory.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package com.gemstone.gemfire.cache.lucene.internal; - -import com.gemstone.gemfire.internal.cache.GemFireCacheImpl; - -public class LuceneRawIndexFactory extends LuceneIndexFactory { - public LuceneIndexImpl create(String indexName, String regionPath, GemFireCacheImpl cache) { - return new LuceneRawIndex(indexName, regionPath, cache); - } -} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/05e6d966/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneResultStructImpl.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneResultStructImpl.java b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneResultStructImpl.java deleted file mode 100644 index a3794f2..0000000 --- a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneResultStructImpl.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package com.gemstone.gemfire.cache.lucene.internal; - -import com.gemstone.gemfire.cache.lucene.LuceneResultStruct; - -public class LuceneResultStructImpl<K,V> implements LuceneResultStruct<K,V> { - K key; - V value; - float score; - - public LuceneResultStructImpl(K key, V value, float score) { - this.key = key; - this.value = value; - this.score = score; - } - - @Override - public Object getProjectedField(String fieldName) { - throw new UnsupportedOperationException(); - } - - @Override - public K getKey() { - return key; - } - - @Override - public V getValue() { - return value; - } - - @Override - public float getScore() { - return score; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((key == null) ? 0 : key.hashCode()); - result = prime * result + Float.floatToIntBits(score); - result = prime * result + ((value == null) ? 0 : value.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - LuceneResultStructImpl other = (LuceneResultStructImpl) obj; - if (key == null) { - if (other.key != null) - return false; - } else if (!key.equals(other.key)) - return false; - if (Float.floatToIntBits(score) != Float.floatToIntBits(other.score)) - return false; - if (value == null) { - if (other.value != null) - return false; - } else if (!value.equals(other.value)) - return false; - return true; - } - - @Override - public String toString() { - return "LuceneResultStructImpl [key=" + key + ", value=" + value - + ", score=" + score + "]"; - } -} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/05e6d966/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneServiceImpl.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneServiceImpl.java b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneServiceImpl.java deleted file mode 100644 index 81a62b8..0000000 --- a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/LuceneServiceImpl.java +++ /dev/null @@ -1,334 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package com.gemstone.gemfire.cache.lucene.internal; - -import java.util.*; - -import com.gemstone.gemfire.cache.lucene.internal.management.LuceneServiceMBean; -import com.gemstone.gemfire.cache.lucene.internal.management.ManagementIndexListener; -import com.gemstone.gemfire.management.internal.beans.CacheServiceMBeanBase; -import org.apache.logging.log4j.Logger; -import org.apache.lucene.analysis.Analyzer; -import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper; -import org.apache.lucene.analysis.standard.StandardAnalyzer; - -import com.gemstone.gemfire.cache.AttributesFactory; -import com.gemstone.gemfire.cache.Cache; -import com.gemstone.gemfire.cache.EvictionAlgorithm; -import com.gemstone.gemfire.cache.EvictionAttributes; -import com.gemstone.gemfire.cache.Region; -import com.gemstone.gemfire.cache.RegionAttributes; -import com.gemstone.gemfire.cache.execute.FunctionService; -import com.gemstone.gemfire.cache.lucene.LuceneIndex; -import com.gemstone.gemfire.cache.lucene.LuceneQueryFactory; -import com.gemstone.gemfire.cache.lucene.internal.directory.DumpDirectoryFiles; -import com.gemstone.gemfire.cache.lucene.internal.distributed.EntryScore; -import com.gemstone.gemfire.cache.lucene.internal.distributed.LuceneFunction; -import com.gemstone.gemfire.cache.lucene.internal.distributed.LuceneFunctionContext; -import com.gemstone.gemfire.cache.lucene.internal.distributed.TopEntries; -import com.gemstone.gemfire.cache.lucene.internal.distributed.TopEntriesCollector; -import com.gemstone.gemfire.cache.lucene.internal.distributed.TopEntriesCollectorManager; -import com.gemstone.gemfire.cache.lucene.internal.filesystem.ChunkKey; -import com.gemstone.gemfire.cache.lucene.internal.filesystem.File; -import com.gemstone.gemfire.cache.lucene.internal.xml.LuceneServiceXmlGenerator; -import com.gemstone.gemfire.internal.DSFIDFactory; -import com.gemstone.gemfire.internal.DataSerializableFixedID; -import com.gemstone.gemfire.internal.cache.extension.Extensible; -import com.gemstone.gemfire.internal.cache.CacheService; -import com.gemstone.gemfire.internal.cache.GemFireCacheImpl; -import com.gemstone.gemfire.internal.cache.InternalRegionArguments; -import com.gemstone.gemfire.internal.cache.PartitionedRegion; -import com.gemstone.gemfire.internal.cache.RegionListener; -import com.gemstone.gemfire.internal.cache.xmlcache.XmlGenerator; -import com.gemstone.gemfire.internal.i18n.LocalizedStrings; -import com.gemstone.gemfire.internal.logging.LogService; - -/** - * Implementation of LuceneService to create lucene index and query. - * - * - * @since GemFire 8.5 - */ -public class LuceneServiceImpl implements InternalLuceneService { - public static LuceneIndexFactory luceneIndexFactory = new LuceneIndexFactory(); - private static final Logger logger = LogService.getLogger(); - - private GemFireCacheImpl cache; - private final HashMap<String, LuceneIndex> indexMap = new HashMap<String, LuceneIndex>(); - private final HashMap<String, LuceneIndexCreationProfile> definedIndexMap = new HashMap<>(); - private IndexListener managementListener; - - public LuceneServiceImpl() { - - } - - public void init(final Cache cache) { - if (cache == null) { - throw new IllegalStateException(LocalizedStrings.CqService_CACHE_IS_NULL.toLocalizedString()); - } - GemFireCacheImpl gfc = (GemFireCacheImpl) cache; - gfc.getCancelCriterion().checkCancelInProgress(null); - - this.cache = gfc; - - FunctionService.registerFunction(new LuceneFunction()); - FunctionService.registerFunction(new DumpDirectoryFiles()); - registerDataSerializables(); - } - - @Override - public CacheServiceMBeanBase getMBean() { - LuceneServiceMBean mbean = new LuceneServiceMBean(this); - this.managementListener = new ManagementIndexListener(mbean); - return mbean; - } - - @Override - public Class<? extends CacheService> getInterface() { - return InternalLuceneService.class; - } - - public static String getUniqueIndexName(String indexName, String regionPath) { - if (!regionPath.startsWith("/")) { - regionPath = "/"+regionPath; - } - String name = indexName + "#" + regionPath.replace('/', '_'); - return name; - } - - @Override - public void createIndex(String indexName, String regionPath, String... fields) { - if(fields == null || fields.length == 0) { - throw new IllegalArgumentException("At least one field must be indexed"); - } - StandardAnalyzer analyzer = new StandardAnalyzer(); - - createIndex(indexName, regionPath, analyzer, null, fields); - } - - @Override - public void createIndex(String indexName, String regionPath, Map<String, Analyzer> fieldAnalyzers) { - if(fieldAnalyzers == null || fieldAnalyzers.isEmpty()) { - throw new IllegalArgumentException("At least one field must be indexed"); - } - Analyzer analyzer = new PerFieldAnalyzerWrapper(new StandardAnalyzer(), fieldAnalyzers); - Set<String> fieldsSet = fieldAnalyzers.keySet(); - String[] fields = (String[])fieldsSet.toArray(new String[fieldsSet.size()]); - - createIndex(indexName, regionPath, analyzer, fieldAnalyzers, fields); - } - - public void createIndex(final String indexName, String regionPath, - final Analyzer analyzer, final Map<String, Analyzer> fieldAnalyzers, - final String... fields) { - - if(!regionPath.startsWith("/")) { - regionPath = "/" + regionPath; - } - - registerDefinedIndex(LuceneServiceImpl.getUniqueIndexName(indexName, regionPath), - new LuceneIndexCreationProfile(indexName, regionPath, fields, analyzer, fieldAnalyzers)); - - Region region = cache.getRegion(regionPath); - if(region != null) { - definedIndexMap.remove(LuceneServiceImpl.getUniqueIndexName(indexName, regionPath)); - throw new IllegalStateException("The lucene index must be created before region"); - } - - final String dataRegionPath = regionPath; - cache.addRegionListener(new RegionListener() { - @Override - public RegionAttributes beforeCreate(Region parent, String regionName, - RegionAttributes attrs, InternalRegionArguments internalRegionArgs) { - RegionAttributes updatedRA = attrs; - String path = parent == null ? "/" + regionName : parent.getFullPath() + "/" + regionName; - - if(path.equals(dataRegionPath)) { - - if (!attrs.getDataPolicy().withPartitioning()) { - // replicated region - throw new UnsupportedOperationException("Lucene indexes on replicated regions are not supported"); - } - - //For now we cannot support eviction with local destroy. - //Eviction with overflow to disk still needs to be supported - EvictionAttributes evictionAttributes = attrs.getEvictionAttributes(); - EvictionAlgorithm evictionAlgorithm = evictionAttributes.getAlgorithm(); - if (evictionAlgorithm != EvictionAlgorithm.NONE && evictionAttributes.getAction().isLocalDestroy()) { - throw new UnsupportedOperationException("Lucene indexes on regions with eviction and action local destroy are not supported"); - } - - String aeqId = LuceneServiceImpl.getUniqueIndexName(indexName, dataRegionPath); - if (!attrs.getAsyncEventQueueIds().contains(aeqId)) { - AttributesFactory af = new AttributesFactory(attrs); - af.addAsyncEventQueueId(aeqId); - updatedRA = af.create(); - } - - // Add index creation profile - internalRegionArgs.addCacheServiceProfile(new LuceneIndexCreationProfile(indexName, dataRegionPath, fields, analyzer, fieldAnalyzers)); - } - return updatedRA; - } - - @Override - public void afterCreate(Region region) { - if(region.getFullPath().equals(dataRegionPath)) { - afterDataRegionCreated(indexName, analyzer, dataRegionPath, fieldAnalyzers, fields); - cache.removeRegionListener(this); - } - } - }); - } - - - /** - * Finish creating the lucene index after the data region is created . - * - * Public because this is called by the Xml parsing code - */ - public void afterDataRegionCreated(final String indexName, - final Analyzer analyzer, final String dataRegionPath, - final Map<String, Analyzer> fieldAnalyzers, final String... fields) { - LuceneIndexImpl index = createIndexRegions(indexName, dataRegionPath); - index.setSearchableFields(fields); - index.setAnalyzer(analyzer); - index.setFieldAnalyzers(fieldAnalyzers); - index.initialize(); - registerIndex(index); - if (this.managementListener != null) { - this.managementListener.afterIndexCreated(index); - } - } - - private LuceneIndexImpl createIndexRegions(String indexName, String regionPath) { - Region dataregion = this.cache.getRegion(regionPath); - if (dataregion == null) { - logger.info("Data region "+regionPath+" not found"); - return null; - } - //Convert the region name into a canonical form - regionPath = dataregion.getFullPath(); - return luceneIndexFactory.create(indexName, regionPath, cache); - } - - private void registerDefinedIndex(final String regionAndIndex, final LuceneIndexCreationProfile luceneIndexCreationProfile) { - if (definedIndexMap.containsKey(regionAndIndex) || indexMap.containsKey(regionAndIndex)) - throw new IllegalArgumentException("Lucene index already exists in region"); - definedIndexMap.put(regionAndIndex, luceneIndexCreationProfile); - } - - @Override - public LuceneIndex getIndex(String indexName, String regionPath) { - Region region = cache.getRegion(regionPath); - if(region == null) { - return null; - } - return indexMap.get(getUniqueIndexName(indexName, region.getFullPath())); - } - - @Override - public Collection<LuceneIndex> getAllIndexes() { - return indexMap.values(); - } - - @Override - public void destroyIndex(LuceneIndex index) { - LuceneIndexImpl indexImpl = (LuceneIndexImpl) index; - indexMap.remove(getUniqueIndexName(index.getName(), index.getRegionPath())); -// indexImpl.close(); - } - - @Override - public LuceneQueryFactory createLuceneQueryFactory() { - return new LuceneQueryFactoryImpl(cache); - } - - @Override - public XmlGenerator<Cache> getXmlGenerator() { - return new LuceneServiceXmlGenerator(); - } - - @Override - public void beforeCreate(Extensible<Cache> source, Cache cache) { - // Nothing to do here. - } - - @Override - public void onCreate(Extensible<Cache> source, Extensible<Cache> target) { - //This is called when CacheCreation (source) is turned into a GemfireCacheImpl (target) - //nothing to do there. - } - - public void registerIndex(LuceneIndex index){ - String regionAndIndex = getUniqueIndexName(index.getName(), index.getRegionPath()); - if( !indexMap.containsKey( regionAndIndex )) { - indexMap.put(regionAndIndex, index); - } - definedIndexMap.remove(regionAndIndex); - } - - public void unregisterIndex(final String region){ - if( indexMap.containsKey( region )) indexMap.remove( region ); - } - - /**Public for test purposes */ - public static void registerDataSerializables() { - DSFIDFactory.registerDSFID( - DataSerializableFixedID.LUCENE_CHUNK_KEY, - ChunkKey.class); - - DSFIDFactory.registerDSFID( - DataSerializableFixedID.LUCENE_FILE, - File.class); - - DSFIDFactory.registerDSFID( - DataSerializableFixedID.LUCENE_FUNCTION_CONTEXT, - LuceneFunctionContext.class); - - DSFIDFactory.registerDSFID( - DataSerializableFixedID.LUCENE_STRING_QUERY_PROVIDER, - StringQueryProvider.class); - - DSFIDFactory.registerDSFID( - DataSerializableFixedID.LUCENE_TOP_ENTRIES_COLLECTOR_MANAGER, - TopEntriesCollectorManager.class); - - DSFIDFactory.registerDSFID( - DataSerializableFixedID.LUCENE_ENTRY_SCORE, - EntryScore.class); - - DSFIDFactory.registerDSFID( - DataSerializableFixedID.LUCENE_TOP_ENTRIES, - TopEntries.class); - - DSFIDFactory.registerDSFID( - DataSerializableFixedID.LUCENE_TOP_ENTRIES_COLLECTOR, - TopEntriesCollector.class); - } - - public Collection<LuceneIndexCreationProfile> getAllDefinedIndexes() { - return definedIndexMap.values(); - } - - public LuceneIndexCreationProfile getDefinedIndex(String indexName, String regionPath) { - return definedIndexMap.get(getUniqueIndexName(indexName , regionPath)); - } -} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/05e6d966/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/PageableLuceneQueryResultsImpl.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/PageableLuceneQueryResultsImpl.java b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/PageableLuceneQueryResultsImpl.java deleted file mode 100644 index 1563df0..0000000 --- a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/PageableLuceneQueryResultsImpl.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package com.gemstone.gemfire.cache.lucene.internal; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.NoSuchElementException; - -import com.gemstone.gemfire.cache.Region; -import com.gemstone.gemfire.cache.lucene.PageableLuceneQueryResults; -import com.gemstone.gemfire.cache.lucene.LuceneResultStruct; -import com.gemstone.gemfire.cache.lucene.internal.distributed.EntryScore; - -/** - * Implementation of PageableLuceneQueryResults that fetchs a page at a time - * from the server, given a set of EntryScores (key and score). - * - * @param <K> The type of the key - * @param <V> The type of the value - */ -public class PageableLuceneQueryResultsImpl<K,V> implements PageableLuceneQueryResults<K,V> { - - /** - * list of docs matching search query - */ - private final List<EntryScore<K>> hits; - - /** - * * Current page of results - */ - private List<LuceneResultStruct<K,V>> currentPage; - /** - * The maximum score. Lazily evaluated - */ - private float maxScore = Float.MIN_VALUE; - - /** - * The user region where values are stored. - */ - private final Region<K, V> userRegion; - - /** - * The start of the next page of results we want to fetch - */ - private int currentHit = 0; - - /** - * The page size the user wants. - */ - private int pageSize; - - public PageableLuceneQueryResultsImpl(List<EntryScore<K>> hits, Region<K,V> userRegion, int pageSize) { - this.hits = hits; - this.userRegion = userRegion; - this.pageSize = pageSize == 0 ? Integer.MAX_VALUE : pageSize; - } - - - public List<LuceneResultStruct<K,V>> getHitEntries(int fromIndex, int toIndex) { - List<EntryScore<K>> scores = hits.subList(fromIndex, toIndex); - ArrayList<K> keys = new ArrayList<K>(scores.size()); - for(EntryScore<K> score : scores) { - keys.add(score.getKey()); - } - - Map<K,V> values = userRegion.getAll(keys); - - ArrayList<LuceneResultStruct<K,V>> results = new ArrayList<LuceneResultStruct<K,V>>(scores.size()); - for(EntryScore<K> score : scores) { - V value = values.get(score.getKey()); - if (value!=null) - results.add(new LuceneResultStructImpl(score.getKey(), value, score.getScore())); - } - return results; - } - - @Override - public List<LuceneResultStruct<K,V>> next() { - if(!hasNext()) { - throw new NoSuchElementException(); - } - List<LuceneResultStruct<K,V>> result = advancePage(); - currentPage = null; - return result; - } - - private List<LuceneResultStruct<K, V>> advancePage() { - if(currentPage != null) { - return currentPage; - } - - int resultSize = (pageSize != Integer.MAX_VALUE) ? pageSize : hits.size(); - currentPage = new ArrayList<LuceneResultStruct<K,V>>(resultSize); - while (currentPage.size()<pageSize && currentHit < hits.size()) { - int end = currentHit + pageSize - currentPage.size(); - end = end > hits.size() ? hits.size() : end; - currentPage.addAll(getHitEntries(currentHit, end)); - currentHit = end; - } - return currentPage; - } - - @Override - public boolean hasNext() { - - advancePage(); - if ( currentPage.isEmpty() ) { - return false; - } - return true; - } - - @Override - public int size() { - return hits.size(); - } - - @Override - public float getMaxScore() { - if(maxScore == Float.MIN_VALUE) { - for(EntryScore<K> score : hits) { - maxScore = Math.max(maxScore, score.getScore()); - } - } - - return maxScore; - } -} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/05e6d966/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/PartitionedRepositoryManager.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/PartitionedRepositoryManager.java b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/PartitionedRepositoryManager.java deleted file mode 100644 index d5dd7b1..0000000 --- a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/PartitionedRepositoryManager.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package com.gemstone.gemfire.cache.lucene.internal; - -import java.io.IOException; - -import com.gemstone.gemfire.cache.lucene.internal.repository.IndexRepository; -import com.gemstone.gemfire.cache.lucene.internal.repository.serializer.LuceneSerializer; -import com.gemstone.gemfire.internal.cache.PartitionedRegion; - -/** - * Manages index repositories for partitioned regions. - * - * This class lazily creates the IndexRepository for each individual - * bucket. If a Bucket is rebalanced, this class will create a new - * index repository when the bucket returns to this node. - */ -public class PartitionedRepositoryManager extends AbstractPartitionedRepositoryManager { - - public static IndexRepositoryFactory indexRepositoryFactory = new IndexRepositoryFactory(); - - public PartitionedRepositoryManager(LuceneIndexImpl index, - LuceneSerializer serializer) { - super(index, serializer); - } - - @Override - public IndexRepository createOneIndexRepository(Integer bucketId, - LuceneSerializer serializer, LuceneIndexImpl index, - PartitionedRegion userRegion) throws IOException { - return indexRepositoryFactory.createIndexRepository(bucketId, serializer, index, userRegion); - } -} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/05e6d966/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/RawIndexRepositoryFactory.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/RawIndexRepositoryFactory.java b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/RawIndexRepositoryFactory.java deleted file mode 100755 index 131e297..0000000 --- a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/RawIndexRepositoryFactory.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.gemstone.gemfire.cache.lucene.internal; - -import java.io.File; -import java.io.IOException; - -import org.apache.lucene.index.IndexWriter; -import org.apache.lucene.index.IndexWriterConfig; -import org.apache.lucene.store.Directory; -import org.apache.lucene.store.NIOFSDirectory; -import org.apache.lucene.store.RAMDirectory; - -import com.gemstone.gemfire.cache.lucene.internal.directory.RegionDirectory; -import com.gemstone.gemfire.cache.lucene.internal.repository.IndexRepository; -import com.gemstone.gemfire.cache.lucene.internal.repository.IndexRepositoryImpl; -import com.gemstone.gemfire.cache.lucene.internal.repository.serializer.LuceneSerializer; -import com.gemstone.gemfire.internal.cache.BucketRegion; -import com.gemstone.gemfire.internal.cache.PartitionedRegion; - -public class RawIndexRepositoryFactory extends IndexRepositoryFactory { - public RawIndexRepositoryFactory() { - } - - public IndexRepository createIndexRepository(final Integer bucketId, - LuceneSerializer serializer, - LuceneIndexImpl index, PartitionedRegion userRegion) - throws IOException - { - final IndexRepository repo; - 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); - } -} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/05e6d966/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/RawLuceneRepositoryManager.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/RawLuceneRepositoryManager.java b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/RawLuceneRepositoryManager.java deleted file mode 100755 index 234245e..0000000 --- a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/RawLuceneRepositoryManager.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.gemstone.gemfire.cache.lucene.internal; - -import java.io.IOException; - -import com.gemstone.gemfire.cache.lucene.internal.repository.IndexRepository; -import com.gemstone.gemfire.cache.lucene.internal.repository.serializer.LuceneSerializer; -import com.gemstone.gemfire.internal.cache.BucketNotFoundException; -import com.gemstone.gemfire.internal.cache.PartitionedRegion; - -public class RawLuceneRepositoryManager extends AbstractPartitionedRepositoryManager { - public static IndexRepositoryFactory indexRepositoryFactory = new RawIndexRepositoryFactory(); - - public RawLuceneRepositoryManager(LuceneIndexImpl index, - LuceneSerializer serializer) { - super(index, serializer); - } - - public void close() { - for (IndexRepository repo:indexRepositories.values()) { - repo.cleanup(); - } - } - - @Override - public IndexRepository createOneIndexRepository(Integer bucketId, - LuceneSerializer serializer, LuceneIndexImpl index, - PartitionedRegion userRegion) throws IOException { - return indexRepositoryFactory.createIndexRepository(bucketId, serializer, index, userRegion); - } -} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/05e6d966/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/StringQueryProvider.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/StringQueryProvider.java b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/StringQueryProvider.java deleted file mode 100644 index c5d145e..0000000 --- a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/StringQueryProvider.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package com.gemstone.gemfire.cache.lucene.internal; - -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; - -import org.apache.logging.log4j.Logger; -import org.apache.lucene.queryparser.flexible.core.QueryNodeException; -import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser; -import org.apache.lucene.search.Query; - -import com.gemstone.gemfire.DataSerializer; -import com.gemstone.gemfire.GemFireCheckedException; -import com.gemstone.gemfire.GemFireException; -import com.gemstone.gemfire.cache.lucene.LuceneIndex; -import com.gemstone.gemfire.cache.lucene.LuceneQueryException; -import com.gemstone.gemfire.cache.lucene.LuceneQueryProvider; -import com.gemstone.gemfire.cache.query.QueryException; -import com.gemstone.gemfire.internal.DataSerializableFixedID; -import com.gemstone.gemfire.internal.Version; -import com.gemstone.gemfire.internal.logging.LogService; - -/** - * Constructs a Lucene Query object by parsing a search string. The class uses {@link StandardQueryParser}. It sets - * searchable fields in a {@link LuceneIndex} as default fields. - */ -public class StringQueryProvider implements LuceneQueryProvider, DataSerializableFixedID { - - private static final long serialVersionUID = 1L; - - private static final Logger logger = LogService.getLogger(); - - // the following members hold input data and needs to be sent on wire - private String query; - - // the following members hold derived objects and need not be serialized - private transient Query luceneQuery; - - private String defaultField; - - public StringQueryProvider() { - this(null, null); - } - - public StringQueryProvider(String query, String defaultField) { - this.query = query; - this.defaultField = defaultField; - } - - @Override - public synchronized Query getQuery(LuceneIndex index) throws LuceneQueryException { - if (luceneQuery == null) { - String[] fields = index.getFieldNames(); - LuceneIndexImpl indexImpl = (LuceneIndexImpl) index; - StandardQueryParser parser = new StandardQueryParser(indexImpl.getAnalyzer()); - try { - luceneQuery = parser.parse(query, defaultField); - if (logger.isDebugEnabled()) { - logger.debug("User query " + query + " is parsed to be: " + luceneQuery); - } - } catch (QueryNodeException e) { - logger.debug("Query node exception:" + query, e); - throw new LuceneQueryException("Malformed lucene query: " + query, e); - } - } - return luceneQuery; - } - - /** - * @return the query string used to construct this query provider - */ - public String getQueryString() { - return query; - } - - @Override - public Version[] getSerializationVersions() { - return null; - } - - @Override - public int getDSFID() { - return LUCENE_STRING_QUERY_PROVIDER; - } - - @Override - public void toData(DataOutput out) throws IOException { - DataSerializer.writeString(query, out); - DataSerializer.writeString(defaultField, out); - } - - @Override - public void fromData(DataInput in) throws IOException, ClassNotFoundException { - query = DataSerializer.readString(in); - defaultField = DataSerializer.readString(in); - } -} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/05e6d966/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/cli/LuceneCliStrings.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/cli/LuceneCliStrings.java b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/cli/LuceneCliStrings.java deleted file mode 100644 index b424d8f..0000000 --- a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/cli/LuceneCliStrings.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package com.gemstone.gemfire.cache.lucene.internal.cli; - -public class LuceneCliStrings { - //Common parameters/options - public static final String LUCENE__INDEX_NAME = "name"; - public static final String LUCENE__REGION_PATH = "region"; - - //List lucene index commands - public static final String LUCENE_LIST_INDEX = "list lucene indexes"; - public static final String LUCENE_LIST_INDEX__HELP = "Display the list of lucene indexes created for all members."; - public static final String LUCENE_LIST_INDEX__ERROR_MESSAGE = "An error occurred while collecting all lucene index information across the Geode cluster: %1$s"; - public static final String LUCENE_LIST_INDEX__INDEXES_NOT_FOUND_MESSAGE = "No lucene indexes found"; - public static final String LUCENE_LIST_INDEX__STATS = "with-stats"; - public static final String LUCENE_LIST_INDEX__STATS__HELP = "Display lucene index stats"; - - //Create lucene index commands - public static final String LUCENE_CREATE_INDEX = "create lucene index"; - public static final String LUCENE_CREATE_INDEX__HELP = "Create a lucene index that can be used to execute queries."; - public static final String LUCENE_CREATE_INDEX__NAME__HELP = "Name of the lucene index to create."; - public static final String LUCENE_CREATE_INDEX__REGION_HELP = "Name/Path of the region where the lucene index is created on."; - public static final String LUCENE_CREATE_INDEX__FIELD = "field"; - public static final String LUCENE_CREATE_INDEX__FIELD_HELP = "fields on the region values which are stored in the lucene index."; - public static final String LUCENE_CREATE_INDEX__ANALYZER = "analyzer"; - public static final String LUCENE_CREATE_INDEX__ANALYZER_HELP = "Type of the analyzer for each field."; - public static final String LUCENE_CREATE_INDEX__GROUP = "group"; - public static final String LUCENE_CREATE_INDEX__GROUP__HELP = "Group of members in which the lucene index will be created."; - public static final String CREATE_INDEX__SUCCESS__MSG = "Index successfully created with following details"; - public static final String CREATE_INDEX__FAILURE__MSG = "Failed to create index \"{0}\" due to following reasons"; - public static final String CREATE_INDEX__NAME__MSG = "Name : {0}"; - public static final String CREATE_INDEX__REGIONPATH__MSG = "RegionPath : {0}"; - public static final String CREATE_INDEX__MEMBER__MSG = "Members which contain the index"; - public static final String CREATE_INDEX__NUMBER__AND__MEMBER = "{0}. {1}"; - public static final String CREATE_INDEX__EXCEPTION__OCCURRED__ON = "Occurred on following members"; - - //Describe lucene index commands - public static final String LUCENE_DESCRIBE_INDEX = "describe lucene index"; - public static final String LUCENE_DESCRIBE_INDEX__HELP = "Display the describe of lucene indexes created for all members."; - public static final String LUCENE_DESCRIBE_INDEX__ERROR_MESSAGE = "An error occurred while collecting lucene index information across the Geode cluster: %1$s"; - public static final String LUCENE_DESCRIBE_INDEX__NAME__HELP = "Name of the lucene index to describe."; - public static final String LUCENE_DESCRIBE_INDEX__REGION_HELP = "Name/Path of the region where the lucene index to be described exists."; - - - //Search lucene index commands - public static final String LUCENE_SEARCH_INDEX = "search lucene"; - public static final String LUCENE_SEARCH_INDEX__HELP = "Search lucene index"; - public static final String LUCENE_SEARCH_INDEX__ERROR_MESSAGE = "An error occurred while searching lucene index across the Geode cluster: %1$s"; - public static final String LUCENE_SEARCH_INDEX__NAME__HELP = "Name of the lucene index to search."; - public static final String LUCENE_SEARCH_INDEX__REGION_HELP = "Name/Path of the region where the lucene index exists."; - public static final String LUCENE_SEARCH_INDEX__QUERY_STRING="queryStrings"; - public static final String LUCENE_SEARCH_INDEX__LIMIT="limit"; - public static final String LUCENE_SEARCH_INDEX__LIMIT__HELP="Number of search results needed"; - public static final String LUCENE_SEARCH_INDEX__QUERY_STRING__HELP="Query string to search the lucene index"; - public static final String LUCENE_SEARCH_INDEX__DEFAULT_FIELD="defaultField"; - public static final String LUCENE_SEARCH_INDEX__DEFAULT_FIELD__HELP="Default field to search in"; - public static final String LUCENE_SEARCH_INDEX__NO_RESULTS_MESSAGE="No results"; - public static final String LUCENE_SEARCH_INDEX__PAGE_SIZE="pageSize"; - public static final String LUCENE_SEARCH_INDEX__PAGE_SIZE__HELP="Number of results to be returned in a page"; - public static final String LUCENE_SEARCH_INDEX__KEYSONLY="keys-only"; - public static final String LUCENE_SEARCH_INDEX__KEYSONLY__HELP="Return only keys of search results."; - -} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/05e6d966/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/cli/LuceneIndexCommands.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/cli/LuceneIndexCommands.java b/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/cli/LuceneIndexCommands.java deleted file mode 100755 index 200a14f..0000000 --- a/geode-lucene/src/main/java/com/gemstone/gemfire/cache/lucene/internal/cli/LuceneIndexCommands.java +++ /dev/null @@ -1,470 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.gemstone.gemfire.cache.lucene.internal.cli; - -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; - -import org.apache.geode.security.ResourcePermission.Operation; -import org.apache.geode.security.ResourcePermission.Resource; -import org.springframework.shell.core.annotation.CliAvailabilityIndicator; -import org.springframework.shell.core.annotation.CliCommand; -import org.springframework.shell.core.annotation.CliOption; - -import com.gemstone.gemfire.SystemFailure; -import com.gemstone.gemfire.cache.Cache; -import com.gemstone.gemfire.cache.execute.Execution; -import com.gemstone.gemfire.cache.execute.FunctionAdapter; -import com.gemstone.gemfire.cache.execute.FunctionInvocationTargetException; -import com.gemstone.gemfire.cache.execute.ResultCollector; - -import com.gemstone.gemfire.cache.lucene.internal.cli.functions.LuceneCreateIndexFunction; -import com.gemstone.gemfire.cache.lucene.internal.cli.functions.LuceneDescribeIndexFunction; -import com.gemstone.gemfire.cache.lucene.internal.cli.functions.LuceneListIndexFunction; -import com.gemstone.gemfire.cache.lucene.internal.cli.functions.LuceneSearchIndexFunction; -import com.gemstone.gemfire.distributed.DistributedMember; -import com.gemstone.gemfire.internal.cache.execute.AbstractExecution; -import com.gemstone.gemfire.internal.security.IntegratedSecurityService; -import com.gemstone.gemfire.internal.security.SecurityService; -import com.gemstone.gemfire.management.cli.CliMetaData; -import com.gemstone.gemfire.management.cli.ConverterHint; -import com.gemstone.gemfire.management.cli.Result; -import com.gemstone.gemfire.management.internal.cli.CliUtil; -import com.gemstone.gemfire.management.internal.cli.commands.AbstractCommandsSupport; -import com.gemstone.gemfire.management.internal.cli.functions.CliFunctionResult; -import com.gemstone.gemfire.management.internal.cli.i18n.CliStrings; -import com.gemstone.gemfire.management.internal.cli.result.CommandResult; -import com.gemstone.gemfire.management.internal.cli.result.CommandResultException; -import com.gemstone.gemfire.management.internal.cli.result.ResultBuilder; -import com.gemstone.gemfire.management.internal.cli.result.TabularResultData; -import com.gemstone.gemfire.management.internal.cli.shell.Gfsh; -import com.gemstone.gemfire.management.internal.configuration.domain.XmlEntity; -import com.gemstone.gemfire.management.internal.security.ResourceOperation; - -/** - * The LuceneIndexCommands class encapsulates all Geode shell (Gfsh) commands related to Lucene indexes defined in Geode. - * </p> - * @see AbstractCommandsSupport - * @see LuceneIndexDetails - * @see LuceneListIndexFunction - */ -@SuppressWarnings("unused") -public class LuceneIndexCommands extends AbstractCommandsSupport { - private static final LuceneCreateIndexFunction createIndexFunction = new LuceneCreateIndexFunction(); - private static final LuceneDescribeIndexFunction describeIndexFunction = new LuceneDescribeIndexFunction(); - private static final LuceneSearchIndexFunction searchIndexFunction = new LuceneSearchIndexFunction(); - private List<LuceneSearchResults> searchResults=null; - - private SecurityService securityService = IntegratedSecurityService.getSecurityService(); - - @CliCommand(value = LuceneCliStrings.LUCENE_LIST_INDEX, help = LuceneCliStrings.LUCENE_LIST_INDEX__HELP) - @CliMetaData(shellOnly = false, relatedTopic={CliStrings.TOPIC_GEODE_REGION, CliStrings.TOPIC_GEODE_DATA }) - @ResourceOperation(resource = Resource.CLUSTER, operation = Operation.READ) - public Result listIndex( - @CliOption(key = LuceneCliStrings.LUCENE_LIST_INDEX__STATS, - mandatory=false, - specifiedDefaultValue = "true", - unspecifiedDefaultValue = "false", - help = LuceneCliStrings.LUCENE_LIST_INDEX__STATS__HELP) final boolean stats) { - - try { - return toTabularResult(getIndexListing(),stats); - } - catch (FunctionInvocationTargetException ignore) { - return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COULD_NOT_EXECUTE_COMMAND_TRY_AGAIN, - LuceneCliStrings.LUCENE_LIST_INDEX)); - } - catch (VirtualMachineError e) { - SystemFailure.initiateFailure(e); - throw e; - } - catch (Throwable t) { - SystemFailure.checkFailure(); - getCache().getLogger().info(t); - return ResultBuilder.createGemFireErrorResult(String.format(LuceneCliStrings.LUCENE_LIST_INDEX__ERROR_MESSAGE, - toString(t, isDebugging()))); - } - } - - @SuppressWarnings("unchecked") - protected List<LuceneIndexDetails> getIndexListing() { - final Execution functionExecutor = getMembersFunctionExecutor(getMembers(getCache())); - - if (functionExecutor instanceof AbstractExecution) { - ((AbstractExecution) functionExecutor).setIgnoreDepartedMembers(true); - } - - final ResultCollector resultsCollector = functionExecutor.execute(new LuceneListIndexFunction()); - final List<Set<LuceneIndexDetails>> results = (List<Set<LuceneIndexDetails>>) resultsCollector.getResult(); - - return results.stream() - .flatMap(set -> set.stream()) - .sorted() - .collect(Collectors.toList()); - } - - protected Result toTabularResult(final List<LuceneIndexDetails> indexDetailsList, boolean stats) { - if (!indexDetailsList.isEmpty()) { - final TabularResultData indexData = ResultBuilder.createTabularResultData(); - - for (final LuceneIndexDetails indexDetails : indexDetailsList) { - indexData.accumulate("Index Name", indexDetails.getIndexName()); - indexData.accumulate("Region Path", indexDetails.getRegionPath()); - indexData.accumulate("Indexed Fields", indexDetails.getSearchableFieldNamesString()); - indexData.accumulate("Field Analyzer", indexDetails.getFieldAnalyzersString()); - indexData.accumulate("Status", indexDetails.getInitialized() == true ? "Initialized" : "Defined"); - - if (stats == true) { - if (!indexDetails.getInitialized()) { - indexData.accumulate("Query Executions", "NA"); - indexData.accumulate("Updates", "NA"); - indexData.accumulate("Commits", "NA"); - indexData.accumulate("Documents", "NA"); - } - else { - indexData.accumulate("Query Executions", indexDetails.getIndexStats().get("queryExecutions")); - indexData.accumulate("Updates", indexDetails.getIndexStats().get("updates")); - indexData.accumulate("Commits", indexDetails.getIndexStats().get("commits")); - indexData.accumulate("Documents", indexDetails.getIndexStats().get("documents")); - } - } - } - return ResultBuilder.buildResult(indexData); - } - else { - return ResultBuilder.createInfoResult(LuceneCliStrings.LUCENE_LIST_INDEX__INDEXES_NOT_FOUND_MESSAGE); - } - } - - @CliCommand(value = LuceneCliStrings.LUCENE_CREATE_INDEX, help = LuceneCliStrings.LUCENE_CREATE_INDEX__HELP) - @CliMetaData(shellOnly = false, relatedTopic={CliStrings.TOPIC_GEODE_REGION, CliStrings.TOPIC_GEODE_DATA }, writesToSharedConfiguration=true) - //TODO : Add optionContext for indexName - public Result createIndex( - @CliOption(key = LuceneCliStrings.LUCENE__INDEX_NAME, - mandatory=true, - help = LuceneCliStrings.LUCENE_CREATE_INDEX__NAME__HELP) final String indexName, - - @CliOption (key = LuceneCliStrings.LUCENE__REGION_PATH, - mandatory = true, - optionContext = ConverterHint.REGIONPATH, - help = LuceneCliStrings.LUCENE_CREATE_INDEX__REGION_HELP) final String regionPath, - - @CliOption(key = LuceneCliStrings.LUCENE_CREATE_INDEX__FIELD, - mandatory = true, - help = LuceneCliStrings.LUCENE_CREATE_INDEX__FIELD_HELP) - @CliMetaData (valueSeparator = ",") final String[] fields, - - @CliOption(key = LuceneCliStrings.LUCENE_CREATE_INDEX__ANALYZER, - mandatory = false, - unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, - help = LuceneCliStrings.LUCENE_CREATE_INDEX__ANALYZER_HELP) - @CliMetaData (valueSeparator = ",") final String[] analyzers, - - @CliOption (key = LuceneCliStrings.LUCENE_CREATE_INDEX__GROUP, - optionContext = ConverterHint.MEMBERGROUP, - unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, - help = LuceneCliStrings.LUCENE_CREATE_INDEX__GROUP__HELP) - @CliMetaData (valueSeparator = ",") final String[] groups) { - - Result result = null; - XmlEntity xmlEntity = null; - - this.securityService.authorizeRegionManage(regionPath); - try { - final Cache cache = getCache(); - LuceneIndexInfo indexInfo = new LuceneIndexInfo(indexName, regionPath, fields, analyzers); - final ResultCollector<?, ?> rc = this.executeFunctionOnGroups(createIndexFunction, groups, indexInfo); - final List<CliFunctionResult> funcResults = (List<CliFunctionResult>) rc.getResult(); - - final TabularResultData tabularResult = ResultBuilder.createTabularResultData(); - for (final CliFunctionResult cliFunctionResult : funcResults) { - tabularResult.accumulate("Member",cliFunctionResult.getMemberIdOrName()); - - if (cliFunctionResult.isSuccessful()) { - tabularResult.accumulate("Status","Successfully created lucene index"); -// if (xmlEntity == null) { -// xmlEntity = cliFunctionResult.getXmlEntity(); -// } - } - else { - tabularResult.accumulate("Status","Failed: "+cliFunctionResult.getMessage()); - } - } - result = ResultBuilder.buildResult(tabularResult); - } - catch (CommandResultException crex) { - result = crex.getResult(); - } catch (Exception e) { - result = ResultBuilder.createGemFireErrorResult(e.getMessage()); - } -// TODO - store in cluster config -// if (xmlEntity != null) { -// result.setCommandPersisted((new SharedConfigurationWriter()).addXmlEntity(xmlEntity, groups)); -// } - - return result; - } - - @CliCommand(value = LuceneCliStrings.LUCENE_DESCRIBE_INDEX, help = LuceneCliStrings.LUCENE_DESCRIBE_INDEX__HELP) - @CliMetaData(shellOnly = false, relatedTopic={CliStrings.TOPIC_GEODE_REGION, CliStrings.TOPIC_GEODE_DATA }) - @ResourceOperation(resource = Resource.CLUSTER, operation = Operation.READ) - public Result describeIndex( - @CliOption(key = LuceneCliStrings.LUCENE__INDEX_NAME, - mandatory=true, - help = LuceneCliStrings.LUCENE_DESCRIBE_INDEX__NAME__HELP) final String indexName, - - @CliOption (key = LuceneCliStrings.LUCENE__REGION_PATH, - mandatory = true, - optionContext = ConverterHint.REGIONPATH, - help = LuceneCliStrings.LUCENE_DESCRIBE_INDEX__REGION_HELP) final String regionPath) { - try { - LuceneIndexInfo indexInfo = new LuceneIndexInfo(indexName, regionPath); - return toTabularResult(getIndexDetails(indexInfo),true); - } - catch (FunctionInvocationTargetException ignore) { - return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COULD_NOT_EXECUTE_COMMAND_TRY_AGAIN, - LuceneCliStrings.LUCENE_DESCRIBE_INDEX)); - } - catch (VirtualMachineError e) { - SystemFailure.initiateFailure(e); - throw e; - } - catch (IllegalArgumentException e) { - return ResultBuilder.createInfoResult(e.getMessage()); - } - catch (Throwable t) { - SystemFailure.checkFailure(); - getCache().getLogger().info(t); - return ResultBuilder.createGemFireErrorResult(String.format(LuceneCliStrings.LUCENE_DESCRIBE_INDEX__ERROR_MESSAGE, - toString(t, isDebugging()))); - } - } - - @SuppressWarnings("unchecked") - protected List<LuceneIndexDetails> getIndexDetails(LuceneIndexInfo indexInfo) throws Exception { - this.securityService.authorizeRegionManage(indexInfo.getRegionPath()); - final ResultCollector<?, ?> rc = this.executeFunctionOnGroups(describeIndexFunction, new String[] {}, indexInfo); - final List<LuceneIndexDetails> funcResults = (List<LuceneIndexDetails>) rc.getResult(); - return funcResults.stream().filter(indexDetails -> indexDetails != null).collect(Collectors.toList()); - } - - @CliCommand(value = LuceneCliStrings.LUCENE_SEARCH_INDEX, help = LuceneCliStrings.LUCENE_SEARCH_INDEX__HELP) - @CliMetaData(shellOnly = false, relatedTopic = { CliStrings.TOPIC_GEODE_REGION, CliStrings.TOPIC_GEODE_DATA }) - @ResourceOperation(resource = Resource.CLUSTER, operation = Operation.READ) - public Result searchIndex( - @CliOption(key = LuceneCliStrings.LUCENE__INDEX_NAME, - mandatory = true, - help = LuceneCliStrings.LUCENE_SEARCH_INDEX__NAME__HELP) final String indexName, - - @CliOption(key = LuceneCliStrings.LUCENE__REGION_PATH, - mandatory = true, - optionContext = ConverterHint.REGIONPATH, - help = LuceneCliStrings.LUCENE_SEARCH_INDEX__REGION_HELP) final String regionPath, - - @CliOption(key = LuceneCliStrings.LUCENE_SEARCH_INDEX__QUERY_STRING, - mandatory = true, - help = LuceneCliStrings.LUCENE_SEARCH_INDEX__QUERY_STRING__HELP) final String queryString, - - @CliOption(key = LuceneCliStrings.LUCENE_SEARCH_INDEX__DEFAULT_FIELD, - mandatory = true, - help = LuceneCliStrings.LUCENE_SEARCH_INDEX__DEFAULT_FIELD__HELP) final String defaultField, - - @CliOption(key = LuceneCliStrings.LUCENE_SEARCH_INDEX__LIMIT, - mandatory = false, - unspecifiedDefaultValue = "-1", - help = LuceneCliStrings.LUCENE_SEARCH_INDEX__LIMIT__HELP) final int limit, - - @CliOption(key = LuceneCliStrings.LUCENE_SEARCH_INDEX__PAGE_SIZE, - mandatory = false, - unspecifiedDefaultValue = "-1", - help = LuceneCliStrings.LUCENE_SEARCH_INDEX__PAGE_SIZE__HELP) int pageSize, - - @CliOption(key = LuceneCliStrings.LUCENE_SEARCH_INDEX__KEYSONLY, - mandatory = false, - unspecifiedDefaultValue = "false", - help = LuceneCliStrings.LUCENE_SEARCH_INDEX__KEYSONLY__HELP) boolean keysOnly) - { - try { - LuceneQueryInfo queryInfo = new LuceneQueryInfo(indexName, regionPath, queryString, defaultField, limit, keysOnly); - if (pageSize == -1) { - pageSize = Integer.MAX_VALUE; - } - searchResults = getSearchResults(queryInfo); - return displayResults(pageSize, keysOnly); - } - catch (FunctionInvocationTargetException ignore) { - return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COULD_NOT_EXECUTE_COMMAND_TRY_AGAIN, - LuceneCliStrings.LUCENE_SEARCH_INDEX)); - } - catch (VirtualMachineError e) { - SystemFailure.initiateFailure(e); - throw e; - } - catch (IllegalArgumentException e) { - return ResultBuilder.createInfoResult(e.getMessage()); - } - catch (Throwable t) { - SystemFailure.checkFailure(); - getCache().getLogger().info(t); - return ResultBuilder.createGemFireErrorResult(String.format(LuceneCliStrings.LUCENE_SEARCH_INDEX__ERROR_MESSAGE, - toString(t, isDebugging()))); - } - } - - private Result displayResults(int pageSize, boolean keysOnly) throws Exception { - if (searchResults.size() == 0) { - return ResultBuilder.createInfoResult(LuceneCliStrings.LUCENE_SEARCH_INDEX__NO_RESULTS_MESSAGE); - } - - Gfsh gfsh = initGfsh(); - boolean pagination = searchResults.size() > pageSize; - int fromIndex = 0; - int toIndex = pageSize < searchResults.size() ? pageSize : searchResults.size(); - int currentPage = 1; - int totalPages = (int) Math.ceil((float) searchResults.size() / pageSize); - boolean skipDisplay = false; - String step = null; - do { - - if (!skipDisplay) { - CommandResult commandResult = (CommandResult) getResults(fromIndex, toIndex, keysOnly); - if (!pagination) { - return commandResult; - } - Gfsh.println(); - while (commandResult.hasNextLine()) { - gfsh.printAsInfo(commandResult.nextLine()); - } - gfsh.printAsInfo("\t\tPage " + currentPage + " of " + totalPages); - String message = ("Press n to move to next page, q to quit and p to previous page : "); - step = gfsh.interact(message); - } - - switch (step) { - case "n": - { - if (currentPage == totalPages) { - gfsh.printAsInfo("No more results to display."); - step = gfsh.interact("Press p to move to last page and q to quit."); - skipDisplay = true; - continue; - } - - if(skipDisplay) { - skipDisplay=false; - } - else { - currentPage++; - int current = fromIndex; - fromIndex = toIndex; - toIndex = (pageSize + fromIndex >= searchResults.size()) ? searchResults.size() : pageSize + fromIndex; - } - break; - } - case "p": { - if (currentPage == 1) { - gfsh.printAsInfo("At the top of the search results."); - step = gfsh.interact("Press n to move to the first page and q to quit."); - skipDisplay=true; - continue; - } - - if (skipDisplay) { - skipDisplay = false; - } - else { - currentPage--; - int current = fromIndex; - toIndex = fromIndex; - fromIndex = current - pageSize <= 0 ? 0 : current - pageSize; - } - break; - } - case "q": - return ResultBuilder.createInfoResult("Search complete."); - default: - Gfsh.println("Invalid option"); - break; - } - } while(true); - } - - protected Gfsh initGfsh() { - return Gfsh.getCurrentInstance(); - } - - private List<LuceneSearchResults> getSearchResults(final LuceneQueryInfo queryInfo) throws Exception { - securityService.authorizeRegionManage(queryInfo.getRegionPath()); - - final String[] groups = {}; - final ResultCollector<?, ?> rc = this.executeSearch(queryInfo); - final List<Set<LuceneSearchResults>> functionResults = (List<Set<LuceneSearchResults>>) rc.getResult(); - - return functionResults.stream() - .flatMap(set -> set.stream()) - .sorted() - .collect(Collectors.toList()); - } - - private Result getResults(int fromIndex, int toIndex, boolean keysonly) throws Exception { - final TabularResultData data = ResultBuilder.createTabularResultData(); - for (int i = fromIndex; i < toIndex; i++) { - if (!searchResults.get(i).getExeptionFlag()) { - data.accumulate("key", searchResults.get(i).getKey()); - if (!keysonly) { - data.accumulate("value", searchResults.get(i).getValue()); - data.accumulate("score", searchResults.get(i).getScore()); - } - } - else { - throw new Exception(searchResults.get(i).getExceptionMessage()); - } - } - return ResultBuilder.buildResult(data); - } - - protected ResultCollector<?, ?> executeFunctionOnGroups(FunctionAdapter function, - String[] groups, - final LuceneIndexInfo indexInfo) throws IllegalArgumentException, CommandResultException - { - final Set<DistributedMember> targetMembers; - if (function != createIndexFunction) { - targetMembers = CliUtil.getMembersForeRegionViaFunction(getCache(), indexInfo.getRegionPath()); - if (targetMembers.isEmpty()) { - throw new IllegalArgumentException("Region not found."); - } - } - else { - targetMembers = CliUtil.findAllMatchingMembers(groups, null); - } - return CliUtil.executeFunction(function, indexInfo, targetMembers); - } - - protected ResultCollector<?, ?> executeSearch(final LuceneQueryInfo queryInfo) throws Exception { - final Set<DistributedMember> targetMembers = CliUtil.getMembersForeRegionViaFunction(getCache(),queryInfo.getRegionPath()); - if (targetMembers.isEmpty()) - throw new IllegalArgumentException("Region not found."); - return CliUtil.executeFunction(searchIndexFunction, queryInfo, targetMembers); - } - - @CliAvailabilityIndicator({LuceneCliStrings.LUCENE_SEARCH_INDEX, LuceneCliStrings.LUCENE_CREATE_INDEX, - LuceneCliStrings.LUCENE_DESCRIBE_INDEX, LuceneCliStrings.LUCENE_LIST_INDEX}) - public boolean indexCommandsAvailable() { - return (!CliUtil.isGfshVM() || (getGfsh() != null && getGfsh().isConnectedAndReady())); - } -}
