clcron / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
1 stars 0 forks source link

Extension proposal for MapMaker#makeComputingMap: a get method without computation #581

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The computing map returned by MapMaker#makeComputingMap does not provide a 
possibility to query a value for a given key without computing it if it is not 
present (i.e. like the get method of common maps). However, there are many use 
cases where the client code would require such a possibility rather than having 
to compute a "dummy" value which is possibly never used again. I can provide 
examples of such use cases upon request. With existing implementation, the 
application code in such cases either needs to check for existence of the value 
using Map#containsKey (guarded by external lock) or simply uses a common 
(concurrent) map instead. 
There is simple way how to add such a possibility without breaking up the 
legacy code using MapMaker#makeComputingMap and with only little changes to 
MapMaker and other affected classes.
First, a simple interface ComputingConcurrentMap should be introduced in order 
to distinguish a "normal" ConcurrentMap from a computing one. This interface 
extends ConcurrentMap by the method getNoCompute(Object) actually providing the 
means to query for a value without computation (but taking into account 
possible parallel computation or put for the same key). Then, the result type 
of MapMaker#makeComputingMap should be changed to the new interface. Last but 
not least, the underlying implementation of MapMaker must be adapted to reflect 
the API changes.
I have already coded the proposed changes and attached following files with the 
result:
- ComputingConcurrentMap.java: the new interface;
- MapMaker.java: changed MapMaker class;
- ComputingConcurrentHashMap.java: changed ComputingConcurrentHashMap class;
- TestConcurrentComputingHashMap.java: a JUnit test case for the changes.
The altered code in changed classes is marked with comments starting with "XXX 
Max:". There are as much as 5 small changes in both existing classes. 

Original issue reported on code.google.com by mirochni...@googlemail.com on 27 Mar 2011 at 9:44

Attachments:

GoogleCodeExporter commented 9 years ago
The rest of the files mentioned in the issue :) 

Original comment by mirochni...@googlemail.com on 27 Mar 2011 at 9:47

Attachments:

GoogleCodeExporter commented 9 years ago
This will become possible with the new Cache interface we're working on.

Original comment by fry@google.com on 28 Mar 2011 at 12:03

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 8 Apr 2011 at 2:06

GoogleCodeExporter commented 9 years ago
Cache will be in release 10, at which point it will be possible to call 
cache.asMap().get(key) to accomplish this.

Original comment by cpov...@google.com on 20 Jun 2011 at 9:29

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:14

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09