You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by "Michael McCandless (JIRA)" <ji...@apache.org> on 2007/01/14 13:08:27 UTC

[jira] Commented: (LUCENE-776) Use WeakHashMap instead of Hashtable in FSDirectory

    [ https://issues.apache.org/jira/browse/LUCENE-776?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12464589 ] 

Michael McCandless commented on LUCENE-776:
-------------------------------------------

Good catch!  And thanks for opening this.

But: how would we actually use WeakHashMap here?

With WeakHashMap, it's the keys that are the weak reference, so we'd
need to have our FSDirectory instance be the keys (I think?).  But
then I don't see how we could cleanly "look up" a given key to see if
we already have an FSDirectory instance for the incoming canonical
path?

Maybe instead we could keep the current Hashtable, but instead of
using the FSDirectory instance directly as the value, wrap it in a
WeakReference (and unwap on looking it up, later)?

One other small concern is: the current "reference counting" approach
has nice "immediacy".  Meaning once the final close() occurs on the
FSDirectory instance, it is immediately removed from the DIRECTORIES
hashtable.  Whereas, with a WeakReference, it would generally be a
much slower thing (I think?) because it's up to GC to decide when it
will actually be removed once only weak references remain?

Though: even with the "immediacy" of reference counting, we still must
then wait for GC to actually clean up the FSDirectory instance we had
removed.  Worse, if that path is re-opened we would create another
FSDirectory instance (ie more garbage to be collected).  So, since we
must wait for GC anyway with our current reference counting solution,
I guess it is in fact better to have a WeakReference so that if a
given path is re-used, and it hasn't been GC'd yet, we would re-use
it?


> Use WeakHashMap instead of Hashtable in FSDirectory
> ---------------------------------------------------
>
>                 Key: LUCENE-776
>                 URL: https://issues.apache.org/jira/browse/LUCENE-776
>             Project: Lucene - Java
>          Issue Type: Improvement
>    Affects Versions: 2.0.0
>            Reporter: Nicolas Lalevée
>            Priority: Trivial
>
> I was just reading the FSDirectory java code, then I found this :
>   /** This cache of directories ensures that there is a unique Directory
>    * instance per path, so that synchronization on the Directory can be used to
>    * synchronize access between readers and writers.
>    *
>    * This should be a WeakHashMap, so that entries can be GC'd, but that would
>    * require Java 1.2.  Instead we use refcounts...
>    */
>   private static final Hashtable DIRECTORIES = new Hashtable();
> Since Lucene is now requiring at least 1.2 (for ThreadLocal for instance, which is using BTW some WeakHashMap), maybe it is time to change ?

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

       

---------------------------------------------------------------------
To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-dev-help@lucene.apache.org