So I hate to make this more complicated but ...

Yes, this is roughly the right approach. However, if you are creating
entities at a rate higher than 1/s, this pattern will break. You'll need to
use the sharded counter property:

http://code.google.com/appengine/articles/sharding_counters.html

The example is in Python, but hopefully you understand what's happening
there.

--
Ikai Lan
Developer Programs Engineer, Google App Engine
plus.ikailan.com | twitter.com/ikai



On Fri, Oct 14, 2011 at 9:44 AM, Phil <philippbsee...@googlemail.com> wrote:

> Hello,
>
> I use Java with JDO for the datastore access on google app engine.
>
> I need the number of rows for the calculation of the number of pages in a
> cell table.
> This number can be very high so I don't want to calculate it when i query
> the data and implemented a class which holds the number of Entities:
>
> import javax.jdo.annotations.PersistenceCapable;
> import javax.jdo.annotations.Persistent;
> import javax.jdo.annotations.PrimaryKey;
>
> @PersistenceCapable
> public class Aggregate {
>
> @PrimaryKey
>  @Persistent
> private String name;
>  @Persistent
>  private int count;
>  public Aggregate(String name) {
> this.name = name;
>  this.count = 0;
> }
>  public String getName() {
>  return this.name;
> }
>  public void setName(String name) {
> this.name = name;
>  }
>  public int getCount() {
>  return this.count;
> }
>  public void setCount(int count) {
> this.count = count;
>  }
> }
>
> To update the count value I do something like that.
>
> @PersistenceCapable
> public class SomeEntity {
>  @PrimaryKey
>  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
> private Key key;
>  ......
>  public SomeEntity() {
>  // increment the count
> PersistenceManager pm = PMF.get().getPersistenceManager();
>  try {
> Aggregate aggregate = (Aggregate) pm.getObjectById(Aggregate.class,
> SomeEntity.class.getSimpleName());
>  int count = aggregate.getCount();
> count++;
>  aggregate.setCount(count);
> pm.makePersistent(aggregate);
>  } catch (JDOObjectNotFoundException jonfe) {
> // the first "someEntity" ever
>  Aggregate aggregate = new Aggregate(Player.class.getSimpleName());
>  aggregate.setCount(1);
> pm.makePersistent(aggregate);
>  } finally {
> pm.close();
>  }
> }
>
> .....
>
> So far, this works, but I really don't like the way how it's done. Are
> there any implemented tools in google app engine to handle something like
> that? Or is this the correct way to keep track of the number of entities?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine for Java" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-appengine-java/-/lK9o1_SMdygJ.
> To post to this group, send email to
> google-appengine-java@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.
>

-- 
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-java@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.

Reply via email to