google-code-export / objectify-appengine

Automatically exported from code.google.com/p/objectify-appengine
MIT License
1 stars 0 forks source link

add Objectify suport for Mapper API #70

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
The mapper api discourages to directly interface the data store. Instead it 
offers the DataStoreMutationPool to enqueue data store operations. They will 
later be executed as batch.

The DataStoreMutationPool is using low level data store API. This is kind of 
painful especially if you create objects because you need to construct the 
entities by hand.

I wanted to suggest to have a small layer in Objectify that eases the interface 
to the DataStoreMutationPool. Maybe an ObjectifyMutationPool which can handle 
POJO just as the regular objectify API (something like 
ObjectifyMutationPool.put(pojo))?

This would be a great feature

Original issue reported on code.google.com by toby.rothe@gmail.com on 18 Jan 2011 at 9:58

GoogleCodeExporter commented 9 years ago

Original comment by lhori...@gmail.com on 18 Jan 2011 at 2:51

GoogleCodeExporter commented 9 years ago
Currently I use the following for transforming entities into objects (and the 
other way arround) in the Mapper API:

public class User {
    private String name;
    //...
    public String getName() {
        return name;
    }
}

public class UserMapper extends AppEngineMapper<Key, Entity, NullWritable, 
NullWritable> {

    @Override
    public void map(Key key, Entity value, Context context) throws IOException, InterruptedException {
        DAOBase db = new DAOBase();
        User user = db.ofy().getFactory().<User>getMetadata(value.getKey()).toObject(value, db.ofy());

        if (!user.getName().equals("admin")) {
            // use toEntity to transform the object into an entity
            // Entity entity = toEntity(db, user);
            DatastoreMutationPool mutationPool = getAppEngineContext(context).getMutationPool();
            mutationPool.delete(value.getKey());
        }
    }

    public Entity toEntity(DAOBase db, User object) {
        final ObjectifyFactory factory = db.ofy().getFactory();
        final com.googlecode.objectify.Key<User> key = factory.getKey(object);
        return factory.<User>getMetadata(key).toEntity(object, db.ofy());
    }
}

In this example every user except "admin" is deleted from the datastore. The 
toObject method is used to create the object. You can use the toEntity method 
to create an entity from the object.

Original comment by dilbert....@gmail.com on 31 Mar 2011 at 4:05