Deep serialization of GAE models

View: New views
6 Messages — Rating Filter:   Alert me  

Deep serialization of GAE models

by Fernando Correia :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

If I have two Google App Engine model objects like these:

class Project(db.Model):
    name = db.StringProperty()

class ProjectParticipant(db.Model):
    project = db.ReferenceProperty(Project, collection_name='participants')
    name = db.StringProperty()

When I return an instance of ProjectParticipant from the GAE service,
an instance of the Project is embedded. That may not be very
efficient. If I have 100 participants in a project, the project's data
will be serialized 100 times...

I'd rather have only the Project's key be serialized. Is there an
option for this in PyAMF? Or is the best strategy to use a Data
Transfer Object and copy the data from the model object to the DTO and
return the DTO?
_______________________________________________
PyAMF users mailing list - users@...
http://lists.pyamf.org/mailman/listinfo/users

Re: Deep serialization of GAE models

by Nick Joyce :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Fernando,

Both AMF0 and AMF3 can contain references to objects so if the participants of a project were rendered individually, the project object would only be rendered once and then references would be made to it (2 bytes usually).

Does that help?

On 2 Aug 2008, at 00:35, Fernando Correia wrote:

If I have two Google App Engine model objects like these:

class Project(db.Model):
   name = db.StringProperty()

class ProjectParticipant(db.Model):
   project = db.ReferenceProperty(Project, collection_name='participants')
   name = db.StringProperty()

When I return an instance of ProjectParticipant from the GAE service,
an instance of the Project is embedded. That may not be very
efficient. If I have 100 participants in a project, the project's data
will be serialized 100 times...

I'd rather have only the Project's key be serialized. Is there an
option for this in PyAMF? Or is the best strategy to use a Data
Transfer Object and copy the data from the model object to the DTO and
return the DTO?
_______________________________________________
PyAMF users mailing list - users@...
http://lists.pyamf.org/mailman/listinfo/users


_______________________________________________
PyAMF users mailing list - users@...
http://lists.pyamf.org/mailman/listinfo/users

Re: Deep serialization of GAE models

by Fernando Correia :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2008/8/2 Nick Joyce <nick@...>:
> Hi Fernando,
> Both AMF0 and AMF3 can contain references to objects so if the participants
> of a project were rendered individually, the project object would only be
> rendered once and then references would be made to it (2 bytes usually).
> Does that help?

Thanks for your help! Sure, in the scenario I described that would
save a lot of bytes.

In other cases, I might actually only to return a key. I will try an
approach with DTO to see if I can have more control over what is
serialized.
_______________________________________________
PyAMF users mailing list - users@...
http://lists.pyamf.org/mailman/listinfo/users

Re: Deep serialization of GAE models

by Fernando Correia :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2008/8/2 Nick Joyce <nick@...>:
> Both AMF0 and AMF3 can contain references to objects so if the participants
> of a project were rendered individually, the project object would only be
> rendered once and then references would be made to it (2 bytes usually).
> Does that help?

When I first read your answer, I thought PyAMF already had support for
this feature of AMF. Now I understand that it doesn't, but it could be
added. Is that what you mean?

I also realize now why the referenced class is being serialized. On
Google App Engine, when you use a reference property, it automatically
fetches the referenced object and returns it. So PyAMF's standard
serialization of GAE object will probably always perform a deep
serialization. I wonder if it will have problems with circular
references.

Anyway, I was able to implement a pattern for returning DTOs. I
explain my approach in this article:

http://fernandoacorreia.wordpress.com/2008/08/02/returning-dtos-from-pyamf/

Thanks for your assistance.
_______________________________________________
PyAMF users mailing list - users@...
http://lists.pyamf.org/mailman/listinfo/users

Re: Deep serialization of GAE models

by Nick Joyce :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 2 Aug 2008, at 16:48, Fernando Correia wrote:

2008/8/2 Nick Joyce <nick@...>:
Both AMF0 and AMF3 can contain references to objects so if the participants
of a project were rendered individually, the project object would only be
rendered once and then references would be made to it (2 bytes usually).
Does that help?

When I first read your answer, I thought PyAMF already had support for
this feature of AMF. Now I understand that it doesn't, but it could be
added. Is that what you mean?

Sorry for not being clear, PyAMF already supports this feature.



I also realize now why the referenced class is being serialized. On
Google App Engine, when you use a reference property, it automatically
fetches the referenced object and returns it. So PyAMF's standard
serialization of GAE object will probably always perform a deep
serialization. I wonder if it will have problems with circular
references.

This is one of the features that the reference type is designed to support. An example:

import pyamf

x = [1, 2, 3]
x.append(x) 

y = pyamf.decode(pyamf.encode(x)).next()

assert id(y[3]) == id(y), 'not a circular reference'



Anyway, I was able to implement a pattern for returning DTOs. I
explain my approach in this article:

http://fernandoacorreia.wordpress.com/2008/08/02/returning-dtos-from-pyamf/

Thanks for your assistance.
_______________________________________________
PyAMF users mailing list - users@...
http://lists.pyamf.org/mailman/listinfo/users


_______________________________________________
PyAMF users mailing list - users@...
http://lists.pyamf.org/mailman/listinfo/users

Re: Deep serialization of GAE models

by Fernando Correia :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you for the clarificatios, Nick.
_______________________________________________
PyAMF users mailing list - users@...
http://lists.pyamf.org/mailman/listinfo/users