Skip to content

Commit

Permalink
Remove SetLength from ICharTermAttribute, apache#1038
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Nov 26, 2024
1 parent 6d62a57 commit 1ed1c01
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Lucene.Net.Collation
/// <remarks>
/// <strong>WARNING:</strong> Make sure you use exactly the same <see cref="Collator"/> at
/// index and query time -- CollationKeys are only comparable when produced by
/// the same <see cref="Collator"/>. <see cref="RuleBasedCollator"/>s are
/// the same <see cref="Collator"/>. <see cref="RuleBasedCollator"/>s are
/// independently versioned, so it is safe to search against stored
/// <see cref="CollationKey"/>s if the following are exactly the same (best practice is
/// to store this information with the index and check that they remain the
Expand All @@ -44,8 +44,8 @@ namespace Lucene.Net.Collation
/// </list>
/// <para/>
/// <see cref="CollationKey"/>s generated by ICU Collators are not compatible with those
/// generated by java.text.Collators. Specifically, if you use
/// <see cref="ICUCollationKeyAnalyzer"/> to generate index terms, do not use
/// generated by java.text.Collators. Specifically, if you use
/// <see cref="ICUCollationKeyAnalyzer"/> to generate index terms, do not use
/// CollationKeyAnalyzer on the query side, or vice versa.
/// <para/>
/// ICUCollationKeyAnalyzer is significantly faster and generates significantly
Expand Down Expand Up @@ -89,7 +89,7 @@ public override bool IncrementToken()
{
termAtt.ResizeBuffer(encodedLength);
}
termAtt.SetLength(encodedLength);
termAtt.Length = encodedLength;
IndexableBinaryStringTools.Encode(reusableKey.Bytes, 0, reusableKey.Length,
termAtt.Buffer, 0, encodedLength);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public override bool IncrementToken()
{
if (!keywordAttr.IsKeyword)
{
termAttr.SetLength(Stem(termAttr.Buffer, termAttr.Length));
termAttr.Length = Stem(termAttr.Buffer, termAttr.Length);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@ public sealed override bool IncrementToken()
{
if (i < terms.Length)
{
termAtt.SetLength(0).Append(terms[i]);
termAtt.Length = 0;
termAtt.Append(terms[i]);
piAtt.PositionIncrement = positionsIncrements[i];
oAtt.SetOffset(startOffsets[i], endOffsets[i]);
pAtt.Payload = payloads[i];
Expand Down Expand Up @@ -913,4 +914,4 @@ public override void Run()
}
}
}
}
}
6 changes: 3 additions & 3 deletions src/Lucene.Net.Tests.Memory/Index/Memory/MemoryIndexTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public override bool IncrementToken()
{
if (termAtt.Length > 0 && termAtt.Buffer[0] == 't')
{
termAtt.SetLength(0);
termAtt.Length = 0;
}
return true;
}
Expand All @@ -318,8 +318,8 @@ public override bool IncrementToken()
};

