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.
 *
 * @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));
  }
}

@@ -393,16 +393,834 @@
 ern.%0A *%0A
+ * This DAO is simple, it provides a get/insert/delte API for String values.%0A * The underlying cassandra implementation stores the values under Keyspace1.key.Standard1.v%0A * where key is the value's key, Standard1 is the name of the column family and %22v%22 is just a column%0A * name that's used to hold the value.%0A * %3Cp%3E%0A * what's interesting to notice here is that ease of operation that the command pattern provides.%0A * The pattern assumes only one keyspace is required to perform the operation (get/insert/remove)%0A * and injects it to the %7B@link Command#execute(Keyspace)%7D abstract method which is implemented%0A * by all the dao methods.%0A * The %7B@link Command#execute(String, int, String)%7D which is then invoked, takes care of creating%0A * the %7B@link Keyspace%7D instance and releasing it after the operation completes.%0A *%0A
  * @auth
@@ -1286,17 +1286,16 @@
 eDao %7B%0A%0A
-%0A
   privat

Add documentation