Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Extract interface from KeyspaceOperator (and rename it) and move the …
Browse files Browse the repository at this point in the history
…interface to the api package
  • Loading branch information
rantav committed Sep 21, 2010
1 parent b9a6d25 commit acd2842
Show file tree
Hide file tree
Showing 57 changed files with 283 additions and 243 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Expand Up @@ -9,6 +9,8 @@ Changes by version:
========
Add CountQuery, SuperCountQuery and SubCountQuery
Move all the API stuff to me.prettyprint.hector.api.*. Extract interfaces and prepare the ground for more avro good.
- Move all the queries and extract their interfaces
- Rename KeyspaceOperator and extract a Keyspace interface from it
Rename a few exceptions to begin with HSomething so they are hard to unintentionally mix with their thrift doubles.


Expand Down
14 changes: 7 additions & 7 deletions src/main/java/me/prettyprint/cassandra/dao/Command.java
Expand Up @@ -3,15 +3,15 @@
import me.prettyprint.cassandra.service.CassandraClient;
import me.prettyprint.cassandra.service.CassandraClientPool;
import me.prettyprint.cassandra.service.CassandraClientPoolFactory;
import me.prettyprint.cassandra.service.Keyspace;
import me.prettyprint.cassandra.service.KeyspaceService;
import me.prettyprint.cassandra.service.CassandraClient.FailoverPolicy;
import me.prettyprint.hector.api.exceptions.HectorException;

import org.apache.cassandra.thrift.ConsistencyLevel;

/**
* Provides an abstraction for running an operation, or a command on a cassandra keyspace.
* Clients of Hector implement the {@link #execute(Keyspace)} and then call
* Clients of Hector implement the {@link #execute(KeyspaceService)} and then call
* {@link #execute(String, int, String)} on an instance of this implementation.
*
* The class provides the comfort of managing connections by borowing and then releasing them.
Expand All @@ -34,10 +34,10 @@ public abstract class Command<OUTPUT> {
* @param ks
* @return
*/
public abstract OUTPUT execute(final Keyspace ks) throws HectorException;
public abstract OUTPUT execute(final KeyspaceService ks) throws HectorException;

