summaryrefslogtreecommitdiffstats
path: root/collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra
diff options
context:
space:
mode:
Diffstat (limited to 'collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra')
-rw-r--r--collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraDatabase.java25
-rw-r--r--collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraGroup.java62
-rw-r--r--collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraSession.java57
3 files changed, 95 insertions, 49 deletions
diff --git a/collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraDatabase.java b/collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraDatabase.java
index 0760a78f34..da12203690 100644
--- a/collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraDatabase.java
+++ b/collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraDatabase.java
@@ -23,17 +23,19 @@ import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
import me.prettyprint.hector.api.exceptions.HInvalidRequestException;
+import me.prettyprint.hector.api.exceptions.HectorException;
import me.prettyprint.hector.api.factory.HFactory;
import org.apache.tuscany.nosqldatastore.Database;
import org.apache.tuscany.nosqldatastore.Group;
+import org.apache.tuscany.nosqldatastore.exception.GroupNotFoundException;
class CassandraDatabase implements Database {
-
+
private Keyspace keyspace;
private Cluster cluster;
-
- CassandraDatabase(String databaseName, Cluster cluster) {
- try{
+
+ CassandraDatabase(String databaseName, Cluster cluster) throws HectorException{
+ try{
KeyspaceDefinition ksDef = new ThriftKsDef(databaseName, "org.apache.cassandra.locator.SimpleStrategy", 1, null);
cluster.addKeyspace(ksDef);
}catch (HInvalidRequestException e){
@@ -44,17 +46,22 @@ class CassandraDatabase implements Database {
}
public Group createGroup(String groupId) {
- CassandraGroup group = new CassandraGroup(groupId, keyspace, cluster);
- return group;
+ Group group = new CassandraGroup(groupId, keyspace, cluster);
+ return group;
}
public Group getGroup(String groupId) {
- return createGroup(groupId);
+ Group group = createGroup(groupId);
+ return group;
}
- public void deleteGroup(String key) {
- cluster.dropColumnFamily(keyspace.getKeyspaceName(), key);
+ public void deleteGroup(String groupId) throws GroupNotFoundException {
+ try{
+ cluster.dropColumnFamily(keyspace.getKeyspaceName(), groupId);
+ } catch(HInvalidRequestException e){
+ throw new GroupNotFoundException("Group does not exists "+groupId);
+ }
}
}
diff --git a/collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraGroup.java b/collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraGroup.java
index 3b29399e3e..30ab8698b4 100644
--- a/collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraGroup.java
+++ b/collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraGroup.java
@@ -29,6 +29,8 @@ import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
import me.prettyprint.hector.api.exceptions.HInvalidRequestException;
import me.prettyprint.hector.api.factory.HFactory;
import org.apache.tuscany.nosqldatastore.Group;
+import org.apache.tuscany.nosqldatastore.exception.DuplicateEntryException;
+import org.apache.tuscany.nosqldatastore.exception.EntryNotFoundException;
class CassandraGroup implements Group {
@@ -36,35 +38,57 @@ class CassandraGroup implements Group {
private ColumnFamilyUpdater<String, String> updater;
private final StringSerializer se = StringSerializer.get();
- CassandraGroup(String groupId, Keyspace keyspace, Cluster cluster) {
+ CassandraGroup(String groupId, Keyspace keyspace, Cluster cluster) {
try{
- ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition(keyspace.getKeyspaceName(), groupId);
- cluster.addColumnFamily(cfDef);
+ ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition(keyspace.getKeyspaceName(), groupId);
+ cluster.addColumnFamily(cfDef);
}catch (HInvalidRequestException e){
System.out.println("Connect to the existing group...");
}
template = new ThriftColumnFamilyTemplate<String, String>(keyspace,groupId,se,se,HFactory.createMutator(keyspace, se));
- }
+ }
- public void addEntry(String key, Object value) {
- updater = template.createUpdater(key);
- updater.setString(key, (String) value);
- template.update(updater);
- template.addColumn(key, se);
- }
+ public void addEntry(String key, String value) throws DuplicateEntryException {
+ ColumnFamilyResult wrapper = template.queryColumns(key);
+ if(wrapper.getString(key) == null){
+ updater = template.createUpdater(key);
+ updater.setString(key, value);
+ template.update(updater);
+ template.addColumn(key, se);
+ return;
+ }
+ throw new DuplicateEntryException("Entry already exist for key "+key);
+ }
- public void deleteEntry(String key) {
- template.deleteColumn(key, key);
-
- }
+ public void deleteEntry(String key) throws EntryNotFoundException {
+ ColumnFamilyResult wrapper = template.queryColumns(key);
+ if(wrapper.getString(key) != null){
+ template.deleteColumn(key, key);
+ return;
+ }
+ throw new EntryNotFoundException("Entry does not exist for key "+key);
+ }
- public Object getValue(String key) {
+ public String getEntry(String key) throws EntryNotFoundException {
ColumnFamilyResult wrapper = template.queryColumns(key);
- return wrapper.getString(key);
+ String entry = wrapper.getString(key);
+ if(entry != null){
+ return entry;
+ }
+ throw new EntryNotFoundException("Entry does not exist for key "+key);
+
}
- public void updateEntry(String key, Object value) {
- addEntry(key, value);
- }
+ public void updateEntry(String key, String value) throws EntryNotFoundException {
+ ColumnFamilyResult wrapper = template.queryColumns(key);
+ if(wrapper.getString(key) != null){
+ updater = template.createUpdater(key);
+ updater.setString(key, value);
+ template.update(updater);
+ template.addColumn(key, se);
+ return;
+ }
+ throw new EntryNotFoundException("Entry does not exist for key "+key);
+ }
}
diff --git a/collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraSession.java b/collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraSession.java
index 7a9faf7b43..186d9f8d1a 100644
--- a/collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraSession.java
+++ b/collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraSession.java
@@ -20,31 +20,46 @@ package org.apache.tuscany.nosqldatastore.cassandra;
import me.prettyprint.cassandra.service.CassandraHostConfigurator;
import me.prettyprint.hector.api.Cluster;
+import me.prettyprint.hector.api.exceptions.HInvalidRequestException;
+import me.prettyprint.hector.api.exceptions.HectorException;
import me.prettyprint.hector.api.factory.HFactory;
import org.apache.tuscany.nosqldatastore.Database;
import org.apache.tuscany.nosqldatastore.Session;
+import org.apache.tuscany.nosqldatastore.exception.DatabaseNotFoundException;
+import org.apache.tuscany.nosqldatastore.exception.GroupNotFoundException;
+import org.apache.tuscany.nosqldatastore.exception.SessionException;
public class CassandraSession implements Session {
-
- private CassandraHostConfigurator cassandraHostConfigurator;
- private Cluster cluster;
-
- public CassandraSession(String clusterName) {
- cluster = HFactory.getOrCreateCluster("Test Cluster", "127.0.0.1:9160");
- }
-
- public Database createDatabase(String keyspaceName) {
- Database database = new CassandraDatabase(keyspaceName, cluster);
- return database;
- }
-
- public Database getDatabase(String keyspaceName) {
- Database database = new CassandraDatabase(keyspaceName, cluster);
- return database;
- }
-
- public void deleteDatabase(String keyspaceName) {
- cluster.dropKeyspace(keyspaceName);
- }
+
+ private CassandraHostConfigurator cassandraHostConfigurator;
+ private Cluster cluster;
+
+ public CassandraSession(String clusterName) {
+ cluster = HFactory.getOrCreateCluster("Test Cluster", "127.0.0.1:9160");
+ }
+
+ public Database createDatabase(String databaseName) throws SessionException {
+ Database database = null;
+ try{
+ database = new CassandraDatabase(databaseName, cluster);
+ } catch (HectorException e){
+ throw new SessionException("Session error");
+ }
+ return database;
+ }
+
+ public Database getDatabase(String databaseName) throws SessionException {
+ return createDatabase(databaseName);
+ }
+
+ public void deleteDatabase(String databaseName) throws DatabaseNotFoundException, SessionException {
+ try{
+ cluster.dropKeyspace(databaseName);
+ } catch(HInvalidRequestException e){
+ throw new DatabaseNotFoundException("Database "+databaseName+" not found");
+ } catch (HectorException e){
+ throw new SessionException("Session error");
+ }
+ }
}