summaryrefslogtreecommitdiffstats
path: root/collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraGroup.java
diff options
context:
space:
mode:
Diffstat (limited to 'collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraGroup.java')
-rw-r--r--collaboration/GSoC-2011-Eranda/cassandra/src/main/java/org/apache/tuscany/nosqldatastore/cassandra/CassandraGroup.java62
1 files changed, 43 insertions, 19 deletions
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);
+ }
}