public void Dispose() { if (_disposed) { throw new ObjectDisposedException(nameof(Index)); } _disposed = true; DisposeWriters(); _indexSearcherHolder?.Dispose(); _converter?.Dispose(); _directory?.Dispose(); foreach (var directory in _suggestionsDirectories) { directory.Value?.Dispose(); } }
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)); } } }
public LuceneIndexPersistence(Index index) { _index = index; _suggestionsDirectories = new Dictionary <string, LuceneVoronDirectory>(); _suggestionsIndexSearcherHolders = new Dictionary <string, IndexSearcherHolder>(); _disposeOnce = new DisposeOnce <SingleAttempt>(() => { DisposeWriters(); _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.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); } }