package me.prettyprint.cassandra.dao;
import static me.prettyprint.cassandra.utils.StringUtils.bytes;
import static me.prettyprint.cassandra.utils.StringUtils.string;
import org.apache.cassandra.service.ColumnPath;
import org.apache.cassandra.service.NotFoundException;

import me.prettyprint.cassandra.service.Keyspace;

/**
 * An example DAO (data access object) which uses the Command pattern.
 *
 * This DAO is simple, it provides a get/insert/delte API for String values.
 * The underlying cassandra implementation stores the values under Keyspace1.key.Standard1.v
 * where key is the value's key, Standard1 is the name of the column family and "v" is just a column
 * name that's used to hold the value.
 * <p>
 * what's interesting to notice here is that ease of operation that the command pattern provides.
 * The pattern assumes only one keyspace is required to perform the operation (get/insert/remove)
 * and injects it to the {@link Command#execute(Keyspace)} abstract method which is implemented
 * by all the dao methods.
 * The {@link Command#execute(String, int, String)} which is then invoked, takes care of creating
 * the {@link Keyspace} instance and releasing it after the operation completes.
 *
 * @author Ran Tavory (rantav@gmail.com)
 *
 */
public class ExampleDao {

  private final static String CASSANDRA_KEYSPACE = "Keyspace1";
  private final static int CASSANDRA_PORT = 9170;
  private final static String CASSANDRA_HOST = "localhost";
  private final String CF_NAME = "Standard1";
  /** Column name where values are stored */
  private final String COLUMN_NAME = "v";

  public void insert(final String key, final String value) throws Exception {
    execute(new Command<Void>(){
      public Void execute(final Keyspace ks) throws Exception {
        ks.insert(key, createColumnPath(COLUMN_NAME), bytes(value));
        return null;
      }
    });
  }

  public String get(final String key) throws Exception {
    return execute(new Command<String>(){
      public String execute(final Keyspace ks) throws Exception {
        try {
          return string(ks.getColumn(key, createColumnPath(COLUMN_NAME)).getValue());
        } catch (NotFoundException e) {
          return null;
        }

      }
    });
  }

  public void delete(final String key) throws Exception {
    execute(new Command<Void>(){
      public Void execute(final Keyspace ks) throws Exception {
        ks.remove(key, createColumnPath(COLUMN_NAME));
        return null;
      }
    });
  }

  protected static <T> T execute(Command<T> command) throws Exception {
    return command.execute(CASSANDRA_HOST, CASSANDRA_PORT, CASSANDRA_KEYSPACE);
  }

  protected ColumnPath createColumnPath(String columnName) {
    return new ColumnPath(CF_NAME , null /*superColumn*/, bytes(columnName));
  }
}

@@ -1594,16 +1594,144 @@
 = %22v%22;%0A%0A
+  /**%0A   * Insert a new value keyed by key%0A   * @param key Key for the value%0A   * @param value the String value to insert%0A   */%0A
   public
@@ -1792,32 +1792,32 @@
 ows Exception %7B%0A
-
     execute(new 
@@ -2000,32 +2000,143 @@
  %7D%0A    %7D);%0A  %7D%0A%0A
+  /**%0A   * Get a string value.%0A   * @return The string value; null if no value exists for the given key.%0A   */%0A
   public String 
@@ -2457,17 +2457,16 @@
       %7D%0A
-%0A
       %7D%0A
@@ -2478,16 +2478,61 @@
 );%0A  %7D%0A%0A
+  /**%0A   * Delete a key from cassandra%0A   */%0A
   public

Improve documentation