コード例 #1
0
        public LuceneIndexPersistence(Index index)
        {
            _index = index;

            var fields = index.Definition.MapFields.Values.ToList();

            switch (_index.Type)
            {
            case IndexType.AutoMap:
                _converter = new LuceneDocumentConverter(fields, reduceOutput: false);
                break;

            case IndexType.AutoMapReduce:
                var autoMapReduceIndexDefinition = (AutoMapReduceIndexDefinition)_index.Definition;
                fields.AddRange(autoMapReduceIndexDefinition.GroupByFields.Values);

                _converter = new LuceneDocumentConverter(fields, reduceOutput: true);
                break;

            case IndexType.MapReduce:
            case IndexType.Map:
                _converter = new AnonymousLuceneDocumentConverter(fields, reduceOutput: _index.Type.IsMapReduce());
                break;

            case IndexType.Faulty:
                _converter = null;
                break;

            default:
                throw new NotSupportedException(_index.Type.ToString());
            }

            _fields = fields.ToDictionary(x => IndexField.ReplaceInvalidCharactersInFieldName(x.Name), x => (object)null);
            _indexSearcherHolder = new IndexSearcherHolder(() => new IndexSearcher(_directory, true), _index._indexStorage.DocumentDatabase);
        }
コード例 #2
0
        public LuceneIndexPersistence(Index index)
        {
            _index = index;
            _suggestionsDirectories          = new Dictionary <string, LuceneVoronDirectory>();
            _suggestionsIndexSearcherHolders = new Dictionary <string, IndexSearcherHolder>();

            var fields = index.Definition.IndexFields.Values;

            switch (_index.Type)
            {
            case IndexType.AutoMap:
                _converter = new LuceneDocumentConverter(fields);
                break;

            case IndexType.AutoMapReduce:
                _converter = new LuceneDocumentConverter(fields, reduceOutput: true);
                break;

            case IndexType.MapReduce:
                _converter = new AnonymousLuceneDocumentConverter(fields, _index.IsMultiMap, reduceOutput: true);
                break;

            case IndexType.Map:
                _converter = new AnonymousLuceneDocumentConverter(fields, _index.IsMultiMap);
                break;

            case IndexType.Faulty:
                _converter = null;
                break;

            default:
                throw new NotSupportedException(_index.Type.ToString());
            }

            _fields = fields.ToDictionary(x => x.Name, x => x);
            _indexSearcherHolder = new IndexSearcherHolder(state => new IndexSearcher(_directory, true, state), _index._indexStorage.DocumentDatabase);

            foreach (var field in _fields)
            {
                if (!field.Value.HasSuggestions)
                {
                    continue;
                }

                string fieldName = field.Key;
                _suggestionsIndexSearcherHolders[fieldName] = new IndexSearcherHolder(state => new IndexSearcher(_suggestionsDirectories[fieldName], true, state), _index._indexStorage.DocumentDatabase);
            }
        }
コード例 #3
0
        public IndexReadOperation(Index index, LuceneVoronDirectory directory, IndexSearcherHolder searcherHolder, Transaction readTransaction)
            : base(index.Name, LoggingSource.Instance.GetLogger <IndexReadOperation>(index._indexStorage.DocumentDatabase.Name))
        {
            try
            {
                _analyzer = CreateAnalyzer(() => new LowerCaseKeywordAnalyzer(), index.Definition.IndexFields, forQuerying: true);
            }
            catch (Exception e)
            {
                throw new IndexAnalyzerException(e);
            }

            _maxNumberOfOutputsPerDocument = index.MaxNumberOfOutputsPerDocument;
            _indexType              = index.Type;
            _indexHasBoostedFields  = index.HasBoostedFields;
            _releaseReadTransaction = directory.SetTransaction(readTransaction, out _state);
            _releaseSearcher        = searcherHolder.GetSearcher(readTransaction, _state, out _searcher);
        }
コード例 #4
0
        public IndexFacetedReadOperation(string indexName,
                                         Dictionary <string, IndexField> fields,
                                         LuceneVoronDirectory directory,
                                         IndexSearcherHolder searcherHolder,
                                         Transaction readTransaction,
                                         DocumentDatabase documentDatabase)
            : base(indexName, LoggingSource.Instance.GetLogger <IndexFacetedReadOperation>(documentDatabase.Name))
        {
            try
            {
                _analyzer = CreateAnalyzer(() => new LowerCaseKeywordAnalyzer(), fields, forQuerying: true);
            }
            catch (Exception e)
            {
                throw new IndexAnalyzerException(e);
            }

            _releaseReadTransaction = directory.SetTransaction(readTransaction, out _state);
            _currentStateHolder     = searcherHolder.GetStateHolder(readTransaction);
            _searcher = _currentStateHolder.GetIndexSearcher(_state);
        }
