lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [07/50] [abbrv] lucenenet git commit: Lucene.Net.Facet.Taxonomy.WriterCache.CharBlockArray: Refactored to use BinaryReader/BinaryWriter for serialzation and eliminated the 2 serialization support classes StreamUtils and CharBlockArrayConverter
Date Sat, 09 Sep 2017 00:31:25 GMT
Lucene.Net.Facet.Taxonomy.WriterCache.CharBlockArray: Refactored to use BinaryReader/BinaryWriter
for serialzation and eliminated the 2 serialization support classes StreamUtils and CharBlockArrayConverter


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

Branch: refs/heads/master
Commit: 3a959d5bf0ec67475febe51ef8b34184f8cafa88
Parents: 0486457
Author: Shad Storhaug <shad@shadstorhaug.com>
Authored: Sat Sep 2 00:33:56 2017 +0700
Committer: Shad Storhaug <shad@shadstorhaug.com>
Committed: Sat Sep 2 00:33:56 2017 +0700

----------------------------------------------------------------------
 src/Lucene.Net.Facet/Lucene.Net.Facet.csproj    |   1 -
 .../Taxonomy/WriterCache/CharBlockArray.cs      |  99 +++++++++++-------
 .../WriterCache/CharBlockArrayConverter.cs      | 101 -------------------
 src/Lucene.Net/Support/IO/StreamUtils.cs        |  51 ----------
 4 files changed, 63 insertions(+), 189 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/3a959d5b/src/Lucene.Net.Facet/Lucene.Net.Facet.csproj
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Facet/Lucene.Net.Facet.csproj b/src/Lucene.Net.Facet/Lucene.Net.Facet.csproj
index d58da16..a7b9eda 100644
--- a/src/Lucene.Net.Facet/Lucene.Net.Facet.csproj
+++ b/src/Lucene.Net.Facet/Lucene.Net.Facet.csproj
@@ -60,7 +60,6 @@
   
   <ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.5' ">
     <PackageReference Include="System.Threading.Thread" Version="4.0.0" />
-    <PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
     <PackageReference Include="System.Net.Primitives" Version="4.3.0" />
   </ItemGroup>
 

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/3a959d5b/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 d70c225..b27532d 100644
--- a/src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArray.cs
+++ b/src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArray.cs
@@ -1,13 +1,8 @@
-using Lucene.Net.Store;
-using Lucene.Net.Support;
-using Lucene.Net.Support.IO;
+using Lucene.Net.Support;
 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Text;
