Hi,

I'm confused as to how to deal with a large number of objects and
transactions. I will use a Farm and Cows to illustrate my confusion:

Option1:
Create an owned relationship:

   class Farm {
       Cows mCows;
       int mNumCows;
   }

   class Cows {
       List<Cow> mCowList;
   }

   class Cow {
   }

I have a problem understanding how I would add new cows though,
because I can have thousands of cows per farm:

    tx.begin();

    // Cows not loaded from datastore yet.
    Farm farm = "select from datastore where farm.id = farmid";

    // What happens here if I have 10k cows? Probably just timeout
here?
    Cows cows = farm.getCows();
    cows.addCow(new Cow());

    // This is safe, we are in a transaction.
    farm.mNumCows++;

    // Persists the added cow, and count in one shot, great.
    pm.makePersistent(farm);

    tx.commit();

in the above, I don't know what will happen when my # of cows grows to
a huge amount. There's really no need for me though to keep the cows
as a child of Farm entities, other than to provide support for
transactions. That is option #2:


Option 2:
The two classes are unowned. Now it should be really fast to add new
Cow instances. I can still look up cows by an owner farmId:

    class Farm {
        int mNumCows;
    }

    class Cow {
        Key mFarmOwnerKey;
    }

    tx.begin(); // <- illegal!

    Farm farm = "select from datastore where farm.id = farmid";

    Cow cow = new Cow();
    cow.mFarmOwnerKey = farm.getKey();
    pm.makePersistent(cow);

    farm.mNumCows++;
    pm.makePersistent(farm);

    tx.commit(); // <- illegal!

so the above would be really easy to do , but now I can't use
transactions to safely increment the # of Cows on a Farm when I add a
new Cow. It would have to be something more like:

    tx.begin();
    // add cow
    tx.end();

    tx.begin();
    // increment cow count on farm.
    tx.end();

so this would happen in two separate transactions. The problem here is
that it's possible for the first transaction to fail, but the first to
work, so the # of cows may go out of sync with what's actually in the
datastore.

After watching some of the google i/o videos it seems like the
recommended practice is to always write to the datastore to do fast
reads later on. So every time I add a cow to the datastore, I want to
be able to write out a bunch of statistics to the owner Farm object so
that reads will be very fast, as this is how most users of my site
will be interacting with these objects (mostly just reading Farms).

I can't figure out a good solution for this pattern, how do we handle
this? I'm probably just still stuck in relational-database thinking,
cannot figure this out,

Thanks

-- 
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 [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-java?hl=en.

Reply via email to