Skip to content

Commit e7126dc

Browse files
committed
Dispose IndexSearcher's underlying IndexReader when reloading/disposing context.
1 parent 722dfdb commit e7126dc

File tree

7 files changed

+71
-6
lines changed

7 files changed

+71
-6
lines changed

Ciao.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" DefaultTargets="Bootstrap" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup Label="Version">
4-
<VersionPrefix>3.3.2</VersionPrefix>
4+
<VersionPrefix>3.3.3</VersionPrefix>
55
<VersionSuffix></VersionSuffix>
66
</PropertyGroup>
77

source/Lucene.Net.Linq.Tests/ContextTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,26 @@ public void ReloadDisposesSearcher()
123123
searcher.AssertWasCalled(s => s.Dispose());
124124
}
125125

126+
[Test]
127+
public void DisposeDisposesSearcher()
128+
{
129+
var searcher = context.CurrentTracker.Searcher;
130+
131+
context.Dispose();
132+
133+
searcher.AssertWasCalled(s => s.Dispose());
134+
}
135+
136+
[Test]
137+
public void DisposeDisposesSearcherReader()
138+
{
139+
context.CurrentTracker.Searcher.Dispose();
140+
141+
context.Dispose();
142+
143+
context.FakeReader.AssertWasCalled(r => r.Dispose());
144+
}
145+
126146
[Test]
127147
public void ReloadFiresLoadingEvent()
128148
{
@@ -269,4 +289,4 @@ public bool IsClosed
269289
}
270290
}
271291
}
272-
}
292+
}

source/Lucene.Net.Linq.Tests/Integration/AllowSpecialCharactersTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@ public class PathDocument
8484
public string Name { get; set; }
8585
}
8686
}
87-
}
87+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using Lucene.Net.Store;
5+
using NUnit.Framework;
6+
using Version = Lucene.Net.Util.Version;
7+
8+
namespace Lucene.Net.Linq.Tests.Integration
9+
{
10+
[TestFixture]
11+
public class ReleaseTests
12+
{
13+
public class Document
14+
{
15+
public int Id { get; set; }
16+
}
17+
18+
[Test]
19+
public void CommitInSessionAndDisposeProviderReleasesFiles()
20+
{
21+
var dir = "index." + DateTime.Now.Ticks;
22+
System.IO.Directory.CreateDirectory(dir);
23+
var provider = new LuceneDataProvider(FSDirectory.Open(dir), Version.LUCENE_30);
24+
using (provider)
25+
{
26+
using (var session = provider.OpenSession<Document>())
27+
{
28+
session.Add(new Document { Id = 1 });
29+
30+
session.Commit();
31+
32+
session.Add(new Document { Id = 2 });
33+
}
34+
}
35+
36+
TestDelegate call = () => System.IO.Directory.Delete(dir, true);
37+
Assert.That(call, Throws.Nothing);
38+
}
39+
}
40+
}

source/Lucene.Net.Linq.Tests/Lucene.Net.Linq.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
<Compile Include="Integration\OrderByTests.cs" />
122122
<Compile Include="Integration\PorterStemAnalyzer.cs" />
123123
<Compile Include="Integration\RangeTests.cs" />
124+
<Compile Include="Integration\ReleaseTests.cs" />
124125
<Compile Include="Integration\StatisticTests.cs" />
125126
<Compile Include="LuceneQueryExecutorTests.cs" />
126127
<Compile Include="Mapping\FieldMappingInfoBuilderSortFieldTests.cs" />

source/Lucene.Net.Linq/Context.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public virtual void Reload()
8080
searcher = CreateSearcher();
8181
reader = searcher.IndexReader;
8282
}
83-
else if (!ReopenSearcher(out searcher))
83+
else if (!ReopenSearcher(out searcher))
8484
{
8585
return;
8686
}
@@ -252,7 +252,11 @@ public bool TryDispose()
252252
undisposedTrackers.Remove(this);
253253
}
254254

255+
var reader = searcher.IndexReader;
255256
searcher.Dispose();
257+
// NB IndexSearcher.Dispose() does not Dispose externally provided IndexReader:
258+
reader.Dispose();
259+
256260
disposed = true;
257261
}
258262
else
@@ -284,4 +288,4 @@ internal interface ISearcherHandle : IDisposable
284288
IndexSearcher Searcher { get; }
285289
}
286290

287-
}
291+
}

source/Lucene.Net.Linq/LuceneSession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,4 +351,4 @@ public void Clear()
351351
}
352352
}
353353
}
354-
}
354+
}

0 commit comments

Comments
 (0)