private async Task WriteAsync(string indexName, Action <IndexWriter> action, bool close = false)
        {
            if (!_writers.TryGetValue(indexName, out var writer))
            {
                var indexAnalyzer = await _luceneIndexSettingsService.LoadIndexAnalyzerAsync(indexName);

                lock (this)
                {
                    if (!_writers.TryGetValue(indexName, out writer))
                    {
                        var directory = CreateDirectory(indexName);
                        var analyzer  = _luceneAnalyzerManager.CreateAnalyzer(indexAnalyzer);
                        var config    = new IndexWriterConfig(LuceneSettings.DefaultVersion, analyzer)
                        {
                            OpenMode         = OpenMode.CREATE_OR_APPEND,
                            WriteLockTimeout = Lock.LOCK_POLL_INTERVAL * 3
                        };

                        writer = new IndexWriterWrapper(directory, config);

                        if (close)
                        {
                            action?.Invoke(writer);
                            writer.Dispose();
                            _timestamps[indexName] = _clock.UtcNow;
                            return;
                        }

                        _writers[indexName] = writer;
                    }
                }
            }

            if (writer.IsClosing)
            {
                return;
            }

            action?.Invoke(writer);

            _timestamps[indexName] = _clock.UtcNow;
        }
Exemple #2
0
        private void Write(string indexName, Action <IndexWriter> action, bool close = false)
        {
            if (!_writers.TryGetValue(indexName, out var writer))
            {
                lock (this)
                {
                    if (!_writers.TryGetValue(indexName, out writer))
                    {
                        var directory = CreateDirectory(indexName);
                        var analyzer  = _luceneAnalyzerManager.CreateAnalyzer(LuceneSettings.StandardAnalyzer);
                        var config    = new IndexWriterConfig(LuceneSettings.DefaultVersion, analyzer)
                        {
                            OpenMode = OpenMode.CREATE_OR_APPEND
                        };

                        writer = _writers[indexName] = new IndexWriterWrapper(directory, config);
                    }
                }
            }

            if (writer.IsClosing)
            {
                return;
            }

            action?.Invoke(writer);

            if (close && !writer.IsClosing)
            {
                lock (this)
                {
                    if (!writer.IsClosing)
                    {
                        writer.IsClosing = true;
                        writer.Dispose();
                        _writers.TryRemove(indexName, out writer);
                    }
                }
            }

            _timestamps[indexName] = _clock.UtcNow;
        }