0

I have a data structure where I have objects, and they have targets. and they have a many to many relationship. For eg: object1 has targets: t1, t2, t3 and object2 has targets: t2, t3.

I want to write a Guava LoadingCache in order to store each of these objects by target. Now, the problem with this is: Objects 1 and 2 both will be stored under t2, and t3. And this is wastage.

So, one solution I thought of was to have another map of ids. And in the target cache, I'll store the object by their id.

Now, the question is: Since in a LoadingCache we dont have methods exposed that take care of clearing the cache(or a way to listen to when the cache gets cleared.), I have no way of keeping the id map upto date, when the cache actually gets updated.

Is there a good way of keeping these two maps in sync?

SherinThomas
  • 1,881
  • 4
  • 16
  • 20
  • I think I have a rough idea of what you want to achieve, however, it would be better to have simplified code as example. My specific question at the moment is: What needs to be kept in sync? Is just value changing or the target association? Maybe a description of the original problem scenario would be great also to avoid the XY-Problem. – cruftex Jun 20 '14 at 12:48

1 Answers1

3

I question your premises, specifically that "this is wastage." You're storing multiple references to objects, not multiple copies of the objects themselves, and references are cheap. Having another map of ids is likely to be strictly more expensive than your original approach in the first place.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Louis, I think the assumption is not correct, since the loading cache will construct a new instance for each target. The loader could, however, query the cache in turn whether there is a mapping on an "alias" target.... – cruftex Jun 20 '14 at 12:43
  • @cruftex, it is not a given that the cache must construct a new instance for each target, and only the OP can tell us which is the case. To me, it sounds like the cache is probably not constructing a new instance for each target. – Louis Wasserman Jun 20 '14 at 15:30
  • So the cache will look like this >, and immutablelist makes a new copy, as far as I know. So it actually does make copies of the objects. Or am I missing something here? – SherinThomas Jun 20 '14 at 16:58
  • What? ImmutableList doesn't copy objects added to it. There's no way in Java to universally copy objects. – Louis Wasserman Jun 20 '14 at 16:59
  • AFAIK ImmutableList does'nt store by reference, because if I have the reference to the object, and if I change it. Then the list is not immutable anymore. And I did run a small program to check if changes made to the object using its reference, is reflected in the object stored in the list or not. And it does not. I am new to this, so it'd be great if you can explain this in a bit more detail. – SherinThomas Jun 20 '14 at 17:05
  • 1
    @SThomas, that's simply not the case. ImmutableList does store by reference, and you can change its elements if they're of a mutable type. It seems likely that you either misunderstand something or that your program is incorrect. See e.g. http://stackoverflow.com/q/2185789/869736 for some explanation. – Louis Wasserman Jun 20 '14 at 17:14