[appengine-java] Re: Best practice modeling a 1:n user-relationship with List properties

2010-08-27 Thread Frederik Pfisterer
Hi,

my recommendation would be to use List . In your reasoning for
query 2 be aware of the lazy loading that JDO performs in the
background to retrieve the related objects.
Query one and two are similar (if nickname is a field of MyAppUser):
Query q = pm.newQuery(MyAppUser.class);
q.setFilter("userKey == userKeyParam");
q.declareParameters("String userKeyParam");

q.execute(appUserKey);

will give you a list of his friends.

Hope this helps,
Fred

On 24 Aug., 12:53, androidDeveloper  wrote:
> Hi all,
>
> I am trying to model a 1:n relationship between different "myAppUser"
> instances on Google App Engines Datastore. MyAppUser has a reference
> to 0...n other users (friends).
> MyAppUser is currently defined as showed below:
>
> class MyAppUser {
> @PrimaryKey
> @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
> @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value =
> "true")
> private String primaryKey;
>
> @Persistent
> private User user
>
> @Persistent
> private List friendsList;  Or alternative private List
> friendsList;
> ...
>
> }
>
> I have two queries:
> Query 1: I want to select all MyAppUsers where a specific User is in
> the friendsList.
> Query 2: I want to get all nicknames of the friends of a specific
> MyAppUser. This can be achieved by getting a MyAppsUser by id and then
> calling for each user user.getNickname().
>
> Question query 1: Is a query with a filter on "List friendsList"
> faster or slower than a query with a filter on "List
> friendsList" or is it equal?
>
> For query 2 I would say that a "List friendsList" is faster,
> because I have direct access to the nicknames. With only the id stored
> in the friendsList, I have to make another query for each friend in
> the list to get his nickname.

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.



[appengine-java] Re: Best practice modeling a 1:n user-relationship with List properties

2010-08-27 Thread androidDeveloper
Hi,

>be aware of the lazy loading that JDO performs
Question 1: Do we have lazy loading on lists of Core Value Types like
Google account (com.google.appengine.api.users.User) too?

Question 2a:
If a myAppUser has for example 1000 friends. Then with List I
would have to read the referenced myAppUser's for each userId in the
list to get the nicknames. To get all nicknames in one query I would
have to put a where condition on the userId of each user and
concatenate the conditions with "Or, ||". I read, that a query has a
limit of 30 "Ors". So I would have to split the Query in 30-packages.
Is this a good solution or would it be better to hold the nicknames in
a separate denormalized list in myAppUser? (Problem with the
denormalized nickname-list would be, that I would have to update the
list each time a user changes his nickname)

Question 2b:
Would it be too much overhead to read the nicknames in a separate
Query for each userId? Or is this a bad solution because of the
datastore calling overhead?

Question 3:
Is it possible to put a query-filter on a subfield of a Google account
user object (for example the nickname-field)? In my tests I was not
successful...


On 27 Aug., 11:13, Frederik Pfisterer  wrote:
> Hi,
>
> my recommendation would be to use List . In your reasoning for
> query 2 be aware of the lazy loading that JDO performs in the
> background to retrieve the related objects.
> Query one and two are similar (if nickname is a field of MyAppUser):
> Query q = pm.newQuery(MyAppUser.class);
> q.setFilter("userKey == userKeyParam");
> q.declareParameters("String userKeyParam");
>
> q.execute(appUserKey);
>
> will give you a list of his friends.
>
> Hope this helps,
> Fred
>
> On 24 Aug., 12:53, androidDeveloper  wrote:
>
> > Hi all,
>
> > I am trying to model a 1:n relationship between different "myAppUser"
> > instances on Google App Engines Datastore. MyAppUser has a reference
> > to 0...n other users (friends).
> > MyAppUser is currently defined as showed below:
>
> > class MyAppUser {
> > @PrimaryKey
> > @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
> > @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value =
> > "true")
> > private String primaryKey;
>
> > @Persistent
> > private User user
>
> > @Persistent
> > private List friendsList;  Or alternative private List
> > friendsList;
> > ...
>
> > }
>
> > I have two queries:
> > Query 1: I want to select all MyAppUsers where a specific User is in
> > the friendsList.
> > Query 2: I want to get all nicknames of the friends of a specific
> > MyAppUser. This can be achieved by getting a MyAppsUser by id and then
> > calling for each user user.getNickname().
>
> > Question query 1: Is a query with a filter on "List friendsList"
> > faster or slower than a query with a filter on "List
> > friendsList" or is it equal?
>
> > For query 2 I would say that a "List friendsList" is faster,
> > because I have direct access to the nicknames. With only the id stored
> > in the friendsList, I have to make another query for each friend in
> > the list to get his nickname.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.



