Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/main/java/com/googlecode/objectify/Objectify.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,40 @@ public interface Objectify
*/
Loader load();

/**
* <p>Start an add command chain. Allows you to add entity objects (datastore.add). Note that all command
* chain objects are immutable.</p>
*
* <p>Saves do NOT cascade; if you wish to save an object graph, you must save each individual entity.</p>
*
* <p>A quick example:
* {@code ofy().add().entities(e1, e2, e3).now();}</p>
*
* <p><b>All command objects are immutable; this method returns a new object rather than modifying the
* current command object.</b></p>
*
* @return the next step in the immutable command chain.
*/
Saver add();

/**
* <p>Start an update command chain. Allows you to update (re-save) entity objects (datastore.update). Note that all command
* chain objects are immutable.</p>
*
* <p>Saves do NOT cascade; if you wish to save an object graph, you must save each individual entity.</p>
*
* <p>A quick example:
* {@code ofy().update().entities(e1, e2, e3).now();}</p>
*
* <p><b>All command objects are immutable; this method returns a new object rather than modifying the
* current command object.</b></p>
*
* @return the next step in the immutable command chain.
*/
Saver update();

/**
* <p>Start a save command chain. Allows you to save (or re-save) entity objects. Note that all command
* <p>Start a save command chain. Allows you to save (or re-save) entity objects (datastore.put). Note that all command
* chain objects are immutable.</p>
*
* <p>Saves do NOT cascade; if you wish to save an object graph, you must save each individual entity.</p>
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/googlecode/objectify/impl/AddSaverImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.googlecode.objectify.impl;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Result;
import java.util.Map;

/**
* Implementation of the Add/Create in datastore
*/
public class AddSaverImpl extends SaverImpl {

/** */
public AddSaverImpl(ObjectifyImpl ofy) {
super(ofy);
}

/* (non-Javadoc)
* @see com.googlecode.objectify.cmd.Saver#entities(java.lang.Iterable)
*/
@Override
public <E> Result<Map<Key<E>, E>> entities(final Iterable<E> entities) {
return ofy.createWriteEngine().create(entities);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public interface AsyncDatastoreReaderWriter {

Future<List<Key>> put(final Iterable<? extends FullEntity<?>> entities);

Future<List<Key>> add(final Iterable<? extends FullEntity<?>> entities);

Future<Void> update(final Iterable<? extends Entity> entities);

default Future<Map<Key, Entity>> get(final Key... keys) {
return get(Arrays.asList(keys));
}
Expand All @@ -38,4 +42,4 @@ default Future<List<Key>> put(final FullEntity<?>... entities) {
}

Future<AggregationResults> runAggregation(final AggregationQuery query);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,34 @@ public Future<List<Key>> put(final Iterable<? extends FullEntity<?>> entities) {
return new FutureNow<>(result);
}

@Override
public Future<List<Key>> add(final Iterable<? extends FullEntity<?>> entities) {
final Iterable<? extends List<? extends FullEntity<?>>> partitions = Iterables.partition(entities, MAX_WRITE_SIZE);

final List<Key> result = new ArrayList<>();

for (final List<? extends FullEntity<?>> partition : partitions) {
final List<Entity> saved = datastoreReaderWriter.put(Iterables.toArray(partition, FullEntity.class));
saved.stream().map(Entity::getKey).forEach(result::add);
}

return new FutureNow<>(result);
}

@Override
public Future<Void> update(final Iterable<? extends Entity> entities) {
final Iterable<? extends List<? extends Entity>> partitions = Iterables.partition(entities, MAX_WRITE_SIZE);

for (final List<? extends Entity> partition : partitions) {
datastoreReaderWriter.update(Iterables.toArray(partition, Entity.class));
}

return new FutureNow<>(null);
}

@Override
public Future<AggregationResults> runAggregation(final AggregationQuery query) {
final AggregationResults results = datastoreReaderWriter.runAggregation(query);
return new FutureNow<>(results);
}
}
}
22 changes: 19 additions & 3 deletions src/main/java/com/googlecode/objectify/impl/ObjectifyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,27 @@ public Loader load() {
}

/* (non-Javadoc)
* @see com.googlecode.objectify.Objectify#put()
* @see com.googlecode.objectify.Objectify#add()
*/
@Override
public Saver add() {
return new AddSaverImpl(this);
}

/* (non-Javadoc)
* @see com.googlecode.objectify.Objectify#update()
*/
@Override
public Saver update() {
return new UpdateSaverImpl(this);
}

/* (non-Javadoc)
* @see com.googlecode.objectify.Objectify#save()
*/
@Override
public Saver save() {
return new SaverImpl(this);
return new PutSaverImpl(this);
}

/* (non-Javadoc)
Expand Down Expand Up @@ -356,4 +372,4 @@ public void close() {

factory().close(this);
}
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/googlecode/objectify/impl/PutSaverImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.googlecode.objectify.impl;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Result;
import java.util.Map;

/**
* Implementation of the Put interface.
*/
public class PutSaverImpl extends SaverImpl {

/** */
public PutSaverImpl(final ObjectifyImpl ofy) {
super(ofy);
}

/* (non-Javadoc)
* @see com.googlecode.objectify.cmd.Saver#entities(java.lang.Iterable)
*/
@Override
public <E> Result<Map<Key<E>, E>> entities(final Iterable<E> entities) {
return ofy.createWriteEngine().save(entities);
}

}
10 changes: 4 additions & 6 deletions src/main/java/com/googlecode/objectify/impl/SaverImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@


/**
* Implementation of the Put interface.
* Implementation of the Saver interface.
*
* @author Jeff Schnitzer <jeff@infohazard.org>
*/
public class SaverImpl implements Saver
public abstract class SaverImpl implements Saver
{
/** */
private final ObjectifyImpl ofy;
protected final ObjectifyImpl ofy;

/** */
public SaverImpl(final ObjectifyImpl ofy) {
Expand Down Expand Up @@ -56,9 +56,7 @@ public <E> Result<Map<Key<E>, E>> entities(final E... entities) {
* @see com.googlecode.objectify.cmd.Saver#entities(java.lang.Iterable)
*/
@Override
public <E> Result<Map<Key<E>, E>> entities(final Iterable<E> entities) {
return ofy.createWriteEngine().save(entities);
}
abstract public <E> Result<Map<Key<E>, E>> entities(final Iterable<E> entities);

/* (non-Javadoc)
* @see com.googlecode.objectify.cmd.Saver#toEntity(java.lang.Object)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.googlecode.objectify.impl;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Result;
import java.util.Map;

/**
* Implementation of the Update in datastore
*/
public class UpdateSaverImpl extends SaverImpl {

/** */
public UpdateSaverImpl(ObjectifyImpl ofy) {
super(ofy);
}

/* (non-Javadoc)
* @see com.googlecode.objectify.cmd.Saver#entities(java.lang.Iterable)
*/
@Override
public <E> Result<Map<Key<E>, E>> entities(final Iterable<E> entities) {
return ofy.createWriteEngine().update(entities);
}

}
Loading