gora-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lewi...@apache.org
Subject svn commit: r1366517 [1/2] - in /gora/branches/goraamazon: ./ gora-core/src/main/java/org/apache/gora/query/ws/impl/ gora-core/src/test/java/org/apache/gora/store/ gora-dynamodb/ gora-dynamodb/conf/ gora-dynamodb/src/examples/ gora-dynamodb/src/example...
Date Fri, 27 Jul 2012 19:59:52 GMT
Author: lewismc
Date: Fri Jul 27 19:59:51 2012
New Revision: 1366517

URL: http://svn.apache.org/viewvc?rev=1366517&view=rev
Log:
patch 2 commit

Added:
    gora/branches/goraamazon/gora-dynamodb/src/examples/
    gora/branches/goraamazon/gora-dynamodb/src/examples/java/
    gora/branches/goraamazon/gora-dynamodb/src/examples/java/org/
    gora/branches/goraamazon/gora-dynamodb/src/examples/java/org/apache/
    gora/branches/goraamazon/gora-dynamodb/src/examples/java/org/apache/gora/
    gora/branches/goraamazon/gora-dynamodb/src/examples/java/org/apache/gora/examples/
    gora/branches/goraamazon/gora-dynamodb/src/examples/java/org/apache/gora/examples/generated/
    gora/branches/goraamazon/gora-dynamodb/src/examples/java/org/apache/gora/examples/generated/person.java
    gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/store/DynamoDBMapping.java
    gora/branches/goraamazon/gora-dynamodb/src/test/
    gora/branches/goraamazon/gora-dynamodb/src/test/conf/
    gora/branches/goraamazon/gora-dynamodb/src/test/conf/AwsCredentials.properties
      - copied unchanged from r1365199, gora/branches/goraamazon/gora-dynamodb/conf/AwsCredentials.properties
    gora/branches/goraamazon/gora-dynamodb/src/test/conf/gora-dynamodb-mapping.xml
    gora/branches/goraamazon/gora-dynamodb/src/test/conf/gora.properties
    gora/branches/goraamazon/gora-dynamodb/src/test/java/
    gora/branches/goraamazon/gora-dynamodb/src/test/java/org/
    gora/branches/goraamazon/gora-dynamodb/src/test/java/org/apache/
    gora/branches/goraamazon/gora-dynamodb/src/test/java/org/apache/gora/
    gora/branches/goraamazon/gora-dynamodb/src/test/java/org/apache/gora/dynamodb/
    gora/branches/goraamazon/gora-dynamodb/src/test/java/org/apache/gora/dynamodb/GoraDynamoDBTestDriver.java
    gora/branches/goraamazon/gora-dynamodb/src/test/java/org/apache/gora/dynamodb/TestDynamoDBStore.java
Removed:
    gora/branches/goraamazon/gora-dynamodb/conf/AwsCredentials.properties
Modified:
    gora/branches/goraamazon/gora-core/src/main/java/org/apache/gora/query/ws/impl/QueryWSBase.java
    gora/branches/goraamazon/gora-core/src/test/java/org/apache/gora/store/DataStoreTestBase.java
    gora/branches/goraamazon/gora-dynamodb/pom.xml
    gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBColumn.java
    gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBQuery.java
    gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBResult.java
    gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBResultSet.java
    gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBRow.java
    gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/store/DynamoDBStore.java
    gora/branches/goraamazon/gora-hbase/src/main/java/org/apache/gora/hbase/store/HBaseStore.java
    gora/branches/goraamazon/pom.xml

Modified: gora/branches/goraamazon/gora-core/src/main/java/org/apache/gora/query/ws/impl/QueryWSBase.java
URL: http://svn.apache.org/viewvc/gora/branches/goraamazon/gora-core/src/main/java/org/apache/gora/query/ws/impl/QueryWSBase.java?rev=1366517&r1=1366516&r2=1366517&view=diff
==============================================================================
--- gora/branches/goraamazon/gora-core/src/main/java/org/apache/gora/query/ws/impl/QueryWSBase.java (original)
+++ gora/branches/goraamazon/gora-core/src/main/java/org/apache/gora/query/ws/impl/QueryWSBase.java Fri Jul 27 19:59:51 2012
@@ -18,9 +18,9 @@
 
 package org.apache.gora.query.ws.impl;
 
-//import org.apache.commons.lang.builder.EqualsBuilder;
-//import org.apache.commons.lang.builder.HashCodeBuilder;
-//import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+import org.apache.commons.lang.builder.ToStringBuilder;
 import org.apache.gora.persistency.Persistent;
 import org.apache.gora.query.Query;
 import org.apache.gora.query.Result;
@@ -29,8 +29,7 @@ import org.apache.gora.store.DataStore;
 /**
  * Base class for Query implementations.
  */