/**
* Some terms to be indexed, in addition to random words.
* These terms are commonly used in the queries.
* Some terms to be indexed, in addition to random words.
* These terms are commonly used in the queries.
*/
private static readonly string[] TEST_TERMS = {"term", "Term", "tErm", "TERM",
"telm", "stop", "drop", "roll", "phrase", "a", "c", "bar", "blar",
Expand Down
6 changes: 3 additions & 3 deletions src/Lucene.Net/Analysis/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ namespace Lucene.Net.Analysis
/// Failing that, to create a new <see cref="Token"/> you should first use
/// one of the constructors that starts with null text. To load
/// the token from a char[] use <see cref="ICharTermAttribute.CopyBuffer(char[], int, int)"/>.
/// To load from a <see cref="string"/> use <see cref="ICharTermAttribute.SetEmpty()"/> followed by
/// To load from a <see cref="string"/> use <see cref="ICharTermAttribute.SetEmpty()"/> followed by
/// <see cref="ICharTermAttribute.Append(string)"/> or <see cref="ICharTermAttribute.Append(string, int, int)"/>.
/// Alternatively you can get the <see cref="Token"/>'s termBuffer by calling either <see cref="ICharTermAttribute.Buffer"/>,
/// if you know that your text is shorter than the capacity of the termBuffer
/// or <see cref="ICharTermAttribute.ResizeBuffer(int)"/>, if there is any possibility
/// that you may need to grow the buffer. Fill in the characters of your term into this
/// buffer, with <see cref="string.ToCharArray(int, int)"/> if loading from a string,
/// or with <see cref="System.Array.Copy(System.Array, int, System.Array, int, int)"/>,
/// and finally call <see cref="ICharTermAttribute.SetLength(int)"/> to
/// or with <see cref="System.Array.Copy(System.Array, int, System.Array, int, int)"/>,
/// and finally assign to <see cref="ICharTermAttribute.Length"/> to
/// set the length of the term text. See <a target="_top"
/// href="https://issues.apache.org/jira/browse/LUCENE-969">LUCENE-969</a>
/// for details.</para>
Expand Down
31 changes: 11 additions & 20 deletions src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ public interface ICharTermAttribute : IAttribute, ICharSequence, IAppendable
/// <param name="length"> the number of characters to copy </param>
void CopyBuffer(char[] buffer, int offset, int length);


/// <summary>
/// Returns the internal termBuffer character array which
/// you can then directly alter. If the array is too
/// small for your token, use <see cref="ResizeBuffer(int)"/>
/// to increase it. After
/// altering the buffer be sure to call <see cref="SetLength(int)"/>
/// altering the buffer be sure to set <see cref="Length"/>
/// to record the number of valid
/// characters that were placed into the termBuffer.
/// <para>
Expand All @@ -62,29 +61,21 @@ public interface ICharTermAttribute : IAttribute, ICharSequence, IAppendable
char[] ResizeBuffer(int newSize);

/// <summary>
/// Gets or Sets the number of valid characters (in
/// Gets or sets the number of valid characters (length of the term) in
/// the termBuffer array.
/// <seealso cref="SetLength(int)"/>
/// </summary>
new int Length { get; set; } // LUCENENET: To mimic StringBuilder, we allow this to be settable.

// LUCENENET specific: Redefining this[] to make it settable
new char this[int index] { get; set; }

/// <summary>
/// Set number of valid characters (length of the term) in
/// the termBuffer array. Use this to truncate the termBuffer
/// Use this setter to truncate the termBuffer
/// or to synchronize with external manipulation of the termBuffer.
/// Note: to grow the size of the array,
/// use <see cref="ResizeBuffer(int)"/> first.
/// NOTE: This is exactly the same operation as calling the <see cref="Length"/> setter, the primary
/// difference is that this method returns a reference to the current object so it can be chained.
/// <code>
/// obj.SetLength(30).Append("hey you");
/// </code>
/// </summary>
/// <param name="length"> the truncated length </param>
ICharTermAttribute SetLength(int length);
/// <remarks>
/// LUCENENET: To mimic StringBuilder, we allow this to be settable.
/// This replaces the chainable SetLength method in the Java version.
/// </remarks>
new int Length { get; set; }

// LUCENENET specific: Redefining this[] to make it settable
new char this[int index] { get; set; }

/// <summary>
/// Sets the length of the termBuffer to zero.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,6 @@ public override void CopyTo(IAttribute target) // LUCENENET specific - intention

char[] ICharTermAttribute.ResizeBuffer(int newSize) => ResizeBuffer(newSize);

ICharTermAttribute ICharTermAttribute.SetLength(int length) => SetLength(length);

ICharTermAttribute ICharTermAttribute.SetEmpty() => SetEmpty();

ICharTermAttribute ICharTermAttribute.Append(ICharSequence value) => Append(value);
Expand Down

0 comments on commit 1ed1c01

Please sign in to comment.