/**
* Call this method to run the code within the {@link #execute(Keyspace)} method.
* Call this method to run the code within the {@link #execute(KeyspaceService)} method.
*
* @param host
* @param port
Expand Down Expand Up @@ -79,7 +79,7 @@ public final OUTPUT execute(String[] hosts, String keyspace, ConsistencyLevel co
public final OUTPUT execute(CassandraClientPool pool, String[] hosts, String keyspace,
ConsistencyLevel consistency) throws HectorException {
CassandraClient c = pool.borrowClient(hosts);
Keyspace ks = c.getKeyspace(keyspace, consistency);
KeyspaceService ks = c.getKeyspace(keyspace, consistency);
try {
return execute(ks);
} finally {
Expand All @@ -97,7 +97,7 @@ public OUTPUT execute(String[] hosts, String keyspace, ConsistencyLevel consiste

protected OUTPUT execute(CassandraClient c, String keyspace, ConsistencyLevel consistency,
FailoverPolicy failoverPolicy) throws HectorException {
Keyspace ks = c.getKeyspace(keyspace, consistency, failoverPolicy);
KeyspaceService ks = c.getKeyspace(keyspace, consistency, failoverPolicy);
try {
return execute(ks);
} finally {
Expand All @@ -111,7 +111,7 @@ protected OUTPUT execute(CassandraClient c, String keyspace, ConsistencyLevel co
*/
protected final OUTPUT execute(CassandraClient c, String keyspace, ConsistencyLevel consistency)
throws HectorException {
Keyspace ks = c.getKeyspace(keyspace, consistency);
KeyspaceService ks = c.getKeyspace(keyspace, consistency);
try {
return execute(ks);
} finally {
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/me/prettyprint/cassandra/dao/SimpleCassandraDao.java
Expand Up @@ -8,10 +8,10 @@
import java.util.HashMap;
import java.util.Map;

import me.prettyprint.cassandra.model.KeyspaceOperator;
import me.prettyprint.cassandra.model.Mutator;
import me.prettyprint.cassandra.model.Result;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.beans.Rows;
import me.prettyprint.hector.api.exceptions.HectorException;
Expand All @@ -21,7 +21,7 @@
public class SimpleCassandraDao {

private String columnFamilyName;
private KeyspaceOperator keyspaceOperator;
private Keyspace keyspace;
private final StringSerializer serializer = StringSerializer.get();

/**
Expand All @@ -31,7 +31,7 @@ public class SimpleCassandraDao {
* @param value the String value to insert
*/
public void insert(String key, String columnName, String value) {
createMutator(keyspaceOperator).insert(
createMutator(keyspace).insert(
key, columnFamilyName, createColumn(columnName, value, serializer, serializer));
}

Expand All @@ -41,7 +41,7 @@ public void insert(String key, String columnName, String value) {
* @return The string value; null if no value exists for the given key.
*/
public String get(String key, String columnName) throws HectorException {
ColumnQuery<String, String> q = createStringColumnQuery(keyspaceOperator);
ColumnQuery<String, String> q = createStringColumnQuery(keyspace);
Result<HColumn<String, String>> r = q.setKey(key).
setName(columnName).
setColumnFamily(columnFamilyName).
Expand All @@ -56,7 +56,7 @@ public String get(String key, String columnName) throws HectorException {
* @return
*/
public Map<String, String> getMulti(String columnName, String... keys) {
MultigetSliceQuery<String,String> q = createMultigetSliceQuery(keyspaceOperator, serializer, serializer);
MultigetSliceQuery<String,String> q = createMultigetSliceQuery(keyspace, serializer, serializer);
q.setColumnFamily(columnFamilyName);
q.setKeys(keys);
q.setColumnNames(columnName);
Expand All @@ -77,10 +77,10 @@ public Map<String, String> getMulti(String columnName, String... keys) {
* Insert multiple values for a given columnName
*/
public void insertMulti(String columnName, Map<String, String> keyValues) {
Mutator m = createMutator(keyspaceOperator);
Mutator m = createMutator(keyspace);
for (Map.Entry<String, String> keyValue: keyValues.entrySet()) {
m.addInsertion(keyValue.getKey(), columnFamilyName,
createColumn(columnName, keyValue.getValue(), keyspaceOperator.createTimestamp(), serializer, serializer));
createColumn(columnName, keyValue.getValue(), keyspace.createTimestamp(), serializer, serializer));
}
m.execute();
}
Expand All @@ -90,7 +90,7 @@ public void insertMulti(String columnName, Map<String, String> keyValues) {
* Delete multiple values
*/
public void delete(String columnName, String... keys) {
Mutator m = createMutator(keyspaceOperator);
Mutator m = createMutator(keyspace);
for (String key: keys) {
m.addDeletion(key, columnFamilyName, columnName, serializer);
}
Expand All @@ -102,8 +102,8 @@ public void setColumnFamilyName(String columnFamilyName) {
}


public void setKeyspaceOperator(KeyspaceOperator keyspaceOperator) {
this.keyspaceOperator = keyspaceOperator;
public void setKeyspace(Keyspace keyspace) {
this.keyspace = keyspace;
}


Expand Down
6 changes: 3 additions & 3 deletions src/main/java/me/prettyprint/cassandra/dao/SpringCommand.java
Expand Up @@ -2,7 +2,7 @@

import me.prettyprint.cassandra.service.CassandraClient;
import me.prettyprint.cassandra.service.CassandraClientPool;
import me.prettyprint.cassandra.service.Keyspace;
import me.prettyprint.cassandra.service.KeyspaceService;
import me.prettyprint.hector.api.exceptions.HectorException;

import org.apache.cassandra.thrift.ConsistencyLevel;
Expand All @@ -24,11 +24,11 @@ public SpringCommand(CassandraClientPool cassandraClientPool) {
* @param ks
* @return
*/
public abstract OUTPUT execute(final Keyspace ks) throws HectorException;
public abstract OUTPUT execute(final KeyspaceService ks) throws HectorException;

public final OUTPUT execute(String keyspace, ConsistencyLevel consistency) throws HectorException {
CassandraClient c = cassandraClientPool.borrowClient();
Keyspace ks = c.getKeyspace(keyspace, consistency);
KeyspaceService ks = c.getKeyspace(keyspace, consistency);
try {
return execute(ks);
} finally {
Expand Down
Expand Up @@ -5,7 +5,7 @@
import me.prettyprint.cassandra.service.CassandraClient;
import me.prettyprint.cassandra.service.CassandraClientPool;
import me.prettyprint.cassandra.service.CassandraClientPoolFactory;
import me.prettyprint.cassandra.service.Keyspace;
import me.prettyprint.cassandra.service.KeyspaceService;
import me.prettyprint.hector.api.exceptions.HectorException;

import org.apache.cassandra.thrift.Column;
Expand All @@ -27,7 +27,7 @@ public static void main(String[] args) throws HectorException {
// A load balanced version would look like this:
// CassandraClient client = pool.borrowClient(new String[] {"cas1:9160", "cas2:9160", "cas3:9160"});

Keyspace keyspace = null;
KeyspaceService keyspace = null;
try {
keyspace = client.getKeyspace("Keyspace1");
ColumnPath columnPath = new ColumnPath("Standard1");
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/me/prettyprint/cassandra/examples/ExampleDao.java
Expand Up @@ -3,7 +3,7 @@
import static me.prettyprint.cassandra.utils.StringUtils.bytes;
import static me.prettyprint.cassandra.utils.StringUtils.string;
import me.prettyprint.cassandra.dao.Command;
import me.prettyprint.cassandra.service.Keyspace;
import me.prettyprint.cassandra.service.KeyspaceService;
import me.prettyprint.hector.api.exceptions.HNotFoundException;
import me.prettyprint.hector.api.exceptions.HectorException;

Expand All @@ -19,10 +19,10 @@
* <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
* and injects it to the {@link Command#execute(KeyspaceService)} 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.
* the {@link KeyspaceService} instance and releasing it after the operation completes.
*
* @author Ran Tavory (rantav@gmail.com)
* @deprecated use ExampleDaoV2
Expand Down Expand Up @@ -53,7 +53,7 @@ public static void main(String[] args) throws HectorException {
public void insert(final String key, final String value) throws HectorException {
execute(new Command<Void>() {
@Override
public Void execute(final Keyspace ks) throws HectorException {
public Void execute(final KeyspaceService ks) throws HectorException {
ks.insert(key, createColumnPath(COLUMN_NAME), bytes(value));
return null;
}
Expand All @@ -68,7 +68,7 @@ public Void execute(final Keyspace ks) throws HectorException {
public String get(final String key) throws HectorException {
return execute(new Command<String>() {
@Override
public String execute(final Keyspace ks) throws HectorException {
public String execute(final KeyspaceService ks) throws HectorException {
try {
return string(ks.getColumn(key, createColumnPath(COLUMN_NAME)).getValue());
} catch (HNotFoundException e) {
Expand All @@ -84,7 +84,7 @@ public String execute(final Keyspace ks) throws HectorException {
public void delete(final String key) throws HectorException {
execute(new Command<Void>() {
@Override
public Void execute(final Keyspace ks) throws HectorException {
public Void execute(final KeyspaceService ks) throws HectorException {
ks.remove(key, createColumnPath(COLUMN_NAME));
return null;
}
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/me/prettyprint/cassandra/examples/ExampleDaoV2.java
Expand Up @@ -2,19 +2,19 @@

import static me.prettyprint.hector.api.factory.HFactory.createColumn;
import static me.prettyprint.hector.api.factory.HFactory.createColumnQuery;
import static me.prettyprint.hector.api.factory.HFactory.createKeyspaceOperator;
import static me.prettyprint.hector.api.factory.HFactory.createKeyspace;
import static me.prettyprint.hector.api.factory.HFactory.createMultigetSliceQuery;
import static me.prettyprint.hector.api.factory.HFactory.createMutator;
import static me.prettyprint.hector.api.factory.HFactory.getOrCreateCluster;

import java.util.HashMap;
import java.util.Map;

import me.prettyprint.cassandra.model.KeyspaceOperator;
import me.prettyprint.cassandra.model.Mutator;
import me.prettyprint.cassandra.model.Result;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.beans.Rows;
import me.prettyprint.hector.api.exceptions.HectorException;
Expand All @@ -30,18 +30,18 @@ public class ExampleDaoV2 {
private final static String COLUMN_NAME = "v";
private final StringSerializer serializer = StringSerializer.get();

private final KeyspaceOperator keyspaceOperator;
private final Keyspace keyspace;

public static void main(String[] args) throws HectorException {
Cluster c = getOrCreateCluster("MyCluster", HOST_PORT);
ExampleDaoV2 ed = new ExampleDaoV2(createKeyspaceOperator(KEYSPACE, c));
ExampleDaoV2 ed = new ExampleDaoV2(createKeyspace(KEYSPACE, c));
ed.insert("key1", "value1");

System.out.println(ed.get("key1"));
}

public ExampleDaoV2(KeyspaceOperator ko) {
keyspaceOperator = ko;
public ExampleDaoV2(Keyspace keyspace) {
this.keyspace = keyspace;
}

/**
Expand All @@ -51,12 +51,12 @@ public ExampleDaoV2(KeyspaceOperator ko) {
* @param value the String value to insert
*/
public void insert(final String key, final String value) {
createMutator(keyspaceOperator).insert(
key, CF_NAME, createColumn(COLUMN_NAME, value, serializer, serializer));
Mutator m = createMutator(keyspace);
m.insert(key, CF_NAME, createColumn(COLUMN_NAME, value, serializer, serializer));
}

private long createTimestamp() {
return keyspaceOperator.createTimestamp();
return keyspace.createTimestamp();
}

/**
Expand All @@ -65,7 +65,7 @@ private long createTimestamp() {
* @return The string value; null if no value exists for the given key.
*/
public String get(final String key) throws HectorException {
ColumnQuery<String, String> q = createColumnQuery(keyspaceOperator, serializer, serializer);
ColumnQuery<String, String> q = createColumnQuery(keyspace, serializer, serializer);
Result<HColumn<String, String>> r = q.setKey(key).
setName(COLUMN_NAME).
setColumnFamily(CF_NAME).
Expand All @@ -80,7 +80,7 @@ public String get(final String key) throws HectorException {
* @return
*/
public Map<String, String> getMulti(String... keys) {
MultigetSliceQuery<String,String> q = createMultigetSliceQuery(keyspaceOperator, serializer, serializer);
MultigetSliceQuery<String,String> q = createMultigetSliceQuery(keyspace, serializer, serializer);
q.setColumnFamily(CF_NAME);
q.setKeys(keys);
q.setColumnNames(COLUMN_NAME);
Expand All @@ -101,7 +101,7 @@ public Map<String, String> getMulti(String... keys) {
* Insert multiple values
*/
public void insertMulti(Map<String, String> keyValues) {
Mutator m = createMutator(keyspaceOperator);
Mutator m = createMutator(keyspace);
for (Map.Entry<String, String> keyValue: keyValues.entrySet()) {
m.addInsertion(keyValue.getKey(), CF_NAME,
createColumn(COLUMN_NAME, keyValue.getValue(), createTimestamp(), serializer, serializer));
Expand All @@ -113,7 +113,7 @@ public void insertMulti(Map<String, String> keyValues) {
* Delete multiple values
*/
public void delete(String... keys) {
Mutator m = createMutator(keyspaceOperator);
Mutator m = createMutator(keyspace);
for (String key: keys) {
m.addDeletion(key, CF_NAME, COLUMN_NAME, serializer);
}
Expand Down
Expand Up @@ -5,7 +5,7 @@
import me.prettyprint.cassandra.dao.SpringCommand;
import me.prettyprint.cassandra.service.CassandraClient;
import me.prettyprint.cassandra.service.CassandraClientPool;
import me.prettyprint.cassandra.service.Keyspace;
import me.prettyprint.cassandra.service.KeyspaceService;
import me.prettyprint.hector.api.exceptions.HNotFoundException;
import me.prettyprint.hector.api.exceptions.HectorException;

Expand Down Expand Up @@ -41,7 +41,7 @@ public ExampleSpringDao(CassandraClientPool cassandraClientPool) {
public void insert(final String key, final String value) throws HectorException {
execute(new SpringCommand<Void>(cassandraClientPool){
@Override
public Void execute(final Keyspace ks) throws HectorException {
public Void execute(final KeyspaceService ks) throws HectorException {
ks.insert(key, createColumnPath(columnName), bytes(value));
return null;
}
Expand All @@ -55,7 +55,7 @@ public Void execute(final Keyspace ks) throws HectorException {
public String get(final String key) throws HectorException {
return execute(new SpringCommand<String>(cassandraClientPool){
@Override
public String execute(final Keyspace ks) throws HectorException {
public String execute(final KeyspaceService ks) throws HectorException {
try {
return string(ks.getColumn(key, createColumnPath(columnName)).getValue());
} catch (HNotFoundException e) {
Expand All @@ -71,7 +71,7 @@ public String execute(final Keyspace ks) throws HectorException {
public void delete(final String key) throws HectorException {
execute(new SpringCommand<Void>(cassandraClientPool){
@Override
public Void execute(final Keyspace ks) throws HectorException {
public Void execute(final KeyspaceService ks) throws HectorException {
ks.remove(key, createColumnPath(columnName));
return null;
}
Expand Down
@@ -1,5 +1,6 @@
package me.prettyprint.cassandra.model;

import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.query.ColumnQuery;

Expand All @@ -17,7 +18,7 @@ public abstract class AbstractColumnQuery<N, V> extends AbstractQuery<N, V, HCol
protected String key;
protected N name;

protected AbstractColumnQuery(KeyspaceOperator ko, Serializer<N> nameSerializer,
protected AbstractColumnQuery(Keyspace ko, Serializer<N> nameSerializer,
Serializer<V> valueSerializer) {
super(ko, nameSerializer, valueSerializer);
}
Expand Down

0 comments on commit acd2842

Please sign in to comment.