lucene-lucene-net-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jon Davis (JIRA)" <j...@apache.org>
Subject [jira] Issue Comment Edited: (LUCENENET-103) Need to implement ICloneable on RAMDirectory
Date Tue, 16 Oct 2007 21:44:50 GMT

    [ https://issues.apache.org/jira/browse/LUCENENET-103?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12535347
] 

stimpy77 edited comment on LUCENENET-103 at 10/16/07 2:44 PM:
---------------------------------------------------------------

I should add, the objective for cloning was to make it more performant. The Directory copy
approach was slower. For our purposes, the difference was 1299ms for Directory.Copy, versus
669ms for a deep clone, and 9.66ms for a shallow clone (and a LOT less RAM usage). We are
going with a shallow clone because this is a multi-threaded server and there are thread locks
all over the Lucene objects, but we don't modify a RAMDirectory once it is loaded. Rather,
we rebuild the RAMDirectory in the equivalent of a cron job, then clone it across multiple
threads.

In my environment I implemented ICloneable already, using the referenced hyperlink.

{{
namespace Lucene.Net.Store
{
 ...	
	public sealed class RAMDirectory : Directory, ICloneable
	{
...

        #region ICloneable Members
        /// <summary>
        /// Creates a clone of the RAMDirectory.
        /// If <paramref name="deep"/> is true, the clone is made 
        /// at the byte level. 
        /// If <paramref name="deep"/> is false, the clone is made
        /// at the buffers' references level.
        /// </summary>
        /// <param name="deep">
        /// If <paramref name="deep"/> is true, the clone is made 
        /// at the byte level. 
        /// If <paramref name="deep"/> is false, the clone is made
        /// at the buffers' references level.
        /// </param>
        /// <returns>A RAMDirectory object.</returns>
        public object Clone(bool deep)
        {
            RAMDirectory clone = new RAMDirectory();
            System.Collections.IDictionaryEnumerator enmr = files.GetEnumerator();
            while (enmr.MoveNext())
            {
                string name = (string)enmr.Key;
                RAMFile rf = (RAMFile)enmr.Value;
                clone.files.Add(name, rf.Clone(deep));
            }
            return clone;
        }

        public object Clone()
        {
            return Clone(false);
        }

        #endregion
    }

	class RAMFile : ICloneable
	{

...

        #region ICloneable Members

        /// <summary>
        /// Creates a clone of the RAMFile.
        /// If <paramref name="deep"/> is true, the clone is made 
        /// at the byte level. 
        /// If <paramref name="deep"/> is false, the clone is made
        /// at the buffers' references level.
        /// </summary>
        /// <param name="deep">
        /// If <paramref name="deep"/> is true, the clone is made 
        /// at the byte level. 
        /// If <paramref name="deep"/> is false, the clone is made
        /// at the buffers' references level.
        /// </param>
        /// <returns>A RAMFile object.</returns>
        public object Clone(bool deep)
        {
            RAMFile clone = new RAMFile();
            if (!deep)
            {
                clone.buffers = (System.Collections.ArrayList) buffers.Clone();
            }
            else
            {
                clone.buffers = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
                for (int i = 0; i < buffers.Count; i++)
                {
                    byte[] buf = (byte[])buffers[i];
                    byte[] cloneBuf = (byte[])buf.Clone();
                    clone.buffers.Add(cloneBuf);
                }
            }
            clone.length = length;
            clone.lastModified = lastModified;
            return clone;
        }
        /// <summary>
        /// Performs a shallow copy of the object.
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            return Clone(false);
        }

        #endregion
    }
}
}}

      was (Author: stimpy77):
    I should add, the objective for cloning was to make it more performant. The Directory
copy approach was slower. For our purposes, the difference was 1299ms for Directory.Copy,
versus 669ms for a deep clone, and 9.66ms for a shallow clone (and a LOT less RAM usage).
We are going with a shallow clone because this is a multi-threaded server and there are thread
locks all over the Lucene objects, but we don't modify a RAMDirectory once it is loaded. Rather,
we rebuild the RAMDirectory in the equivalent of a cron job, then clone it across multiple
threads.

In my environment I implemented ICloneable already, using the referenced hyperlink.

namespace Lucene.Net.Store
{
 ...	
	public sealed class RAMDirectory : Directory, ICloneable
	{
...

        #region ICloneable Members
        /// <summary>
        /// Creates a clone of the RAMDirectory.
        /// If <paramref name="deep"/> is true, the clone is made 
        /// at the byte level. 
        /// If <paramref name="deep"/> is false, the clone is made
        /// at the buffers' references level.
        /// </summary>
        /// <param name="deep">
        /// If <paramref name="deep"/> is true, the clone is made 
        /// at the byte level. 
        /// If <paramref name="deep"/> is false, the clone is made
        /// at the buffers' references level.
        /// </param>
        /// <returns>A RAMDirectory object.</returns>
        public object Clone(bool deep)
        {
            RAMDirectory clone = new RAMDirectory();
            System.Collections.IDictionaryEnumerator enmr = files.GetEnumerator();
            while (enmr.MoveNext())
            {
                string name = (string)enmr.Key;
                RAMFile rf = (RAMFile)enmr.Value;
                clone.files.Add(name, rf.Clone(deep));
            }
            return clone;
        }

        public object Clone()
        {
            return Clone(false);
        }

        #endregion
    }

	class RAMFile : ICloneable
	{

...

        #region ICloneable Members

        /// <summary>
        /// Creates a clone of the RAMFile.
        /// If <paramref name="deep"/> is true, the clone is made 
        /// at the byte level. 
        /// If <paramref name="deep"/> is false, the clone is made
        /// at the buffers' references level.
        /// </summary>
        /// <param name="deep">
        /// If <paramref name="deep"/> is true, the clone is made 
        /// at the byte level. 
        /// If <paramref name="deep"/> is false, the clone is made
        /// at the buffers' references level.
        /// </param>
        /// <returns>A RAMFile object.</returns>
        public object Clone(bool deep)
        {
            RAMFile clone = new RAMFile();
            if (!deep)
            {
                clone.buffers = (System.Collections.ArrayList) buffers.Clone();
            }
            else
            {
                clone.buffers = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
                for (int i = 0; i < buffers.Count; i++)
                {
                    byte[] buf = (byte[])buffers[i];
                    byte[] cloneBuf = (byte[])buf.Clone();
                    clone.buffers.Add(cloneBuf);
                }
            }
            clone.length = length;
            clone.lastModified = lastModified;
            return clone;
        }
        /// <summary>
        /// Performs a shallow copy of the object.
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            return Clone(false);
        }

        #endregion
    }
}
  
> Need to implement ICloneable on RAMDirectory
> --------------------------------------------
>
>                 Key: LUCENENET-103
>                 URL: https://issues.apache.org/jira/browse/LUCENENET-103
>             Project: Lucene.Net
>          Issue Type: Improvement
>         Environment: C# 2.0
>            Reporter: Jon Davis
>            Priority: Minor
>
> IClonable needs to be added to Lucene.net's RAMDirectory.
> See Lucene (Java) item resolution at: 
> http://www.mail-archive.com/lucene-dev@jakarta.apache.org/msg03725.html

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message