[appengine-java] Re: Best practice modeling a 1:n user-relationship with List properties

2010-08-29 Thread Frederik Pfisterer
Hi,

from experience with GAE + JDO I'd recommend using List since
List currently has some issues you'll encounter down the road.
If your list is actually List you'd have an object graph
that can currently only be achieved with List.

Also in your reasoning for query 2 you have to consider what JDO does
in the background: for what you call "direct access" it is actually
performing a lookup, and lazy loading the objects upon access.

To achieve what you want I'd do the following:
query 1: query on keys (it's an order of magnitude faster, as it
doesn't need to actually retrieve the objects, only keys from the
index):
Query q = pm.newQuery("select primaryKey from " +
MyAppUser.class.getName());
q.setFilter("primaryKey == userKeyParam");
q.declareParameters("String userKeyParam");
Execution gives you the List of keys of the "friends".

query 2: same as above but with actually retreiving the object instead
of the key:
Query q = pm.newQuery(MyAppUser.class);
execution gives you the List you can iterate through and
get the nicknames.

In general: Use List and you'll save yourself a lot of
hassle ;-)

Hope this helps,
Fred

On 24 Aug., 12:53, androidDeveloper  wrote:
> Hi all,
>
> I am trying to model a 1:n relationship between different "myAppUser"
> instances on Google App Engines Datastore. MyAppUser has a reference
> to 0...n other users (friends).
> MyAppUser is currently defined as showed below:
>
> class MyAppUser {
> @PrimaryKey
> @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
> @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value =
> "true")
> private String primaryKey;
>
> @Persistent
> private User user
>
> @Persistent
> private List friendsList;  Or alternative private List
> friendsList;
> ...
>
> }
>
> I have two queries:
> Query 1: I want to select all MyAppUsers where a specific User is in
> the friendsList.
> Query 2: I want to get all nicknames of the friends of a specific
> MyAppUser. This can be achieved by getting a MyAppsUser by id and then
> calling for each user user.getNickname().
>
> Question query 1: Is a query with a filter on "List friendsList"
> faster or slower than a query with a filter on "List
> friendsList" or is it equal?
>
> For query 2 I would say that a "List friendsList" is faster,
> because I have direct access to the nicknames. With only the id stored
> in the friendsList, I have to make another query for each friend in
> the list to get his nickname.

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.



[appengine-java] Re: Best practice modeling a 1:n user-relationship with List properties

2010-08-30 Thread androidDeveloper
thanks a lot for the reply!

One last question:

> query 2: ...
> Query q = pm.newQuery(MyAppUser.class);
> execution gives you the List you can iterate through and
> get the nicknames.

What if there would be many instances of "MyAppUser" in datastore.
Think of 1 Million or more. Then I would have to iterate over 1
million records each time I collect the nicknames of max 10-50 users.
Would it not be better to keep some denormalized fields in MyAppUser
with nicknames or use "contains" in the query with the friendsIdlist?



On 27 Aug., 10:06, Frederik Pfisterer  wrote:
> Hi,
>
> from experience with GAE + JDO I'd recommend using List since
> List currently has some issues you'll encounter down the road.
> If your list is actually List you'd have an object graph
> that can currently only be achieved with List.
>
> Also in your reasoning for query 2 you have to consider what JDO does
> in the background: for what you call "direct access" it is actually
> performing a lookup, and lazy loading the objects upon access.
>
> To achieve what you want I'd do the following:
> query 1: query on keys (it's an order of magnitude faster, as it
> doesn't need to actually retrieve the objects, only keys from the
> index):
> Query q = pm.newQuery("select primaryKey from " +
> MyAppUser.class.getName());
>                 q.setFilter("primaryKey == userKeyParam");
>                 q.declareParameters("String userKeyParam");
> Execution gives you the List of keys of the "friends".
>
> query 2: same as above but with actually retreiving the object instead
> of the key:
> Query q = pm.newQuery(MyAppUser.class);
> execution gives you the List you can iterate through and
> get the nicknames.
>
> In general: Use List and you'll save yourself a lot of
> hassle ;-)
>
> Hope this helps,
> Fred
>
> On 24 Aug., 12:53, androidDeveloper  wrote:
>
> > Hi all,
>
> > I am trying to model a 1:n relationship between different "myAppUser"
> > instances on Google App Engines Datastore. MyAppUser has a reference
> > to 0...n other users (friends).
> > MyAppUser is currently defined as showed below:
>
> > class MyAppUser {
> > @PrimaryKey
> > @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
> > @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value =
> > "true")
> > private String primaryKey;
>
> > @Persistent
> > private User user
>
> > @Persistent
> > private List friendsList;  Or alternative private List
> > friendsList;
> > ...
>
> > }
>
> > I have two queries:
> > Query 1: I want to select all MyAppUsers where a specific User is in
> > the friendsList.
> > Query 2: I want to get all nicknames of the friends of a specific
> > MyAppUser. This can be achieved by getting a MyAppsUser by id and then
> > calling for each user user.getNickname().
>
> > Question query 1: Is a query with a filter on "List friendsList"
> > faster or slower than a query with a filter on "List
> > friendsList" or is it equal?
>
> > For query 2 I would say that a "List friendsList" is faster,
> > because I have direct access to the nicknames. With only the id stored
> > in the friendsList, I have to make another query for each friend in
> > the list to get his nickname.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.



