From ab2c29fcff9565ac7d3308307d4b87016d0de967 Mon Sep 17 00:00:00 2001 From: Erich Nachbar Date: Mon, 1 Mar 2010 16:11:24 -0800 Subject: [PATCH 1/5] trivial fixes in ant build file to allow javadoc creation. --- build.xml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/build.xml b/build.xml index 022584c..15ea083 100644 --- a/build.xml +++ b/build.xml @@ -9,6 +9,7 @@ + @@ -89,19 +90,20 @@ - + - + From 4c9f168392a0872416f291f810570ae24df5fd04 Mon Sep 17 00:00:00 2001 From: Erich Nachbar Date: Mon, 1 Mar 2010 16:11:40 -0800 Subject: [PATCH 2/5] excluding docs directory --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d1c2c71..fb89be6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ build/ classes/ dist/ *.class -*~ \ No newline at end of file +*~ +docs/ From d5e1ee828c723a5e8f2794938a62fb788eb34dc3 Mon Sep 17 00:00:00 2001 From: Erich Nachbar Date: Thu, 4 Mar 2010 00:09:40 -0800 Subject: [PATCH 3/5] added simple unit test to demonstrate usage --- .../meetup/beeno/BasicOrmPersistanceTest.java | 133 ++++++++++++++++++ test/java/meetup/beeno/SimpleEntity.java | 64 +++++++++ 2 files changed, 197 insertions(+) create mode 100644 test/java/meetup/beeno/BasicOrmPersistanceTest.java create mode 100644 test/java/meetup/beeno/SimpleEntity.java diff --git a/test/java/meetup/beeno/BasicOrmPersistanceTest.java b/test/java/meetup/beeno/BasicOrmPersistanceTest.java new file mode 100644 index 0000000..fd8864f --- /dev/null +++ b/test/java/meetup/beeno/BasicOrmPersistanceTest.java @@ -0,0 +1,133 @@ +package meetup.beeno; + +import static org.junit.Assert.*; + +import java.util.List; + +import junit.framework.Assert; + +import meetup.beeno.Criteria; +import meetup.beeno.EntityService; +import meetup.beeno.HBaseException; +import meetup.beeno.Query; +import meetup.beeno.mapping.MappingException; +import meetup.beeno.util.HUtil; + +import org.apache.hadoop.hbase.HBaseConfiguration; +import org.apache.hadoop.hbase.HColumnDescriptor; +import org.apache.hadoop.hbase.HTableDescriptor; +import org.apache.hadoop.hbase.TableNotFoundException; +import org.apache.hadoop.hbase.client.HBaseAdmin; +import org.apache.hadoop.hbase.client.HTable; +import org.apache.hadoop.hbase.client.HTablePool; +import org.apache.hadoop.hbase.util.Bytes; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/* + * Commands to create the correspondign hbase schema in HBase Shell + * create 'test_simple-by_photoId', '__idx__', 'props' + * create 'test_simple', 'props' + * Note: This is not required. The test case creates the corresponding tables automatically. + */ + +public class BasicOrmPersistanceTest { + + private static HUtil _hUtil; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + + HBaseConfiguration conf = new HBaseConfiguration(); + conf.set("hbase.master", "localhost"); + HTablePool pool = new HTablePool(conf, 10); + + _hUtil = new HUtil(); + _hUtil.setPool(pool); + + HBaseAdmin admin = new HBaseAdmin(conf); + + // drop tables + try { + admin.disableTable("test_simple-by_photoId"); + admin.deleteTable("test_simple-by_photoId"); + } catch (TableNotFoundException e) { + // silently swallow + } + try { + admin.disableTable("test_simple"); + admin.deleteTable("test_simple"); + } catch (TableNotFoundException e) { + // silently swallow + } + + // create tables + HTableDescriptor by_photoId_idx = new HTableDescriptor( + "test_simple-by_photoId"); + by_photoId_idx.addFamily(new HColumnDescriptor("__idx__")); + by_photoId_idx.addFamily(new HColumnDescriptor("props")); + admin.createTable(by_photoId_idx); + + // create tables & index + HTableDescriptor test_simple = new HTableDescriptor("test_simple"); + test_simple.addFamily(new HColumnDescriptor("props")); + admin.createTable(test_simple); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testCreationAndCriteriaRetrieval() throws HBaseException { + + EntityService service = EntityService + .create(SimpleEntity.class); + + for (int i = 0; i < 10; i++) { + SimpleEntity se = new SimpleEntity(); + se.setId("r:" + System.currentTimeMillis()); + se.setStringProperty("superman"); + se.setDoubleProperty(12.1111D); + se.setPhotoIdProperty("PHOTOID" + i); + service.save(se); + } + + // query rows + Query query = service.query().using( + Criteria.eq("photoIdProperty", "PHOTOID5")); + List items = query.execute(); + for (SimpleEntity e : items) { + System.out.println("items: " + ((SimpleEntity) e).getId()); + } + assertEquals(1, items.size()); + } + + @Test + public void testCreationAndNonExistantCriteriaRetrieval() throws HBaseException { + + EntityService service = EntityService.create(SimpleEntity.class); + + // query rows + Query query = service.query().using( + Criteria.eq("IDONTEXISTFORSURE", "PHOTOID5")); + List items = query.execute(); + for (SimpleEntity e : items) { + System.out.println("items: " + ((SimpleEntity) e).getId()); + } + assertEquals(0, items.size()); + } + +} diff --git a/test/java/meetup/beeno/SimpleEntity.java b/test/java/meetup/beeno/SimpleEntity.java new file mode 100644 index 0000000..1f279f0 --- /dev/null +++ b/test/java/meetup/beeno/SimpleEntity.java @@ -0,0 +1,64 @@ +package meetup.beeno; + +import meetup.beeno.HEntity; +import meetup.beeno.HIndex; +import meetup.beeno.HProperty; +import meetup.beeno.HRowKey; + +/** + * Simple entity class with mapped properties + */ +@HEntity(name="test_simple") +public class SimpleEntity { + String id; + String stringProperty; + int intProperty; + float floatProperty; + double doubleProperty; + long updated = System.currentTimeMillis(); + String photoId; + + public SimpleEntity() { + } + + @HRowKey + public String getId() { return this.id; } + public void setId(String id) { this.id = id; } + + @HProperty(family="props", name="stringcol") + public String getStringProperty() { return stringProperty; } + public void setStringProperty( String stringProperty ) { + this.stringProperty = stringProperty; + } + + @HProperty(family="props", name="photoId", indexes = {@HIndex(date_col="props:updated", date_invert=true)}) + public String getPhotoIdProperty() { return photoId; } + public void setPhotoIdProperty( String photoId ) { + this.photoId = photoId; + } + + @HProperty(family="props", name="intcol") + public int getIntProperty() { return intProperty; } + public void setIntProperty( int intProperty ) { + this.intProperty = intProperty; + } + + @HProperty(family="props", name="floatcol") + public float getFloatProperty() { return floatProperty; } + public void setFloatProperty( float floatProperty ) { + this.floatProperty = floatProperty; + } + + @HProperty(family="props", name="doublecol") + public double getDoubleProperty() { return doubleProperty; } + public void setDoubleProperty( double doubleProperty ) { + this.doubleProperty = doubleProperty; + } + + @HProperty(family="props", name="updated") + public long getUpdated() { return updated; } + public void setUpdated( long updateTime ) { + this.updated = updateTime; + } +} + From c63577ee89e6d0b3a94d6d0c340c26b6d38bc053 Mon Sep 17 00:00:00 2001 From: Erich Nachbar Date: Thu, 4 Mar 2010 00:10:08 -0800 Subject: [PATCH 4/5] added some more libs to project classpath to make unit tests compile --- .classpath | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.classpath b/.classpath index dc5f793..bc15cad 100644 --- a/.classpath +++ b/.classpath @@ -10,5 +10,8 @@ + + + From 3af01e8b65ff0ac56eef537ef6141db659e00dd9 Mon Sep 17 00:00:00 2001 From: Erich Nachbar Date: Thu, 4 Mar 2010 14:55:18 -0800 Subject: [PATCH 5/5] fix for delivering wrong result set in case of a missing/misspleled property. Throws now an Exception. --- src/java/meetup/beeno/ScanByIndex.java | 9 +++-- .../meetup/beeno/BasicOrmPersistanceTest.java | 35 +++++++++++++------ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/java/meetup/beeno/ScanByIndex.java b/src/java/meetup/beeno/ScanByIndex.java index 4812ddb..4b639bf 100644 --- a/src/java/meetup/beeno/ScanByIndex.java +++ b/src/java/meetup/beeno/ScanByIndex.java @@ -11,6 +11,7 @@ import meetup.beeno.mapping.EntityInfo; import meetup.beeno.mapping.IndexMapping; +import meetup.beeno.mapping.MappingException; import meetup.beeno.util.HUtil; import meetup.beeno.util.PBUtil; @@ -151,9 +152,10 @@ protected ResultScanner getIndexScanner(String tablename, * for the query. * @param expressions * @return + * @throws MappingException */ protected Criteria.PropertyExpression selectIndexedExpression(EntityInfo info, - List expressions) { + List expressions) throws MappingException { // first look for a direct match expression for (Criteria.Expression e : expressions) { if (e instanceof Criteria.RequireExpression) @@ -179,8 +181,9 @@ protected Criteria.PropertyExpression selectIndexedExpression(EntityInfo info, return propExpr; } } - - return null; + // not property match found + // notifying user to avoid returning wrong result set + throw new MappingException(info.getClass(), "Can't find property " + expressions); } diff --git a/test/java/meetup/beeno/BasicOrmPersistanceTest.java b/test/java/meetup/beeno/BasicOrmPersistanceTest.java index fd8864f..13a89c6 100644 --- a/test/java/meetup/beeno/BasicOrmPersistanceTest.java +++ b/test/java/meetup/beeno/BasicOrmPersistanceTest.java @@ -66,7 +66,7 @@ public static void setUpBeforeClass() throws Exception { // create tables HTableDescriptor by_photoId_idx = new HTableDescriptor( - "test_simple-by_photoId"); + "test_simple-by_photoId"); by_photoId_idx.addFamily(new HColumnDescriptor("__idx__")); by_photoId_idx.addFamily(new HColumnDescriptor("props")); admin.createTable(by_photoId_idx); @@ -94,7 +94,7 @@ public void tearDown() throws Exception { public void testCreationAndCriteriaRetrieval() throws HBaseException { EntityService service = EntityService - .create(SimpleEntity.class); + .create(SimpleEntity.class); for (int i = 0; i < 10; i++) { SimpleEntity se = new SimpleEntity(); @@ -106,8 +106,7 @@ public void testCreationAndCriteriaRetrieval() throws HBaseException { } // query rows - Query query = service.query().using( - Criteria.eq("photoIdProperty", "PHOTOID5")); + Query query = service.query().using(Criteria.eq("photoIdProperty", "PHOTOID5")); List items = query.execute(); for (SimpleEntity e : items) { System.out.println("items: " + ((SimpleEntity) e).getId()); @@ -121,13 +120,29 @@ public void testCreationAndNonExistantCriteriaRetrieval() throws HBaseException EntityService service = EntityService.create(SimpleEntity.class); // query rows - Query query = service.query().using( - Criteria.eq("IDONTEXISTFORSURE", "PHOTOID5")); - List items = query.execute(); - for (SimpleEntity e : items) { - System.out.println("items: " + ((SimpleEntity) e).getId()); + try{ + Query query = service.query().using(Criteria.eq("IDONTEXISTFORSURE", "PHOTOID5")); + List items = query.execute(); + fail("Should have gotten a QueryException for the missing properties."); + } catch(QueryException e){ + //Sweet! Got our expected exception. + } + } + + @Test + public void testCreationOfMixedExistingAndNonExistantCriteriaRetrieval() throws HBaseException { + + EntityService service = EntityService.create(SimpleEntity.class); + + // query rows + try{ + Query query = service.query().using(Criteria.eq("IDONTEXISTFORSURE", "PHOTOID5")).where(Criteria.eq("photoIdProperty", "PHOTOID5")); + List items = query.execute(); + fail("Should have gotten a QueryException for the missing properties."); + } catch(QueryException e){ + //Sweet! Got our expected exception. } - assertEquals(0, items.size()); } + }