コード例 #5
0
        public IndexFacetedReadOperation(Index index,
                                         IndexDefinitionBaseServerSide indexDefinition,
                                         LuceneVoronDirectory directory,
                                         IndexSearcherHolder searcherHolder,
                                         QueryBuilderFactories queryBuilderFactories,
                                         Transaction readTransaction,
                                         DocumentDatabase documentDatabase)
            : base(index, LoggingSource.Instance.GetLogger <IndexFacetedReadOperation>(documentDatabase.Name))
        {
            try
            {
                _analyzer = CreateAnalyzer(index, indexDefinition, forQuerying: true);
            }
            catch (Exception e)
            {
                throw new IndexAnalyzerException(e);
            }

            _queryBuilderFactories  = queryBuilderFactories;
            _releaseReadTransaction = directory.SetTransaction(readTransaction, out _state);
            _releaseSearcher        = searcherHolder.GetSearcher(readTransaction, _state, out _searcher);
        }
コード例 #6
0
        public LuceneIndexPersistence(Index index)
        {
            _index  = index;
            _logger = LoggingSource.Instance.GetLogger <LuceneIndexPersistence>(index.DocumentDatabase.Name);
            _suggestionsDirectories          = new Dictionary <string, LuceneVoronDirectory>();
            _suggestionsIndexSearcherHolders = new Dictionary <string, IndexSearcherHolder>();
            _disposeOnce = new DisposeOnce <SingleAttempt>(() =>
            {
                DisposeWriters();

                _lastReader?.Dispose();
                _indexSearcherHolder?.Dispose();
                _converter?.Dispose();
                _directory?.Dispose();

                foreach (var directory in _suggestionsDirectories)
                {
                    directory.Value?.Dispose();
                }
            });

            var fields = index.Definition.IndexFields.Values;

            switch (_index.Type)
            {
            case IndexType.AutoMap:
                _converter = new LuceneDocumentConverter(fields);
                break;

            case IndexType.AutoMapReduce:
                _converter = new LuceneDocumentConverter(fields, reduceOutput: true);
                break;

            case IndexType.MapReduce:
                _converter = new AnonymousLuceneDocumentConverter(fields, _index.IsMultiMap, reduceOutput: true);
                break;

            case IndexType.Map:
                _converter = new AnonymousLuceneDocumentConverter(fields, _index.IsMultiMap);
                break;

            case IndexType.JavaScriptMap:
                _converter = new JintLuceneDocumentConverter(fields);
                break;

            case IndexType.JavaScriptMapReduce:
                _converter = new JintLuceneDocumentConverter(fields, reduceOutput: true);
                break;

            case IndexType.Faulty:
                _converter = null;
                break;

            default:
                throw new NotSupportedException(_index.Type.ToString());
            }

            _fields = fields.ToDictionary(x => x.Name, x => x);

            _indexSearcherHolder = new IndexSearcherHolder(CreateIndexSearcher, _index._indexStorage.DocumentDatabase);

            foreach (var field in _fields)
            {
                if (!field.Value.HasSuggestions)
                {
                    continue;
                }

                string fieldName = field.Key;
                _suggestionsIndexSearcherHolders[fieldName] = new IndexSearcherHolder(state => new IndexSearcher(_suggestionsDirectories[fieldName], true, state), _index._indexStorage.DocumentDatabase);
            }

            IndexSearcher CreateIndexSearcher(IState state)
            {
                lock (this)
                {
                    var reader = _lastReader;

                    if (reader != null)
                    {
                        if (reader.RefCount <= 0)
                        {
                            reader = null;
                        }
                        else
                        {
                            try
                            {
                                var newReader = reader.Reopen(state);
                                if (newReader != reader)
                                {
                                    reader.DecRef(state);
                                }

                                reader = _lastReader = newReader;
                            }
                            catch (Exception e)
                            {
                                if (_logger.IsInfoEnabled)
                                {
                                    _logger.Info($"Could not reopen the index reader for index '{_index.Name}'.", e);
                                }

                                // fallback strategy in case of a reader to be closed
                                // before Reopen and DecRef are executed
                                reader = null;
                            }
                        }
                    }

                    if (reader == null)
                    {
                        reader = _lastReader = IndexReader.Open(_directory, readOnly: true, state);
                    }

                    reader.IncRef();

                    return(new IndexSearcher(reader));
                }
            }
        }
コード例 #7
0
 public LuceneSuggestionIndexReader(Index index, LuceneVoronDirectory directory, IndexSearcherHolder searcherHolder, Transaction readTransaction)
     : base(index, LoggingSource.Instance.GetLogger <LuceneSuggestionIndexReader>(index._indexStorage.DocumentDatabase.Name))
 {
     _releaseReadTransaction = directory.SetTransaction(readTransaction, out _state);
     _releaseSearcher        = searcherHolder.GetSearcher(readTransaction, _state, out _searcher);
 }