/** * Gets the field model for a given attribute. * @param <V> The field model's value type. * @param attributeName The attribute name. * @return The field model. */ public <V> DynamoDBMapperFieldModel<T,V> field(String attributeName) { return this.model.field(attributeName); }
public DynamoDBMapperTableModel<T> build() { final DynamoDBMapperTableModel<T> result = new DynamoDBMapperTableModel<T>(this); if (properties.tableName() != null) { result.hashKey(); //<- make sure the hash key is present } return result; } }
/** * Creates a new key map from the specified object. * @param <H> The hash key type. * @param <R> The range key type. * @param object The object instance. * @return The key map. */ public <H,R> Map<String,AttributeValue> convertKey(final T key) { final DynamoDBMapperFieldModel<T,H> hk = this.<H>hashKey(); final DynamoDBMapperFieldModel<T,R> rk = this.<R>rangeKeyIfExists(); return this.<H,R>convertKey(hk.get(key), (rk == null ? (R)null : rk.get(key))); }
/** * Constructs a new table mapper for the given class. * @param model The field model factory. * @param mapper The DynamoDB mapper. * @param db The service object to use for all service calls. */ protected DynamoDBTableMapper(AmazonDynamoDB db, DynamoDBMapper mapper, final DynamoDBMapperConfig config, final DynamoDBMapperTableModel<T> model) { this.rk = model.rangeKeyIfExists(); this.hk = model.hashKey(); this.model = model; this.config = config; this.mapper = mapper; this.db = db; }
@Override public <T> CreateTableRequest generateCreateTableRequest(Class<T> clazz, DynamoDBMapperConfig config) { config = mergeConfig(config); final DynamoDBMapperTableModel<T> model = getTableModel(clazz, config); final CreateTableRequest request = new CreateTableRequest(); request.setTableName(getTableName(clazz, config)); request.withKeySchema(new KeySchemaElement(model.hashKey().name(), HASH)); if (model.rangeKeyIfExists() != null) { request.withKeySchema(new KeySchemaElement(model.rangeKey().name(), RANGE)); } request.setGlobalSecondaryIndexes(model.globalSecondaryIndexes()); request.setLocalSecondaryIndexes(model.localSecondaryIndexes()); for (final DynamoDBMapperFieldModel<T,Object> field : model.fields()) { if (field.keyType() != null || field.indexed()) { request.withAttributeDefinitions(new AttributeDefinition() .withAttributeType(ScalarAttributeType.valueOf(field.attributeType().name())) .withAttributeName(field.name()) ); } } return request; }
for (final DynamoDBMapperFieldModel<T,Object> field : model.fields()) { if (field.keyType() == HASH || !field.globalSecondaryIndexNames(HASH).isEmpty()) { final Object value = field.get(expression.getHashKeyValues()); && (!rangeKeyConditions.isEmpty()); final String userProvidedIndexName = queryRequest.getIndexName(); final String primaryHashKeyName = model.hashKey().name(); rangeKeyNameForThisQuery = rangeKeyName; final DynamoDBMapperFieldModel<T,Object> rk = model.field(rangeKeyName); final boolean hashOnlyLSIQuery = (userProvidedIndexName != null) && ( !hasRangeKeyCondition ) && model.localSecondaryIndex(userProvidedIndexName) != null; final boolean userProvidedLSI = userProvidedLSIWithRangeKeyCondition || hashOnlyLSIQuery; final boolean hashOnlyGSIQuery = (userProvidedIndexName != null) && ( !hasRangeKeyCondition ) && model.globalSecondaryIndex(userProvidedIndexName) != null; final boolean userProvidedGSI = userProvidedGSIWithRangeKeyCondition || hashOnlyGSIQuery; final DynamoDBMapperFieldModel<T,Object> hk = model.field(hashKeyName);
Map<String, AttributeValue> key = model.convertKey(object); if ( config.getSaveBehavior() != SaveBehavior.CLOBBER && model.versioned() ) { for ( final DynamoDBMapperFieldModel<T,Object> field : model.versions() ) { final AttributeValue current = field.getAndConvert(object); if (current == null) {
/** * Determnes if any of the primary keys require auto-generation. */ private static <T> boolean anyKeyGeneratable( final DynamoDBMapperTableModel<T> model, final T object, final SaveBehavior saveBehavior ) { for (final DynamoDBMapperFieldModel<T, Object> field : model.keys()) { if (canGenerate(model, object, saveBehavior, field)) { return true; } } return false; }
/** * Creates a new object instance with the keys populated. * @param <H> The hash key type. * @param <R> The range key type. * @param hashKey The hash key. * @param rangeKey The range key (optional if not present on table). * @return The new instance. */ public <H,R> T createKey(final H hashKey, final R rangeKey) { final T key = StandardBeanProperties.DeclaringReflect.<T>newInstance(targetType); if (hashKey != null) { final DynamoDBMapperFieldModel<T,H> hk = hashKey(); hk.set(key, hashKey); } if (rangeKey != null) { final DynamoDBMapperFieldModel<T,R> rk = rangeKey(); rk.set(key, rangeKey); } return key; }
for ( final DynamoDBMapperFieldModel<Object,Object> field : model.fields() ) { AttributeValue currentValue = null; if ( canGenerate(model, toWrite, config.getSaveBehavior(), field) && !field.versioned() ) { final DynamoDBMapperTableModel<Object> model = getTableModel(clazz, config); Map<String, AttributeValue> key = model.convertKey(toDelete);
/** * {@inheritDoc} */ @Override public Map<String,AttributeValue> convert(final T object) { final Map<String,AttributeValue> map = new LinkedHashMap<String,AttributeValue>(); for (final DynamoDBMapperFieldModel<T,Object> field : fields()) { try { final AttributeValue value = field.getAndConvert(object); if (value != null) { map.put(field.name(), value); } } catch (final RuntimeException e) { throw new DynamoDBMappingException( targetType.getSimpleName() + "[" + field.name() + "]; could not convert attribute", e ); } } return map; }
/** * Gets the hash key field model for the specified type. * @param <H> The hash key type. * @return The hash key field model. * @throws DynamoDBMappingException If the hash key is not present. */ public DynamoDBMapperFieldModel<T,H> hashKey() { return this.model.hashKey(); }
@Override public Map<String, List<Object>> batchLoad(Map<Class<?>, List<KeyPair>> itemsToGet, DynamoDBMapperConfig config) { config = mergeConfig(config); List<Object> keys = new ArrayList<Object>(); if ( itemsToGet != null ) { for ( Class<?> clazz : itemsToGet.keySet() ) { if ( itemsToGet.get(clazz) != null ) { final DynamoDBMapperTableModel model = getTableModel(clazz, config); for ( KeyPair keyPair : itemsToGet.get(clazz) ) { keys.add(model.createKey(keyPair.getHashKey(), keyPair.getRangeKey())); } } } } return batchLoad(keys, config); }
requestItems.get(tableName).getKeys().add(model.convertKey(keyObject));
/** * Gets the global secondary indexes for the given class. * @return The map of index name to GlobalSecondaryIndexes. */ public Collection<GlobalSecondaryIndex> globalSecondaryIndexes() { if (globalSecondaryIndexes.isEmpty()) { return null; } final Collection<GlobalSecondaryIndex> copies = new ArrayList<GlobalSecondaryIndex>(globalSecondaryIndexes.size()); for (final String indexName : globalSecondaryIndexes.keySet()) { copies.add(globalSecondaryIndex(indexName)); } return copies; }
/** * Gets the local secondary indexes for the given class. * @param indexNames The index names. * @return The map of index name to LocalSecondaryIndexes. */ public Collection<LocalSecondaryIndex> localSecondaryIndexes() { if (localSecondaryIndexes.isEmpty()) { return null; } final Collection<LocalSecondaryIndex> copies = new ArrayList<LocalSecondaryIndex>(localSecondaryIndexes.size()); for (final String indexName : localSecondaryIndexes.keySet()) { copies.add(localSecondaryIndex(indexName)); } return copies; }
@Override public <T> CreateTableRequest generateCreateTableRequest(Class<T> clazz, DynamoDBMapperConfig config) { config = mergeConfig(config); final DynamoDBMapperTableModel<T> model = getTableModel(clazz, config); final CreateTableRequest request = new CreateTableRequest(); request.setTableName(getTableName(clazz, config)); request.withKeySchema(new KeySchemaElement(model.hashKey().name(), HASH)); if (model.rangeKeyIfExists() != null) { request.withKeySchema(new KeySchemaElement(model.rangeKey().name(), RANGE)); } request.setGlobalSecondaryIndexes(model.globalSecondaryIndexes()); request.setLocalSecondaryIndexes(model.localSecondaryIndexes()); for (final DynamoDBMapperFieldModel<T,Object> field : model.fields()) { if (field.keyType() != null || field.indexed()) { request.withAttributeDefinitions(new AttributeDefinition() .withAttributeType(ScalarAttributeType.valueOf(field.attributeType().name())) .withAttributeName(field.name()) ); } } return request; }
for (final DynamoDBMapperFieldModel<T,Object> field : model.fields()) { if (field.keyType() == HASH || !field.globalSecondaryIndexNames(HASH).isEmpty()) { final Object value = field.get(expression.getHashKeyValues()); && (!rangeKeyConditions.isEmpty()); final String userProvidedIndexName = queryRequest.getIndexName(); final String primaryHashKeyName = model.hashKey().name(); rangeKeyNameForThisQuery = rangeKeyName; final DynamoDBMapperFieldModel<T,Object> rk = model.field(rangeKeyName); final boolean hashOnlyLSIQuery = (userProvidedIndexName != null) && ( !hasRangeKeyCondition ) && model.localSecondaryIndex(userProvidedIndexName) != null; final boolean userProvidedLSI = userProvidedLSIWithRangeKeyCondition || hashOnlyLSIQuery; final boolean hashOnlyGSIQuery = (userProvidedIndexName != null) && ( !hasRangeKeyCondition ) && model.globalSecondaryIndex(userProvidedIndexName) != null; final boolean userProvidedGSI = userProvidedGSIWithRangeKeyCondition || hashOnlyGSIQuery; final DynamoDBMapperFieldModel<T,Object> hk = model.field(hashKeyName);
Map<String, AttributeValue> key = model.convertKey(object); if ( config.getSaveBehavior() != SaveBehavior.CLOBBER && model.versioned() ) { for ( final DynamoDBMapperFieldModel<T,Object> field : model.versions() ) { final AttributeValue current = field.getAndConvert(object); if (current == null) {
/** * Creates a new key map from the specified hash and range key. * @param <H> The hash key type. * @param <R> The range key type. * @param hashKey The hash key. * @param rangeKey The range key (optional if not present on table). * @return The key map. */ public <H,R> Map<String,AttributeValue> convertKey(final H hashKey, final R rangeKey) { final Map<String,AttributeValue> key = new LinkedHashMap<String,AttributeValue>(4); final DynamoDBMapperFieldModel<T,H> hk = this.<H>hashKey(); final AttributeValue hkValue = hashKey == null ? null : hk.convert(hashKey); if (hkValue != null) { key.put(hk.name(), hkValue); } else { throw new DynamoDBMappingException( targetType.getSimpleName() + "[" + hk.name() + "]; no HASH key value present" ); } final DynamoDBMapperFieldModel<T,R> rk = this.<R>rangeKeyIfExists(); final AttributeValue rkValue = rangeKey == null ? null : rk.convert(rangeKey); if (rkValue != null) { key.put(rk.name(), rkValue); } else if (rk != null) { throw new DynamoDBMappingException( targetType.getSimpleName() + "[" + rk.name() + "]; no RANGE key value present" ); } return key; }