Add autoFlush flag to Schema

The flag controls whether or not writes are sent immediately to
the database.  By delaying writes larger units can get sent in a
single request.  Right now we only plan to make use of the flush
hint on NoSQL based systems.

Change-Id: I79b2a1aec822e1c4b6abf5c6788429ab7becce1d
Signed-off-by: Shawn O. Pearce <sop@google.com>
diff --git a/src/main/java/com/google/gwtorm/client/Schema.java b/src/main/java/com/google/gwtorm/client/Schema.java
index 95aa9e4..e18133a 100644
--- a/src/main/java/com/google/gwtorm/client/Schema.java
+++ b/src/main/java/com/google/gwtorm/client/Schema.java
@@ -55,6 +55,31 @@
  * </pre>
  */
 public interface Schema {
+  /** @return true if auto flush is enabled (default). */
+  boolean isAutoFlush();
+
+  /**
+   * Set (or unset) the auto-flush flag for this connection.
+   * <p>
+   * If true writes are sent to the database by the time the method returns. If
+   * false, writes will be sent at any time, or some later point in the future.
+   * Callers should use {@link #flush()} to ensure the writes are visible, or
+   * reset the auto flush flag to true.
+   *
+   * @param autoFlush the new setting.
+   * @throws OrmException previously autoFlush was false, the new setting is
+   *         true, and flushed writes cannot be sent.
+   */
+  void setAutoFlush(boolean autoFlush) throws OrmException;
+
+  /**
+   * Ensures all modifications are now visible to others.
+   *
+   * @throws OrmException one or more modifications cannot be applied. The
+   *         writes are now inconsistent.
+   */
+  void flush() throws OrmException;
+
   /**
    * Add any missing columns, create any missing tables or sequences.
    * <p>
diff --git a/src/main/java/com/google/gwtorm/jdbc/JdbcSchema.java b/src/main/java/com/google/gwtorm/jdbc/JdbcSchema.java
index 13ce9f9..a4ecb8f 100644
--- a/src/main/java/com/google/gwtorm/jdbc/JdbcSchema.java
+++ b/src/main/java/com/google/gwtorm/jdbc/JdbcSchema.java
@@ -40,6 +40,20 @@
     conn = dbDef.newConnection();
   }
 
+  @Override
+  public boolean isAutoFlush() {
+    return true; // We are always flushing.
+  }
+
+  @Override
+  public void setAutoFlush(boolean autoFlush) {
+  }
+
+  @Override
+  public void flush() {
+    // Do nothing, we flush by default during execution.
+  }
+
   public final Connection getConnection() {
     return conn;
   }
diff --git a/src/main/java/com/google/gwtorm/nosql/NoSqlSchema.java b/src/main/java/com/google/gwtorm/nosql/NoSqlSchema.java
index 697a6c3..f130ca7 100644
--- a/src/main/java/com/google/gwtorm/nosql/NoSqlSchema.java
+++ b/src/main/java/com/google/gwtorm/nosql/NoSqlSchema.java
@@ -21,10 +21,26 @@
 
 /** Internal base class for implementations of {@link Schema}. */
 public abstract class NoSqlSchema extends AbstractSchema {
+  private boolean autoFlush = true;
+
   protected NoSqlSchema(final NoSqlDatabase<?, ?, ?> d) {
   }
 
   @Override
+  public boolean isAutoFlush() {
+    return autoFlush;
+  }
+
+  @Override
+  public void setAutoFlush(boolean autoFlush) throws OrmException {
+    if (!this.autoFlush && autoFlush) {
+      flush();
+    }
+
+    this.autoFlush = autoFlush;
+  }
+
+  @Override
   public void pruneSchema(StatementExecutor e) throws OrmException {
     // Assume no action is required in a default NoSQL environment.
   }
diff --git a/src/main/java/com/google/gwtorm/nosql/heap/TreeMapSchema.java b/src/main/java/com/google/gwtorm/nosql/heap/TreeMapSchema.java
index 8742d80..310b328 100644
--- a/src/main/java/com/google/gwtorm/nosql/heap/TreeMapSchema.java
+++ b/src/main/java/com/google/gwtorm/nosql/heap/TreeMapSchema.java
@@ -36,6 +36,11 @@
   }
 
   @Override
+  public void flush() {
+    // We don't buffer writes.
+  }
+
+  @Override
   public void close() {
     // Nothing to do.
   }