lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [27/50] [abbrv] lucenenet git commit: Fixed bugs with keeping track of the current position in Core.Support.MemoryMappedFileByteBuffer
Date Sun, 23 Oct 2016 13:02:13 GMT
Fixed bugs with keeping track of the current position in Core.Support.MemoryMappedFileByteBuffer


Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/302c7ada
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/302c7ada
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/302c7ada

Branch: refs/heads/master
Commit: 302c7adaf9c2e7ce52042d3d24847b4bd64f2f32
Parents: cd60689
Author: Shad Storhaug <shad@shadstorhaug.com>
Authored: Wed Oct 19 07:04:46 2016 +0700
Committer: Shad Storhaug <shad@shadstorhaug.com>
Committed: Thu Oct 20 18:20:58 2016 +0700

----------------------------------------------------------------------
 .../Support/MemoryMappedFileByteBuffer.cs       | 294 ++++++++++---------
 1 file changed, 162 insertions(+), 132 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/302c7ada/src/Lucene.Net.Core/Support/MemoryMappedFileByteBuffer.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Support/MemoryMappedFileByteBuffer.cs b/src/Lucene.Net.Core/Support/MemoryMappedFileByteBuffer.cs
index 1726bd7..1b388f1 100644
--- a/src/Lucene.Net.Core/Support/MemoryMappedFileByteBuffer.cs
+++ b/src/Lucene.Net.Core/Support/MemoryMappedFileByteBuffer.cs
@@ -6,6 +6,8 @@ namespace Lucene.Net.Support
     internal sealed class MemoryMappedFileByteBuffer : ByteBuffer, IDisposable
     {
         private MemoryMappedViewAccessor _accessor;
+        private readonly int offset; // always 0 (add constructors to fix this)
+        new private bool bigEndian = true;
 
         public MemoryMappedFileByteBuffer(MemoryMappedViewAccessor accessor, int mark, int
pos, int lim, int cap)
             : base(mark, pos, lim, cap)
@@ -13,60 +15,56 @@ namespace Lucene.Net.Support
             _accessor = accessor;
         }
 
-        internal override byte _get(int i)
+        public override ByteBuffer Slice()
         {
-            throw new NotImplementedException();
+            return new MemoryMappedFileByteBuffer(_accessor, -1, 0, Remaining, Remaining);
         }
 
-        internal override void _put(int i, byte b)
+        public override ByteBuffer Duplicate()
         {
-            throw new NotImplementedException();
+            return new MemoryMappedFileByteBuffer(_accessor, MarkValue, Position, Limit,
Capacity);
         }
 
-        public override bool IsDirect
+        public override ByteBuffer AsReadOnlyBuffer()
         {
-            get { return true; }
+            throw new NotImplementedException();
         }
 
-        public override bool IsReadOnly
+
+        private int Ix(int i)
         {
-            get { return false; }
+            return i + offset;
         }
 
-        public override ByteBuffer Slice()
+        public override byte Get()
         {
-            return new MemoryMappedFileByteBuffer(_accessor, -1, 0, Remaining, Remaining);
+            return _accessor.ReadByte(Ix(NextGetIndex()));
         }
 
-        public override ByteBuffer Duplicate()
+        public override byte Get(int index)
         {
-            return new MemoryMappedFileByteBuffer(_accessor, MarkValue, Position, Limit,
Capacity);
+            return _accessor.ReadByte(Ix(CheckIndex(index)));
         }
 
-        public override ByteBuffer AsReadOnlyBuffer()
+        public override bool IsDirect
         {
-            throw new NotImplementedException();
+            get { return false; }
         }
 
-        public override byte Get()
+        public override bool IsReadOnly
         {
-            return _accessor.ReadByte(Position++);
+            get { return false; }
         }
 
         public override ByteBuffer Put(byte b)
         {
-            _accessor.Write(Position++, b);
+            _accessor.Write(Ix(NextPutIndex()), b);
             return this;
         }
 
-        public override byte Get(int index)
-        {
-            return _accessor.ReadByte(index);
-        }
-
         public override ByteBuffer Put(int index, byte b)
         {
-            _accessor.Write(index, b);
+            _accessor.Write(Ix(CheckIndex(index)), b);
             return this;
         }
 
@@ -75,206 +73,238 @@ namespace Lucene.Net.Support
             throw new NotSupportedException();
         }
 
-        public override char GetChar()
+        internal override byte _get(int i)
         {
-            char c = _accessor.ReadChar(Position);
-            Position += 2;
-
-            //conform to how the index was written
-            return Number.FlipEndian(c);
+            throw new NotSupportedException();
         }
 
-        public override ByteBuffer PutChar(char value)
+        internal override void _put(int i, byte b)
         {
-            //conform to how the index was written
-            _accessor.Write(Position, Number.FlipEndian(value));
-            Position += 2;
+            throw new NotSupportedException();
+        }
 
