Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GAE Go — How to use GetMulti with non-existent entity keys?

I've found myself needing to do a GetMulti operation with an array of keys for which some entities exist, but some do not.

My current code, below, returns an error (datastore: no such entity).

err := datastore.GetMulti(c, keys, infos)

So how can I do this? I'd use a "get or insert" method, but there isn't one.

like image 425
Matthew H Avatar asked Mar 06 '13 16:03

Matthew H


People also ask

Is a Datastore an entity?

Data objects in Datastore are known as entities. An entity has one or more named properties, each of which can have one or more values. Entities of the same kind do not need to have the same properties, and an entity's values for a given property do not all need to be of the same data type.

What is a Datastore key?

Each entity in a Datastore mode database has a key that uniquely identifies it. The key consists of the following components: The namespace of the entity, which allows for multitenancy. The kind of the entity, which categorizes it for the purpose of queries.

How do I add entities to Google Datastore?

In Java, you create a new entity by constructing an instance of class Entity , supplying the entity's kind as an argument to the Entity() constructor. After populating the entity's properties if necessary, you save it to the datastore by passing it as an argument to the DatastoreService. put() method.


1 Answers

GetMulti can return a appengine.MultiError in this case. Loop through that and look for datastore.ErrNoSuchEntity. For example:

if err := datastore.GetMulti(c, keys, dst); err != nil {
    if me, ok := err.(appengine.MultiError); ok {
        for i, merr := range me {
            if merr == datastore.ErrNoSuchEntity {
                // keys[i] is missing
            }
        }
    } else {
        return err
    }
}
like image 175
mjibson Avatar answered Oct 14 '22 16:10

mjibson