Skip to content

Commit

Permalink
Use index writer to amend index after Dynamo Launch
Browse files Browse the repository at this point in the history
  • Loading branch information
QilongTang committed Oct 23, 2023
1 parent bdc6ba4 commit b103148
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/DynamoCore/Utilities/LuceneSearchUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,6 +61,11 @@ internal class LuceneSearchUtility
/// </summary>
internal LuceneStartConfig startConfig;

/// <summary>
/// Index open mode for Lucene index writer
/// </summary>
internal OpenMode openMode { get; set; }

/// <summary>
/// Default start config for Lucene, it will use RAM storage type and empty directory
/// </summary>
Expand Down Expand Up @@ -95,6 +99,7 @@ public enum LuceneStorage
/// <summary>
/// Constructor for LuceneSearchUtility, it will use the storage type passed as parameter
/// </summary>
/// <param name="model"></param>
/// <param name="config"></param>
internal LuceneSearchUtility(DynamoModel model, LuceneStartConfig config)
{
Expand Down Expand Up @@ -132,12 +137,13 @@ internal void InitializeLuceneConfig()
/// <summary>
/// Create index writer for followup doc indexing
/// </summary>
internal void CreateLuceneIndexWriter()
/// <param name="mode"></param>
internal void CreateLuceneIndexWriter(OpenMode mode = OpenMode.CREATE)
{
// Create an index writer
IndexWriterConfig indexConfig = new IndexWriterConfig(LuceneConfig.LuceneNetVersion, Analyzer)
{
OpenMode = OpenMode.CREATE
OpenMode = mode
};
try
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b103148

Please sign in to comment.