Exemplo n.º 1
0
        public IndexWriteOperation(Index index, LuceneVoronDirectory directory, LuceneDocumentConverterBase converter, Transaction writeTransaction, LuceneIndexPersistence persistence)
            : base(index, LoggingSource.Instance.GetLogger <IndexWriteOperation>(index._indexStorage.DocumentDatabase.Name))
        {
            _converter       = converter;
            DocumentDatabase = index._indexStorage.DocumentDatabase;

            try
            {
                _analyzer = CreateAnalyzer(() => new LowerCaseKeywordAnalyzer(), index.Definition.IndexFields);
            }
            catch (Exception e)
            {
                throw new IndexAnalyzerException(e);
            }

            try
            {
                _releaseWriteTransaction = directory.SetTransaction(writeTransaction, out _state);
                _writer = persistence.EnsureIndexWriter(_state);

                _suggestionsWriters = persistence.EnsureSuggestionIndexWriter(_state);
                _hasSuggestions     = _suggestionsWriters.Count > 0;

                _locker = directory.MakeLock("writing-to-index.lock");

                if (_locker.Obtain() == false)
                {
                    throw new InvalidOperationException($"Could not obtain the 'writing-to-index' lock for '{_indexName}' index.");
                }
            }
            catch (Exception e)
            {
                throw new IndexWriteException(e);
            }
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public IndexWriteOperation(string indexName, Dictionary <string, IndexField> fields,
                                   LuceneVoronDirectory directory, LuceneDocumentConverterBase converter,
                                   Transaction writeTransaction, LuceneIndexPersistence persistence, DocumentDatabase documentDatabase)
            : base(indexName, LoggingSource.Instance.GetLogger <IndexWriteOperation>(documentDatabase.Name))
        {
            _converter = converter;
            try
            {
                _analyzer = CreateAnalyzer(() => new LowerCaseKeywordAnalyzer(), fields);
            }
            catch (Exception e)
            {
                throw new IndexAnalyzerException(e);
            }

            try
            {
                _releaseWriteTransaction = directory.SetTransaction(writeTransaction);

                _writer = persistence.EnsureIndexWriter();

                _locker = directory.MakeLock("writing-to-index.lock");

                if (_locker.Obtain() == false)
                {
                    throw new InvalidOperationException($"Could not obtain the 'writing-to-index' lock for '{_indexName}' index.");
                }
            }
            catch (Exception e)
            {
                throw new IndexWriteException(e);
            }
        }
Exemplo n.º 4
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);
            }
        }
Exemplo n.º 5
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));
                }
            }
        }
 public OutputReduceIndexWriteOperation(MapReduceIndex index, LuceneVoronDirectory directory, LuceneDocumentConverterBase converter, Transaction writeTransaction,
                                        LuceneIndexPersistence persistence, JsonOperationContext indexContext)
     : base(index, directory, converter, writeTransaction, persistence)
 {
     Debug.Assert(index.OutputReduceToCollection != null);
     _txHolder = new TransactionHolder(writeTransaction);
     _outputReduceToCollectionCommandBatcher = index.OutputReduceToCollection.CreateCommandBatcher(indexContext, _txHolder);
 }
        public OutputReduceIndexWriteOperation(MapReduceIndex index, LuceneVoronDirectory directory, LuceneDocumentConverterBase converter, Transaction writeTransaction, LuceneIndexPersistence persistence)
            : base(index, directory, converter, writeTransaction, persistence)
        {
            var outputReduceToCollection = index.Definition.OutputReduceToCollection;

            Debug.Assert(string.IsNullOrWhiteSpace(outputReduceToCollection) == false);
            _outputReduceToCollectionCommand = new OutputReduceToCollectionCommand(DocumentDatabase, outputReduceToCollection, index);
        }