[appengine-java] Re: Best practice modeling a 1:n user-relationship with List properties

2010-08-31 Thread Frederik Pfisterer
Sorry, for some reason my post got lost and then I posted another one
twice ... seems google groups had some issues.
You are unlikely to have 1 million friends, so the resultset over
which you'll iterate will be the 10-50 users in the list:
Mind the filter:

Query q = pm.newQuery(MyAppUser.class);
q.setFilter("userKey == userKeyParam");
q.declareParameters("String userKeyParam");
q.execute(appUserKey);

With the appUserKey you pass to q.execute you get the 10-50 users
which are your friends.
Does this make sense?


On 30 Aug., 08:42, androidDeveloper  wrote:
> thanks a lot for the reply!
>
> One last question:
>
> > query 2: ...
> > Query q = pm.newQuery(MyAppUser.class);
> > execution gives you the List you can iterate through and
> > get the nicknames.
>
> What if there would be many instances of "MyAppUser" in datastore.
> Think of 1 Million or more. Then I would have to iterate over 1
> million records each time I collect the nicknames of max 10-50 users.
> Would it not be better to keep some denormalized fields in MyAppUser
> with nicknames or use "contains" in the query with the friendsIdlist?
>
> On 27 Aug., 10:06, Frederik Pfisterer  wrote:
>
> > Hi,
>
> > from experience with GAE + JDO I'd recommend using List since
> > List currently has some issues you'll encounter down the road.
> > If your list is actually List you'd have an object graph
> > that can currently only be achieved with List.
>
> > Also in your reasoning for query 2 you have to consider what JDO does
> > in the background: for what you call "direct access" it is actually
> > performing a lookup, and lazy loading the objects upon access.
>
> > To achieve what you want I'd do the following:
> > query 1: query on keys (it's an order of magnitude faster, as it
> > doesn't need to actually retrieve the objects, only keys from the
> > index):
> > Query q = pm.newQuery("select primaryKey from " +
> > MyAppUser.class.getName());
> >                 q.setFilter("primaryKey == userKeyParam");
> >                 q.declareParameters("String userKeyParam");
> > Execution gives you the List of keys of the "friends".
>
> > query 2: same as above but with actually retreiving the object instead
> > of the key:
> > Query q = pm.newQuery(MyAppUser.class);
> > execution gives you the List you can iterate through and
> > get the nicknames.
>
> > In general: Use List and you'll save yourself a lot of
> > hassle ;-)
>
> > Hope this helps,
> > Fred
>
> > On 24 Aug., 12:53, androidDeveloper  wrote:
>
> > > Hi all,
>
> > > I am trying to model a 1:n relationship between different "myAppUser"
> > > instances on Google App Engines Datastore. MyAppUser has a reference
> > > to 0...n other users (friends).
> > > MyAppUser is currently defined as showed below:
>
> > > class MyAppUser {
> > > @PrimaryKey
> > > @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
> > > @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value =
> > > "true")
> > > private String primaryKey;
>
> > > @Persistent
> > > private User user
>
> > > @Persistent
> > > private List friendsList;  Or alternative private List
> > > friendsList;
> > > ...
>
> > > }
>
> > > I have two queries:
> > > Query 1: I want to select all MyAppUsers where a specific User is in
> > > the friendsList.
> > > Query 2: I want to get all nicknames of the friends of a specific
> > > MyAppUser. This can be achieved by getting a MyAppsUser by id and then
> > > calling for each user user.getNickname().
>
> > > Question query 1: Is a query with a filter on "List friendsList"
> > > faster or slower than a query with a filter on "List
> > > friendsList" or is it equal?
>
> > > For query 2 I would say that a "List friendsList" is faster,
> > > because I have direct access to the nicknames. With only the id stored
> > > in the friendsList, I have to make another query for each friend in
> > > the list to get his nickname.

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.



