[infinispan-dev] Questions on AtomicMap and improvement proposals

Manik Surtani manik at jboss.org
Thu Apr 14 10:30:04 EDT 2011


Apologies for the late response.

On 13 Apr 2011, at 10:38, Emmanuel Bernard wrote:

> In exchange for answers, I will improve at least the JavaDoc and maybe create a wiki from this info.

Sounds like a fair exchange.  :-)

> I've tried to search the wiki but found nothing on AtomicMap (only forum posts) and I've read the AtomicMap JavaDoc but I am still a bit unsure of what's going on.
> 
> When I need to create an AtomicMap, I must use 
> AtomicMap<String, Object> resultset = AtomicMapLookup.getAtomicMap(cache, "my_atomic_map_key");

Well, AtomicMapLookup.getAtomicMap(cache, myAtomicMapName).

> Questions
> Here are a few questions for you
> 
> 1. How can I apply advanced flags to get or create the AtomicMap?
> cache.withFlag(SKIP_CACHE_STORE,FORCE_ASYNCHRONOUS).get("my_atomic_map_key");

You can't right now; as a "dirty hack", you can just do what getAtomicMap() does internally, adding the flags you need.

https://github.com/infinispan/infinispan/blob/master/core/src/main/java/org/infinispan/atomic/AtomicMapLookup.java#L49

The real solution: create a feature request on JIRA so that we can add flags to getAtomicMap() as well.

> 3. Is that legal to use other cache methods like replace on an AtomicMap value, what are the consequences?

cache.putIfAbsent() is used internally by getAtomicMap().  replace() is harder, since it may mean that a thread may be storing stuff in an AtomicMap that has been replaced, so those changes get lost.  Why would you need to replace() an atomic map?

> 3. When I expect a map to be present in the key, can I use cache.get("my_atomic_map_key") and still benefit from my properly proxied AtomicMap? Or am I forced to use AtomicMapLookup.getAtomicMap?

You need to use AML.getAtomicMap() to make sure your proxy is properly created/wrapped.  See:

https://github.com/infinispan/infinispan/blob/master/core/src/main/java/org/infinispan/atomic/AtomicMapLookup.java#L49

> 4. Using AtomicMapLookup.getAtomicMap, how can I differentiate an empty key where a new map has been created from a retrieval of an empty map stored in the key?

You could use getAtomicMap(cache, mapName, createIfAbsent) so you get a null if createIfAbsent == false and no such atomic map exists.  Does this help?

> 5. Must I use AtomicMapLookup.remove method to remove an atomic map?

You can just use cache.remove(), but for the sake of clean generics, it is better to go via AML.remove().

> 6. Can I clone an AtomicMap?

No, it's not cloneable, since a clone that is not stored in the cache (in a threadsafe manner) is meaningless, and Java's clone() API doesn't allow you to pass in a new name for the clone.

However, we could provide a AtomicMap.clone(nameOfClone) method, which could take care of this (internally call AML.getAtomicMap(nameOfClone) and copy contents across).

> For using methods like replace(), it seems one would need to clone the atomicmap to compare it to the initial value. When I tried briefly Iw as unsure I could do that.

Yep.

> Proposed improvements
> 
> I'm asking all the questions because these can potentially make AtomicMap users life quite hard and abstracting Infinispan from OGM will make a damn ugly interface / contract
> 
> 1. Would it be possible to let people get their proxied AtomicMap from cache.get() and other get methods?
> It seems that either the marshaller or the get operations (probably the get ops as local cache should work as well) should be able to intercept an AtomicMap and essentually do what AtomicMapLookup is doing (and make sure it is properly wrapped).
> If that's possible that would be already a big benefit for the user as it would only be limited to AtomicMap creation or cloning

Fair point, certain AML code could be moved into the CacheDelegate.  But I fear a lot of instanceof checking which may be unnecessary if using the cache to store anything other than AtomicMaps.

> 2. Would it be possible to let people create standalone AtomicMaps and associate them to the cache lasily and in a transparent fashion.
> AtomicMap<String, Object> resultSet = AtomicMapLookup.createAtomicMap();
> cache.put(key, resultSet); //lazily set cache, deltaMapKey, batchContainer and icc for new AtomicMaps

The problem there is that the put() won't be atomic.  You'd need to mandate a putIfAbsent, but that means any initialization of the AtomicMap (setting values) could potentially be wasted.  This is why the AML registers the AtomicMap in the cache before allowing user code to store values.

> 3. Could we guarantee some equality between Map and AtomicMap?
> if I do Map 
> 
> map = new HashMap(atomicMap);
> atomicMap.put("key", "value");
> cache.replace(key, map, atomicMap);
> 
> Am I guaranteed that it can work (if no one has pushed a change behind my back)?

Not at the moment, but this is something we could possibly do.

Hope this helps
Manik
--
Manik Surtani
manik at jboss.org
twitter.com/maniksurtani

Lead, Infinispan
http://www.infinispan.org



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/infinispan-dev/attachments/20110414/ad4c4c66/attachment.html 


More information about the infinispan-dev mailing list