-            return this;
+
+        public override char GetChar()
+        {
+            var littleEndian = _accessor.ReadChar(Ix(NextGetIndex(2)));
+            if (bigEndian)
+            {
+                return Number.FlipEndian(littleEndian);
+            }
+            return littleEndian;
         }
 
         public override char GetChar(int index)
         {
-            var c = _accessor.ReadChar(index);
+            var littleEndian = _accessor.ReadChar(Ix(CheckIndex(index, 2)));
+            if (bigEndian)
+            {
+                return Number.FlipEndian(littleEndian);
+            }
+            return littleEndian;
+        }
 
-            //conform to how the index was written
-            return Number.FlipEndian(c);
+        public override ByteBuffer PutChar(char value)
+        {
+            _accessor.Write(Ix(NextPutIndex(2)), bigEndian ? Number.FlipEndian(value) : value);
+            return this;
         }
 
+        
+
         public override ByteBuffer PutChar(int index, char value)
         {
-            _accessor.Write(index, Number.FlipEndian(value));
-
+            _accessor.Write(Ix(CheckIndex(index, 2)), bigEndian ? Number.FlipEndian(value)
: value);
             return this;
         }
 
         public override short GetShort()
         {
-            var c = _accessor.ReadInt16(Position);
-            Position += 2;
-
-            //conform to how the index was written
-            return Number.FlipEndian(c);
+            var littleEndian = _accessor.ReadInt16(Ix(NextGetIndex(2)));
+            if (bigEndian)
+            {
+                return Number.FlipEndian(littleEndian);
+            }
+            return littleEndian;
         }
 
-        public override ByteBuffer PutShort(short value)
+        public override short GetShort(int index)
         {
-            //conform to how the index was written
-            _accessor.Write(Position, Number.FlipEndian(value));
-            Position += 2;
-
-            return this;
+            var littleEndian = _accessor.ReadInt16(Ix(CheckIndex(index, 2)));
+            if (bigEndian)
+            {
+                return Number.FlipEndian(littleEndian);
+            }
+            return littleEndian;
         }
 
-        public override short GetShort(int index)
+        public override ByteBuffer PutShort(short value)
         {
-            var c = _accessor.ReadInt16(index);
-
-            //conform to how the index was written
-            return Number.FlipEndian(c);
+            _accessor.Write(Ix(NextPutIndex(2)), bigEndian ? Number.FlipEndian(value) : value);
+            return this;
         }
 
         public override ByteBuffer PutShort(int index, short value)
         {
-            //conform to how the index was written
-            _accessor.Write(index, Number.FlipEndian(value));
-
+            _accessor.Write(Ix(CheckIndex(index, 2)), bigEndian ? Number.FlipEndian(value)
: value);
             return this;
         }
 
         public override int GetInt()
         {
-            var c = _accessor.ReadInt32(Position);
-            Position += 4;
+            var littleEndian = _accessor.ReadInt32(Ix(NextGetIndex(4)));
+            if (bigEndian)
+            {
+                return Number.FlipEndian(littleEndian);
+            }
+            return littleEndian;
+        }
 
-            //conform to how the index was written
-            return Number.FlipEndian(c);
+        public override int GetInt(int index)
+        {
+            var littleEndian = _accessor.ReadInt32(Ix(CheckIndex(index, 4)));
+            if (bigEndian)
+            {
+                return Number.FlipEndian(littleEndian);
+            }
+            return littleEndian;
         }
 
         public override ByteBuffer PutInt(int value)
         {
-            //conform to how the index was written
-            _accessor.Write(Position, Number.FlipEndian(value));
-            Position += 4;
-
+            _accessor.Write(Ix(NextPutIndex(4)), bigEndian ? Number.FlipEndian(value) : value);
             return this;
         }
 
-        public override int GetInt(int index)
-        {
-            var c = _accessor.ReadInt32(index);
-
-            //conform to how the index was written
-            return Number.FlipEndian(c);
-        }
+        
 
         public override ByteBuffer PutInt(int index, int value)
         {
-            //conform to how the index was written
-            _accessor.Write(index, Number.FlipEndian(value));
-
+            _accessor.Write(Ix(CheckIndex(index, 4)), bigEndian ? Number.FlipEndian(value)
: value);
             return this;
         }
 
         public override long GetLong()
         {
-            var c = _accessor.ReadInt64(Position);
-            Position += 8;
-
-            //conform to how the index was written
-            return Number.FlipEndian(c);
+            var littleEndian = _accessor.ReadInt64(Ix(NextGetIndex(8)));
+            if (bigEndian)
+            {
+                return Number.FlipEndian(littleEndian);
+            }
+            return littleEndian;
         }
 
-        public override ByteBuffer PutLong(long value)
+        public override long GetLong(int index)
         {
-            //conform to how the index was written
-            _accessor.Write(Position, Number.FlipEndian(value));
-            Position += 8;
-
-            return this;
+            var littleEndian = _accessor.ReadInt64(Ix(CheckIndex(index, 8)));
+            if (bigEndian)
+            {
+                return Number.FlipEndian(littleEndian);
+            }
+            return littleEndian;
         }
 
