Package com.amazonaws.services.dynamodb.model

Examples of com.amazonaws.services.dynamodb.model.AttributeValue


  /**
   * Builds query expression depending on query type (range or scan)
   */
  public void buildExpression(){
    AttributeValue hashAttrValue = buildKeyHashAttribute();
    if (hashAttrValue == null)
      throw new IllegalStateException("There is not a key schema defined.");
    if (DynamoDBQuery.getType().equals(RANGE_QUERY)){
      Condition newCondition = buildRangeCondition();
      buildQueryExpression(newCondition, hashAttrValue);
View Full Code Here


   * @returnAttributeValue build from query
   */
  private AttributeValue buildKeyHashAttribute(){
    String pAttrType = getKeySchema().getHashKeyElement().getAttributeType();
    if(pAttrType.equals("S"))
      return new AttributeValue().withS(getHashKey(query.getKey()).toString());
    else if(pAttrType.equals("N"))
      return new AttributeValue().withN(getHashKey(query.getKey()).toString());
    return null;
  }
View Full Code Here

  KeySchemaElement kRangeSchema = getKeySchema().getRangeKeyElement();
  Condition rangeKeyCondition = null;
  if(kRangeSchema != null){
    rangeKeyCondition = new Condition();
    rangeKeyCondition.setComparisonOperator(ComparisonOperator.BETWEEN.toString());
    AttributeValue startVal = null, endVal = null;
    //startVal = buildKeyHashAttribute();
    if(kRangeSchema.getAttributeType().equals("S")){
      startVal = new AttributeValue().withS(getRangeKey(query.getStartKey()).toString());
      endVal = new AttributeValue().withS(getRangeKey(query.getEndKey()).toString());
    }
    else if (kRangeSchema.getAttributeType().equals("N")){
      startVal = new AttributeValue().withN(getRangeKey(query.getStartKey()).toString());
      endVal = new AttributeValue().withN(getRangeKey(query.getEndKey()).toString());
    }
    rangeKeyCondition.withAttributeValueList(startVal, endVal);
  }
  return rangeKeyCondition;
  }
View Full Code Here

        logger.debug("updatekey: " + key + " from table: " + table);

        Map<String, AttributeValueUpdate> attributes = new HashMap<String, AttributeValueUpdate>(
                values.size());
        for (Entry<String, ByteIterator> val : values.entrySet()) {
            AttributeValue v = new AttributeValue(val.getValue().toString());
            attributes.put(val.getKey(), new AttributeValueUpdate()
                    .withValue(v).withAction("PUT"));
        }

        UpdateItemRequest req = new UpdateItemRequest(table, createPrimaryKey(key), attributes);
View Full Code Here

    @Override
    public int insert(String table, String key,HashMap<String, ByteIterator> values) {
        logger.debug("insertkey: " + primaryKeyName + "-" + key + " from table: " + table);
        Map<String, AttributeValue> attributes = createAttributes(values);
        // adding primary key
        attributes.put(primaryKeyName, new AttributeValue(key));

        PutItemRequest putItemRequest = new PutItemRequest(table, attributes);
        PutItemResult res = null;
        try {
            res = dynamoDB.putItem(putItemRequest);
View Full Code Here

    private static Map<String, AttributeValue> createAttributes(
            HashMap<String, ByteIterator> values) {
        Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>(
                values.size() + 1); //leave space for the PrimaryKey
        for (Entry<String, ByteIterator> val : values.entrySet()) {
            attributes.put(val.getKey(), new AttributeValue(val.getValue()
                    .toString()));
        }
        return attributes;
    }
View Full Code Here

        }
        return rItems;
    }

    private static Key createPrimaryKey(String key) {
        Key k = new Key().withHashKeyElement(new AttributeValue().withS(key));
        return k;
    }
View Full Code Here

        String tableName = getTableName(clazz, config);

        // Fill in the hash key element in the service request
        Method hashKeyGetter = reflector.getHashKeyGetter(clazz);
        AttributeValue hashKeyElement = getHashKeyElement(hashKey, hashKeyGetter);

        // Determine the range key, if provided
        AttributeValue rangeKeyElement = null;
        if ( rangeKey != null ) {
            Method rangeKeyMethod = reflector.getRangeKeyGetter(clazz);
            if ( rangeKeyMethod == null ) {
                throw new DynamoDBMappingException("Zero-parameter range key property must be annotated with "
                        + DynamoDBRangeKey.class);
View Full Code Here

        @SuppressWarnings("unchecked")
        Class<? extends T> clazz = (Class<? extends T>) object.getClass();
        String tableName = getTableName(clazz, config);

        Method hashKeyGetter = reflector.getHashKeyGetter(clazz);
        AttributeValue hashKeyElement = getHashKeyElement(safeInvoke(hashKeyGetter, object), hashKeyGetter);

        AttributeValue rangeKeyElement = null;
        Method rangeKeyGetter = reflector.getRangeKeyGetter(clazz);
        if ( rangeKeyGetter != null ) {
            rangeKeyElement = getRangeKeyElement(safeInvoke(rangeKeyGetter, object), rangeKeyGetter);
        }

        Key objectKey = new Key().withHashKeyElement(hashKeyElement).withRangeKeyElement(rangeKeyElement);

        Map<String, AttributeValueUpdate> updateValues = new HashMap<String, AttributeValueUpdate>();
        Map<String, ExpectedAttributeValue> expectedValues = new HashMap<String, ExpectedAttributeValue>();

        boolean forcePut = config.getSaveBehavior() == SaveBehavior.CLOBBER;
        boolean nonKeyAttributePresent = false;
        List<ValueUpdate> inMemoryUpdates = new LinkedList<ValueUpdate>();

        /*
         * First look at the keys and construct updates for them independently
         */
        List<Method> keyGetterMethods = new LinkedList<Method>();
        keyGetterMethods.add(hashKeyGetter);
        if ( rangeKeyGetter != null ) {
            keyGetterMethods.add(rangeKeyGetter);
        }

        /*
         * Determine if there are any auto-assigned keys to assign. If so, force
         * a put and assign the keys
         */
        if ( !forcePut ) {
            for ( Method method : keyGetterMethods ) {
                Object getterResult = safeInvoke(method, object);
                if ( getterResult == null && reflector.isAssignableKey(method) ) {
                    forcePut = true;
                }
            }
        }

        /*
         * If we're doing a put, that means that we need update values for the
         * key attributes.
         */
        if ( forcePut ) {
            for ( Method method : keyGetterMethods ) {
                Object getterResult = safeInvoke(method, object);
                String attributeName = reflector.getAttributeName(method);

                if ( getterResult == null && reflector.isAssignableKey(method) ) {
                    AttributeValue newVersionValue = getAutoGeneratedKeyAttributeValue(method, getterResult);
                    updateValues.put(attributeName,
                            new AttributeValueUpdate().withAction("PUT").withValue(newVersionValue));
                    inMemoryUpdates.add(new ValueUpdate(method, newVersionValue, object));

                    if ( config.getSaveBehavior() != SaveBehavior.CLOBBER ) {
                        // Add an expect clause to make sure that the item
                        // doesn't already exist, since it's supposed to be new
                        ExpectedAttributeValue expected = new ExpectedAttributeValue();
                        expected.setExists(false);
                        expectedValues.put(attributeName, expected);
                    }
                } else {
                    AttributeValue currentValue = getSimpleAttributeValue(method, getterResult);
                    if ( currentValue != null ) {
                        updateValues.put(attributeName,
                                new AttributeValueUpdate().withValue(currentValue).withAction("PUT"));
                    } else {
                        throw new DynamoDBMappingException("Null value for non-generated key for method " + method);
                    }
                }
            }
        } else {
            /*
             * If we don't have the required keys by this point, it's an error
             */
            if ( hashKeyElement == null ) {
                throw new DynamoDBMappingException("No value provided for hash key for object " + object);
            }
            if ( rangeKeyGetter != null && rangeKeyElement == null ) {
                throw new DynamoDBMappingException("No value provided for range key for object " + object);
            }
        }

        // Look at every getter and construct an update object for it
        for ( Method method : reflector.getRelevantGetters(clazz) ) {

            // Skip any key methods, since they are handled separately
            if ( method.equals(hashKeyGetter) || method.equals(rangeKeyGetter) )
                continue;

            nonKeyAttributePresent = true;

            Object getterResult = safeInvoke(method, object);
            String attributeName = reflector.getAttributeName(method);

            /*
             * If this is a versioned field, update it
             */
            if ( reflector.isVersionAttributeGetter(method) ) {
                if ( config.getSaveBehavior() != SaveBehavior.CLOBBER ) {
                    // First establish the expected (current) value for the
                    // update call
                    ExpectedAttributeValue expected = new ExpectedAttributeValue();

                    // For new objects, insist that the value doesn't exist.
                    // For existing ones, insist it has the old value.
                    AttributeValue currentValue = getSimpleAttributeValue(method, getterResult);
                    expected.setExists(currentValue != null);
                    if ( currentValue != null ) {
                        expected.setValue(currentValue);
                    }
                    expectedValues.put(attributeName, expected);
                }

                AttributeValue newVersionValue = getVersionAttributeValue(method, getterResult);
                updateValues
                        .put(attributeName, new AttributeValueUpdate().withAction("PUT").withValue(newVersionValue));
                inMemoryUpdates.add(new ValueUpdate(method, newVersionValue, object));
            }

            /*
             * Otherwise apply the update value for this attribute.
             */
            else  {
                AttributeValue currentValue = getSimpleAttributeValue(method, getterResult);
                if ( currentValue != null ) {
                    updateValues.put(attributeName, new AttributeValueUpdate().withValue(currentValue)
                            .withAction("PUT"));
                } else if ( config.getSaveBehavior() != SaveBehavior.CLOBBER ) {
                    updateValues.put(attributeName, new AttributeValueUpdate().withAction("DELETE"));
View Full Code Here

        Class<?> clazz = object.getClass();

        String tableName = getTableName(clazz, config);

        Method hashKeyGetter = reflector.getHashKeyGetter(clazz);
        AttributeValue hashKeyElement = getHashKeyElement(safeInvoke(hashKeyGetter, object), hashKeyGetter);

        AttributeValue rangeKeyElement = null;
        Method rangeKeyGetter = reflector.getRangeKeyGetter(clazz);
        if ( rangeKeyGetter != null ) {
            rangeKeyElement = getRangeKeyElement(safeInvoke(rangeKeyGetter, object), rangeKeyGetter);
        }

        Key objectKey = new Key().withHashKeyElement(hashKeyElement).withRangeKeyElement(rangeKeyElement);

        /*
         * If there is a version field, make sure we assert its value. If the
         * version field is null (only should happen in unusual circumstances),
         * pretend it doesn't have a version field after all.
         */
        Map<String, ExpectedAttributeValue> expectedValues = new HashMap<String, ExpectedAttributeValue>();
        if ( config.getSaveBehavior() != SaveBehavior.CLOBBER ) {
            for ( Method method : reflector.getRelevantGetters(clazz) ) {

                if ( reflector.isVersionAttributeGetter(method) ) {
                    Object getterResult = safeInvoke(method, object);
                    String attributeName = reflector.getAttributeName(method);

                    ExpectedAttributeValue expected = new ExpectedAttributeValue();
                    AttributeValue currentValue = getSimpleAttributeValue(method, getterResult);
                    expected.setExists(currentValue != null);
                    if ( currentValue != null )
                        expected.setValue(currentValue);
                    expectedValues.put(attributeName, expected);
                    break;
View Full Code Here

TOP

Related Classes of com.amazonaws.services.dynamodb.model.AttributeValue

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.