summaryrefslogtreecommitdiffstats
path: root/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseGroup.java
diff options
context:
space:
mode:
authoreranda <eranda@13f79535-47bb-0310-9956-ffa450edef68>2011-07-09 09:02:32 +0000
committereranda <eranda@13f79535-47bb-0310-9956-ffa450edef68>2011-07-09 09:02:32 +0000
commit611747a6fea6b03c16d955af639d101319cf0b7f (patch)
tree314c6e820ba60d23fe00e9e44fad7cd5bf7c3fa9 /collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseGroup.java
parent800d0da7d863cc75004b9e034646d457156edf97 (diff)
1. Remove static variable in SessionFactory
2. Set spaces to 4 3. Exception handling 4. Rename *Test to *TestCase 5. Convert test cases in to JUnit 6. Rename addValue() method in Group.java to addEntry() 7. Updated the addEntry method signature to accept String type only git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1144620 13f79535-47bb-0310-9956-ffa450edef68
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());
}
}