lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From synhers...@apache.org
Subject [03/46] lucenenet git commit: Finished implementation of Facet.Taxonomy.WriterCache.TestCharBlockArray test and fixed the implementation of CharBlockArray and CompactLabelToOrdinal to make the test pass.
Date Tue, 04 Oct 2016 20:01:33 GMT
Finished implementation of Facet.Taxonomy.WriterCache.TestCharBlockArray test and fixed the
implementation of CharBlockArray and CompactLabelToOrdinal to make the test pass.


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

Branch: refs/heads/master
Commit: 861aa7377a915465fac212c8db66b1a76996b28e
Parents: bcbfad5
Author: Shad Storhaug <shad@shadstorhaug.com>
Authored: Fri Sep 23 23:38:42 2016 +0700
Committer: Shad Storhaug <shad@shadstorhaug.com>
Committed: Mon Oct 3 23:30:36 2016 +0700

----------------------------------------------------------------------
 src/Lucene.Net.Core/Support/StreamUtils.cs      | 11 ++--
 src/Lucene.Net.Facet/Properties/AssemblyInfo.cs |  3 +
 .../Taxonomy/WriterCache/CharBlockArray.cs      | 18 +++---
 .../WriterCache/CompactLabelToOrdinal.cs        | 26 +++------
 .../Taxonomy/WriterCache/TestCharBlockArray.cs  | 58 +++++++++-----------
 5 files changed, 48 insertions(+), 68 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/861aa737/src/Lucene.Net.Core/Support/StreamUtils.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Support/StreamUtils.cs b/src/Lucene.Net.Core/Support/StreamUtils.cs
index 75e7d15..7e05717 100644
--- a/src/Lucene.Net.Core/Support/StreamUtils.cs
+++ b/src/Lucene.Net.Core/Support/StreamUtils.cs
@@ -13,13 +13,12 @@ namespace Lucene.Net.Support
     {
         static readonly BinaryFormatter Formatter = new BinaryFormatter();
 
-        public static MemoryStream SerializeToStream(object o)
+        public static void SerializeToStream(object o, Stream outputStream)
         {
-            using (var stream = new MemoryStream())
-            {
-                Formatter.Serialize(stream, o);
-                return stream;
-            }
+            // LUCENENET TODO: It would probably be better to serialize to
+            // XML so this works across .NET framework versions or alternatively
+            // find/create an alternative binary formatter implementation that works that
way.
+            Formatter.Serialize(outputStream, o);
         }
 
         public static object DeserializeFromStream(Stream stream)

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/861aa737/src/Lucene.Net.Facet/Properties/AssemblyInfo.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Facet/Properties/AssemblyInfo.cs b/src/Lucene.Net.Facet/Properties/AssemblyInfo.cs
index 741693d..7325e4c 100644
--- a/src/Lucene.Net.Facet/Properties/AssemblyInfo.cs
+++ b/src/Lucene.Net.Facet/Properties/AssemblyInfo.cs
@@ -22,6 +22,9 @@ using System.Runtime.InteropServices;
 // The following GUID is for the ID of the typelib if this project is exposed to COM
 [assembly: Guid("8dd11ab2-c5b3-4691-99da-2941f27e0e10")]
 
+// for testing
+[assembly: InternalsVisibleTo("Lucene.Net.Tests.Facet")]
+
 // Version information for an assembly consists of the following four values:
 //
 //      Major Version

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/861aa737/src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArray.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArray.cs b/src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArray.cs
index a6a60e7..a38329b 100644
--- a/src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArray.cs
+++ b/src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArray.cs
@@ -54,7 +54,10 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
 
             public object Clone()
             {
-                throw new NotImplementedException();
+                var clone = new Block(chars.Length);
+                clone.length = length;
+                Array.Copy(chars, clone.chars, chars.Length);
+                return clone;
             }
         }
 
@@ -214,21 +217,14 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
             return sb.ToString();
         }
 
-        internal virtual void Flush(OutputStreamDataOutput @out)
+        internal virtual void Flush(Stream @out)
         {
-            
-            using (var ms = StreamUtils.SerializeToStream(this))
-            {
-                var bytes = ms.ToArray();
-                @out.WriteBytes(bytes, 0, bytes.Length);
-            }
+            StreamUtils.SerializeToStream(this, @out);
         }
 