-public abstract class QueryWSBase<K, T extends Persistent> 
-implements Query<K,T>{
+public abstract class QueryWSBase<K, T extends Persistent> implements Query<K,T>{
 	
   protected DataStore<K,T> dataStore;
 
@@ -62,13 +61,6 @@ implements Query<K,T>{
     return dataStore.execute(this);
   }
 
-//  @Override
-//  public void compile() {
-//    if(!isCompiled) {
-//      isCompiled = true;
-//    }
-//  }
-
   @Override
   public void setDataStore(DataStore<K, T> dataStore) {
     this.dataStore = dataStore;
@@ -79,16 +71,6 @@ implements Query<K,T>{
     return dataStore;
   }
 
-//  @Override
-//  public void setQueryString(String queryString) {
-//    this.queryString = queryString;
-//  }
-//
-//  @Override
-//  public String getQueryString() {
-//    return queryString;
-//  }
-
   @Override
   public void setFields(String... fields) {
     this.fields = fields;
@@ -174,16 +156,6 @@ public String[] getFields() {
     return endTime;
   }
 
-//  @Override
-//  public void setFilter(String filter) {
-//    this.filter = filter;
-//  }
-//
-//  @Override
-//  public String getFilter() {
-//    return filter;
-//  }
-
   @Override
   public void setLimit(long limit) {
     this.limit = limit;
@@ -201,66 +173,13 @@ public String[] getFields() {
   public void setConf(Object auth) {
     this.authentication = auth;
   }
-
  
-  /*@SuppressWarnings("unchecked")
-  public void readFields(DataInput in) throws IOException {
-    String dataStoreClass = Text.readString(in);
-    try {
-      dataStore = (DataStore<K, T>) ReflectionUtils.newInstance(ClassLoadingUtils.loadClass(dataStoreClass), conf);
-      dataStore.readFields(in);
-    } catch (ClassNotFoundException ex) {
-      throw new IOException(ex);
-    }
-
-    boolean[] nullFields = IOUtils.readNullFieldsInfo(in);
-
-    if(!nullFields[0])
-      queryString = Text.readString(in);
-    if(!nullFields[1])
-      fields = IOUtils.readStringArray(in);
-    if(!nullFields[2])
-      startKey = IOUtils.deserialize(null, in, null, dataStore.getKeyClass());
-    if(!nullFields[3])
-      endKey = IOUtils.deserialize(null, in, null, dataStore.getKeyClass());
-    if(!nullFields[4])
-      filter = Text.readString(in);
-
-    startTime = WritableUtils.readVLong(in);
-    endTime = WritableUtils.readVLong(in);
-    limit = WritableUtils.readVLong(in);
-  }*/
-
- /* public void write(DataOutput out) throws IOException {
-    //write datastore
-    Text.writeString(out, dataStore.getClass().getCanonicalName());
-    dataStore.write(out);
-
-    IOUtils.writeNullFieldsInfo(out, queryString, (fields)
-        , startKey, endKey, filter);
-
-    if(queryString != null)
-      Text.writeString(out, queryString);
-    if(fields != null)
-      IOUtils.writeStringArray(out, fields);
-    if(startKey != null)
-      IOUtils.serialize(getConf(), out, startKey, dataStore.getKeyClass());
-    if(endKey != null)
-      IOUtils.serialize(getConf(), out, endKey, dataStore.getKeyClass());
-    if(filter != null)
-      Text.writeString(out, filter);
-
-    WritableUtils.writeVLong(out, getStartTime());
-    WritableUtils.writeVLong(out, getEndTime());
-    WritableUtils.writeVLong(out, getLimit());
-  }*/
-
   @SuppressWarnings({ "rawtypes" })
   @Override
   public boolean equals(Object obj) {
     if(obj instanceof QueryWSBase) {
       QueryWSBase that = (QueryWSBase) obj;
-      /*EqualsBuilder builder = new EqualsBuilder();
+      EqualsBuilder builder = new EqualsBuilder();
       builder.append(dataStore, that.dataStore);
       builder.append(queryString, that.queryString);
       builder.append(fields, that.fields);
@@ -269,16 +188,12 @@ public String[] getFields() {
       builder.append(filter, that.filter);
       builder.append(limit, that.limit);
       return builder.isEquals();
-      */
-      if(that!=null)
-    	  return true;
     }
     return false;
   }
 
   @Override
   public int hashCode() {
-    /*
     HashCodeBuilder builder = new HashCodeBuilder();
     builder.append(dataStore);
     builder.append(queryString);
@@ -288,13 +203,10 @@ public String[] getFields() {
     builder.append(filter);
     builder.append(limit);
     return builder.toHashCode();
-    */
-	  return 0;
   }
 
   @Override
   public String toString() {
-    /*
     ToStringBuilder builder = new ToStringBuilder(this);
     builder.append("dataStore", dataStore);
     builder.append("fields", fields);
@@ -304,7 +216,5 @@ public String[] getFields() {
     builder.append("limit", limit);
 
     return builder.toString();
-    */
-    return "";
   }
 }

Modified: gora/branches/goraamazon/gora-core/src/test/java/org/apache/gora/store/DataStoreTestBase.java
URL: http://svn.apache.org/viewvc/gora/branches/goraamazon/gora-core/src/test/java/org/apache/gora/store/DataStoreTestBase.java?rev=1366517&r1=1366516&r2=1366517&view=diff
==============================================================================
--- gora/branches/goraamazon/gora-core/src/test/java/org/apache/gora/store/DataStoreTestBase.java (original)
+++ gora/branches/goraamazon/gora-core/src/test/java/org/apache/gora/store/DataStoreTestBase.java Fri Jul 27 19:59:51 2012
@@ -53,10 +53,11 @@ public abstract class DataStoreTestBase 
   protected DataStore<String,WebPage> webPageStore;
 
   @Deprecated
-  protected abstract DataStore<String,Employee> createEmployeeDataStore() throws IOException ;
+  protected abstract DataStore<String,Employee> createEmployeeDataStore() throws IOException;
 
   @Deprecated
   protected abstract DataStore<String,WebPage> createWebPageDataStore() throws IOException;
+  
 
   /** junit annoyingly forces BeforeClass to be static, so this method
    * should be called from a static block

Modified: gora/branches/goraamazon/gora-dynamodb/pom.xml
URL: http://svn.apache.org/viewvc/gora/branches/goraamazon/gora-dynamodb/pom.xml?rev=1366517&r1=1366516&r2=1366517&view=diff
==============================================================================
--- gora/branches/goraamazon/gora-dynamodb/pom.xml (original)
+++ gora/branches/goraamazon/gora-dynamodb/pom.xml Fri Jul 27 19:59:51 2012
@@ -55,17 +55,25 @@
        <directory>target</directory>
        <outputDirectory>target/classes</outputDirectory>
        <finalName>${project.artifactId}-${project.version}</finalName>
-   <!--<testOutputDirectory>target/test-classes</testOutputDirectory>-->
-   <!--<testSourceDirectory>src/test/java</testSourceDirectory>-->
+       <testOutputDirectory>target/test-classes</testOutputDirectory>
+       <testSourceDirectory>src/test/java</testSourceDirectory>
        <sourceDirectory>src/main/java</sourceDirectory>
        <resources>
-          <resource>
-            <directory>${basedir}/src/main/resources</directory>
-          </resource>
-          <resource>
-            <directory>${basedir}/conf</directory>
-          </resource>
+           <resource>
+               <directory>${basedir}/src/main/resources</directory>
+           </resource>
+           <resource>
+               <directory>${basedir}/conf</directory>
+           </resource>
        </resources>
+       <testResources>
+            <testResource>
+                <directory>src/test/conf/</directory>
+                <includes>
+                    <include>**</include>
+                </includes>
+            </testResource>
+       </testResources>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
@@ -115,14 +123,21 @@
            <groupId>org.apache.gora</groupId>
            <artifactId>gora-core</artifactId>
            <classifier>tests</classifier>
+           <scope>test</scope>
        </dependency>
        
-       <!-- Misc Dependencies -->
+       <!-- Amazon Dependencies -->
        <dependency>
-       	   <groupId>com.amazonaws</groupId>
+           <groupId>com.amazonaws</groupId>
 	   <artifactId>aws-java-sdk</artifactId>
        </dependency>
        
+       <!-- Misc Dependencies -->
+       <dependency>
+            <groupId>org.jdom</groupId>
+            <artifactId>jdom</artifactId>
+       </dependency>
+        
        <!-- Logging Dependencies -->
        <dependency>
            <groupId>org.slf4j</groupId>

Added: gora/branches/goraamazon/gora-dynamodb/src/examples/java/org/apache/gora/examples/generated/person.java
URL: http://svn.apache.org/viewvc/gora/branches/goraamazon/gora-dynamodb/src/examples/java/org/apache/gora/examples/generated/person.java?rev=1366517&view=auto
==============================================================================
--- gora/branches/goraamazon/gora-dynamodb/src/examples/java/org/apache/gora/examples/generated/person.java (added)
+++ gora/branches/goraamazon/gora-dynamodb/src/examples/java/org/apache/gora/examples/generated/person.java Fri Jul 27 19:59:51 2012
@@ -0,0 +1,221 @@
+package org.apache.gora.examples.generated;
+
+import java.util.Set;
+
+import org.apache.gora.persistency.Persistent;
+import org.apache.gora.persistency.StateManager;
+
+import com.amazonaws.services.dynamodb.datamodeling.DynamoDBAttribute;
+import com.amazonaws.services.dynamodb.datamodeling.DynamoDBHashKey;
+import com.amazonaws.services.dynamodb.datamodeling.DynamoDBRangeKey;
+import com.amazonaws.services.dynamodb.datamodeling.DynamoDBTable;
+
+@DynamoDBTable(tableName = "person")
+public class person implements Persistent{
+	
+	// hash key
+	private String hashKey;
+	
+	// range hash key
+    //private String dateOfBirth;
+    
+    private String firstName;
+    
+    private String lastName;
+    
+    private double salary;
+    
+    private Set<String> visitedPlaces;
+    
+    public void setNew(boolean pNew){}
+    
+    public void setDirty(boolean pDirty){}
+    
+    @DynamoDBHashKey
+    public String getHashKey() {  return hashKey;  }
+    public void setHashKey(String pSsn) {  this.hashKey = pSsn;  }
+    /*
+    @DynamoDBRangeKey
+    public String getRangeKey() {  return dateOfBirth;  }
+    public void setRangeKey(String pDateOfBirth) {  this.dateOfBirth = pDateOfBirth; }
+   
+    @DynamoDBAttribute(attributeName = "ssn")
+    public String getSsn() {  return ssn;  }
+    public void setSsn(String pSsn) {  this.ssn = pSsn;  }
+     */
+    @DynamoDBAttribute(attributeName = "firstName")
+    public String getFirstName() {  return firstName;  }
+    public void setFirstName(String pFirstName) {  this.firstName = pFirstName;  }
+    
+    @DynamoDBAttribute(attributeName = "lastName")
+    public String getLastName() {  return lastName;  }
+    public void setLastName(String pLastName) {  this.lastName = pLastName;  }
+    
+    @DynamoDBAttribute(attributeName = "salary")
+    public double getSalary() {  return salary; }
+    public void setSalary(double pSalary) {  this.salary = pSalary; }
+    
+    @DynamoDBAttribute(attributeName = "places")
+    public Set<String> getPlacesVisited() { 
+    	return visitedPlaces; 
+    }    
+    public void setPlacesVisited(Set<String> pVisitedPlaces) { this.visitedPlaces = pVisitedPlaces; }
+    
+    // TODO The persistent abstract class should not force us to rewrite all these methods 
+	@Override
+	public StateManager getStateManager() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public Persistent newInstance(StateManager stateManager) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public String[] getFields() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public String getField(int index) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public int getFieldIndex(String field) {
+		// TODO Auto-generated method stub
+		return 0;
+	}
+
+	@Override
+	public void clear() {
+		// TODO Auto-generated method stub
+		
+	}
+	
+	@Override
+	public person clone() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public boolean isNew() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	@Override
+	public void setNew() {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void clearNew() {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public boolean isDirty() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	@Override
+	public boolean isDirty(int fieldIndex) {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	@Override
+	public boolean isDirty(String field) {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	@Override
+	public void setDirty() {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void setDirty(int fieldIndex) {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void setDirty(String field) {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void clearDirty(int fieldIndex) {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void clearDirty(String field) {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void clearDirty() {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public boolean isReadable(int fieldIndex) {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	@Override
+	public boolean isReadable(String field) {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	@Override
+	public void setReadable(int fieldIndex) {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void setReadable(String field) {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void clearReadable(int fieldIndex) {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void clearReadable(String field) {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void clearReadable() {
+		// TODO Auto-generated method stub
+		
+	}
+
+}

Modified: gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBColumn.java
URL: http://svn.apache.org/viewvc/gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBColumn.java?rev=1366517&r1=1366516&r2=1366517&view=diff
==============================================================================
--- gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBColumn.java (original)
+++ gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBColumn.java Fri Jul 27 19:59:51 2012
@@ -1,84 +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.gora.dynamodb.query;
-
-import java.nio.ByteBuffer;
-
-//import org.slf4j.Logger;
-//import org.slf4j.LoggerFactory;
-
-/**
- * Represents a unit of data: a key value pair tagged by a family name
- */
-public abstract class DynamoDBColumn {
-  //public static final Logger LOG = LoggerFactory.getLogger(DynamoDBColumn.class);
-
-  public static final int SUB = 0;
-  public static final int SUPER = 1;
-  
-  private String family;
-  private int type;
-  
-  public String getFamily() {
-    return family;
-  }
-  public void setFamily(String family) {
-    this.family = family;
-  }
-  public int getType() {
-    return type;
-  }
-  public void setType(int type) {
-    this.type = type;
-  }
-  
-  public abstract ByteBuffer getName();
-  public abstract Object getValue();
-  
-
-/*  protected Object fromByteBuffer(Type type, ByteBuffer byteBuffer) {
-    Object value = null;
-    switch (type) {
-      case STRING:
-        value = new Utf8(StringSerializer.get().fromByteBuffer(byteBuffer));
-        break;
-      case BYTES:
-        value = byteBuffer;
-        break;
-      case INT:
-        value = IntegerSerializer.get().fromByteBuffer(byteBuffer);
-        break;
-      case LONG:
-        value = LongSerializer.get().fromByteBuffer(byteBuffer);
-        break;
-      case FLOAT:
-        value = FloatSerializer.get().fromByteBuffer(byteBuffer);
-        break;
-      case DOUBLE:
-        value = DoubleSerializer.get().fromByteBuffer(byteBuffer);
-        break;
-
-      default:
-        LOG.info("Type is not supported: " + type);
-
-    }
-    return value;
-  }
-*/
-}

Modified: gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBQuery.java
URL: http://svn.apache.org/viewvc/gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBQuery.java?rev=1366517&r1=1366516&r2=1366517&view=diff
==============================================================================
--- gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBQuery.java (original)
+++ gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBQuery.java Fri Jul 27 19:59:51 2012
@@ -18,85 +18,159 @@
 
 package org.apache.gora.dynamodb.query;
 
-import java.util.Collection;
-
 import org.apache.gora.persistency.Persistent;
 import org.apache.gora.query.Query;
 import org.apache.gora.query.ws.impl.QueryWSBase;
 import org.apache.gora.store.DataStore;
 
-import com.amazonaws.auth.AWSCredentials;
-import com.amazonaws.services.dynamodb.model.QueryRequest;
+import com.amazonaws.services.dynamodb.datamodeling.DynamoDBQueryExpression;
+import com.amazonaws.services.dynamodb.datamodeling.DynamoDBScanExpression;
+import com.amazonaws.services.dynamodb.model.AttributeValue;
+import com.amazonaws.services.dynamodb.model.ComparisonOperator;
+import com.amazonaws.services.dynamodb.model.Condition;
+import com.amazonaws.services.dynamodb.model.KeySchema;
+import com.amazonaws.services.dynamodb.model.KeySchemaElement;
 
 public class DynamoDBQuery<K, T extends Persistent> extends QueryWSBase<K, T> {
 	
-	/**
-	 * Query object to perform requests to the datastore
-	 */
-	//private Query<K, T> query;
-	
-	/**
-	 * Maps fields to DynamoDB attributes.
-	 */
-	private Collection<String> attributesColl;
-	
-	/**
-	 * Query object to perform requests to the datastore
-	 */
-	QueryRequest dynamoDBquery;
-
+	private boolean consistencyReadLevel;
+	
+	private static ComparisonOperator rangeCompOp;
+	
+	private static ComparisonOperator scanCompOp;
+	
+	private static final String RANGE_QUERY = "range";
+	
+	private static final String SCAN_QUERY = "scan";
+	
+	private String type;
+	
+	private Query<K, T> query;
+	
+	private Object dynamoDBExpression;
+	
+	private KeySchema keySchema;
+	
 	public DynamoDBQuery(){
 		super(null);
-		this.dynamoDBquery = new QueryRequest();
 	}
   
 	public DynamoDBQuery(DataStore<K, T> dataStore) {
 		super(dataStore);
 	}
+	public void buildExpression(){
+		Condition newCondition = buildRangeCondition();
+		AttributeValue hashAttrValue = buildKeyHashAttribute();
+		if (hashAttrValue == null)
+			throw new IllegalStateException("There is not a key schema defined.");
+		if(newCondition != null){
+			setType(RANGE_QUERY);
+			buildQueryExpression(newCondition, hashAttrValue);
+		}
+		else{
+			setType(SCAN_QUERY);
+			buildScanExpression(hashAttrValue);
+		}
+	}
+	public void buildScanExpression(AttributeValue pHashAttrValue){
+		DynamoDBScanExpression newScanExpression = new DynamoDBScanExpression();
+		// TODO right now we only support scanning using the key, but we should support other types of scans
+		newScanExpression.addFilterCondition(getKeySchema().getHashKeyElement().getAttributeName(), buildKeyScanCondition());
+		dynamoDBExpression = newScanExpression;
+	}
+	
+	public void buildQueryExpression(Condition pNewCondition, AttributeValue pHashAttrValue) {
+		DynamoDBQueryExpression newQueryExpression = new DynamoDBQueryExpression(pHashAttrValue); 
+		newQueryExpression.setConsistentRead(getConsistencyReadLevel());
+		newQueryExpression.setRangeKeyCondition(pNewCondition);
+		dynamoDBExpression = newQueryExpression;
+	}
 	
-	public void setAttrCollection(Collection<String> attrsColl){
-		this.attributesColl = attrsColl;
+	private AttributeValue buildKeyHashAttribute(){
+		String pAttrType = getKeySchema().getHashKeyElement().getAttributeType();
+		if(pAttrType.equals("S"))
+			return new AttributeValue().withS(query.getKey().toString());
+		else if(pAttrType.equals("N"))
+			return new AttributeValue().withN(query.getKey().toString());
+		return null;
 	}
 	
-	public Collection<String> getAttrCollection(){
-		return attributesColl;
+	private Condition buildKeyScanCondition(){
+		Condition scanKeyCondition = new Condition();
+		scanKeyCondition.setComparisonOperator(getScanCompOp());
+		scanKeyCondition.withAttributeValueList(buildKeyHashAttribute());
+		return scanKeyCondition;
 	}
-	public void setTableName(String tableName){
-		this.dynamoDBquery.setTableName(tableName);
+	private Condition buildRangeCondition(){
+		KeySchemaElement kRangeSchema = getKeySchema().getRangeKeyElement();
+		Condition rangeKeyCondition = null;
+		if(kRangeSchema != null){
+			rangeKeyCondition = new Condition();
+			rangeKeyCondition.setComparisonOperator(ComparisonOperator.BETWEEN.toString());
+			AttributeValue startVal, endVal = null;
+			startVal = buildKeyHashAttribute();
+			if(kRangeSchema.getAttributeType().equals("S"))
+				endVal = new AttributeValue().withS(query.getStartKey().toString());
+			else if (kRangeSchema.getAttributeType().equals("N"))
+				endVal = new AttributeValue().withN(query.getStartKey().toString());
+			rangeKeyCondition.withAttributeValueList(startVal, endVal);
+		}
+		return rangeKeyCondition;
 	}
 	
-	public void setLimit(int limit){
-		this.dynamoDBquery.setLimit(limit);
+	public boolean getConsistencyReadLevel(){
+		return consistencyReadLevel;
 	}
 	
-	public void setConsistencyRead(boolean consistency){
-		this.dynamoDBquery.setConsistentRead(consistency);
+	public void setConsistencyReadLevel(boolean pConsistencyReadLevel){
+		this.consistencyReadLevel = pConsistencyReadLevel;
 	}
 	
-	public void setCredentials(AWSCredentials credentials){
-		this.dynamoDBquery.setRequestCredentials(credentials);
+	public KeySchema getKeySchema(){
+		return keySchema;
+	}
+	
+	public Object getQueryExpression(){
+		return dynamoDBExpression;
+	}
+	
+	public void setKeySchema(KeySchema pKeySchema){
+		this.keySchema = pKeySchema;
+	}
+	
+	public void setQuery(Query<K, T> pQuery){
+		this.query = pQuery;
+	}
+	
+	public Query<K, T> getQuery(){
+		return this.query;
+	}
+	
+	public String getType() {
+		return type;
+	}
+	
+	public void setType(String pType) {
+		this.type = pType;
 	}
-  
-  /**
-   * @param family the family name
-   * @return an array of the query column names belonging to the family
-   */
-  public String[] getColumns(String family) {
-    
-    //List<String> columnList = attributesMap.get(family);
-    String[] columns = new String[2];
-    //for (int i = 0; i < columns.length; ++i) {
-    //  columns[i] = columnList.get(i);
-    //}
-    return columns;
-  }
-  
-  public QueryRequest getQuery() {
-    return dynamoDBquery;
-  }
-  
-  public void setQuery(Query<K, T> query) {
-    this.dynamoDBquery = (QueryRequest)query;
-  }
 
+	public static ComparisonOperator getScanCompOp() {
+		if (scanCompOp == null)
+			scanCompOp = ComparisonOperator.GE;
+		return scanCompOp;
+	}
+
+	public static void setScanCompOp(ComparisonOperator scanCompOp) {
+		DynamoDBQuery.scanCompOp = scanCompOp;
+	}
+	
+	public static ComparisonOperator getRangeCompOp(){
+		if (rangeCompOp == null)
+			rangeCompOp = ComparisonOperator.BETWEEN;
+		return rangeCompOp;
+	}
+	
+	public static void setRangeCompOp(ComparisonOperator pRangeCompOp){
+		rangeCompOp = pRangeCompOp;
+	}
 }

Modified: gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBResult.java
URL: http://svn.apache.org/viewvc/gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBResult.java?rev=1366517&r1=1366516&r2=1366517&view=diff
==============================================================================
--- gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBResult.java (original)
+++ gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBResult.java Fri Jul 27 19:59:51 2012
@@ -19,45 +19,48 @@
 package org.apache.gora.dynamodb.query;
 
 import java.io.IOException;
+import java.util.List;
 
 import org.apache.gora.persistency.Persistent;
 import org.apache.gora.query.Query;
 import org.apache.gora.query.ws.impl.ResultWSBase;
 import org.apache.gora.store.DataStore;
-//import org.slf4j.Logger;
-//import org.slf4j.LoggerFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class DynamoDBResult<K, T extends Persistent> extends ResultWSBase<K, T> {
-  //public static final Logger LOG = LoggerFactory.getLogger(DynamoDBResult.class);
+  public static final Logger LOG = LoggerFactory.getLogger(DynamoDBResult.class);
   
   private int rowNumber;
 
-  private DynamoDBResultSet<K> dynamoDBResultSet;
+  private List<T> dynamoDBResultSet;
 
-  public DynamoDBResult(DataStore<K, T> dataStore, Query<K, T> query) {
-    super(dataStore, query);
+  public DynamoDBResult(DataStore<K, T> dataStore, Query<K, T> query, List<T> objList) {
+	super(dataStore, query);
+	LOG.debug("DynamoDB result created.");
+    this.setResultSet(objList);
   }
 
-  public void setResultSet(DynamoDBResultSet<K> dynamoDBResultSet) {
-    this.dynamoDBResultSet = dynamoDBResultSet;
+  public void setResultSet(List<T> objList) {
+    this.dynamoDBResultSet = objList;
+    this.limit = objList.size();
   }
 
-@Override
-public float getProgress() throws IOException, InterruptedException, Exception {
+  public float getProgress() throws IOException, InterruptedException, Exception {
 	// TODO Auto-generated method stub
 	return 0;
-}
+  }
 
-@Override
-public void close() throws IOException {
-	// TODO Auto-generated method stub
-	
-}
+  protected boolean nextInner() throws Exception {
+	if (offset < 0 || offset > dynamoDBResultSet.size())
+		return false;
+	persistent = dynamoDBResultSet.get((int) this.offset);
+	return true;
+  }
 
-@Override
-protected boolean nextInner() throws Exception {
+  @Override
+  public void close() throws IOException {
 	// TODO Auto-generated method stub
-	return false;
-}
+  }
 
 }

Modified: gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBResultSet.java
URL: http://svn.apache.org/viewvc/gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBResultSet.java?rev=1366517&r1=1366516&r2=1366517&view=diff
==============================================================================
--- gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBResultSet.java (original)
+++ gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBResultSet.java Fri Jul 27 19:59:51 2012
@@ -1,54 +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.gora.dynamodb.query;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-
-/**
- * List data structure to keep the order coming from the Cassandra selects.
- */
-public class DynamoDBResultSet<K> extends ArrayList<DynamoDBRow<K>> {
-
-  /**
-   * 
-   */
-  private static final long serialVersionUID = -7620939600192859652L;
-
-  /**
-   * Maps keys to indices in the list.
-   */
-  private HashMap<K, Integer> indexMap = new HashMap<K, Integer>();
-
-  public DynamoDBRow<K> getRow(K key) {
-    Integer integer = this.indexMap.get(key);
-    if (integer == null) {
-      return null;
-    }
-    
-    return this.get(integer);
-  }
-
-  public void putRow(K key, DynamoDBRow<K> cassandraRow) {
-    this.add(cassandraRow);
-    this.indexMap.put(key, this.size()-1);
-  } 
-  
-
-}

Modified: gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBRow.java
URL: http://svn.apache.org/viewvc/gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBRow.java?rev=1366517&r1=1366516&r2=1366517&view=diff
==============================================================================
--- gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBRow.java (original)
+++ gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/query/DynamoDBRow.java Fri Jul 27 19:59:51 2012
@@ -1,42 +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.gora.dynamodb.query;
-
-import java.util.ArrayList;
-
-/**
- * List of key value pairs representing a row, tagged by a key.
- */
-public class DynamoDBRow<K> extends ArrayList<DynamoDBColumn> {
-
-  /**
-   * 
-   */
-  private static final long serialVersionUID = -7620939600192859652L;
-  private K key;
-
-  public K getKey() {
-    return this.key;
-  }
-
-  public void setKey(K key) {
-    this.key = key;
-  }
-
-}

Added: gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/store/DynamoDBMapping.java
URL: http://svn.apache.org/viewvc/gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/store/DynamoDBMapping.java?rev=1366517&view=auto
==============================================================================
--- gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/store/DynamoDBMapping.java (added)
+++ gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/store/DynamoDBMapping.java Fri Jul 27 19:59:51 2012
@@ -0,0 +1,252 @@
+/**
+ * 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.gora.dynamodb.store;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.amazonaws.services.dynamodb.model.KeySchema;
+import com.amazonaws.services.dynamodb.model.KeySchemaElement;
+import com.amazonaws.services.dynamodb.model.ProvisionedThroughput;
+
+public class DynamoDBMapping {
+  
+  public static final Logger LOG = LoggerFactory.getLogger(DynamoDBMapping.class);
+  
+  // a map from field name to attribute value
+  private final Map<String, List<Map<String, String>>> tablesToItems;
+  
+  private final Map<String, KeySchema> tablesToKeySchemas;
+  
+  private final Map<String, ProvisionedThroughput> tablesToPrTh;
+  
+  public DynamoDBMapping(Map<String, List<Map<String, String>>> tables,
+	      				Map<String, KeySchema> tablesToKeySchemas,
+	      				Map<String, ProvisionedThroughput> provisionedThroughput) {
+	    this.tablesToItems = tables;
+	    this.tablesToKeySchemas = tablesToKeySchemas;
+	    this.tablesToPrTh = provisionedThroughput;
+	  }
+
+  public Map<String,List<Map<String, String>>> getTables(){
+	  return tablesToItems;
+  }
+  
+  public List<Map<String, String>> getItems(String tableName){
+	  return tablesToItems.get(tableName);
+  }
+	  
+  public KeySchema getKeySchema(String tableName) {
+    return tablesToKeySchemas.get(tableName);
+  }
+  
+  public ProvisionedThroughput getProvisionedThroughput(String tableName){
+	  return tablesToPrTh.get(tableName);
+  }
+  /**
+   * A builder for creating the mapper. This will allow building a thread safe
+   * {@link HBaseMapping} using simple immutabilty.
+   *
+   */
+  public static class DynamoDBMappingBuilder {
+
+	  private String tableName;
+	  
+	  /**
+	   * This data structure can hold several tables, with their own items.
+	   * Map<TableName, List<Map<AttributeName,AttributeType>>
+	   */
+	  private Map<String, List<Map<String, String>>> tablesToItems = 
+			  new HashMap<String, List<Map<String, String>>>();
+	  
+	  private Map<String, KeySchema> tablesToKeySchemas = 
+			  new HashMap<String, KeySchema>();
+	  
+	  private Map<String, ProvisionedThroughput> tablesToPrTh =
+			  new HashMap<String, ProvisionedThroughput>();
+	  
+	  public void setTableName(String tabName){
+		  tableName = tabName;
+	  }
+	  
+	  public String getTableName(String tableName){
+		  return tableName;
+	  }
+	  
+	  /**
+	   * Sets the provisioned throughput for the specified table
+	   * @param tableName
+	   * @param readCapUnits
+	   * @param writeCapUnits
+	   */
+	  public void setProvisionedThroughput(String tableName, long readCapUnits, long writeCapUnits){
+		  ProvisionedThroughput ptDesc = new ProvisionedThroughput()
+		  													.withReadCapacityUnits(readCapUnits)
+		  													.withWriteCapacityUnits(writeCapUnits);
+		  tablesToPrTh.put(tableName, ptDesc);
+	  }
+	  
+	  /**
+	   * Sets the hash range key schema for the specified table
+	   * @param tableName
+	   * @param rangeKeyName
+	   * @param rangeKeyType
+	   */
+	  public void setHashRangeKeySchema(String tableName, String rangeKeyName, String rangeKeyType){
+		  KeySchema kSchema = tablesToKeySchemas.get(tableName);
+		  if ( kSchema == null)
+			  kSchema = new KeySchema();
+		   
+	      KeySchemaElement rangeKeyElement = new KeySchemaElement().
+	    		  										withAttributeName(rangeKeyName).
+	    			  									withAttributeType(rangeKeyType);
+	      kSchema.setRangeKeyElement(rangeKeyElement);
+	      tablesToKeySchemas.put(tableName, kSchema);
+	  }
+	  
+	  /**
+	   * Sets the hash key schema for the specified table
+	   * @param tableName
+	   * @param keyName
+	   * @param keyType
+	   */
+	  public void setHashKeySchema(String tableName, String keyName, String keyType){
+		  KeySchema kSchema = tablesToKeySchemas.get(tableName);
+		  if ( kSchema == null)
+			  kSchema = new KeySchema();
+		  
+		  KeySchemaElement hashKey = new KeySchemaElement().
+	    			  					 		withAttributeName(keyName).
+	    			  					 		withAttributeType(keyType);
+	      kSchema.setHashKeyElement(hashKey);
+		  tablesToKeySchemas.put(tableName, kSchema);
+	  }
+	  
+	  /**
+	   * Checks if a table exists, and if doesn't exist it creates the new table. 
+	   * @param tableName
+	   * @return The table identified by the parameter
+	   */
+	  private List<Map<String, String>> getOrCreateTable(String tableName) {
+	      
+    	List<Map<String, String>> items = tablesToItems.get(tableName);
+	    if (items == null) {
+	      items = new ArrayList<Map<String, String>>();
+	      tablesToItems.put(tableName, items);
+	    }
+	    return items;
+	  }
+	  
+      /**
+       * Gets the attribute for a specific item. The idea is to be able to get different items with different attributes.
+       * TODO This method is incomplete because the itemNumber might not be present and this would be a problem
+       * @param items
+       * @param itemNumber
+       * @return
+       */
+	  private HashMap<String, String> getOrCreateItemAttribs(List<Map<String, String>> items, int itemNumber){
+    	  HashMap<String, String> itemAttribs;
+    	  
+    	  if (items.isEmpty())
+    		  items.add(new HashMap<String, String>());
+    	  
+    	  itemAttribs = (HashMap<String, String>) items.get(itemNumber);
+    	  if (itemAttribs == null)
+    		  items.add(new HashMap<String, String>());
+    	  
+    	  return (HashMap<String, String>) items.get(itemNumber);
+    	  
+      }
+      
+	  public void addAttribute(String tableName, String attributeName, String attrType, int itemNumber) {
+	    // selecting table
+	    List<Map<String, String>> items = getOrCreateTable(tableName);
+	    // add attribute to item
+	    HashMap<String, String> itemAttribs = getOrCreateItemAttribs(items, itemNumber);
+	    itemAttribs.put(attributeName, attrType);
+    	//items.add(itemAttribs);
+	    // add item to table
+	    //tablesToItems.put(tableName, items);
+	  }
+	  /**
+	   * Method to verify whether or not the schemas have been initialized
+	   * @return
+	   */
+	  private String verifyAllKeySchemas(){
+		  
+		  String wrongTable = "";
+		  // if there are not tables defined
+		  if (tablesToItems.isEmpty()) return "";
+		  
+		  for(String tableName : tablesToItems.keySet()){
+			  // if there are not schemas defined
+			  if (tablesToKeySchemas.isEmpty()) return "";
+			  if (!verifyKeySchema(tableName)) return "";
+		  }
+		  
+		  return wrongTable;
+		  
+	  }
+	  
+	  private boolean verifyKeySchema(String tableName){
+		  KeySchema kSchema = tablesToKeySchemas.get(tableName);
+		  
+		  if (kSchema == null) 
+			  return false;
+			  
+		  KeySchemaElement rangeKey = kSchema.getRangeKeyElement();
+		  KeySchemaElement hashKey = kSchema.getHashKeyElement();
+		  // A range key must have a hash key as well
+		  if (rangeKey != null){
+			  if (hashKey != null)	
+				  return true;
+			  else 	  
+				  return false;
+		  }
+		  // A hash key may exist by itself
+		  if (hashKey != null)	  
+			  return true;
+		  return false;
+	  }
+	  
+	  /**
+	   * @return A newly constructed mapping.
+	   */
+	  public DynamoDBMapping build() {
+		  
+		  if (tableName == null) throw new IllegalStateException("tableName is not specified");
+	    
+		  // verifying items for at least a table
+		  if (tablesToItems.isEmpty()) throw new IllegalStateException("No tables");
+	      
+		  // verifying if key schemas have been properly defined
+		  String wrongTableName = verifyAllKeySchemas();  
+		  if (!wrongTableName.equals("")) throw new IllegalStateException("no key schemas defined for table " + wrongTableName);
+	     
+		  // Return the tableDescription and all the attributes needed
+	      return new DynamoDBMapping(tablesToItems,tablesToKeySchemas, tablesToPrTh);
+	  }
+  }
+
+}

Modified: gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/store/DynamoDBStore.java
URL: http://svn.apache.org/viewvc/gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/store/DynamoDBStore.java?rev=1366517&r1=1366516&r2=1366517&view=diff
==============================================================================
--- gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/store/DynamoDBStore.java (original)
+++ gora/branches/goraamazon/gora-dynamodb/src/main/java/org/apache/gora/dynamodb/store/DynamoDBStore.java Fri Jul 27 19:59:51 2012
@@ -19,270 +19,512 @@
 package org.apache.gora.dynamodb.store;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
+import java.lang.reflect.Method;
 
 import org.apache.gora.dynamodb.query.DynamoDBQuery;
 import org.apache.gora.dynamodb.query.DynamoDBResult;
+import org.apache.gora.dynamodb.store.DynamoDBMapping.DynamoDBMappingBuilder;
 import org.apache.gora.persistency.BeanFactory;
 import org.apache.gora.persistency.Persistent;
 import org.apache.gora.query.PartitionQuery;
 import org.apache.gora.query.Query;
 import org.apache.gora.query.Result;
-import org.apache.gora.query.ws.impl.PartitionWSQueryImpl;
 import org.apache.gora.store.ws.impl.WSDataStoreBase;
-//import org.slf4j.Logger;
-//import org.slf4j.LoggerFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.jdom.Document;
+import org.jdom.Element;
+import org.jdom.input.SAXBuilder;
 
+import com.amazonaws.AmazonServiceException;
 import com.amazonaws.auth.AWSCredentials;
 import com.amazonaws.auth.PropertiesCredentials;
+import com.amazonaws.services.dynamodb.AmazonDynamoDB;
+import com.amazonaws.services.dynamodb.AmazonDynamoDBAsyncClient;
 import com.amazonaws.services.dynamodb.AmazonDynamoDBClient;
-import com.amazonaws.services.dynamodb.model.QueryResult;
+import com.amazonaws.services.dynamodb.datamodeling.DynamoDBMapper;
+import com.amazonaws.services.dynamodb.datamodeling.DynamoDBQueryExpression;
+import com.amazonaws.services.dynamodb.datamodeling.DynamoDBScanExpression;
+import com.amazonaws.services.dynamodb.model.CreateTableRequest;
+import com.amazonaws.services.dynamodb.model.DeleteTableRequest;
+import com.amazonaws.services.dynamodb.model.DeleteTableResult;
+import com.amazonaws.services.dynamodb.model.DescribeTableRequest;
+import com.amazonaws.services.dynamodb.model.KeySchema;
+import com.amazonaws.services.dynamodb.model.ProvisionedThroughput;
+import com.amazonaws.services.dynamodb.model.ResourceNotFoundException;
+import com.amazonaws.services.dynamodb.model.TableDescription;
+import com.amazonaws.services.dynamodb.model.TableStatus;
+
 
 public class DynamoDBStore<K, T extends Persistent> extends WSDataStoreBase<K, T> {
 	
-  //public static final Logger LOG = LoggerFactory.getLogger(DynamoDBStore.class);
+  public static final Logger LOG = LoggerFactory.getLogger(DynamoDBStore.class);
+
+  // TODO this should be loaded from the class path
+  private static final String MAPPING_FILE_PATH = "gora-dynamodb/conf/";
+  
+  private static String preferredSchema;
   
   /**
-   * Path where the AWS Credential will reside
+   * The mapping file to create the tables from
+   */
+  private static final String MAPPING_FILE = "gora-dynamodb-mapping.xml";
+
+  /**
+   * Path where the AWS Credential will reside.
    */
-  private static String awsCredentialsProperties = "conf/AwsCredentials.properties";
+  // TODO this should point to properties file within the DynamoDB module 
+  private static String awsCredentialsProperties = "AwsCredentials.properties";
   
+  /**
+   * Name of the cloud database provider.
+   */
   private static String wsProvider = "Amazon.Web.Services";
-	 
+  
+  private static String CLI_TYP_PROP = "gora.dynamodb.client";
+  
+  private static String ENDPOINT_PROP = "gora.dynamodb.endpoint";
+
+  /**
+   * The mapping object that contains the mapping file
+   */
+  private DynamoDBMapping mapping;
+  
   /**
-   * TODO Amazon DynamoDB decorator 
-   * because all DynamoDBAsyncClients are DynamoDBClients   
+   * Amazon DynamoDB client which can be asynchronous or nor   
    */
-  private AmazonDynamoDBClient dynamoDBClient;
+  private AmazonDynamoDB dynamoDBClient;
+  
+  private String consistency;
+  
+  /**
+   * The values are Avro fields pending to be stored.
+   *
+   * We want to iterate over the keys in insertion order.
+   * We don't want to lock the entire collection before iterating over the keys, since in the meantime other threads are adding entries to the map.
+   */
+  private Map<K, T> buffer = new LinkedHashMap<K, T>();
+  
+  Class<T> persistentClass;  
 
   public DynamoDBStore(){
   }
 
-  @Override
-  public void initialize(Class<K> keyClass, Class<T> persistentClass,
-	      Properties properties) throws Exception {
-	  try {
-		File file = new File(awsCredentialsProperties);
-		AWSCredentials credentials = new PropertiesCredentials(file);
-		setConf(credentials);
-		setWsProvider(wsProvider);
-		//TODO Create decorator to create different AmazonDynamoDB clients
-		// this is because all amazonDynamoDBAsynClients are AmazonDynamoDBClients
-		dynamoDBClient = new AmazonDynamoDBClient(credentials);
-		
-	  }
-	    catch (Exception e) {
-	      throw new IOException(e.getMessage(), e);
-	  }
+  public void initialize(Class<K> keyClass, Class<T> pPersistentClass,
+	     Properties properties) throws Exception {
+	 try {
+
+		 getCredentials();
+		 setWsProvider(wsProvider);
+		 preferredSchema = properties.getProperty("preferred.schema.name");
+		 //preferredSchema = "person";
+		 dynamoDBClient = getClient(properties.getProperty(CLI_TYP_PROP),(AWSCredentials)getConf());
+		 //dynamoDBClient = getClient("sync",(AWSCredentials)getConf());
+		 dynamoDBClient.setEndpoint(properties.getProperty(ENDPOINT_PROP));
+		 //dynamoDBClient.setEndpoint("http://dynamodb.us-east-1.amazonaws.com/");
+		 mapping = readMapping();
+		 
+		 consistency = properties.getProperty("gora.dynamodb.consistent.reads");
+		 
+		 persistentClass = pPersistentClass;
+	 }
+	 catch (Exception e) {
+	     throw new IOException(e.getMessage(), e);
+	 }
   }
-  /*
-  public void close() throws IOException {
-    LOG.debug("close");
-    flush();
+  
+   /**
+    * Method to create the specific client to be used
+    * @param clientType
+    * @param credentials
+    * @return
+    */
+  public AmazonDynamoDB getClient(String clientType, AWSCredentials credentials){
+	  if (clientType.equals("sync"))
+		  return new AmazonDynamoDBClient(credentials);
+	  if (clientType.equals("async"))
+		  return new AmazonDynamoDBAsyncClient(credentials);
+	  return null;
   }
+  
+  @SuppressWarnings("unchecked")
+  private DynamoDBMapping readMapping() throws IOException {
 
-  @Override
-  public void createSchema() {
-    LOG.debug("create schema");
-    this.cassandraClient.checkKeyspace();
-  }
+    DynamoDBMappingBuilder mappingBuilder = new DynamoDBMappingBuilder();
 
-  @Override
-  public boolean delete(K key) throws IOException {
-    LOG.debug("delete " + key);
-    return false;
-  }
+    try {
+      SAXBuilder builder = new SAXBuilder();
+      //Document doc = builder.build(getClass().getClassLoader() .getResourceAsStream(MAPPING_FILE_PATH + MAPPING_FILE));
+      Document doc = builder.build(new File(MAPPING_FILE_PATH + MAPPING_FILE));
+      
+      Element root = doc.getRootElement();
 
-  @Override
-  public long deleteByQuery(Query<K, T> query) throws IOException {
-    LOG.debug("delete by query " + query);
-    return 0;
-  }
+      List<Element> tableElements = root.getChildren("table");
+      for(Element tableElement : tableElements) {
+    	  
+    	String tableName = tableElement.getAttributeValue("name");
+    	long readCapacUnits = Long.parseLong(tableElement.getAttributeValue("readcunit"));
+    	long writeCapacUnits = Long.parseLong(tableElement.getAttributeValue("readcunit"));
+    	
+    	mappingBuilder.setTableName(tableName);
+    	mappingBuilder.setProvisionedThroughput(tableName, readCapacUnits, writeCapacUnits);
+    	LOG.debug("Basic table properties have been set: Name, and Provisioned throughput.");
+    	
+    	// Retrieving key's features
+    	List<Element> fieldElements = tableElement.getChildren("key");
+    	for(Element fieldElement : fieldElements) {
+    		String keyName  = fieldElement.getAttributeValue("name");
+    		String keyType  = fieldElement.getAttributeValue("type");
+    		String keyAttrType  = fieldElement.getAttributeValue("att-type");
+    		if(keyType.equals("hash"))
+    			mappingBuilder.setHashKeySchema(tableName, keyName, keyAttrType);
+    		else if(keyType.equals("hashrange"))
+    			mappingBuilder.setHashRangeKeySchema(tableName, keyName, keyAttrType);
+    	}
+    	LOG.debug("Table key schemas have been set.");
+    	
+    	// Retrieving attributes
+        fieldElements = tableElement.getChildren("attribute");
+        for(Element fieldElement : fieldElements) {
+          String attributeName  = fieldElement.getAttributeValue("name");
+          String attributeType = fieldElement.getAttributeValue("type");
+          mappingBuilder.addAttribute(tableName, attributeName, attributeType, 0);
+        }
+        LOG.debug("Table attributes have been read.");
+      }
 
-  @Override
-  public void deleteSchema() throws IOException {
-    LOG.debug("delete schema");
-    this.cassandraClient.dropKeyspace();
-  }*/
+    } catch(IOException ex) {
+      LOG.error("Error while performing xml mapping.");
+      ex.printStackTrace();
+      throw ex;
+
+    } catch(Exception ex) {
+      ex.printStackTrace();
+      throw new IOException(ex);
+    }
 
+    return mappingBuilder.build();
+  }
+  
+  /**
+   * Creates the AWSCredentials object based on the properties file.
+   * @return AWSCredentials object
+   * @throws FileNotFoundException
+   * @throws IllegalArgumentException
+   * @throws IOException
+   */
+  private AWSCredentials getCredentials() throws FileNotFoundException, IllegalArgumentException, IOException{
+	  // TODO this should be done using a class loader, and reading it as a stream
+	  File file = new File(MAPPING_FILE_PATH + awsCredentialsProperties);
+	  AWSCredentials credentials = new PropertiesCredentials(file);
+	  setConf(credentials);
+	  return credentials;
+  }
+
+  private DynamoDBQuery<K, T> buildDynamoDBQuery(Query<K, T> query){
+	  if(getSchemaName() == null)
+		  throw new IllegalStateException("There is not a preferred schema defined.");
+	  
+	  DynamoDBQuery<K, T> dynamoDBQuery = new DynamoDBQuery<K, T>();
+	  dynamoDBQuery.setKeySchema(mapping.getKeySchema(getSchemaName()));
+	  dynamoDBQuery.setQuery(query);
+	  dynamoDBQuery.setConsistencyReadLevel(getConsistencyReads());
+	  dynamoDBQuery.buildExpression();
+	  dynamoDBQuery.getQueryExpression();
+	  
+	  return dynamoDBQuery;
+  }
+  private boolean getConsistencyReads(){
+	  if(consistency != null)
+		  if(consistency.equals("true")) 
+			  return true;
+	  return false;
+  }
+  
   @Override
   public Result<K, T> execute(Query<K, T> query) throws Exception {
-    
-	 DynamoDBQuery<K, T> dynamoDBQuery = new DynamoDBQuery<K, T>();
-	 DynamoDBResult<K, T> dynamoDBResult = new DynamoDBResult<K, T>(this, dynamoDBQuery);
 	 
-	 dynamoDBQuery.setQuery(query);
-	 
-	 // TODO we should return the object that the class is supposed to return
-	 QueryResult dynamodbResult = dynamoDBClient.query(dynamoDBQuery.getQuery());
-	 
-	 return (Result<K, T>)dynamoDBResult;
-  }
-
-  /*
-  private void addSubColumns(String family, DynamoDBQuery<K, T> dynamoDBQuery,
-      CassandraResultSet cassandraResultSet) {
-    // select family columns that are included in the query
-    List<Row<K, ByteBuffer, ByteBuffer>> rows = this.cassandraClient.execute(dynamoDBQuery, family);
-    
-    for (Row<K, ByteBuffer, ByteBuffer> row : rows) {
-      K key = row.getKey();
-      
-      // find associated row in the resultset
-      CassandraRow<K> cassandraRow = cassandraResultSet.getRow(key);
-      if (cassandraRow == null) {
-        cassandraRow = new CassandraRow<K>();
-        cassandraResultSet.putRow(key, cassandraRow);
-        cassandraRow.setKey(key);
-      }
-      
-      ColumnSlice<ByteBuffer, ByteBuffer> columnSlice = row.getColumnSlice();
-      
-      for (HColumn<ByteBuffer, ByteBuffer> hColumn : columnSlice.getColumns()) {
-        CassandraSubColumn cassandraSubColumn = new CassandraSubColumn();
-        cassandraSubColumn.setValue(hColumn);
-        cassandraSubColumn.setFamily(family);
-        cassandraRow.add(cassandraSubColumn);
-      }
-      
-    }
-  }
-  */
-/*
-  private void addSuperColumns(String family, CassandraQuery<K, T> cassandraQuery, 
-      CassandraResultSet cassandraResultSet) {
-    
-    List<SuperRow<K, String, ByteBuffer, ByteBuffer>> superRows = this.cassandraClient.executeSuper(cassandraQuery, family);
-    for (SuperRow<K, String, ByteBuffer, ByteBuffer> superRow: superRows) {
-      K key = superRow.getKey();
-      CassandraRow<K> cassandraRow = cassandraResultSet.getRow(key);
-      if (cassandraRow == null) {
-        cassandraRow = new CassandraRow();
-        cassandraResultSet.putRow(key, cassandraRow);
-        cassandraRow.setKey(key);
-      }
-      
-      SuperSlice<String, ByteBuffer, ByteBuffer> superSlice = superRow.getSuperSlice();
-      for (HSuperColumn<String, ByteBuffer, ByteBuffer> hSuperColumn: superSlice.getSuperColumns()) {
-        CassandraSuperColumn cassandraSuperColumn = new CassandraSuperColumn();
-        cassandraSuperColumn.setValue(hSuperColumn);
-        cassandraSuperColumn.setFamily(family);
-        cassandraRow.add(cassandraSuperColumn);
-      }
-    }
+	 DynamoDBQuery<K, T> dynamoDBQuery = buildDynamoDBQuery(query);
+	 DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
+	 List<T> objList = null;
+	 if (dynamoDBQuery.getType().equals("range"))
+		 objList = mapper.query(persistentClass, (DynamoDBQueryExpression)dynamoDBQuery.getQueryExpression());
+	 if (dynamoDBQuery.getType().equals("scan"))
+		 objList = mapper.scan(persistentClass, (DynamoDBScanExpression)dynamoDBQuery.getQueryExpression());
+	 return new DynamoDBResult<K, T>(this, query, objList);  
   }
-*/
- 
+  
   @Override
   public T get(K key, String[] fields) throws Exception {
-    DynamoDBQuery<K,T> query = new DynamoDBQuery<K,T>();
+   /* DynamoDBQuery<K,T> query = new DynamoDBQuery<K,T>();
     query.setDataStore(this);
-    query.setKeyRange(key, key);
-    query.setFields(fields);
-    query.setLimit(1);
+    //query.setKeyRange(key, key);
+    //query.setFields(fields);
+    //query.setLimit(1);
     Result<K,T> result = execute(query);
     boolean hasResult = result.next();
-    return hasResult ? result.get() : null;
+    return hasResult ? result.get() : null;*/
+	  return null;
   }
 
+  /**
+   * Gets the object with the specific key
+   */
+  public T get(K key) throws Exception {
+	DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
+	T object = (T) mapper.load(persistentClass, key); 
+	return object;
+  }
+  
   public Query<K, T> newQuery() {
     Query<K,T> query = new DynamoDBQuery<K, T>(this);
    // query.setFields(getFieldsToQuery(null));
     return query;
   }
 
-@Override
-public String getSchemaName() {
-	// TODO Auto-generated method stub
+  public String getSchemaName() {
+	if (preferredSchema != null)
+		return preferredSchema;
 	return null;
-}
-
-@Override
-public void createSchema() throws Exception {
-	// TODO Auto-generated method stub
-	
-}
+  }
+  
+  public void setSchemaName(String pSchemaName){
+	  preferredSchema = pSchemaName;
+  }
+  
+  public void createSchema() throws Exception {
+	  if (mapping.getTables().isEmpty())	throw new IllegalStateException("There are not tables defined.");
+	  if (preferredSchema == null){
+		  LOG.debug("create schemas");
+		  // read the mapping object
+		  for(String tableName : mapping.getTables().keySet())
+			  executeCreateTableRequest(tableName);
+		  LOG.debug("tables created successfully.");
+	  }
+	  else{
+		  LOG.debug("create schema " + preferredSchema);
+		  executeCreateTableRequest(preferredSchema);
+	  }
+  }
+  
+  /**
+   * Executes a create table request using the DynamoDB client
+   * @param tableName
+   */
+  private void executeCreateTableRequest(String tableName){
+	  CreateTableRequest createTableRequest = getCreateTableRequest(tableName,
+				mapping.getKeySchema(tableName), 
+				mapping.getProvisionedThroughput(tableName));
+	  // use the client to perform the request
+	  dynamoDBClient.createTable(createTableRequest).getTableDescription();
+	  // wait for table to become active
+	  waitForTableToBecomeAvailable(tableName);
+  }
+  
+  /**
+   * Builds the necessary requests to create tables 
+   * @param tableName
+   * @param keySchema
+   * @param proThrou
+   * @return
+   */
+  private CreateTableRequest getCreateTableRequest(String tableName, KeySchema keySchema, ProvisionedThroughput proThrou){
+	  CreateTableRequest createTableRequest = new CreateTableRequest();
+	  createTableRequest.setTableName(tableName);
+	  createTableRequest.setKeySchema(keySchema);
+	  createTableRequest.setProvisionedThroughput(proThrou);
+	  return createTableRequest;
+  }
+  
+  /**
+   * Deletes all tables present in the mapping object.
+   */
+  public void deleteSchema() throws Exception {
+	  if (mapping.getTables().isEmpty())	throw new IllegalStateException("There are not tables defined.");
+	  if (preferredSchema == null){
+		LOG.debug("Delete schemas");
+		if (mapping.getTables().isEmpty())	throw new IllegalStateException("There are not tables defined.");
+		// read the mapping object
+		for(String tableName : mapping.getTables().keySet())
+		   executeDeleteTableRequest(tableName);
+		LOG.debug("All schemas deleted successfully.");
+	  }
+	  else{
+		  LOG.debug("create schema " + preferredSchema);
+		  executeDeleteTableRequest(preferredSchema);
+	  }
+  }
+  
+  /**
+   * Executes a delete table request using the DynamoDB client
+   * @param tableName
+   */
+  public void executeDeleteTableRequest(String tableName){
+	  DeleteTableRequest deleteTableRequest = new DeleteTableRequest()
+      .withTableName(tableName);
+	  DeleteTableResult result = dynamoDBClient.deleteTable(deleteTableRequest);
+	  LOG.debug("Schema: " + result.getTableDescription() + " deleted successfully.");
+  }
+  
+  /**
+   * Waits up to 6 minutes to confirm if a table has been created or not
+   * @param tableName
+   */
+  private void waitForTableToBecomeAvailable(String tableName) {
+      LOG.debug("Waiting for " + tableName + " to become available");
 
-@Override
-public void deleteSchema() throws Exception {
-	// TODO Auto-generated method stub
-	
-}
+      long startTime = System.currentTimeMillis();
+      long endTime = startTime + (10 * 60 * 1000);
+      while (System.currentTimeMillis() < endTime) {
+          try {Thread.sleep(1000 * 20);} catch (Exception e) {}
+          try {
+              DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
+              TableDescription tableDescription = dynamoDBClient.describeTable(request).getTable();
+              String tableStatus = tableDescription.getTableStatus();
+              
+              LOG.debug(tableName + " - current state: " + tableStatus);
+              
+              if (tableStatus.equals(TableStatus.ACTIVE.toString())) return;
+          } catch (AmazonServiceException ase) {
+              if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == false) throw ase;
+          }
+      }
 
-@Override
-public boolean schemaExists() throws Exception {
-	// TODO Auto-generated method stub
-	return false;
-}
+      throw new RuntimeException("Table " + tableName + " never became active");
+  }
 
-@Override
-public K newKey() throws Exception {
-	// TODO Auto-generated method stub
-	return null;
-}
+  /**
+   * Verifies if the specified schemas exist
+   */
+  public boolean schemaExists() throws Exception {
+	TableDescription success = null;
+	if (mapping.getTables().isEmpty())	throw new IllegalStateException("There are not tables defined.");
+	if (preferredSchema == null){
+		LOG.debug("Verifying schemas");
+		if (mapping.getTables().isEmpty())	throw new IllegalStateException("There are not tables defined.");
+		// read the mapping object
+		for(String tableName : mapping.getTables().keySet()){
+		   success = getTableSchema(tableName);
+		   if (success == null) return false;
+		}
+	}
+	else{
+		LOG.debug("Verifying schema " + preferredSchema);
+		success = getTableSchema(preferredSchema);
+	}
+	LOG.debug("Finished verifying schemas.");
+	return (success != null)? true: false;
+  }
 
-@Override
-public T newPersistent() throws Exception {
+  /**
+   * Retrieves the table description for the specific resource name
+   * @param tableName
+   * @return
+   */
+  private TableDescription getTableSchema(String tableName){
+	TableDescription tableDescription = null;
+  	try{
+  		DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName);
+  		tableDescription = dynamoDBClient.describeTable(describeTableRequest).getTable();
+  	}
+  	catch(ResourceNotFoundException e){
+  		return tableDescription;
+  	}
+  	return tableDescription;
+  }
+  
+  public K newKey() throws Exception {
 	// TODO Auto-generated method stub
 	return null;
-}
+  }
 
-@Override
-public T get(K key) throws Exception {
-	// TODO Auto-generated method stub
-	return null;
-}
+  public T newPersistent() throws Exception {
+	T obj = persistentClass.newInstance();
+	return obj;
+  }
 
-@Override
-public void put(K key, T obj) throws Exception {
-	// TODO Auto-generated method stub
-	
-}
+  public void put(K key, T obj) throws Exception {
+	  Object param = null;
+	  for (Method met : persistentClass.getMethods()) {
+		  if(met.getName().equals("getRangeKey")){
+			  Object [] params = null;
+			  param = met.invoke(obj, params);
+			  break;
+		  }
+	  }
+	  DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
+	  if (param != null)
+		  mapper.load(persistentClass, key.toString(), param.toString());
+	  else
+		  mapper.load(persistentClass, key.toString());
+	  
+	  mapper.save(obj);
+  }
 
-@Override
-public boolean delete(K key) throws Exception {
-	// TODO Auto-generated method stub
-	return false;
-}
+  /**
+   * Deletes the object using key
+   * @return true for a successful process  
+   */
+  public boolean delete(K key) throws Exception {
+	try{
+		DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
+		T object = (T) mapper.load(persistentClass, key);
+		
+		if (object == null) return false;
+		
+		// setting key for dynamodbMapper
+		for (Method met : persistentClass.getMethods()) {
+			  if(met.getName().equals("setHashKey")){
+				  met.invoke(object, key);
+				  break;
+			  }
+		}
+		
+		mapper.delete(object);
+		return true;
+	}catch(Exception e){
+		LOG.debug("Error while deleting value with key " + key.toString());
+		LOG.debug(e.getMessage());
+		return false;
+	}
+  }
 
-@Override
-public long deleteByQuery(Query<K, T> query) throws Exception {
+  public long deleteByQuery(Query<K, T> query) throws Exception {
 	// TODO Auto-generated method stub
 	return 0;
-}
+  }	
 
-@Override
-public List<PartitionQuery<K, T>> getPartitions(Query<K, T> query)
+  public List<PartitionQuery<K, T>> getPartitions(Query<K, T> query)
 		throws IOException {
 	// TODO Auto-generated method stub
 	return null;
-}
+  }
 
-@Override
-public void flush() throws Exception {
+  public void flush() throws Exception {
 	// TODO Auto-generated method stub
 	
-}
+  }
 
-@Override
-public void setBeanFactory(BeanFactory<K, T> beanFactory) {
+  public void setBeanFactory(BeanFactory<K, T> beanFactory) {
 	// TODO Auto-generated method stub
 	
-}
+  }
 
-@Override
-public BeanFactory<K, T> getBeanFactory() {
+  public BeanFactory<K, T> getBeanFactory() {
 	// TODO Auto-generated method stub
 	return null;
-}
+  }
 
-@Override
-public void close() throws IOException, InterruptedException, Exception {
-	// TODO Auto-generated method stub
-	
-}
+  public void close() throws IOException, InterruptedException, Exception {
+	  LOG.debug("Datastore closed.");
+	  flush();
+  }
 
   /**
    * Duplicate instance to keep all the objects in memory till flushing.

Added: gora/branches/goraamazon/gora-dynamodb/src/test/conf/gora-dynamodb-mapping.xml
URL: http://svn.apache.org/viewvc/gora/branches/goraamazon/gora-dynamodb/src/test/conf/gora-dynamodb-mapping.xml?rev=1366517&view=auto
==============================================================================
--- gora/branches/goraamazon/gora-dynamodb/src/test/conf/gora-dynamodb-mapping.xml (added)
+++ gora/branches/goraamazon/gora-dynamodb/src/test/conf/gora-dynamodb-mapping.xml Fri Jul 27 19:59:51 2012
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+   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.
+-->
+
+<gora-orm>
+
+  <table name="person" readcunit="5" writecunit="5"> <!-- optional descriptors for tables -->
+  	<key name="ssn" type="hash" att-type="S"/>
+  	<!-- key name="dateOfBirth" type="hashrange" att-type="S"/-->
+    <attribute name="firstName" type="S"/>
+    <attribute name="lastName" type="S"/>
+    <attribute name="salary" type="N"/>
+    <attribute name="visitedplaces" type="SS"/>
+  </table>
+
+  <table name="webpage" readcunit="5" writecunit="5">
+  	<key name="id" type="hash" att-type="S"/>
+    <attribute name="common" type="S"/>
+    <attribute name="content" type="S"/>
+    <attribute name="parsedContent" type="S"/>
+    <attribute name="outlinks" type="S"/>
+  </table>
+  
+</gora-orm>

Added: gora/branches/goraamazon/gora-dynamodb/src/test/conf/gora.properties
URL: http://svn.apache.org/viewvc/gora/branches/goraamazon/gora-dynamodb/src/test/conf/gora.properties?rev=1366517&view=auto
==============================================================================
--- gora/branches/goraamazon/gora-dynamodb/src/test/conf/gora.properties (added)
+++ gora/branches/goraamazon/gora-dynamodb/src/test/conf/gora.properties Fri Jul 27 19:59:51 2012
@@ -0,0 +1,22 @@
+# 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.
+
+gora.datastore.default=org.apache.gora.dynamodb.store.DynamoDBStore
+gora.datastore.autocreateschema=true
+
+preferred.schema.name=person
+gora.dynamodb.client=sync
+gora.dynamodb.endpoint=http://dynamodb.us-east-1.amazonaws.com/
+gora.dynamodb.consistent.reads=true

Added: gora/branches/goraamazon/gora-dynamodb/src/test/java/org/apache/gora/dynamodb/GoraDynamoDBTestDriver.java
URL: http://svn.apache.org/viewvc/gora/branches/goraamazon/gora-dynamodb/src/test/java/org/apache/gora/dynamodb/GoraDynamoDBTestDriver.java?rev=1366517&view=auto
==============================================================================
--- gora/branches/goraamazon/gora-dynamodb/src/test/java/org/apache/gora/dynamodb/GoraDynamoDBTestDriver.java (added)
+++ gora/branches/goraamazon/gora-dynamodb/src/test/java/org/apache/gora/dynamodb/GoraDynamoDBTestDriver.java Fri Jul 27 19:59:51 2012
@@ -0,0 +1,189 @@
+/**
+ * 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.gora.dynamodb;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import org.apache.gora.GoraTestDriver;
+import org.apache.gora.dynamodb.store.DynamoDBStore;
+import org.apache.gora.persistency.Persistent;
+import org.apache.gora.store.DataStore;
+import org.apache.gora.store.DataStoreFactory;
+import org.apache.gora.store.ws.impl.WSDataStoreFactory;
+import org.apache.gora.util.GoraException;
+
+import com.amazonaws.AmazonServiceException;
+import com.amazonaws.auth.AWSCredentials;
+import com.amazonaws.auth.PropertiesCredentials;
+import com.amazonaws.services.dynamodb.AmazonDynamoDBClient;
+import com.amazonaws.services.dynamodb.model.DeleteTableRequest;
+import com.amazonaws.services.dynamodb.model.DescribeTableRequest;
+import com.amazonaws.services.dynamodb.model.ResourceNotFoundException;
+import com.amazonaws.services.dynamodb.model.TableDescription;
+import com.amazonaws.services.dynamodb.model.TableStatus;
+
+/**
+ * Helper class for third part tests using gora-dynamodb backend. 
+ * @see GoraTestDriver
+ */
+public class GoraDynamoDBTestDriver extends GoraTestDriver {
+
+  static long waitTime = 10L * 60L * 1000L;
+  
+  static AmazonDynamoDBClient dynamoDBClient;
+  
+  static String awsCredentialsFile = "AwsCredentials.properties";
+  
+  static String awsCredentialsPath = "gora-dynamodb/conf/";
+  
+  protected Object auth;
+  
+  public GoraDynamoDBTestDriver() {
+    super(DynamoDBStore.class);
+    
+    try {
+    	
+    	File file = new File(awsCredentialsPath + awsCredentialsFile);
+    	AWSCredentials credentials = new PropertiesCredentials(file);
+    	
+    	auth = credentials;
+        dynamoDBClient = new AmazonDynamoDBClient(credentials);
+        
+	} catch (FileNotFoundException e) {
+		// TODO Auto-generated catch block
+		e.printStackTrace();
+	} catch (IllegalArgumentException e) {
+		// TODO Auto-generated catch block
+		e.printStackTrace();
+	} catch (IOException e) {
+		// TODO Auto-generated catch block
+		e.printStackTrace();
+	}
+    
+  }
+
+  @Override
+  public void setUpClass() throws Exception {
+    super.setUpClass();
+    log.info("Initializing DynamoDB.");
+  }
+
+  @Override
+  public void tearDownClass() throws Exception {
+    super.tearDownClass();
+    log.info("Finishing DynamoDB.");
+  }
+  
+  @Override
+  public void setUp() throws Exception {
+    super.setUp();
+  }
+  
+  public void deleteTable(String tableName) throws Exception {
+	  DeleteTableRequest deleteTableRequest = new DeleteTableRequest().withTableName(tableName);
+	  dynamoDBClient.deleteTable(deleteTableRequest);
+	  waitForTableToBeDeleted(tableName);  
+  }
+  
+  public void waitForTableToBecomeAvailable(String tableName) {
+      log.info("Waiting for " + tableName + " to become ACTIVE...");
+
+      long startTime = System.currentTimeMillis();
+      long endTime = startTime + (10 * 60 * 1000);
+      while (System.currentTimeMillis() < endTime) {
+          try {Thread.sleep(1000 * 20);} catch (Exception e) {}
+          try {
+              DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
+              TableDescription tableDescription = dynamoDBClient.describeTable(request).getTable();
+              String tableStatus = tableDescription.getTableStatus();
+              log.info("  - current state: " + tableStatus);
+              if (tableStatus.equals(TableStatus.ACTIVE.toString())) return;
+          } catch (AmazonServiceException ase) {
+              if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == false) throw ase;
+          }
+      }
+
+      throw new RuntimeException("Table " + tableName + " never went active");
+  }
+  
+  private static void waitForTableToBeDeleted(String tableName) {
+      
+	  log.info("Waiting for " + tableName + " while status DELETING...");
+
+      long startTime = System.currentTimeMillis();
+      long endTime = startTime + (waitTime);
+      
+      while (System.currentTimeMillis() < endTime) {
+          try {Thread.sleep(1000 * 20);} catch (Exception e) {}
+          try {
+              DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
+              TableDescription tableDescription = dynamoDBClient.describeTable(request).getTable();
+              String tableStatus = tableDescription.getTableStatus();
+              log.info("  - current state: " + tableStatus);
+              if (tableStatus.equals(TableStatus.ACTIVE.toString())) {
+                  return;
+              }
+          } catch (AmazonServiceException ase) {
+              if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == true) {
+            	  log.info("Table " + tableName + " is not found. It was deleted.");
+                  return;
+              }
+              else {
+                  throw ase;
+              }
+          }
+      }
+      throw new RuntimeException("Table " + tableName + " did not go active after 10 minutes.");
+  }
+  
+  public Object getAuth() {
+      return auth;
+  }
+  
+  public AmazonDynamoDBClient getDynamoDBClient() {
+    return dynamoDBClient;
+  }
+  
+  public TableDescription checkResource(String tableName){
+  	TableDescription tableDescription = null;
+  	
+  	try{
+  		DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName);
+  		tableDescription = dynamoDBClient.describeTable(describeTableRequest).getTable();
+  	}
+  	catch(ResourceNotFoundException e){
+  		tableDescription = null;
+  	}
+      
+  	return tableDescription;
+  }
+  
+  @SuppressWarnings("unchecked")
+  public<K, T extends Persistent> DataStore<K,T>
+    createDataStore(Class<K> keyClass, Class<T> persistentClass) throws GoraException {
+    setProperties(DataStoreFactory.createProps());
+    DataStore<K,T> dataStore = WSDataStoreFactory.createDataStore(
+        (Class<? extends DataStore<K,T>>)dataStoreClass, keyClass, persistentClass, auth);
+    dataStores.add(dataStore);
+
+    return dataStore;
+  }
+}



Mime
View raw message