Skip to content

Commit b7fb2ee

Browse files
jmdjmd
jmd
authored and
jmd
committed
fix writer disposed exception
1 parent 0d4b952 commit b7fb2ee

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

src/DotJEM.Json.Index2.Management/Writer/IJsonIndexWriter.cs

+51-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
using System.Runtime.CompilerServices;
1+
using System;
2+
using System.Diagnostics;
3+
using System.Runtime.CompilerServices;
4+
using System.Threading;
5+
using System.Threading.Tasks;
26
using DotJEM.Json.Index2.Documents;
37
using DotJEM.Json.Index2.Documents.Info;
48
using DotJEM.ObservableExtensions.InfoStreams;
59
using Lucene.Net.Index;
610
using Newtonsoft.Json.Linq;
11+
using static DotJEM.Json.Index2.Management.Writer.JsonIndexWriter;
712

813
namespace DotJEM.Json.Index2.Management.Writer;
914

@@ -24,16 +29,17 @@ public class JsonIndexWriter : IJsonIndexWriter
2429
private readonly ILuceneDocumentFactory mapper;
2530
private readonly IFieldInformationManager resolver;
2631
private readonly IInfoStream<JsonIndexManager> infoStream = new InfoStream<JsonIndexManager>();
27-
32+
private readonly ThrottledCommit throttledCommit;
33+
2834
private IndexWriter Writer => index.WriterManager.Writer;
29-
3035
public IInfoStream InfoStream => infoStream;
3136

3237
public JsonIndexWriter(IJsonIndex index)
3338
{
3439
this.index = index;
3540
this.mapper = index.Configuration.DocumentFactory;
3641
this.resolver = index.Configuration.FieldInformationManager;
42+
throttledCommit = new ThrottledCommit(this);
3743
}
3844

3945
public void Update(JObject entity)
@@ -61,7 +67,8 @@ public void Delete(JObject entity)
6167

6268
public void Commit()
6369
{
64-
Writer.Commit();
70+
throttledCommit.Invoke();
71+
//Writer.Commit();
6572
DebugInfo($"Writer.Commit()");
6673
}
6774

@@ -79,4 +86,44 @@ public void MaybeMerge()
7986

8087
private void DebugInfo(string message, [CallerMemberName] string caller = null)
8188
=> infoStream.WriteDebug(message, caller);
89+
90+
public class ThrottledCommit
91+
{
92+
private readonly JsonIndexWriter target;
93+
private readonly WaitHandle handle = new AutoResetEvent(false);
94+
private readonly long upperBound = Stopwatch.Frequency * 10;
95+
private readonly long lowerBound = Stopwatch.Frequency / 10;
96+
97+
private long lastInvocation = 0;
98+
private long lastRequest = 0;
99+
100+
public ThrottledCommit(JsonIndexWriter target)
101+
{
102+
this.target = target;
103+
ThreadPool.RegisterWaitForSingleObject(handle, (_,_)=>Tick(), null, 200, false);
104+
}
105+
106+
private void Tick()
107+
{
108+
long time = Stopwatch.GetTimestamp();
109+
if (time - lastInvocation > upperBound)
110+
{
111+
target.Writer.Commit();
112+
lastInvocation = time;
113+
return;
114+
}
115+
116+
if (time - lastRequest > lowerBound)
117+
{
118+
target.Writer.Commit();
119+
lastInvocation = time;
120+
}
121+
}
122+
123+
public void Invoke()
124+
{
125+
lastRequest = Stopwatch.GetTimestamp();
126+
Tick();
127+
}
128+
}
82129
}

src/DotJEM.Json.Index2/IO/JsonIndexWriterManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void Close()
5151
protected override void Dispose(bool disposing)
5252
{
5353
if (disposing)
54-
Writer.Dispose();
54+
Close();
5555
base.Dispose(disposing);
5656
}
5757

0 commit comments

Comments
 (0)