[appengine-java] Re: Best practice modeling a 1:n user-relationship with List properties

2010-08-31 Thread androidDeveloper
Yes, I think that makes sense. Now I understand what you mean.
To only get those users who are my friends, I have to fill the
friendslist differently -> I have to put my userId in the friendslist
of my friends and not the other way (put all the userIds of my friends
in my friendslist). Then I can use your query as you described:
Query q = pm.newQuery(MyAppUser.class);
q.setFilter("userKey == userKeyParam");
q.declareParameters("String userKeyParam");
q.execute(myself);

Thanks for the help!


On 31 Aug., 12:16, Frederik Pfisterer  wrote:
> Sorry, for some reason my post got lost and then I posted another one
> twice ... seems google groups had some issues.
> You are unlikely to have 1 million friends, so the resultset over
> which you'll iterate will be the 10-50 users in the list:
> Mind the filter:
>
> Query q = pm.newQuery(MyAppUser.class);
> q.setFilter("userKey == userKeyParam");
> q.declareParameters("String userKeyParam");
> q.execute(appUserKey);
>
> With the appUserKey you pass to q.execute you get the 10-50 users
> which are your friends.
> Does this make sense?
>
> On 30 Aug., 08:42, androidDeveloper  wrote:
>
> > thanks a lot for the reply!
>
> > One last question:
>
> > > query 2: ...
> > > Query q = pm.newQuery(MyAppUser.class);
> > > execution gives you the List you can iterate through and
> > > get the nicknames.
>
> > What if there would be many instances of "MyAppUser" in datastore.
> > Think of 1 Million or more. Then I would have to iterate over 1
> > million records each time I collect the nicknames of max 10-50 users.
> > Would it not be better to keep some denormalized fields in MyAppUser
> > with nicknames or use "contains" in the query with the friendsIdlist?
>
> > On 27 Aug., 10:06, Frederik Pfisterer  wrote:
>
> > > Hi,
>
> > > from experience with GAE + JDO I'd recommend using List since
> > > List currently has some issues you'll encounter down the road.
> > > If your list is actually List you'd have an object graph
> > > that can currently only be achieved with List.
>
> > > Also in your reasoning for query 2 you have to consider what JDO does
> > > in the background: for what you call "direct access" it is actually
> > > performing a lookup, and lazy loading the objects upon access.
>
> > > To achieve what you want I'd do the following:
> > > query 1: query on keys (it's an order of magnitude faster, as it
> > > doesn't need to actually retrieve the objects, only keys from the
> > > index):
> > > Query q = pm.newQuery("select primaryKey from " +
> > > MyAppUser.class.getName());
> > >                 q.setFilter("primaryKey == userKeyParam");
> > >                 q.declareParameters("String userKeyParam");
> > > Execution gives you the List of keys of the "friends".
>
> > > query 2: same as above but with actually retreiving the object instead
> > > of the key:
> > > Query q = pm.newQuery(MyAppUser.class);
> > > execution gives you the List you can iterate through and
> > > get the nicknames.
>
> > > In general: Use List and you'll save yourself a lot of
> > > hassle ;-)
>
> > > Hope this helps,
> > > Fred
>
> > > On 24 Aug., 12:53, androidDeveloper  wrote:
>
> > > > Hi all,
>
> > > > I am trying to model a 1:n relationship between different "myAppUser"
> > > > instances on Google App Engines Datastore. MyAppUser has a reference
> > > > to 0...n other users (friends).
> > > > MyAppUser is currently defined as showed below:
>
> > > > class MyAppUser {
> > > > @PrimaryKey
> > > > @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
> > > > @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value =
> > > > "true")
> > > > private String primaryKey;
>
> > > > @Persistent
> > > > private User user
>
> > > > @Persistent
> > > > private List friendsList;  Or alternative private List
> > > > friendsList;
> > > > ...
>
> > > > }
>
> > > > I have two queries:
> > > > Query 1: I want to select all MyAppUsers where a specific User is in
> > > > the friendsList.
> > > > Query 2: I want to get all nicknames of the friends of a specific
> > > > MyAppUser. This can be achieved by getting a MyAppsUser by id and then
> > > > calling for each user user.getNickname().
>
> > > > Question query 1: Is a query with a filter on "List friendsList"
> > > > faster or slower than a query with a filter on "List
> > > > friendsList" or is it equal?
>
> > > > For query 2 I would say that a "List friendsList" is faster,
> > > > because I have direct access to the nicknames. With only the id stored
> > > > in the friendsList, I have to make another query for each friend in
> > > > the list to get his nickname.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?