Skip to content

Commit

Permalink
Fix file handle leaks in demo code, apache#615
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Dec 22, 2024
1 parent f74f39c commit 6121f1c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 47 deletions.
71 changes: 39 additions & 32 deletions src/Lucene.Net.Demo/SearchFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,57 +112,64 @@ public static void Main(string[] args)
// :Post-Release-Update-Version.LUCENE_XY:
Analyzer analyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48);

FileStream fileStream = null; // LUCENENET specific - keep track of the FileStream so we can dispose of it
TextReader input = null;
if (queries != null)
{
input = new StreamReader(new FileStream(queries, FileMode.Open, FileAccess.Read), Encoding.UTF8);
fileStream = new FileStream(queries, FileMode.Open, FileAccess.Read);
input = new StreamReader(fileStream, Encoding.UTF8);
}
else
{
input = Console.In;
}
// :Post-Release-Update-Version.LUCENE_XY:
QueryParser parser = new QueryParser(LuceneVersion.LUCENE_48, field, analyzer);
while (true)

using (fileStream) // LUCENENET specific - dispose of the FileStream when we are done with it
{
if (queries is null && queryString is null)
// :Post-Release-Update-Version.LUCENE_XY:
QueryParser parser = new QueryParser(LuceneVersion.LUCENE_48, field, analyzer);
while (true)
{
// prompt the user
Console.WriteLine("Enter query (or press Enter to exit): ");
}
if (queries is null && queryString is null)
{
// prompt the user
Console.WriteLine("Enter query (or press Enter to exit): ");
}

string line = queryString ?? input.ReadLine();
string line = queryString ?? input.ReadLine();

if (line is null || line.Length == 0)
{
break;
}
if (line is null || line.Length == 0)
{
break;
}

line = line.Trim();
if (line.Length == 0)
{
break;
}
line = line.Trim();
if (line.Length == 0)
{
break;
}

Query query = parser.Parse(line);
Console.WriteLine("Searching for: " + query.ToString(field));
Query query = parser.Parse(line);
Console.WriteLine("Searching for: " + query.ToString(field));

if (repeat > 0) // repeat & time as benchmark
{
DateTime start = DateTime.UtcNow;
for (int i = 0; i < repeat; i++)
if (repeat > 0) // repeat & time as benchmark
{
searcher.Search(query, null, 100);
DateTime start = DateTime.UtcNow;
for (int i = 0; i < repeat; i++)
{
searcher.Search(query, null, 100);
}

DateTime end = DateTime.UtcNow;
Console.WriteLine("Time: " + (end - start).TotalMilliseconds + "ms");
}
DateTime end = DateTime.UtcNow;
Console.WriteLine("Time: " + (end - start).TotalMilliseconds + "ms");
}

DoPagingSearch(searcher, query, hitsPerPage, raw, queries is null && queryString is null);
DoPagingSearch(searcher, query, hitsPerPage, raw, queries is null && queryString is null);

if (queryString != null)
{
break;
if (queryString != null)
{
break;
}
}
}
}
Expand Down
31 changes: 16 additions & 15 deletions src/Lucene.Net.Tests.Suggest/Suggest/Fst/LargeInputFST.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,31 @@ public static class LargeInputFST // LUCENENET specific: CA1052 Static holder ty
// LUCENENET specific - renaming from Main() because we must only have 1 entry point.
// Not sure why this utility is in a test project anyway - this seems like something that should
// be in Lucene.Net.Suggest so we can put it into the lucene-cli tool.
public static void Main2(string[] args)
public static void Main2(string[] args)
{
FileInfo input = new FileInfo("/home/dweiss/tmp/shuffled.dict");

int buckets = 20;
int shareMaxTail = 10;
const int buckets = 20;
const int shareMaxTail = 10;

ExternalRefSorter sorter = new ExternalRefSorter(new OfflineSorter());
FSTCompletionBuilder builder = new FSTCompletionBuilder(buckets, sorter, shareMaxTail);

TextReader reader =
new StreamReader(
new FileStream(input.FullName, FileMode.Open), Encoding.UTF8);

BytesRef scratch = new BytesRef();
string line;
int count = 0;
while ((line = reader.ReadLine()) != null)
// LUCENENET specific - dispose of fileStream and reader when done
using (FileStream fileStream = new FileStream(input.FullName, FileMode.Open))
using (TextReader reader = new StreamReader(fileStream, Encoding.UTF8))
{
scratch.CopyChars(line);
builder.Add(scratch, count % buckets);
if ((count++ % 100000) == 0)
BytesRef scratch = new BytesRef();
string line;
int count = 0;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine("Line: " + count);
scratch.CopyChars(line);
builder.Add(scratch, count % buckets);
if ((count++ % 100000) == 0)
{
Console.WriteLine("Line: " + count);
}
}
}

Expand Down

0 comments on commit 6121f1c

Please sign in to comment.