freekid2000 freekid2000 - 1 year ago 49
Java Question

How to associate list of Keys to an Entity

Basically I have a User entity and that user will have followers. One possible way I could think of on how to model this is to have a multi-valued property of type Key, which will store Keys of the followers in a list.

Like this..

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

Entity userEntity;

Key k = entity.getKey();

Entity user = new Entity("User");
List<Key> followers = new ArrayList<Key>();
user.setProperty("FollowersList", followers);

I read somewhere that App Engine limits you to 5,000 entries in a multi-valued property which is something I absolutely do not want.

I am new to NoSQL databases in general. How should I do this?

Answer Source

Storing a list of values is not a good idea if the list is indexed or changes frequently.

Every change will trigger re-saving the entity, which will lead to unnecessary write costs. Now Google changed their pricing model, so it's not as expensive as it used to be, but it still something to keep in mind.

Also, the user entity itself becomes large - you will move around all this data every time you need this entity even though in most use cases you don't need a list of 5,000 followers.

One possible solution is to create an entity kind called "Follower". Entities of these kind will have no properties (unless you need to store some additional data about each relationship). These entities will be child entities of user entities, and their ID will match an ID of a follower.

Key userKey = KeyFactory.createKey("User", userID);
Entity followerEntity = new Entity("Follower", followerUserID, userKey);

Now you can quickly retrieve the list of all followers by running a keys-only (the cheapest and fastest option possible) ancestor query:

Query q = new Query("Follower").setAncestor(userKey).setKeysOnly();

Don't forget to delete "Follower" entities if you delete "User" entity that is their parent - it won't happen automatically, they will simply remain orphaned.