CS6320:  SW Engineering of Web Based Systems

 

GAE: Datastore ---Manipulating Keys

 

REMEMBER from our overview of Datastore....

Key = each entity has a key that uniquely identifies it across entire system

    • COMPONENTS OF A KEY:

      • application ID = this makes sure nothing else about the key can collide with the entities of any other application.
        • It also ensures that no other app can access your app's data, and that your app cannot access data for other apps.
        • You won't see the app ID mentioned in the datastore API; this is automatic.

      • kind = An entity's kind categorizes the entity for the purposes of queries, and for ensuring the uniqueness of the rest of the key.
        • example: a shopping cart application represents each customer order with an entity of the kind "Order."
        • specify when create entity.
        • This is somewhat different than the realtional database concept of table but, that is the closest.

      • entity ID = This can be an arbitrary string specified by the app or it can be generated automatically by the datastore.
              CREATED (only one of following ways):
        • an entity ID given by the app called key name, will be a string
        • an entity ID generated by the datastore called an ID, will be an integer

Comparing GAE Datastore to Relational Database --Caution from your Book.

It's tempting to compare these concepts with similar concepts in relational
databases: kinds are tables; entities are rows; properties are fields
or columns. That's a useful comparison, but watch out for differences.

Unlike a table in a relational database, there is no relationship between
an entity's kind and its properties. Two entities of the same kind can
have different properties set or not set, and can each have a property of
the same name but with values of different types.
You can (and often
will) enforce a data schema in your own code, and App Engine includes
libraries to make this easy, but this is not required by the datastore.

Also unlike relational databases, keys are not properties. You can perform queries on key names just like properties, but you cannot change
a key name after the entity has been created.


And of course, a relational database cannot store multiple values in a single cell, while an App Engine property can have multiopel values.

 

Some things about keys

  • Keys can not be changed once set

  • creating the concept of a foreign key in a relational data base, in GAE we can store the key of another entity B inside an entity A, in this way it is a reference to entity A's entity B.

 

Getting the Key of an Entity Object --- in Java

  • Key k = Entity_Object.getKey();

    • Test for completeness (see next section) k.isComplete()

    • numeric ID of key = k.getId()

    • string name of key = k.getName()

     

     

     

     

     

Converting Key of an Entity TO a String ---- in Java


    KeyFactory.keyToString(entity_object.getKey())

    or KeyFactory.keyToString(key_object)

 

Creating the Key for an Entity --- how it is done --automatically or you create in code

  • Option 1: AUTOMATIC KEY CREATION    

    When you construct a new entity object and do not provide a key name, the entity
    object has a key, but the key does not yet have an ID.

    The ID is populated when entity object is saved to the datastore for the first time.

    If you get the key object prior to saving the object, but it will be incomplete.

    In Java

    Entity e = new Entity("Book"); //associated with kind Book, automatic key creation

  • Option 2: CREATE KEY IN CODE    

    If the entity object was constructed with a key name the key is complete before the
    object is saved—though if the entity has not been saved, the key name is not guaranteed
    to be unique.

    In Java,

    Key k = KeyFactory.createKey("Entity_kind", "alphabeta"); ///takes the kind and the ID or name as arguments

     

     

Retrieving an Entity using its key

  • Get Entity faster with key then a query

Java low-level API,

  • you get an entity by its key using a DatastoreService instance (returned by DatastoreServiceFactory.getDatastoreService()).
  • DatastoreService instance has a get() method that takes a Key for a single entity get, or an Iterable<Key> for a batch get.
  • If given an iterable of keys, get() returns a Map of Key to Entity


SINGLE Entity for a key
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();                  //get the datastore
Entity entity_object = ds.get(key_object);    //get Entity associated with key=key_object

Batch multiple gets for multiple keys
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();                  //get the datastore
Map<Key, Entity> entities = ds.get(new ArrayList(Arrays.asList(k1, k2, k3)));    //get 3 entities for keys k1,k2,k3 respectiviely
Entity e1 = entities.get(k1);
Entity e2 = entities.get(k2);
Entity e3 = entities.get(k3);

© Lynne Grewe