views:

240

answers:

1

correlation use case:

read input 

if (correlation-id is already generated for this input) 
{ 
   lookup the correlation-id from the cache; 
   return correlation-id; 
} 

else 
{ 
   generate the correlation-id; 
   cache it; 
   return correlation-id; 
} 

Constraints: - The number of input records can go till 500K so doesn't want to use strong references. - Doesn't want to generate one-way hashes as of now (i know that if we use one-way hash then there is no need to cache)

Can someone tell me how to use ComputingMap for this. I am asking this because there is a note in the javadoc that says "it uses a identity equality for weak/soft keys".

+1  A: 

With Google Guava/Collection classes and soft or weak keys or values your keys needs to be strong references for the map to use equals() and not == to lookup cached values. If you have weak/soft keys then the lookup is done with identity so you'll always get a cache miss. So if you want the garbage collector to GC items from your cache then you'll need to make the values soft or weak.

I understand Google will add an Equivalence feature in the future so you could state if you want equals() or == and not have this choice made for you by picking strong, weak or soft references.

Since your Tuple object implements equals() and hashCode(), then you just do

new MapMaker()
    .softValues()
    .makeComputingMap<Tuple,String>(new Function[Tuple,String] {
                                         public String apply(Tuple t) {
                                             // generate the correlation-id
                                         }
                                    });
Blair Zajac
so u r suggesting to have strong keys and soft values. strong keys becaz i want to use equals() and soft values to take advantage of memory efiicieny...but i still doesnt understand why identity equality is being used by default. can someone pls explain
Pangea
Yes. That's a draw back of Google's MapMaker is you don't get to choose the equivalence test: strong => equals, weak or soft => ==. I understand they are working on it.You can use extra166y from the JDK concurrency group and use its CustomConcurrentHashMap. It provides additional Equivalence you can use.One drawback from using extra166y is that it doesn't unload itself in a web application context and there's no way to stop the collection thread.http://gee.cs.oswego.edu/dl/concurrency-interest/
Blair Zajac
It's not drawback. It's feature. They said that a map with soft/weak keys are simply wrong. Check Joshua's comment on video about Google Collections. And it's not that hard to remove that feature and using the equals back. You can see my modified `MapMaker.jar` at the bottom of this article: http://satukubik.com/2009/11/13/computing-map-on-google-collections/
nanda
Agreed, most of of the time that's an indication of a bad design. The only time I needed weak keys with equals() is for an interner.
Blair Zajac
i am not sure why weak keys is a bad design. can u try to explain pls
Pangea
@Pangea, Because the weak key can disappear due to garbage collection while you are holding another key that `equals()` the weak key, so your key seems to disappear from the `keySet` even though you are holding a strong reference to it.
finnw