Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/Lucene.Net.Analysis.Common/Analysis/Synonym/SynonymMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ protected Parser(bool dedup, Analyzer analyzer) // LUCENENET: CA1012: Abstract t
/// </summary>
public virtual CharsRef Analyze(string text, CharsRef reuse)
{
Exception priorException = null;
// LUCENENET NOTE: can't use `using` like try-with-resources upstream because TokenStream is ICloseable, not IDisposable
TokenStream ts = analyzer.GetTokenStream("", text);
try
{
Expand Down Expand Up @@ -395,13 +395,9 @@ public virtual CharsRef Analyze(string text, CharsRef reuse)

ts.End();
}
catch (Exception e) when (e.IsIOException())
{
priorException = e;
}
finally
{
IOUtils.CloseWhileHandlingException(priorException, ts);
ts.Close();
}

if (reuse.Length == 0)
Expand Down
115 changes: 68 additions & 47 deletions src/Lucene.Net.Analysis.Kuromoji/Dict/BinaryDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,76 +96,97 @@ private static string LoadDataDir()

protected BinaryDictionary()
{
Stream mapIS = null, dictIS = null, posIS = null;
int[] targetMapOffsets = null, targetMap = null;
string[] posDict = null;
string[] inflFormDict = null;
string[] inflTypeDict = null;
ByteBuffer buffer; // LUCENENET: IDE0059: Remove unnecessary value assignment
bool success = false;

using (Stream mapIS = GetResource(TARGETMAP_FILENAME_SUFFIX))
using (var @in = new InputStreamDataInput(mapIS, leaveOpen: true)) // LUCENENET: CA2000: Use using statement
try
{
CodecUtil.CheckHeader(@in, TARGETMAP_HEADER, VERSION, VERSION);
targetMap = new int[@in.ReadVInt32()];
targetMapOffsets = new int[@in.ReadVInt32()];
int accum = 0, sourceId = 0;
for (int ofs = 0; ofs < targetMap.Length; ofs++)
mapIS = GetResource(TARGETMAP_FILENAME_SUFFIX);
using (var @in = new InputStreamDataInput(mapIS, leaveOpen: true)) // LUCENENET: CA2000: Use using statement
{
int val = @in.ReadVInt32();
if ((val & 0x01) != 0)
CodecUtil.CheckHeader(@in, TARGETMAP_HEADER, VERSION, VERSION);
targetMap = new int[@in.ReadVInt32()];
targetMapOffsets = new int[@in.ReadVInt32()];
int accum = 0, sourceId = 0;
for (int ofs = 0; ofs < targetMap.Length; ofs++)
{
targetMapOffsets[sourceId] = ofs;
sourceId++;
int val = @in.ReadVInt32();
if ((val & 0x01) != 0)
{
targetMapOffsets[sourceId] = ofs;
sourceId++;
}

accum += val >>> 1;
targetMap[ofs] = accum;
}
accum += val >>> 1;
targetMap[ofs] = accum;

if (sourceId + 1 != targetMapOffsets.Length)
throw new IOException("targetMap file format broken");
targetMapOffsets[sourceId] = targetMap.Length;
}
if (sourceId + 1 != targetMapOffsets.Length)
throw new IOException("targetMap file format broken");
targetMapOffsets[sourceId] = targetMap.Length;
}

using (Stream posIS = GetResource(POSDICT_FILENAME_SUFFIX))
using (var @in = new InputStreamDataInput(posIS, leaveOpen: true)) // LUCENENET: CA2000: Use using statement
{
CodecUtil.CheckHeader(@in, POSDICT_HEADER, VERSION, VERSION);
int posSize = @in.ReadVInt32();
posDict = new string[posSize];
inflTypeDict = new string[posSize];
inflFormDict = new string[posSize];
for (int j = 0; j < posSize; j++)
posIS = GetResource(POSDICT_FILENAME_SUFFIX);
using (var @in = new InputStreamDataInput(posIS, leaveOpen: true)) // LUCENENET: CA2000: Use using statement
{
posDict[j] = @in.ReadString();
inflTypeDict[j] = @in.ReadString();
inflFormDict[j] = @in.ReadString();
// this is how we encode null inflections
if (inflTypeDict[j].Length == 0)
CodecUtil.CheckHeader(@in, POSDICT_HEADER, VERSION, VERSION);
int posSize = @in.ReadVInt32();
posDict = new string[posSize];
inflTypeDict = new string[posSize];
inflFormDict = new string[posSize];
for (int j = 0; j < posSize; j++)
{
inflTypeDict[j] = null;
posDict[j] = @in.ReadString();
inflTypeDict[j] = @in.ReadString();
inflFormDict[j] = @in.ReadString();
// this is how we encode null inflections
if (inflTypeDict[j].Length == 0)
{
inflTypeDict[j] = null;
}

if (inflFormDict[j].Length == 0)
{
inflFormDict[j] = null;
}
}
if (inflFormDict[j].Length == 0)
}

ByteBuffer tmpBuffer;

dictIS = GetResource(DICT_FILENAME_SUFFIX);
// no buffering here, as we load in one large buffer
using (var @in = new InputStreamDataInput(dictIS, leaveOpen: true)) // LUCENENET: CA2000: Use using statement
{
CodecUtil.CheckHeader(@in, DICT_HEADER, VERSION, VERSION);
int size = @in.ReadVInt32();
tmpBuffer = ByteBuffer.Allocate(size); // AllocateDirect..?
int read = dictIS.Read(tmpBuffer.Array, 0, size);
if (read != size)
{
inflFormDict[j] = null;
throw EOFException.Create("Cannot read whole dictionary");
}
}
}

ByteBuffer tmpBuffer;

using (Stream dictIS = GetResource(DICT_FILENAME_SUFFIX))
// no buffering here, as we load in one large buffer
using (var @in = new InputStreamDataInput(dictIS, leaveOpen: true)) // LUCENENET: CA2000: Use using statement
buffer = tmpBuffer.AsReadOnlyBuffer();
success = true;
}
finally
{
CodecUtil.CheckHeader(@in, DICT_HEADER, VERSION, VERSION);
int size = @in.ReadVInt32();
tmpBuffer = ByteBuffer.Allocate(size); // AllocateDirect..?
int read = dictIS.Read(tmpBuffer.Array, 0, size);
if (read != size)
if (success)
{
IOUtils.Dispose(mapIS, posIS, dictIS);
}
else
{
throw EOFException.Create("Cannot read whole dictionary");
IOUtils.DisposeWhileHandlingException(mapIS, posIS, dictIS);
}
}
buffer = tmpBuffer.AsReadOnlyBuffer();

this.targetMap = targetMap;
this.targetMapOffsets = targetMapOffsets;
Expand Down
35 changes: 27 additions & 8 deletions src/Lucene.Net.Analysis.Kuromoji/Dict/CharacterDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Lucene.Net.Codecs;
using Lucene.Net.Store;
using Lucene.Net.Util;
using System;
using System.IO;

Expand Down Expand Up @@ -61,15 +62,33 @@ private enum CharacterClass : byte

private CharacterDefinition()
{
using Stream @is = BinaryDictionary.GetTypeResource(GetType(), FILENAME_SUFFIX);
using var @in = new InputStreamDataInput(@is, leaveOpen: true); // LUCENENET: CA2000: Use using statement
CodecUtil.CheckHeader(@in, HEADER, VERSION, VERSION);
@in.ReadBytes(characterCategoryMap, 0, characterCategoryMap.Length);
for (int i = 0; i < CLASS_COUNT; i++)
Stream @is = null;
bool success = false;

try
{
@is = BinaryDictionary.GetTypeResource(GetType(), FILENAME_SUFFIX);
using var @in = new InputStreamDataInput(@is, leaveOpen: true); // LUCENENET: CA2000: Use using statement
CodecUtil.CheckHeader(@in, HEADER, VERSION, VERSION);
@in.ReadBytes(characterCategoryMap, 0, characterCategoryMap.Length);
for (int i = 0; i < CLASS_COUNT; i++)
{
byte b = @in.ReadByte();
invokeMap[i] = (b & 0x01) != 0;
groupMap[i] = (b & 0x02) != 0;
}
success = true;
}
finally
{
byte b = @in.ReadByte();
invokeMap[i] = (b & 0x01) != 0;
groupMap[i] = (b & 0x02) != 0;
if (success)
{
IOUtils.Dispose(@is);
}
else
{
IOUtils.DisposeWhileHandlingException(@is);
}
}
}

Expand Down
45 changes: 32 additions & 13 deletions src/Lucene.Net.Analysis.Kuromoji/Dict/ConnectionCosts.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Lucene.Net.Codecs;
using Lucene.Net.Store;
using Lucene.Net.Support;
using Lucene.Net.Util;
using System;
using System.IO;

Expand Down Expand Up @@ -37,26 +38,44 @@ public sealed class ConnectionCosts

private ConnectionCosts()
{
Stream @is = null;
short[][] costs = null;
bool success = false;

using (Stream @is = BinaryDictionary.GetTypeResource(GetType(), FILENAME_SUFFIX))
using (var @in = new InputStreamDataInput(@is, leaveOpen: true)) // LUCENENET: CA2000: Use using statement
try
{
CodecUtil.CheckHeader(@in, HEADER, VERSION, VERSION);
int forwardSize = @in.ReadVInt32();
int backwardSize = @in.ReadVInt32();
costs = RectangularArrays.ReturnRectangularArray<short>(backwardSize, forwardSize);
int accum = 0;
for (int j = 0; j < costs.Length; j++)
@is = BinaryDictionary.GetTypeResource(GetType(), FILENAME_SUFFIX);
using (var @in = new InputStreamDataInput(@is, leaveOpen: true)) // LUCENENET: CA2000: Use using statement
{
short[] a = costs[j];
for (int i = 0; i < a.Length; i++)
CodecUtil.CheckHeader(@in, HEADER, VERSION, VERSION);
int forwardSize = @in.ReadVInt32();
int backwardSize = @in.ReadVInt32();
costs = RectangularArrays.ReturnRectangularArray<short>(backwardSize, forwardSize);
int accum = 0;
for (int j = 0; j < costs.Length; j++)
{
int raw = @in.ReadVInt32();
accum += (raw >>> 1) ^ -(raw & 1);
a[i] = (short)accum;
short[] a = costs[j];
for (int i = 0; i < a.Length; i++)
{
int raw = @in.ReadVInt32();
accum += (raw >>> 1) ^ -(raw & 1);
a[i] = (short)accum;
}
}
}

success = true;
}
finally
{
if (success)
{
IOUtils.Dispose(@is);
}
else
{
IOUtils.DisposeWhileHandlingException(@is);
}
}

this.costs = costs;
Expand Down
20 changes: 19 additions & 1 deletion src/Lucene.Net.Analysis.Kuromoji/Dict/TokenInfoDictionary.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lucene.Net.Store;
using Lucene.Net.Util;
using Lucene.Net.Util.Fst;
using System;
using System.IO;
Expand Down Expand Up @@ -35,11 +36,28 @@ public sealed class TokenInfoDictionary : BinaryDictionary

private TokenInfoDictionary()
{
Stream @is = null;
FST<Int64> fst = null;
using (Stream @is = GetResource(FST_FILENAME_SUFFIX))
bool success = false;

try
{
@is = GetResource(FST_FILENAME_SUFFIX);
fst = new FST<Int64>(new InputStreamDataInput(@is), PositiveInt32Outputs.Singleton);
success = true;
}
finally
{
if (success)
{
IOUtils.Dispose(@is);
}
else
{
IOUtils.DisposeWhileHandlingException(@is);
}
}

// TODO: some way to configure?
this.fst = new TokenInfoFST(fst, true);
}
Expand Down
16 changes: 10 additions & 6 deletions src/Lucene.Net.Codecs/Memory/FSTOrdTermsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ protected override void Dispose(bool disposing)
{
if (blockOut is null) return;

Exception ioe = null; // LUCENENET: No need to cast to IOException
bool success = false;
try
{
var blockDirStart = blockOut.Position; // LUCENENET specific: Renamed from getFilePointer() to match FileStream
Expand Down Expand Up @@ -227,14 +227,18 @@ protected override void Dispose(bool disposing)
WriteTrailer(blockOut, blockDirStart);
CodecUtil.WriteFooter(indexOut);
CodecUtil.WriteFooter(blockOut);
}
catch (Exception ioe2) when (ioe2.IsIOException())
{
ioe = ioe2;
success = true;
}
finally
{
IOUtils.DisposeWhileHandlingException(ioe, blockOut, indexOut, postingsWriter);
if (success)
{
IOUtils.Dispose(blockOut, indexOut, postingsWriter);
}
else
{
IOUtils.DisposeWhileHandlingException(blockOut, indexOut, postingsWriter);
}
blockOut = null;
}
}
Expand Down
16 changes: 10 additions & 6 deletions src/Lucene.Net.Codecs/Memory/FSTTermsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected override void Dispose(bool disposing)
{
if (_output is null) return;

Exception ioe = null; // LUCENENET: No need to cast to IOException
bool success = false;
try
{
// write field summary
Expand All @@ -195,14 +195,18 @@ protected override void Dispose(bool disposing)
}
WriteTrailer(_output, dirStart);
CodecUtil.WriteFooter(_output);
}
catch (Exception ioe2) when (ioe2.IsIOException())
{
ioe = ioe2;
success = true;
}
finally
{
IOUtils.DisposeWhileHandlingException(ioe, _output, _postingsWriter);
if (success)
{
IOUtils.Dispose(_output, _postingsWriter);
}
else
{
IOUtils.DisposeWhileHandlingException(_output, _postingsWriter);
}
_output = null;
}
}
Expand Down
Loading
Loading