summaryrefslogtreecommitdiffstats
path: root/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseGroup.java
diff options
context:
space:
mode:
Diffstat (limited to 'collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseGroup.java')
-rw-r--r--collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseGroup.java76
1 files changed, 53 insertions, 23 deletions
diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseGroup.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseGroup.java
index f57b70ee60..24cab2d6de 100644
--- a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseGroup.java
+++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseGroup.java
@@ -23,6 +23,8 @@ import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.*;
import org.apache.tuscany.nosqldatastore.Group;
+import org.apache.tuscany.nosqldatastore.exception.DuplicateEntryException;
+import org.apache.tuscany.nosqldatastore.exception.EntryNotFoundException;
import java.io.IOException;
import java.util.HashMap;
@@ -38,44 +40,72 @@ public class HBaseGroup implements Group {
this.cf = cf;
}
- public void addEntry(String key, Object value) {
- Put put = new Put(key.getBytes());
- put.add(cf.getBytes(),key.getBytes(),((String)value).getBytes());
- try {
- table.put(put);
- table.flushCommits();
- } catch (IOException e) {
- System.out.println("Error: Adding the entry for the Key "+key);
- e.printStackTrace();
+ public void addEntry(String key, String value) throws DuplicateEntryException {
+ if(get(key) == null){
+ Put put = new Put(key.getBytes());
+ put.add(cf.getBytes(),key.getBytes(),value.getBytes());
+ try {
+ table.put(put);
+ table.flushCommits();
+ } catch (IOException e) {
+ System.out.println("Error: Adding the entry for the Key "+key);
+ e.printStackTrace();
+ }
+ } else {
+ throw new DuplicateEntryException("Duplicate entry for key "+key);
}
+ }
+ public void updateEntry(String key, String value) throws EntryNotFoundException {
+ if(get(key) != null){
+ Put put = new Put(key.getBytes());
+ put.add(cf.getBytes(),key.getBytes(),value.getBytes());
+ try {
+ table.put(put);
+ table.flushCommits();
+ } catch (IOException e) {
+ System.out.println("Error: Adding the entry for the Key "+key);
+ e.printStackTrace();
+ }
+ } else {
+ throw new EntryNotFoundException("Entry does not exist for key "+key);
+ }
}
- public void updateEntry(String key, Object value) {
- addEntry(key,value);
+ public void deleteEntry(String key) throws EntryNotFoundException {
+ if(get(key) != null){
+ Delete delete = new Delete(key.getBytes());
+ delete.deleteColumn(cf.getBytes(), key.getBytes());
+ try {
+ table.delete(delete);
+ table.flushCommits();
+ } catch (IOException e) {
+ System.out.println("Error: Deleting the entry for the Key "+key);
+ e.printStackTrace();
+ }
+ } else {
+ throw new EntryNotFoundException("Entry does not exist for key "+key);
+ }
}
- public void deleteEntry(String key) {
- Delete delete = new Delete(key.getBytes());
- delete.deleteColumn(cf.getBytes(), key.getBytes());
- try {
- table.delete(delete);
- table.flushCommits();
- } catch (IOException e) {
- System.out.println("Error: Deleting the entry for the Key "+key);
- e.printStackTrace();
+ public String getEntry(String key) throws EntryNotFoundException {
+ byte[] b;
+ if((b = get(key)) != null){
+ return new String(b);
+ } else {
+ throw new EntryNotFoundException("Entry does not exist for key "+key);
}
}
- public Object getValue(String key) {
+ private byte[] get(String key){
Result result = null;
Get get = new Get(key.getBytes());
try {
result = table.get(get);
} catch (IOException e) {
- System.out.println("Error: Retrieving the value or the Key "+key);
+ System.out.println("Error: Getting the entry for the Key "+key);
e.printStackTrace();
}
- return new String(result.getValue(cf.getBytes(),key.getBytes()));
+ return result.getValue(cf.getBytes(),key.getBytes());
}
}