Skip to content

Commit 14aaa98

Browse files
jmdjmd
jmd
authored and
jmd
committed
avoid commiting needlessly.
Also calling tick from invoke is close to a taxing noop as it is only if it exceeds the upper bound it would perform the commit.
1 parent f82c1f2 commit 14aaa98

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,23 @@ public void Update(JObject entity)
4747
Term term = resolver.Resolver.Identity(entity);
4848
LuceneDocumentEntry doc = mapper.Create(entity);
4949
Writer.UpdateDocument(term, doc.Document);
50+
throttledCommit.Increment();
5051
DebugInfo($"Writer.UpdateDocument({term}, <doc>)");
5152
}
5253

5354
public void Create(JObject entity)
5455
{
5556
LuceneDocumentEntry doc = mapper.Create(entity);
5657
Writer.AddDocument(doc.Document);
58+
throttledCommit.Increment();
5759
DebugInfo($"Writer.AddDocument(<doc>)");
5860
}
5961

6062
public void Delete(JObject entity)
6163
{
6264
Term term = resolver.Resolver.Identity(entity);
6365
Writer.DeleteDocuments(term);
66+
throttledCommit.Increment();
6467
DebugInfo($"Writer.UpdateDocuments({term})");
6568
}
6669

@@ -92,10 +95,11 @@ public class ThrottledCommit
9295
private readonly JsonIndexWriter target;
9396
private readonly WaitHandle handle = new AutoResetEvent(false);
9497
private readonly long upperBound = Stopwatch.Frequency * 10;
95-
private readonly long lowerBound = Stopwatch.Frequency / 10;
98+
private readonly long lowerBound = Stopwatch.Frequency / 5;
9699

97100
private long lastInvocation = 0;
98101
private long lastRequest = 0;
102+
private long writes = 0;
99103

100104
public ThrottledCommit(JsonIndexWriter target)
101105
{
@@ -122,6 +126,9 @@ private void Tick()
122126

123127
private void Commit()
124128
{
129+
if(Interlocked.Exchange(ref writes, 0) < 1)
130+
return;
131+
125132
try
126133
{
127134
target.Writer.Commit();
@@ -136,7 +143,11 @@ private void Commit()
136143
public void Invoke()
137144
{
138145
lastRequest = Stopwatch.GetTimestamp();
139-
Tick();
146+
}
147+
148+
public void Increment()
149+
{
150+
Interlocked.Increment(ref writes);
140151
}
141152
}
142153
}

src/DotJEM.Json.Index2.Snapshots/IndexSnapshotHandler.cs

+3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ private static async Task<bool> UnpackSnapshotAsync(IJsonIndex index, ISnapshot
9393
if(!snapshot.Verify())
9494
return false;
9595

96+
// TODO: Lock index and move close here:
97+
// index.WriterManager.Close();
98+
9699
using ISnapshotReader reader = snapshot.OpenReader();
97100
List<IIndexFile> snapshotFiles = reader.GetIndexFiles().ToList();
98101

0 commit comments

Comments
 (0)