Sign in to follow this  
Followers 0
tsichevski

Persistent analog for Java HashSet?

15 posts in this topic

Hi,

I can use the value returned by Storage.createHash() to obtain a persistent analog for java.util.HashMap. Is there a similar method to create a persistent variant of java.util.HashSet?

Regards,

Vladimir

Share this post


Link to post
Share on other sites

You can use either Storage.createHash or Storage.createMap methods to create a Perst analog of java.util.Map

Storage.createSet() creates Perst implementation of java.util.Set

Share this post


Link to post
Share on other sites

You can use either Storage.createHash or Storage.createMap methods to create a Perst analog of java.util.Map

Storage.createSet() creates Perst implementation of java.util.Set

I need the kind of java.util.set.Set that uses hashCode() and equals() to store and look up objects. Perst uses these methods when it deals with keys of Maps created with the Storage.createHash().

The collections created with the Storage.createSet() use supposedly the OID value for both purposes. So I can add to Storage.createSet() any number of newly-created (no OID allocated yet) objects than are equivalent in the hashCode()/equals() sense.

Regards,

Vladimir

Share this post


Link to post
Share on other sites

You can either redefine hashCode/equals methods in the classes you are gowing to put in hash (so that they do not depend on OID), or use the Storage.makePersistent method to force assignment of OID to these objects. You might wonder why Storage.makePersistent is not invoked automatically for PersistentHashImpl key. Perhaps it should:

Index: src/org/garret/perst/impl/PersistentHashImpl.java
===================================================================
--- src/org/garret/perst/impl/PersistentHashImpl.java (revision 16367)
+++ src/org/garret/perst/impl/PersistentHashImpl.java (working copy)
@@ -149,6 +149,7 @@

public V put(K key, V value)
{
+ getStorage().makePersistent(key);
int hashCode = key.hashCode();
HashPage pg = root;
if (pg == null) {


The original assumption was that equality function for hash map key rarely depends on OID, otherwise it is not clear how to use it for lookup. The only exception is when hash map is used to implement a set. But PersistentSet it based on B-Tree and here object is assigned OID during insertion.

Share this post


Link to post
Share on other sites

No, what I need is quite opposite. In my application model I use sets of objects which are supposed to be compared by their natural keys, not by OID. So two objects are EQUAL if their key parts are equal, regardless of are these objects persistent or not.

In pseudo-code it looks like the following:

class MyObject {
  OtherObject keyPart1;
  String keyPart2;
  equals(MyObject other) {
    return other.keyPart1.equals(keyPart1)
      && keyPart2.equals(keyPart2);
  }

  hashCode() {
    return keyPart1.hashCode() * 31 + keyPart2.hashCode();
  }
}

Set persistentSet = Storage.createTruePersistentHashSet(storage);
OtherObject keyPart1 = new OtherObject(...);
String keyPart2 = "someString";
persistentSet.add(new MyObject(keyPart1, keyPart2));

// Must return TRUE, must NOT make the new MyObject persistent
persistentSet.contains(new MyObject(keyPart1, keyPart2));

Share this post


Link to post
Share on other sites

We do not see a better solution for the problem than extracting the key (for example as string) and inserting this object in normal index using this key. Any other solution will require fetching objects during search

Share this post


Link to post
Share on other sites

We do not see a better solution for the problem than extracting the key (for example as string) and inserting this object in normal index using this key. Any other solution will require fetching objects during search

Is it possible to implement the Set on top of the PersistentHashImpl using the keys and ignoring the values? This would be exactly the same way the java.util.HashSet is implemented. It shall not require object fetching during the search.

Share this post


Link to post
Share on other sites

Yes, it is obviously possible. But we don't want to change the implementation of set which is returned now by the Storage.createSet method - it will break backward compatibility.

So we added a Storage.createHashSet method. Attached patch contains this (rename to hashset.patch, if necessary)

hashset.txt

Share this post


Link to post
Share on other sites

We don't have a release date. However, there is a mailing list that we maintain, that we sent notification to when a new release is made. I'll add you to that list. (You can remove yourself, anytime.)

Share this post


Link to post
Share on other sites

We don't have a release date. However, there is a mailing list that we maintain, that we sent notification to when a new release is made. I'll add you to that list. (You can remove yourself, anytime.)

Great! Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0