Sorry, but I don't use Java. I will give you the Python version, it's pretty
straight forward. As I said before, any improvements in my design or code
will be welcome =)

class User(Model):
  # for easy of use, i will consider user_id as the key name of the entity
  name, email, etc
  num_of_followers = IntegerProperty(default=0)

class FollowerInfo(Model):
  master = StringProperty() I've used String property to store the key_name,
but you can use KeyProperty if you prefer. This will be the super-star
posting new things
  followers = ListProperty() # or arrays, the other thing MiuMeet proposed
  full = BooleanProperty() # whether we can store more followers here or not

class Post(Model):
  content, author, etc...

class PostNotify(Model):
  # key name/key_id of the entity will be key_name/key_id of the post
  users = ListProperty()


*User X follows User Y:*

# Check if there is already a FollowerInfo model, and hasn't got more than
100 followers or so.
follower = Query(..).filter('full = ', False).filter('master',
UserHere).fetch(1)
if not follower: # create one if we need it
  follower = FollowerInfo(parent=UserHere, followers=[,])

# Add follower, check if the info is full too
follower.followers.append(FollowerUserHere)
if len(follower.followers) >= 100:
  follower.full = True

follower.put() # save

# maybe this must be in a transaction?
user.num_of_followers += 1
user.put()

*
*
*User X doesn't follow User Y anymore:*
*
*
follower = Query(...).filter('followers = ', FollowerUserHere).fetch(1)
follower.followers.delete(FollowerUserHere)

# check if the model was full, now it will have one empty slot more
if follower.full and len(follower.followers) < 100:
  follower.full = False

follower.put() # save

# again, maybe this must be in a transaction?
user.num_of_followers -= 1
user.put()


*List followers:*
*
*
# I fetch one of the following info models. It may be null (no followers),
one model(1-100 followers). If the user has more following info models
(100-infinite), it will return only one of them.
follower = Query(..).filter('master =', UserHere).fetch(1)
if follower:
  # List here the follower.followers


*Post:*
*
*
if UserHere.num_of_followers > 100:
  taskqueue....() # add new task to process it offline, too much followers!
# process it inline here otherwise (same code as in the task)

TASK:

 - Get each one of the FollowerInfo models for the user
 - Create a PostNotify with the key_name/key_id of the post for each 4500 or
so, followers.
 - Save all the PostNotify entities


*List notifications:*
*
*
# I run a keys-only query, finding the first 50 (or whatever you want
notifications for this user).
# As you don't have to deserialize the list (keys-only), you'll never incur
in that perfomance cost. The key has all the info you need (the post
key_name/key_id, it's exactly the same)
notifications = Query(entity=PostNotify, keys_only=True).filter('users =',
CurrentUserHere).fetch(50)

ids = []
for notification in notifications
  ids.append(notification.key_name)

# Now you have all the posts
posts = db.get(ids)







2011/8/3 Bruno Sandivilli <[email protected]>

> So, if an user X adds and User Y, the user Y will become an ancestor of X ?
> And if the user Y adds the user W too, so user X will have two
> ancestors(???) ? Any code snippets will be greatful.
> Thanks again, for the help guys.
>
>
> 2011/8/2 MiuMeet Support <[email protected]>
>
>> By the way, have a look at:
>> http://devblog.miumeet.com/2011/08/much-more-efficient-implementation-of.html
>>
>> It's much more efficient than db.ListProperty(int)
>>
>> Cheers,
>> -Andrin
>>
>>
>> On Thu, Jul 28, 2011 at 6:17 PM, Pascal Voitot Dev <
>> [email protected]> wrote:
>>
>>> good idea also :)
>>>
>>>
>>> On Wed, Jul 27, 2011 at 7:17 PM, Ernesto Oltra 
>>> <[email protected]>wrote:
>>>
>>>> And for followers, you could too shard the lists. You can have several
>>>> entities, each with, about 100 results or so (or 1000, or 2000, I prefer 
>>>> 100
>>>> for easy of serializing/deserializing). All these would have the user as
>>>> ancestor. When listing, take only one entity, deserializing its lists (only
>>>> 100 results) and show some of them. When listing all, you can use cursors
>>>> and some tricks to have the job done (job = paging =) )
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Google App Engine" group.
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msg/google-appengine/-/tRQCOATsWdUJ.
>>>>
>>>> To post to this group, send email to [email protected].
>>>> To unsubscribe from this group, send email to
>>>> [email protected].
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/google-appengine?hl=en.
>>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Google App Engine" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected].
>>> For more options, visit this group at
>>> http://groups.google.com/group/google-appengine?hl=en.
>>>
>>
>>   --
>> You received this message because you are subscribed to the Google Groups
>> "Google App Engine" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/google-appengine?hl=en.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-appengine?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en.

Reply via email to