From b103148e4c9e8ede791ee4a86b4789bd3e0259d2 Mon Sep 17 00:00:00 2001 From: "Aaron (Qilong)" Date: Mon, 23 Oct 2023 19:25:34 -0400 Subject: [PATCH] Use index writer to amend index after Dynamo Launch --- .../Utilities/LuceneSearchUtility.cs | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/DynamoCore/Utilities/LuceneSearchUtility.cs b/src/DynamoCore/Utilities/LuceneSearchUtility.cs index dbf3274ce19..3f116aa503f 100644 --- a/src/DynamoCore/Utilities/LuceneSearchUtility.cs +++ b/src/DynamoCore/Utilities/LuceneSearchUtility.cs @@ -3,7 +3,6 @@ using System.IO; using System.Linq; using Dynamo.Configuration; -using Dynamo.Core; using Dynamo.Events; using Dynamo.Logging; using Dynamo.Models; @@ -62,6 +61,11 @@ internal class LuceneSearchUtility /// internal LuceneStartConfig startConfig; + /// + /// Index open mode for Lucene index writer + /// + internal OpenMode openMode { get; set; } + /// /// Default start config for Lucene, it will use RAM storage type and empty directory /// @@ -95,6 +99,7 @@ public enum LuceneStorage /// /// Constructor for LuceneSearchUtility, it will use the storage type passed as parameter /// + /// /// internal LuceneSearchUtility(DynamoModel model, LuceneStartConfig config) { @@ -132,12 +137,13 @@ internal void InitializeLuceneConfig() /// /// Create index writer for followup doc indexing /// - internal void CreateLuceneIndexWriter() + /// + internal void CreateLuceneIndexWriter(OpenMode mode = OpenMode.CREATE) { // Create an index writer IndexWriterConfig indexConfig = new IndexWriterConfig(LuceneConfig.LuceneNetVersion, Analyzer) { - OpenMode = OpenMode.CREATE + OpenMode = mode }; try { @@ -418,6 +424,14 @@ internal void CommitWriterChanges() internal void AddNodeTypeToSearchIndex(NodeSearchElement node, Document doc) { if (addedFields == null) return; + // During DynamoModel initialization, the index writer should still be valid here + // If the index writer is null and index not locked, it means the index writer has been disposed, most likely due to DynamoView already launched + // If the index writer is null and index locked, it means user is in a secondary Dynamo session + // Then create a new index writer to amend the index should be safe + if (writer == null && !IndexWriter.IsLocked(this.indexDir)) + { + CreateLuceneIndexWriter(OpenMode.CREATE_OR_APPEND); + } SetDocumentFieldValue(doc, nameof(LuceneConfig.NodeFieldsEnum.FullCategoryName), node.FullCategoryName); SetDocumentFieldValue(doc, nameof(LuceneConfig.NodeFieldsEnum.Name), node.Name);