-        public override long GetLong(int index)
+        public override ByteBuffer PutLong(long value)
         {
-            var c = _accessor.ReadInt64(index);
-
-            //conform to how the index was written
-            return Number.FlipEndian(c);
+            _accessor.Write(Ix(NextPutIndex(8)), bigEndian ? Number.FlipEndian(value) : value);
+            return this;
         }
 
         public override ByteBuffer PutLong(int index, long value)
         {
-            //conform to how the index was written
-            _accessor.Write(index, Number.FlipEndian(value));
-
+            _accessor.Write(Ix(CheckIndex(index, 8)), bigEndian ? Number.FlipEndian(value)
: value);
             return this;
         }
 
         public override float GetFloat()
         {
-            var c = _accessor.ReadSingle(Position);
-            Position += 4;
-
-            //conform to how the index was written
-            return Number.FlipEndian(c);
+            byte[] temp = new byte[4];
+            temp[0] = _accessor.ReadByte(Ix(NextGetIndex()));
+            temp[1] = _accessor.ReadByte(Ix(NextGetIndex()));
+            temp[2] = _accessor.ReadByte(Ix(NextGetIndex()));
+            temp[3] = _accessor.ReadByte(Ix(NextGetIndex()));
+            if (bigEndian)
+            {
+                System.Array.Reverse(temp);
+            }
+            return BitConverter.ToSingle(temp, 0);
         }
 
-        public override ByteBuffer PutFloat(float value)
+        public override float GetFloat(int index)
         {
-            //conform to how the index was written
-            _accessor.Write(Position, Number.FlipEndian(value));
-            Position += 4;
-
-            return this;
+            byte[] temp = new byte[4];
+            temp[0] = _accessor.ReadByte(Ix(NextGetIndex(index)));
+            temp[1] = _accessor.ReadByte(Ix(NextGetIndex()));
+            temp[2] = _accessor.ReadByte(Ix(NextGetIndex()));
+            temp[3] = _accessor.ReadByte(Ix(NextGetIndex()));
+            if (bigEndian)
+            {
+                System.Array.Reverse(temp);
+            }
+            return BitConverter.ToSingle(temp, 0);
         }
 
-        public override float GetFloat(int index)
+        public override ByteBuffer PutFloat(float value)
         {
-            var c = _accessor.ReadSingle(index);
+            var bytes = BitConverter.GetBytes(value);
 
-            //conform to how the index was written
-            return Number.FlipEndian(c);
+            if (bigEndian)
+            {
+                System.Array.Reverse(bytes);
+            }
+
+            _accessor.Write(Ix(NextPutIndex()), bytes[0]);
+            _accessor.Write(Ix(NextPutIndex()), bytes[1]);
+            _accessor.Write(Ix(NextPutIndex()), bytes[2]);
+            _accessor.Write(Ix(NextPutIndex()), bytes[3]);
+            return this;
         }
 
         public override ByteBuffer PutFloat(int index, float value)
         {
-            //conform to how the index was written
-            _accessor.Write(index, Number.FlipEndian(value));
+            var bytes = BitConverter.GetBytes(value);
+
+            if (bigEndian)
+            {
+                System.Array.Reverse(bytes);
+            }
 
+            _accessor.Write(Ix(NextPutIndex(index)), bytes[0]);
+            _accessor.Write(Ix(NextPutIndex()), bytes[1]);
+            _accessor.Write(Ix(NextPutIndex()), bytes[2]);
+            _accessor.Write(Ix(NextPutIndex()), bytes[3]);
             return this;
         }
 
         public override double GetDouble()
         {
-            var c = _accessor.ReadDouble(Position);
-            Position += 4;
-
-            //conform to how the index was written
-            return Number.FlipEndian(c);
+            var littleEndian = _accessor.ReadDouble(Ix(NextGetIndex(8)));
+            if (bigEndian)
+            {
+                return Number.FlipEndian(littleEndian);
+            }
+            return littleEndian;
         }
 
-        public override ByteBuffer PutDouble(double value)
+        public override double GetDouble(int index)
         {
-            //conform to how the index was written
-            _accessor.Write(Position, Number.FlipEndian(value));
-            Position += 8;
-
-            return this;
+            var littleEndian = _accessor.ReadDouble(Ix(CheckIndex(index, 8)));
+            if (bigEndian)
+            {
+                return Number.FlipEndian(littleEndian);
+            }
+            return littleEndian;
         }
 
-        public override double GetDouble(int index)
+        public override ByteBuffer PutDouble(double value)
         {
-            var c = _accessor.ReadDouble(index);
-
-            //conform to how the index was written
-            return Number.FlipEndian(c);
+            _accessor.Write(Ix(NextPutIndex(8)), bigEndian ? Number.FlipEndian(value) : value);
+            return this;
         }
 
         public override ByteBuffer PutDouble(int index, double value)
         {
-            //conform to how the index was written
-            _accessor.Write(index, Number.FlipEndian(value));
-
+            _accessor.Write(Ix(CheckIndex(index, 8)), bigEndian ? Number.FlipEndian(value)
: value);
             return this;
         }
 
@@ -288,7 +318,7 @@ namespace Lucene.Net.Support
 
         public override LongBuffer AsLongBuffer()
         {
-            throw new NotImplementedException();
+            throw new NotSupportedException();
         }
     }
 }
\ No newline at end of file


Mime
View raw message