-        public static CharBlockArray Open(BinaryReader @in)
+        public static CharBlockArray Open(Stream @in)
         {
             return StreamUtils.DeserializeFromStream(@in) as CharBlockArray;
         }
-
     }
-
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/861aa737/src/Lucene.Net.Facet/Taxonomy/WriterCache/CompactLabelToOrdinal.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Facet/Taxonomy/WriterCache/CompactLabelToOrdinal.cs b/src/Lucene.Net.Facet/Taxonomy/WriterCache/CompactLabelToOrdinal.cs
index 07bd162..f717fb1 100644
--- a/src/Lucene.Net.Facet/Taxonomy/WriterCache/CompactLabelToOrdinal.cs
+++ b/src/Lucene.Net.Facet/Taxonomy/WriterCache/CompactLabelToOrdinal.cs
@@ -396,7 +396,7 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
         /// Opens the file and reloads the CompactLabelToOrdinal. The file it expects
         /// is generated from the <seealso cref="#flush(File)"/> command.
         /// </summary>
-        public static CompactLabelToOrdinal Open(string file, float loadFactor, int numHashArrays)
+        internal static CompactLabelToOrdinal Open(string file, float loadFactor, int numHashArrays)
         {
             /// <summary>
             /// Part of the file is the labelRepository, which needs to be rehashed
@@ -422,7 +422,7 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
                 l2o.Init();
 
                 // now read the chars
-                l2o.labelRepository = CharBlockArray.Open(dis);
+                l2o.labelRepository = CharBlockArray.Open(dis.BaseStream);
 
                 l2o.collisionMap = new CollisionMap(l2o.labelRepository);
 
@@ -443,13 +443,13 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
                     // identical code to CategoryPath.hashFromSerialized. since we need to
                     // advance offset, we cannot call the method directly. perhaps if we
                     // could pass a mutable Integer or something...
-                    int length = (short)l2o.labelRepository.CharAt(offset++);
+                    int length = (ushort)l2o.labelRepository.CharAt(offset++);
                     int hash = length;
                     if (length != 0)
                     {
                         for (int i = 0; i < length; i++)
                         {
-                            int len = (short)l2o.labelRepository.CharAt(offset++);
+                            int len = (ushort)l2o.labelRepository.CharAt(offset++);
                             hash = hash * 31 + l2o.labelRepository.SubSequence(offset, offset
+ len).GetHashCode();
                             offset += len;
                         }
@@ -482,24 +482,14 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
 
         }
 
-        public virtual void Flush(Stream stream)
+        internal virtual void Flush(FileStream stream)
         {
-
-            OutputStreamDataOutput dos = new OutputStreamDataOutput(stream);
-
-            try
+            using (BinaryWriter dos = new BinaryWriter(stream))
             {
-                dos.WriteInt(this.counter);
+                dos.Write(this.counter);
 
                 // write the labelRepository
-                this.labelRepository.Flush(dos);
-                // Closes the data output stream
-                dos.Dispose();
-
-            }
-            finally
-            {
-                dos.Dispose();
+                this.labelRepository.Flush(dos.BaseStream);
             }
         }
 

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/861aa737/src/Lucene.Net.Tests.Facet/Taxonomy/WriterCache/TestCharBlockArray.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests.Facet/Taxonomy/WriterCache/TestCharBlockArray.cs b/src/Lucene.Net.Tests.Facet/Taxonomy/WriterCache/TestCharBlockArray.cs
index f4be988..f059a79 100644
--- a/src/Lucene.Net.Tests.Facet/Taxonomy/WriterCache/TestCharBlockArray.cs
+++ b/src/Lucene.Net.Tests.Facet/Taxonomy/WriterCache/TestCharBlockArray.cs
@@ -1,13 +1,9 @@
-´╗┐using System.Text;
-using Lucene.Net.Support;
-using NUnit.Framework;
+´╗┐using NUnit.Framework;
+using System.IO;
+using System.Text;
 
 namespace Lucene.Net.Facet.Taxonomy.WriterCache
 {
-
-
-    using TestUtil = Lucene.Net.Util.TestUtil;
-
     /*
      * Licensed to the Apache Software Foundation (ASF) under one or more
      * contributor license agreements.  See the NOTICE file distributed with
@@ -24,14 +20,13 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
      * See the License for the specific language governing permissions and
      * limitations under the License.
      */
+
     [TestFixture]
     public class TestCharBlockArray : FacetTestCase
     {
 
-        /* not finished yet because of missing charset decoder */
-
-        /*
-        public virtual void testArray()
+        [Test]
+        public virtual void TestArray()
         {
             CharBlockArray array = new CharBlockArray();
             StringBuilder builder = new StringBuilder();
@@ -47,9 +42,8 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
                 // This test is turning random bytes into a string,
                 // this is asking for trouble.
 
-                CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder().onUnmappableCharacter(CodingErrorAction.REPLACE).onMalformedInput(CodingErrorAction.REPLACE);
-                string s = decoder.Decode(ByteBuffer.Wrap(buffer, 0, size)).ToString();
-                array.append(s);
+                string s = Encoding.UTF8.GetString(buffer, 0, size);
+                array.Append(s);
                 builder.Append(s);
             }
 
@@ -59,9 +53,8 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
                 int size = 1 + Random().Next(50);
                 // This test is turning random bytes into a string,
                 // this is asking for trouble.
-                CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder().onUnmappableCharacter(CodingErrorAction.REPLACE).onMalformedInput(CodingErrorAction.REPLACE);
-                string s = decoder.decode(ByteBuffer.Wrap(buffer, 0, size)).ToString();
-                array.append((CharSequence)s);
+                string s = Encoding.UTF8.GetString(buffer, 0, size);
+                array.Append(s);
                 builder.Append(s);
             }
 
@@ -71,29 +64,30 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
                 int size = 1 + Random().Next(50);
                 // This test is turning random bytes into a string,
                 // this is asking for trouble.
-                CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder().onUnmappableCharacter(CodingErrorAction.REPLACE).onMalformedInput(CodingErrorAction.REPLACE);
-                string s = decoder.decode(ByteBuffer.Wrap(buffer, 0, size)).ToString();
+                string s = Encoding.UTF8.GetString(buffer, 0, size);
                 for (int j = 0; j < s.Length; j++)
                 {
-                    array.append(s[j]);
+                    array.Append(s[j]);
                 }
                 builder.Append(s);
             }
 
             AssertEqualsInternal("GrowingCharArray<->StringBuilder mismatch.", builder,
array);
 
-            File tempDir = CreateTempDir("growingchararray");
-            File f = new File(tempDir, "GrowingCharArrayTest.tmp");
-            BufferedOutputStream @out = new BufferedOutputStream(new FileOutputStream(f));
-            array.flush(@out);
-            @out.flush();
-            @out.Close();
+            DirectoryInfo tempDir = CreateTempDir("growingchararray");
+            FileInfo f = new FileInfo(Path.Combine(tempDir.FullName, "GrowingCharArrayTest.tmp"));
+            using (Stream @out = new FileStream(f.FullName, FileMode.OpenOrCreate, FileAccess.Write))
+            {
+                array.Flush(@out);
+                @out.Flush();
+            }
 
-            BufferedInputStream @in = new BufferedInputStream(new FileInputStream(f));
-            array = CharBlockArray.open(@in);
-            AssertEqualsInternal("GrowingCharArray<->StringBuilder mismatch after flush/load.",
builder, array);
-            @in.Close();
-            f.delete();
+            using (Stream @in = new FileStream(f.FullName, FileMode.Open, FileAccess.Read))
+            {
+                array = CharBlockArray.Open(@in);
+                AssertEqualsInternal("GrowingCharArray<->StringBuilder mismatch after
flush/load.", builder, array);
+            }
+            f.Delete();
         }
 
         private static void AssertEqualsInternal(string msg, StringBuilder expected, CharBlockArray
actual)
@@ -104,7 +98,5 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
                 Assert.AreEqual(expected[i], actual.CharAt(i), msg);
             }
         }
-        */
     }
-
 }
\ No newline at end of file


Mime
View raw message