You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2016/10/23 13:02:11 UTC
[25/50] [abbrv] lucenenet git commit: Ported the tests for ByteBuffer
from Java and refactored ByteBuffer. Also ported LongBuffer + tests (required
by Core.Util.TestPackedInts.TestEncodeDecode()).
Ported the tests for ByteBuffer from Java and refactored ByteBuffer. Also ported LongBuffer + tests (required by Core.Util.TestPackedInts.TestEncodeDecode()).
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/d235115d
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/d235115d
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/d235115d
Branch: refs/heads/master
Commit: d235115dd661bfdf843a8fef8a94b39e16600c98
Parents: 2fc1ad2
Author: Shad Storhaug <sh...@shadstorhaug.com>
Authored: Wed Oct 19 05:33:36 2016 +0700
Committer: Shad Storhaug <sh...@shadstorhaug.com>
Committed: Thu Oct 20 18:20:58 2016 +0700
----------------------------------------------------------------------
src/Lucene.Net.Core/Lucene.Net.csproj | 4 +-
src/Lucene.Net.Core/Support/Buffer.cs | 414 ++++-
src/Lucene.Net.Core/Support/BufferExceptions.cs | 24 +
.../Support/BufferUnderflowException.cs | 8 -
src/Lucene.Net.Core/Support/ByteBuffer.cs | 1532 +++++++++++++++---
src/Lucene.Net.Core/Support/ByteOrder.cs | 17 +
src/Lucene.Net.Core/Support/LongBuffer.cs | 422 +++++
.../Support/MemoryMappedFileByteBuffer.cs | 17 +-
src/Lucene.Net.Tests/Lucene.Net.Tests.csproj | 3 +
.../core/Support/BaseBufferTestCase.cs | 130 ++
.../core/Support/TestByteBuffer.cs | 739 +++++++++
.../core/Support/TestLongBuffer.cs | 522 ++++++
.../core/Util/Packed/TestPackedInts.cs | 62 +-
13 files changed, 3572 insertions(+), 322 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/d235115d/src/Lucene.Net.Core/Lucene.Net.csproj
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Lucene.Net.csproj b/src/Lucene.Net.Core/Lucene.Net.csproj
index 99729f8..75817c5 100644
--- a/src/Lucene.Net.Core/Lucene.Net.csproj
+++ b/src/Lucene.Net.Core/Lucene.Net.csproj
@@ -612,10 +612,11 @@
<Compile Include="Support\Buffer.cs" />
<Compile Include="Support\AttributeImplItem.cs" />
<Compile Include="Support\BitSetSupport.cs" />
- <Compile Include="Support\BufferUnderflowException.cs" />
+ <Compile Include="Support\BufferExceptions.cs" />
<Compile Include="Support\BuildType.cs" />
<Compile Include="Support\ByteArrayOutputStream.cs" />
<Compile Include="Support\ByteBuffer.cs" />
+ <Compile Include="Support\ByteOrder.cs" />
<Compile Include="Support\Character.cs" />
<Compile Include="Support\Arrays.cs" />
<Compile Include="Support\CharacterIterator.cs" />
@@ -638,6 +639,7 @@
<Compile Include="Support\IDictionaryExtensions.cs" />
<Compile Include="Support\LimitedConcurrencyLevelTaskScheduler.cs" />
<Compile Include="Support\ListExtensions.cs" />
+ <Compile Include="Support\LongBuffer.cs" />
<Compile Include="Support\LurchTable.cs" />
<Compile Include="Support\MathExtension.cs" />
<Compile Include="Support\MemoryMappedFileByteBuffer.cs" />
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/d235115d/src/Lucene.Net.Core/Support/Buffer.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Support/Buffer.cs b/src/Lucene.Net.Core/Support/Buffer.cs
index 26fbed7..f40bcef 100644
--- a/src/Lucene.Net.Core/Support/Buffer.cs
+++ b/src/Lucene.Net.Core/Support/Buffer.cs
@@ -1,117 +1,397 @@
-\ufeffnamespace Lucene.Net.Support
+\ufeffusing System;
+
+namespace Lucene.Net.Support
{
+ /// <summary>
+ /// Base class for <see cref="ByteBuffer"/> and <see cref="LongBuffer"/> (ported from Java)
+ /// </summary>
public abstract class Buffer
{
- private int _mark = -1;
- private int _position;
- private int _capacity;
- private int _limit;
+ private int mark = -1;
+ private int position;
+ private int capacity;
+ private int limit;
+
+ // Used only by direct buffers
+ // NOTE: hoisted here for speed in JNI GetDirectBufferAddress
+ internal long address;
- public Buffer(int mark, int pos, int lim, int cap)
+ /// <summary>
+ /// Creates a new buffer with the given mark, position, limit, and capacity,
+ /// after checking invariants.
+ /// </summary>
+ internal Buffer(int mark, int pos, int lim, int cap)
{
- this._mark = mark;
- this._position = pos;
- this._limit = lim;
- this._capacity = cap;
+ if (cap < 0)
+ throw new ArgumentException("Negative capacity: " + cap);
+ this.capacity = cap;
+ SetLimit(lim);
+ SetPosition(pos);
+ if (mark >= 0)
+ {
+ if (mark > pos)
+ throw new ArgumentException("mark > position: ("
+ + mark + " > " + pos + ")");
+ this.mark = mark;
+ }
}
- public abstract object Array { get; }
+ /// <summary>
+ /// Returns this buffer's capacity.
+ /// </summary>
+ public int Capacity
+ {
+ get { return capacity; }
+ }
- public abstract int ArrayOffset { get; }
+ /// <summary>
+ /// Returns this buffer's position.
+ /// </summary>
+ public int Position
+ {
+ get { return position; }
+ set { SetPosition(value); }
+ }
- public virtual int Capacity
+ /// <summary>
+ /// Sets this buffer's position. If the mark is defined and larger than the
+ /// new position then it is discarded.
+ /// </summary>
+ /// <param name="newPosition">The new position value; must be non-negative and no larger than the current limit</param>
+ /// <returns>This buffer</returns>
+ /// <exception cref="ArgumentException">If the preconditions on <paramref name="newPosition"/> do not hold</exception>
+ public Buffer SetPosition(int newPosition)
{
- get { return _capacity; }
- set
- {
- _capacity = value;
- }
+ if ((newPosition > limit) || (newPosition < 0))
+ throw new ArgumentException();
+ position = newPosition;
+ if (mark > position) mark = -1;
+ return this;
}
- public virtual int Limit
+
+ /// <summary>
+ /// Returns this buffer's limit.
+ /// </summary>
+ public int Limit
{
- get { return _limit; }
- set
- {
- _limit = value;
+ get { return limit; }
+ set { SetLimit(value); }
+ }
- if (_position > _limit)
- _position = _limit;
+ /// <summary>
+ /// Sets this buffer's limit. If the position is larger than the new limit
+ /// then it is set to the new limit. If the mark is defined and larger than
+ /// the new limit then it is discarded.
+ /// </summary>
+ /// <param name="newLimit">The new limit value; must be non-negative and no larger than this buffer's capacity</param>
+ /// <returns>This buffer</returns>
+ /// <exception cref="ArgumentException">If the preconditions on <paramref name="newLimit"/> do not hold</exception>
+ public Buffer SetLimit(int newLimit)
+ {
+ if ((newLimit > capacity) || (newLimit < 0))
+ throw new ArgumentException();
+ limit = newLimit;
+ if (position > limit) position = limit;
+ if (mark > limit) mark = -1;
+ return this;
+ }
- if (_mark > 0 && _mark > _limit)
- _mark = -1;
- }
+ /// <summary>
+ /// Sets this buffer's mark at its position.
+ /// </summary>
+ /// <returns>This buffer</returns>
+ public Buffer Mark()
+ {
+ mark = position;
+ return this;
}
- public virtual int Position
+ /// <summary>
+ /// Resets this buffer's position to the previously-marked position.
+ ///
+ /// <para>
+ /// Invoking this method neither changes nor discards the mark's
+ /// value.
+ /// </para>
+ /// </summary>
+ /// <returns>This buffer</returns>
+ /// <exception cref="InvalidMarkException">If the mark has not been set</exception>
+ public Buffer Reset()
{
- get { return _position; }
- set
- {
- _position = value;
+ int m = mark;
+ if (m < 0)
+ throw new InvalidMarkException();
+ position = m;
+ return this;
+ }
- if (_mark >= 0 && _mark > _position)
- _mark = -1;
- }
+ /// <summary>
+ /// Clears this buffer. The position is set to zero, the limit is set to
+ /// the capacity, and the mark is discarded.
+ ///
+ /// <para>
+ /// Invoke this method before using a sequence of channel-read or
+ /// <c>Put</c> operations to fill this buffer. For example:
+ ///
+ /// <code>
+ /// buf.Clear(); // Prepare buffer for reading
+ /// in.Read(buf); // Read data
+ /// </code>
+ /// </para>
+ /// <para>
+ /// This method does not actually erase the data in the buffer, but it
+ /// is named as if it did because it will most often be used in situations
+ /// in which that might as well be the case.
+ /// </para>
+ /// </summary>
+ /// <returns>This buffer</returns>
+ public Buffer Clear()
+ {
+ position = 0;
+ limit = capacity;
+ mark = -1;
+ return this;
+ }
+
+ /// <summary>
+ /// Flips this buffer. The limit is set to the current position and then
+ /// the position is set to zero. If the mark is defined then it is
+ /// discarded.
+ ///
+ /// <para>
+ /// After a sequence of channel-read or <c>Put</c> operations, invoke
+ /// this method to prepare for a sequence of channel-write or relative
+ /// <c>Get</c> operations. For example:
+ ///
+ /// <code>
+ /// buf.Put(magic); // Prepend header
+ /// in.Read(buf); // Read data into rest of buffer
+ /// buf.Flip(); // Flip buffer
+ /// out.Write(buf); // Write header + data to channel
+ /// </code>
+ /// </para>
+ /// <para>
+ /// This method is often used in conjunction with the <see cref="ByteBuffer.Compact()"/>
+ /// method when transferring data from one place to another.
+ /// </para>
+ /// </summary>
+ /// <returns>This buffer</returns>
+ public Buffer Flip()
+ {
+ limit = position;
+ position = 0;
+ mark = -1;
+ return this;
}
- public virtual int Remaining
+ /// <summary>
+ /// Rewinds this buffer. The position is set to zero and the mark is
+ /// discarded.
+ ///
+ /// <para>
+ /// Invoke this method before a sequence of channel-write or <c>Get</c>
+ /// operations, assuming that the limit has already been set
+ /// appropriately. For example:
+ ///
+ /// <code>
+ /// out.Write(buf); // Write remaining data
+ /// buf.Rewind(); // Rewind buffer
+ /// buf.Get(array); // Copy data into array
+ /// </code>
+ /// </para>
+ /// </summary>
+ /// <returns>This buffer</returns>
+ public Buffer Rewind()
+ {
+ position = 0;
+ mark = -1;
+ return this;
+ }
+
+ /// <summary>
+ /// Returns the number of elements between the current position and the
+ /// limit.
+ /// </summary>
+ public int Remaining
{
- get { return _limit - _position; }
+ get { return limit - position; }
}
+ /// <summary>
+ /// Tells whether there are any elements between the current position and
+ /// the limit.
+ /// </summary>
+ public bool HasRemaining
+ {
+ get { return position < limit; }
+ }
+
+ /// <summary>
+ /// Tells whether or not this buffer is read-only.
+ /// </summary>
+ /// <returns>
+ /// <c>true</c> if, and only if, this buffer is read-only
+ /// </returns>
+ public abstract bool IsReadOnly { get; }
+
+ /// <summary>
+ /// Tells whether or not this buffer is backed by an accessible
+ /// array.
+ ///
+ /// <para>
+ /// If this method returns <c>true</c> then the <see cref="Array"/>
+ /// and <see cref="ArrayOffset"/> properties may be safely invoked.
+ /// </para>
+ /// </summary>
+ /// <returns>
+ /// <c>true</c> if, and only if, this buffer is backed by an array and is not read-only
+ /// </returns>
public abstract bool HasArray { get; }
+ /// <summary>
+ /// Returns the array that backs this
+ /// buffer <i>(optional operation)</i>.
+ ///
+ /// <para>
+ /// This property is intended to allow array-backed buffers to be
+ /// passed to native code more efficiently. Concrete subclasses
+ /// provide more strongly-typed return values for this property.
+ /// </para>
+ /// <para>
+ /// Modifications to this buffer's content will cause the returned
+ /// array's content to be modified, and vice versa.
+ /// </para>
+ /// <para>
+ /// Check the <see cref="HasArray"/> property before using this
+ /// property in order to ensure that this buffer has an accessible backing
+ /// array.
+ /// </para>
+ /// </summary>
+ /// <returns>
+ /// The array that backs this buffer
+ /// </returns>
+ /// <exception cref="ReadOnlyBufferException">If this buffer is backed by an array but is read-only</exception>
+ /// <exception cref="InvalidOperationException">If this buffer is not backed by an accessible array</exception>
+ public abstract object Array { get; }
+
+
+ /// <summary>
+ /// Returns the offset within this buffer's backing array of the first
+ /// element of the buffer <i>(optional operation)</i>.
+ ///
+ /// <para>
+ /// If this buffer is backed by an array then buffer position <c>p</c>
+ /// corresponds to array index <c>p</c> + <see cref="ArrayOffset"/><c>.
+ /// </para>
+ /// <para>
+ /// Check the <see cref="HasArray"/> property before using this
+ /// property in order to ensure that this buffer has an accessible backing
+ /// array.
+ /// </para>
+ /// </summary>
+ /// <returns>
+ /// The offset within this buffer's array
+ /// of the first element of the buffer
+ /// </returns>
+ /// <exception cref="ReadOnlyBufferException">If this buffer is backed by an array but is read-only</exception>
+ /// <exception cref="InvalidOperationException">If this buffer is not backed by an accessible array</exception>
+ public abstract int ArrayOffset { get; }
+
+ /// <summary>
+ /// Tells whether or not this buffer is <c>direct</c>
+ /// </summary>
+ /// <returns><c>true</c> if, and only if, this buffer is direct</returns>
public abstract bool IsDirect { get; }
- public abstract bool IsReadOnly { get; }
+ // -- internal members for bounds checking, etc. --
- public virtual bool HasRemaining
+ /// <summary>
+ /// Checks the current position against the limit, throwing a
+ /// <see cref="BufferUnderflowException"/> if it is not smaller than the limit, and then
+ /// increments the position.
+ /// </summary>
+ /// <returns>The current position value, before it is incremented</returns>
+ internal int NextGetIndex()
{
- get { return _limit == _position; }
+ if (position >= limit)
+ throw new BufferUnderflowException();
+ return position++;
}
- public virtual int Mark
+ internal int NextGetIndex(int nb)
{
- get { return _mark; }
- set
- {
- _mark = value;
- }
+ if (limit - position < nb)
+ throw new BufferUnderflowException();
+ int p = position;
+ position += nb;
+ return p;
}
- public Buffer Reset()
+ /// <summary>
+ /// Checks the current position against the limit, throwing a <see cref="BufferOverflowException"/>
+ /// if it is not smaller than the limit, and then
+ /// increments the position.
+ /// </summary>
+ /// <returns>The current position value, before it is incremented</returns>
+ internal int NextPutIndex()
{
- if (_mark >= 0)
- _position = _mark;
+ if (position >= limit)
+ throw new BufferOverflowException();
+ return position++;
+ }
- return this;
+ internal int NextPutIndex(int nb)
+ {
+ if (limit - position < nb)
+ throw new BufferOverflowException();
+ int p = position;
+ position += nb;
+ return p;
}
- public Buffer Clear()
+ /// <summary>
+ /// Checks the given index against the limit, throwing an <see cref="IndexOutOfRangeException"/>
+ /// if it is not smaller than the limit or is smaller than zero.
+ /// </summary>
+ /// <param name="i"></param>
+ /// <returns></returns>
+ internal int CheckIndex(int i)
{
- _position = 0;
- _limit = _capacity;
- _mark = -1;
- return this;
+ if ((i < 0) || (i >= limit))
+ throw new IndexOutOfRangeException();
+ return i;
}
- public Buffer Flip()
+ internal int CheckIndex(int i, int nb)
{
- _limit = _position;
- _position = 0;
- if (_mark >= 0)
- _mark = -1;
+ if ((i < 0) || (nb > limit - i))
+ throw new IndexOutOfRangeException();
+ return i;
+ }
- return this;
+ internal int MarkValue
+ {
+ get { return mark; }
}
- public Buffer Rewind()
+ internal void Truncate()
{
- _position = 0;
- _mark = -1;
+ mark = -1;
+ position = 0;
+ limit = 0;
+ capacity = 0;
+ }
- return this;
+ internal void DiscardMark()
+ {
+ mark = -1;
+ }
+
+ internal static void CheckBounds(int off, int len, int size)
+ {
+ if ((off | len | (off + len) | (size - (off + len))) < 0)
+ throw new IndexOutOfRangeException();
}
}
-}
\ No newline at end of file
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/d235115d/src/Lucene.Net.Core/Support/BufferExceptions.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Support/BufferExceptions.cs b/src/Lucene.Net.Core/Support/BufferExceptions.cs
new file mode 100644
index 0000000..c9307aa
--- /dev/null
+++ b/src/Lucene.Net.Core/Support/BufferExceptions.cs
@@ -0,0 +1,24 @@
+\ufeffusing System;
+
+namespace Lucene.Net.Support
+{
+ [Serializable]
+ internal sealed class BufferUnderflowException : Exception
+ {
+ }
+
+ [Serializable]
+ internal sealed class BufferOverflowException : Exception
+ {
+ }
+
+ [Serializable]
+ internal sealed class ReadOnlyBufferException : Exception
+ {
+ }
+
+ [Serializable]
+ internal sealed class InvalidMarkException : Exception
+ {
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/d235115d/src/Lucene.Net.Core/Support/BufferUnderflowException.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Support/BufferUnderflowException.cs b/src/Lucene.Net.Core/Support/BufferUnderflowException.cs
deleted file mode 100644
index bd86a98..0000000
--- a/src/Lucene.Net.Core/Support/BufferUnderflowException.cs
+++ /dev/null
@@ -1,8 +0,0 @@
-\ufeffusing System;
-
-namespace Lucene.Net.Support
-{
- internal sealed class BufferUnderflowException : Exception
- {
- }
-}
\ No newline at end of file