From 611747a6fea6b03c16d955af639d101319cf0b7f Mon Sep 17 00:00:00 2001 From: eranda Date: Sat, 9 Jul 2011 09:02:32 +0000 Subject: 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 --- collaboration/GSoC-2011-Eranda/hbase/pom.xml | 5 ++ .../apache/tuscany/nosqldatastore/Database.java | 60 ++++++++------ .../org/apache/tuscany/nosqldatastore/Group.java | 64 +++++++++------ .../org/apache/tuscany/nosqldatastore/Session.java | 62 ++++++++------ .../tuscany/nosqldatastore/SessionFactory.java | 22 ++--- .../exception/DatabaseNotFoundException.java | 26 ++++++ .../exception/DuplicateEntryException.java | 25 ++++++ .../exception/EntryNotFoundException.java | 25 ++++++ .../exception/GroupNotFoundException.java | 25 ++++++ .../nosqldatastore/exception/SessionException.java | 25 ++++++ .../nosqldatastore/hbase/HBaseDatatabase.java | 37 ++++----- .../tuscany/nosqldatastore/hbase/HBaseGroup.java | 76 +++++++++++------ .../tuscany/nosqldatastore/hbase/HBaseSession.java | 37 ++++++--- .../tuscany/nosqldatastore/hbase/HBaseTest.java | 48 ----------- .../nosqldatastore/hbase/HBaseTestCase.java | 96 ++++++++++++++++++++++ 15 files changed, 441 insertions(+), 192 deletions(-) create mode 100644 collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/DatabaseNotFoundException.java create mode 100644 collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/DuplicateEntryException.java create mode 100644 collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/EntryNotFoundException.java create mode 100644 collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/GroupNotFoundException.java create mode 100644 collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/SessionException.java delete mode 100644 collaboration/GSoC-2011-Eranda/hbase/src/test/java/org/apache/tuscany/nosqldatastore/hbase/HBaseTest.java create mode 100644 collaboration/GSoC-2011-Eranda/hbase/src/test/java/org/apache/tuscany/nosqldatastore/hbase/HBaseTestCase.java (limited to 'collaboration/GSoC-2011-Eranda/hbase') diff --git a/collaboration/GSoC-2011-Eranda/hbase/pom.xml b/collaboration/GSoC-2011-Eranda/hbase/pom.xml index fb454ef78a..a726ce23fb 100644 --- a/collaboration/GSoC-2011-Eranda/hbase/pom.xml +++ b/collaboration/GSoC-2011-Eranda/hbase/pom.xml @@ -38,6 +38,11 @@ under the License. hbase 0.90.3 + + junit + junit + 4.8.2 + diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Database.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Database.java index ad80b06f04..8dfdee5356 100644 --- a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Database.java +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Database.java @@ -1,26 +1,34 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.nosqldatastore; - -public interface Database { - Group createGroup(String groupId); - Group getGroup(String groupId); - void deleteGroup(String groupId); -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.nosqldatastore; + +import org.apache.tuscany.nosqldatastore.exception.GroupNotFoundException; + +/* +Database: +Use to manipulate the groups, where groups are use to categorize the data +1. Creating a group +2. Retrieving a group +3. Deleting a group +*/ +public interface Database { + Group createGroup(String groupId); + Group getGroup(String groupId); + void deleteGroup(String groupId) throws GroupNotFoundException; +} diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Group.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Group.java index 634ab59cf0..78eeffc383 100644 --- a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Group.java +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Group.java @@ -1,27 +1,37 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.nosqldatastore; - -public interface Group { - void addEntry(String key, Object value); - void updateEntry(String key, Object value); - void deleteEntry(String key); - Object getValue(String key); -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.nosqldatastore; + +import org.apache.tuscany.nosqldatastore.exception.DuplicateEntryException; +import org.apache.tuscany.nosqldatastore.exception.EntryNotFoundException; + +/* +Group: +Group use to categorize the data into different categories +1. Add entry to the database +2. Update an existing entry +3. Delete an existing entry +4. Get a existing entry + */ +public interface Group { + void addEntry(String key, String value) throws DuplicateEntryException; + void updateEntry(String key, String value) throws EntryNotFoundException; + void deleteEntry(String key) throws EntryNotFoundException; + String getEntry(String key) throws EntryNotFoundException; +} diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Session.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Session.java index 5aa9641715..3d2b93cc4b 100644 --- a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Session.java +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Session.java @@ -1,26 +1,36 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tuscany.nosqldatastore; - -public interface Session { - Database createDatabase(String databaseName); - Database getDatabase(String databaseName); - void deleteDatabase(String databaseName); -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.nosqldatastore; + +import org.apache.tuscany.nosqldatastore.exception.DatabaseNotFoundException; +import org.apache.tuscany.nosqldatastore.exception.GroupNotFoundException; +import org.apache.tuscany.nosqldatastore.exception.SessionException; + +/* +Session: +Session is a database session use to access the database +1. Create a database +2. Get instance of existing database +3. Delete a database +*/ +public interface Session { + Database createDatabase(String databaseName) throws SessionException; + Database getDatabase(String databaseName) throws SessionException; + void deleteDatabase(String databaseName) throws DatabaseNotFoundException, SessionException; +} diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/SessionFactory.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/SessionFactory.java index bc645bc118..ead8b2ab59 100644 --- a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/SessionFactory.java +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/SessionFactory.java @@ -22,15 +22,15 @@ package org.apache.tuscany.nosqldatastore; import org.apache.tuscany.nosqldatastore.hbase.HBaseSession; public class SessionFactory { - - private static org.apache.tuscany.nosqldatastore.Session session; - - private SessionFactory(){ - } - - public static Session getHBaseSession(){ - session = new HBaseSession("hbase"); - return session; - } - + + private static org.apache.tuscany.nosqldatastore.Session session; + + public SessionFactory(){ + } + + public Session getHBaseSession(){ + session = new HBaseSession("hbase"); + return session; + } + } diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/DatabaseNotFoundException.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/DatabaseNotFoundException.java new file mode 100644 index 0000000000..871d0ac7ed --- /dev/null +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/DatabaseNotFoundException.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.nosqldatastore.exception; + +public class DatabaseNotFoundException extends Exception{ + + public DatabaseNotFoundException(String msg){ + super(msg); + } +} diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/DuplicateEntryException.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/DuplicateEntryException.java new file mode 100644 index 0000000000..4bd19fcd99 --- /dev/null +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/DuplicateEntryException.java @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.nosqldatastore.exception; + +public class DuplicateEntryException extends Exception{ + public DuplicateEntryException(String msg) { + super(msg); + } +} diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/EntryNotFoundException.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/EntryNotFoundException.java new file mode 100644 index 0000000000..f2f459d200 --- /dev/null +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/EntryNotFoundException.java @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.nosqldatastore.exception; + +public class EntryNotFoundException extends Exception{ + public EntryNotFoundException(String msg) { + super(msg); + } +} diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/GroupNotFoundException.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/GroupNotFoundException.java new file mode 100644 index 0000000000..70f5326745 --- /dev/null +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/GroupNotFoundException.java @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.nosqldatastore.exception; + +public class GroupNotFoundException extends Exception{ + public GroupNotFoundException(String msg){ + super(msg); + } +} diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/SessionException.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/SessionException.java new file mode 100644 index 0000000000..016c7e6264 --- /dev/null +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/exception/SessionException.java @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.nosqldatastore.exception; + +public class SessionException extends Exception{ + public SessionException(String msg){ + super(msg); + } +} diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseDatatabase.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseDatatabase.java index 5d49fd76da..962a76eb5d 100644 --- a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseDatatabase.java +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseDatatabase.java @@ -29,30 +29,27 @@ import org.apache.tuscany.nosqldatastore.Group; import java.io.IOException; public class HBaseDatatabase implements Database{ - + private HTable table; private HBaseAdmin admin; - HBaseDatatabase(String dbname, HBaseAdmin admin){ + HBaseDatatabase(String dbname, HBaseAdmin admin) throws IOException { Configuration conf = HBaseConfiguration.create(); - try { - try{ - HTableDescriptor descriptor = new HTableDescriptor(dbname); - admin.createTable(descriptor); - } catch (MasterNotRunningException e) { - e.printStackTrace(); - } catch (ZooKeeperConnectionException e) { - e.printStackTrace(); - } catch (TableExistsException e){ - System.out.println("connected to existing database...."); - } finally { - table = new HTable(conf,dbname); - System.out.println(new String(table.getTableName())); - this.admin = admin; - table.flushCommits(); - } - }catch (IOException ex) { - ex.printStackTrace(); + + try{ + HTableDescriptor descriptor = new HTableDescriptor(dbname); + admin.createTable(descriptor); + } catch (MasterNotRunningException e) { + e.printStackTrace(); + } catch (ZooKeeperConnectionException e) { + e.printStackTrace(); + } catch (TableExistsException e){ + System.out.println("connected to existing database...."); + } finally { + table = new HTable(conf,dbname); + System.out.println(new String(table.getTableName())); + this.admin = admin; + table.flushCommits(); } } 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()); } } diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseSession.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseSession.java index 83648eadc0..6279c75181 100644 --- a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseSession.java +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseSession.java @@ -26,6 +26,8 @@ import org.apache.hadoop.hbase.ZooKeeperConnectionException; import org.apache.hadoop.hbase.client.HBaseAdmin; 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.SessionException; import java.io.IOException; @@ -46,23 +48,36 @@ public class HBaseSession implements Session{ } } - public Database createDatabase(String databaseName) { - Database database = new HBaseDatatabase(databaseName, admin); - return database; + public Database createDatabase(String databaseName) throws SessionException { + Database database = null; + try { + database = new HBaseDatatabase(databaseName, admin); + } catch (IOException e) { + throw new SessionException("Session Error"); + } + return database; } - public Database getDatabase(String databaseName) { - Database database = new HBaseDatatabase(databaseName, admin); - return database; + public Database getDatabase(String databaseName) throws SessionException { + Database database = null; + try { + database = new HBaseDatatabase(databaseName, admin); + } catch (IOException e) { + throw new SessionException("Session Error"); + } + return database; } - public void deleteDatabase(String databaseName) { + public void deleteDatabase(String databaseName) throws DatabaseNotFoundException, SessionException { try { - admin.disableTable(databaseName); - admin.deleteTable(databaseName); + if(admin.tableExists(databaseName)){ + admin.disableTable(databaseName); + admin.deleteTable(databaseName); + } else { + throw new DatabaseNotFoundException("Database "+databaseName+" not found"); + } } catch (IOException e) { - System.out.println("Error: Creating the database "+databaseName); - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + throw new SessionException("Session Error"); } } } diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/test/java/org/apache/tuscany/nosqldatastore/hbase/HBaseTest.java b/collaboration/GSoC-2011-Eranda/hbase/src/test/java/org/apache/tuscany/nosqldatastore/hbase/HBaseTest.java deleted file mode 100644 index 1d51187176..0000000000 --- a/collaboration/GSoC-2011-Eranda/hbase/src/test/java/org/apache/tuscany/nosqldatastore/hbase/HBaseTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tuscany.nosqldatastore.hbase; - -import org.apache.tuscany.nosqldatastore.Database; -import org.apache.tuscany.nosqldatastore.Group; -import org.apache.tuscany.nosqldatastore.Session; -import org.apache.tuscany.nosqldatastore.SessionFactory; - -public class HBaseTest { - - Session session; - - private HBaseTest(){ - session = SessionFactory.getHBaseSession(); - } - - public static void main(String[] args){ - HBaseTest test = new HBaseTest(); - test.start(); - } - - public void start(){ - Database database = session.createDatabase("SampleDB"); - Group group = database.createGroup("group1"); - group.addEntry("eranda", "Eranda Sooriyabandara"); - System.out.println(group.getValue("eranda")); - group.deleteEntry("eranda"); - database.deleteGroup("group1"); - session.deleteDatabase("SampleDB"); - } -} diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/test/java/org/apache/tuscany/nosqldatastore/hbase/HBaseTestCase.java b/collaboration/GSoC-2011-Eranda/hbase/src/test/java/org/apache/tuscany/nosqldatastore/hbase/HBaseTestCase.java new file mode 100644 index 0000000000..f88d3b9d4b --- /dev/null +++ b/collaboration/GSoC-2011-Eranda/hbase/src/test/java/org/apache/tuscany/nosqldatastore/hbase/HBaseTestCase.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.nosqldatastore.hbase; + +import org.apache.tuscany.nosqldatastore.Database; +import org.apache.tuscany.nosqldatastore.Group; +import org.apache.tuscany.nosqldatastore.Session; +import org.apache.tuscany.nosqldatastore.SessionFactory; + +import org.apache.tuscany.nosqldatastore.exception.EntryNotFoundException; +import org.junit.Before; +import org.junit.Test; + +import org.junit.Assert; + +public class HBaseTestCase { + + private Session session; + + @Before + public void init(){ + SessionFactory factory = new SessionFactory(); + session = factory.getHBaseSession(); + } + + @Test + public void testAddEntry() throws Exception { + Database db = session.createDatabase("TwitApp"); + Group group = db.createGroup("twits"); + + group.addEntry("twitName1", "Here I am"); + group.addEntry("twitName2", "I am not here"); + group.addEntry("twitName3", "Sometimes"); + + Assert.assertEquals(group.getEntry("twitName1"), "Here I am"); + Assert.assertEquals(group.getEntry("twitName2"), "I am not here"); + Assert.assertEquals(group.getEntry("twitName3"), "Sometimes"); + + db.deleteGroup("twits"); + session.deleteDatabase("TwitApp"); + } + + @Test + public void testDeleteEntry() throws Exception { + Database db = session.createDatabase("TwitApp"); + Group group = db.createGroup("twits"); + + group.addEntry("twitName1", "Here I am"); + group.addEntry("twitName2", "I am not here"); + group.addEntry("twitName3", "Sometimes"); + + group.deleteEntry("twitName1"); + + try { + Assert.assertNull(group.getEntry("twitName1")); + Assert.fail("Deleted entry exists"); + } catch(EntryNotFoundException e) { + //Expected + } + + db.deleteGroup("twits"); + session.deleteDatabase("TwitApp"); + } + + @Test + public void testUpdateEntry() throws Exception { + Database db = session.createDatabase("TwitApp"); + Group group = db.createGroup("twits"); + + group.addEntry("twitName1", "Here I am"); + group.addEntry("twitName2", "I am not here"); + group.addEntry("twitName3", "Sometimes"); + + group.updateEntry("twitName3", "Never"); + Assert.assertEquals(group.getEntry("twitName3"), "Never"); + + db.deleteGroup("twits"); + session.deleteDatabase("TwitApp"); + } +} -- cgit v1.2.3