6
6
using System . Threading ;
7
7
using DotJEM . Json . Index2 . Documents ;
8
8
using DotJEM . Json . Index2 . Documents . Info ;
9
+ using DotJEM . Json . Index2 . IO ;
9
10
using DotJEM . ObservableExtensions . InfoStreams ;
10
11
using Lucene . Net . Index ;
11
12
using Newtonsoft . Json . Linq ;
13
+ using static Lucene . Net . Documents . Field ;
12
14
13
15
namespace DotJEM . Json . Index2 . Management . Writer ;
14
16
@@ -19,8 +21,8 @@ public interface IJsonIndexWriter
19
21
void Create ( IEnumerable < JObject > entities ) ;
20
22
void Update ( JObject entity ) ;
21
23
void Delete ( JObject entity ) ;
22
- void Commit ( ) ;
23
- void Flush ( bool triggerMerge , bool applyAllDeletes ) ;
24
+ void Commit ( bool force = false ) ;
25
+ void Flush ( bool triggerMerge = false , bool applyAllDeletes = false ) ;
24
26
void MaybeMerge ( ) ;
25
27
}
26
28
@@ -32,7 +34,7 @@ public class JsonIndexWriter : IJsonIndexWriter
32
34
private readonly IInfoStream < JsonIndexManager > infoStream = new InfoStream < JsonIndexManager > ( ) ;
33
35
private readonly ThrottledCommit throttledCommit ;
34
36
35
- private IndexWriter Writer => index . WriterManager . Writer ;
37
+ private ILease < IndexWriter > WriterLease => index . WriterManager . Lease ( ) ;
36
38
public IInfoStream InfoStream => infoStream ;
37
39
38
40
public JsonIndexWriter ( IJsonIndex index )
@@ -46,52 +48,60 @@ public JsonIndexWriter(IJsonIndex index)
46
48
47
49
public void Update ( JObject entity )
48
50
{
51
+ using ILease < IndexWriter > lease = WriterLease ;
52
+
49
53
Term term = resolver . Resolver . Identity ( entity ) ;
50
54
LuceneDocumentEntry doc = mapper . Create ( entity ) ;
51
- Writer . UpdateDocument ( term , doc . Document ) ;
55
+ lease . Value . UpdateDocument ( term , doc . Document ) ;
52
56
throttledCommit . Increment ( ) ;
53
57
DebugInfo ( $ "Writer.UpdateDocument({ term } , <doc>)") ;
54
58
}
55
59
56
60
public void Create ( JObject entity )
57
61
{
62
+ using ILease < IndexWriter > lease = WriterLease ;
63
+
58
64
LuceneDocumentEntry doc = mapper . Create ( entity ) ;
59
- Writer . AddDocument ( doc . Document ) ;
65
+ lease . Value . AddDocument ( doc . Document ) ;
60
66
throttledCommit . Increment ( ) ;
61
67
DebugInfo ( $ "Writer.AddDocument(<doc>)") ;
62
68
}
63
69
64
70
public void Create ( IEnumerable < JObject > entities )
65
71
{
66
- Writer . AddDocuments ( entities . Select ( entity => mapper . Create ( entity ) . Document ) ) ;
72
+ using ILease < IndexWriter > lease = WriterLease ;
73
+ lease . Value . AddDocuments ( entities . Select ( entity => mapper . Create ( entity ) . Document ) ) ;
67
74
throttledCommit . Increment ( ) ;
68
75
DebugInfo ( $ "Writer.AddDocuments(<doc>)") ;
69
76
}
70
77
71
78
public void Delete ( JObject entity )
72
79
{
80
+ using ILease < IndexWriter > lease = WriterLease ;
73
81
Term term = resolver . Resolver . Identity ( entity ) ;
74
- Writer . DeleteDocuments ( term ) ;
82
+ lease . Value . DeleteDocuments ( term ) ;
75
83
throttledCommit . Increment ( ) ;
76
84
DebugInfo ( $ "Writer.UpdateDocuments({ term } )") ;
77
85
}
78
86
79
87
80
- public void Commit ( )
88
+ public void Commit ( bool force = false )
81
89
{
82
- throttledCommit . Invoke ( ) ;
90
+ throttledCommit . Invoke ( force ) ;
83
91
DebugInfo ( $ "Writer.Commit()") ;
84
92
}
85
93
86
- public void Flush ( bool triggerMerge , bool applyAllDeletes )
94
+ public void Flush ( bool triggerMerge = false , bool applyAllDeletes = false )
87
95
{
88
- Writer . Flush ( triggerMerge , applyAllDeletes ) ;
96
+ using ILease < IndexWriter > lease = WriterLease ;
97
+ lease . Value . Flush ( triggerMerge , applyAllDeletes ) ;
89
98
DebugInfo ( $ "Writer.Flush({ triggerMerge } , { applyAllDeletes } )") ;
90
99
}
91
100
92
101
public void MaybeMerge ( )
93
102
{
94
- Writer . MaybeMerge ( ) ;
103
+ using ILease < IndexWriter > lease = WriterLease ;
104
+ lease . Value . MaybeMerge ( ) ;
95
105
DebugInfo ( $ "Writer.MaybeMerge()") ;
96
106
}
97
107
@@ -113,20 +123,17 @@ public class ThrottledCommit
113
123
public ThrottledCommit ( JsonIndexWriter target )
114
124
{
115
125
this . target = target ;
116
- ThreadPool . RegisterWaitForSingleObject ( handle , ( _ , _ ) => Tick ( ) , null , 200 , false ) ;
126
+ ThreadPool . RegisterWaitForSingleObject ( handle , ( _ , _ ) => Tick ( false ) , null , 200 , false ) ;
117
127
}
118
128
119
- private void Tick ( )
129
+ private void Tick ( bool force )
120
130
{
121
131
long time = Stopwatch . GetTimestamp ( ) ;
122
- if ( time - lastInvocation > upperBound )
123
- {
124
- Commit ( ) ;
125
- lastInvocation = time ;
126
- return ;
127
- }
128
-
129
- if ( time - lastRequest > lowerBound )
132
+ // ReSharper disable once InvertIf
133
+ if ( force
134
+ || time - lastInvocation > upperBound
135
+ || time - lastRequest > lowerBound
136
+ )
130
137
{
131
138
Commit ( ) ;
132
139
lastInvocation = time ;
@@ -135,15 +142,18 @@ private void Tick()
135
142
136
143
private void Commit ( )
137
144
{
138
- if ( Interlocked . Exchange ( ref writes , 0 ) < 1 )
145
+ long writesRead = Interlocked . Exchange ( ref writes , 0 ) ;
146
+ if ( writesRead < 1 )
139
147
return ;
140
148
141
- if ( Interlocked . Exchange ( ref calls , 0 ) < 1 )
149
+ long callsRead = Interlocked . Exchange ( ref calls , 0 ) ;
150
+ if ( callsRead < 1 )
142
151
return ;
143
152
144
153
try
145
154
{
146
- target . Writer . Commit ( ) ;
155
+ using ILease < IndexWriter > lease = target . WriterLease ;
156
+ lease . Value . Commit ( ) ;
147
157
}
148
158
catch ( Exception e )
149
159
{
@@ -152,10 +162,14 @@ private void Commit()
152
162
}
153
163
}
154
164
155
- public void Invoke ( )
165
+ public void Invoke ( bool force )
156
166
{
157
167
Interlocked . Increment ( ref calls ) ;
158
168
lastRequest = Stopwatch . GetTimestamp ( ) ;
169
+ if ( force )
170
+ {
171
+ Tick ( force ) ;
172
+ }
159
173
}
160
174
161
175
public void Increment ( )
0 commit comments