-#if !FEATURE_SERIALIZABLE
-using Newtonsoft.Json;
-#endif
 
 namespace Lucene.Net.Facet.Taxonomy.WriterCache
 {
@@ -31,24 +26,19 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
     /// <summary>
     /// Similar to <see cref="StringBuilder"/>, but with a more efficient growing strategy.
     /// This class uses char array blocks to grow.
-    /// 
+    /// <para/>
     /// @lucene.experimental
     /// </summary>
-#if FEATURE_SERIALIZABLE
-    [Serializable]
-#else
-    [JsonConverter(typeof(CharBlockArrayConverter))]
-
-#endif
-    public class CharBlockArray : ICharSequence
+    // LUCENENET NOTE: The serialization features here are strictly for testing purposes,
+    // therefore it doesn't make any difference what type of serialization is used. 
+    // To make things simpler, we are using BinaryReader and BinaryWriter since 
+    // BinaryFormatter is not implemented in .NET Standard 1.x.
+    internal class CharBlockArray : ICharSequence
     {
         //private const long serialVersionUID = 1L; // LUCENENET: Not used
 
         private const int DEFAULT_BLOCK_SIZE = 32 * 1024; // 32 KB default size
 
-#if FEATURE_SERIALIZABLE
-        [Serializable]
-#endif
         internal sealed class Block
         {
             //internal const long serialVersionUID = 1L; // LUCENENET: Not used
@@ -69,6 +59,23 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
                 Array.Copy(chars, clone.chars, chars.Length);
                 return clone;
             }
+
+            // LUCENENET specific
+            public void Serialize(BinaryWriter writer)
+            {
+                writer.Write(chars.Length);
+                writer.Write(chars);
+                writer.Write(length);
+            }
+
+            // LUCENENET specific
+            // Deserialization constructor
+            public Block(BinaryReader reader)
+            {
+                int charsLength = reader.ReadInt32();
+                this.chars = reader.ReadChars(charsLength);
+                this.length = reader.ReadInt32();
+            }
         }
 
         internal IList<Block> blocks;
@@ -242,29 +249,49 @@ namespace Lucene.Net.Facet.Taxonomy.WriterCache
 
         internal virtual void Flush(Stream @out)
         {
-#if FEATURE_SERIALIZABLE
-            StreamUtils.SerializeToStream(this, @out);
-#else
-            byte[] bytes = null;
-            var json = JsonConvert.SerializeObject(this, new CharBlockArrayConverter());
-            bytes = Encoding.UTF8.GetBytes(json);
-            @out.Write(bytes, 0, bytes.Length);
-#endif
+            using (var writer = new BinaryWriter(@out, new UTF8Encoding(false, true), true))
+            {
+                writer.Write(blocks.Count);
+                int currentIndex = 0;
+                for (int i = 0; i < blocks.Count; i++)
+                {
+                    var block = blocks[i];
+                    block.Serialize(writer);
+                    if (block == current)
+                    {
+                        currentIndex = i;
+                    }
+                }
+                // Write the index of the current block so we can
+                // set the reference when deserializing
+                writer.Write(currentIndex);
+                writer.Write(blockSize);
+                writer.Write(length);
+                writer.Flush();
+            }
+        }
+
+        // LUCENENET specific
+        // Deserialization constructor
+        internal CharBlockArray(BinaryReader reader)
+        {
+            var blocksCount = reader.ReadInt32();
+            this.blocks = new List<Block>(blocksCount);
+            for (int i = 0; i < blocksCount; i++)
+            {
+                blocks.Add(new Block(reader));
+            }
+            this.current = blocks[reader.ReadInt32()];
+            this.blockSize = reader.ReadInt32();
+            this.length = reader.ReadInt32();
         }
 
         public static CharBlockArray Open(Stream @in)
         {
-#if FEATURE_SERIALIZABLE
-            return StreamUtils.DeserializeFromStream(@in) as CharBlockArray;
-#else
-            var contents = new byte[@in.Length];
-            @in.Read(contents, 0, (int)@in.Length);
-
-            var json = Encoding.UTF8.GetString(contents);
-            var deserialized = JsonConvert.DeserializeObject<CharBlockArray>(json);
-
-            return deserialized;
-#endif
+            using (var writer = new BinaryReader(@in, new UTF8Encoding(false, true), true))
+            {
+                return new CharBlockArray(writer);
+            }
         }
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/3a959d5b/src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArrayConverter.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArrayConverter.cs b/src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArrayConverter.cs
deleted file mode 100644
index 43f865e..0000000
--- a/src/Lucene.Net.Facet/Taxonomy/WriterCache/CharBlockArrayConverter.cs
+++ /dev/null
@@ -1,101 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
-*/
-
-#if !FEATURE_SERIALIZABLE
-using Newtonsoft.Json;
-using Newtonsoft.Json.Linq;
-using System;
-using System.Linq;
-using System.Reflection;
-
-namespace Lucene.Net.Facet.Taxonomy.WriterCache
-{
-    internal class CharBlockArrayConverter : JsonConverter
-    {
-        private const string BLOCK_SIZE = "blockSize";
-        private const string CONTENTS = "contents";
-
-        public override bool CanConvert(Type objectType)
-        {
-            return typeof(CharBlockArray).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo());
-        }
-
-        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
-        {
-            var jsonObect = JObject.Load(reader);
-            var properties = jsonObect.Properties().ToArray();
-
-            int blockSize = -1;
-            string contents = null;
-
-            Func<bool> arePropertiesSet = () => blockSize > 0 && !string.IsNullOrEmpty(contents);
-
-            foreach (var property in properties)
-            {
-                if (property.Name.Equals(CharBlockArrayConverter.BLOCK_SIZE, StringComparison.OrdinalIgnoreCase))
-                {
-                    blockSize = property.Value.Value<int>();
-                }
-                else if (property.Name.Equals(CharBlockArrayConverter.CONTENTS, StringComparison.OrdinalIgnoreCase))
-                {
-                    contents = property.Value.Value<string>();
-                }
-
-                if (arePropertiesSet())
-                {
-                    break;
-                }
-            }
-
-            if (!arePropertiesSet())
-            {
-                return null;
-            }
-
-            var deserialized = new CharBlockArray(blockSize);
-
-            deserialized.Append(contents);
-
-            return deserialized;
-        }
-
-        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
-        {
-            var charBlockArray = value as CharBlockArray;
-
-            if (charBlockArray == null)
-            {
-                return;
-            }
-
-            writer.WriteStartObject();
-
-            writer.WritePropertyName(BLOCK_SIZE);
-            serializer.Serialize(writer, charBlockArray.blockSize);
-
-            writer.WritePropertyName(CONTENTS);
-            serializer.Serialize(writer, charBlockArray.ToString());
-
-            writer.WriteEndObject();
-        }
-    }
-}
-#endif

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/3a959d5b/src/Lucene.Net/Support/IO/StreamUtils.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Support/IO/StreamUtils.cs b/src/Lucene.Net/Support/IO/StreamUtils.cs
deleted file mode 100644
index 965f50e..0000000
--- a/src/Lucene.Net/Support/IO/StreamUtils.cs
+++ /dev/null
@@ -1,51 +0,0 @@
-#if FEATURE_SERIALIZABLE
-using System.IO;
-using System.Runtime.Serialization.Formatters.Binary;
-
-namespace Lucene.Net.Support.IO
-{
-    /*
-	 * Licensed to the Apache Software Foundation (ASF) under one or more
-	 * contributor license agreements.  See the NOTICE file distributed with
-	 * this work for additional information regarding copyright ownership.
-	 * The ASF licenses this file to You under the Apache License, Version 2.0
-	 * (the "License"); you may not use this file except in compliance with
-	 * the License.  You may obtain a copy of the License at
-	 *
-	 *     http://www.apache.org/licenses/LICENSE-2.0
-	 *
-	 * Unless required by applicable law or agreed to in writing, software
-	 * distributed under the License is distributed on an "AS IS" BASIS,
-	 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-	 * See the License for the specific language governing permissions and
-	 * limitations under the License.
-	 */
-
-    public static class StreamUtils
-    {
-        static readonly BinaryFormatter formatter = new BinaryFormatter();
-
-        public static void SerializeToStream(object o, Stream outputStream)
-        {
-            formatter.Serialize(outputStream, o);
-        }
-
-        public static void SerializeToStream(object o, BinaryWriter writer)
-        {
-            formatter.Serialize(writer.BaseStream, o);
-        }
-
-        public static object DeserializeFromStream(Stream stream)
-        {
-            object o = formatter.Deserialize(stream);
-            return o;
-        }
-
-        public static object DeserializeFromStream(BinaryReader reader)
-        {
-            object o = formatter.Deserialize(reader.BaseStream);
-            return o;
-        }
-    }
-}
-#endif


Mime
View raw message