Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011-2015 Amazon Technologies, Inc.
   *
   * Licensed 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://aws.amazon.com/apache2.0
   *
  * This file 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 com.amazonaws.services.dynamodbv2.datamodeling;
 
Immutable configuration object for service call behavior. An instance of this configuration is supplied to every DynamoDBMapper at construction; if not provided explicitly, DEFAULT is used. New instances can be given to the mapper object on individual save, load, and delete operations to override the defaults. For example:
 DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
 // Force this read to be consistent
 DomainClass obj = mapper.load(DomainClass.class, key, new DynamoDBMapperConfig(ConsistentReads.CONSISTENT));
 // Force this save operation to use putItem rather than updateItem
 mapper.save(obj, new DynamoDBMapperConfig(SaveBehavior.CLOBBER));
 // Save the object into a different table
 mapper.save(obj, new DynamoDBMapperConfig(new TableNameOverride("AnotherTable")));
 // Delete the object even if the version field is out of date
 mapper.delete(obj, new DynamoDBMapperConfig(SaveBehavior.CLOBBER));
 
 
 public class DynamoDBMapperConfig {

    
A fluent builder for DynamoDBMapperConfig objects.
 
     public static class Builder {
 
         private SaveBehavior saveBehavior;
         private ConsistentReads consistentReads;
         private TableNameOverride tableNameOverride;
         private TableNameResolver tableNameResolver;
         private ConversionSchema conversionSchema;

        
Creates a new builder initialized with the DynamoDBMapperConfig.DEFAULT values.
 
         public Builder() {
              = .getSaveBehavior();
              = .getConsistentReads();
              = .getTableNameOverride();
              = .getTableNameResolver();
              = .getConversionSchema();
         }

        

Returns:
the currently-configured save behavior
 
         public SaveBehavior getSaveBehavior() {
             return ;
         }

        

Parameters:
value the new save behavior
 
         public void setSaveBehavior(SaveBehavior value) {
              = value;
         }

        

Parameters:
value the new save behavior
Returns:
this builder
 
         public Builder withSaveBehavior(SaveBehavior value) {
             setSaveBehavior(value);
             return this;
         }


        

Returns:
the currently-configured consistent read behavior
 
         public ConsistentReads getConsistentReads() {
             return ;
         }

        

Parameters:
value the new consistent read behavior
        public void setConsistentReads(ConsistentReads value) {
             = value;
        }

        

Parameters:
value the new consistent read behavior
Returns:
this builder
        public Builder withConsistentReads(ConsistentReads value) {
            setConsistentReads(value);
            return this;
        }


        

Returns:
the current table name override
        public TableNameOverride getTableNameOverride() {
            return ;
        }

        

Parameters:
value the new table name override
        public void setTableNameOverride(TableNameOverride value) {
             = value;
        }

        

Parameters:
value the new table name override
Returns:
this builder
        public Builder withTableNameOverride(TableNameOverride value) {
            setTableNameOverride(value);
            return this;
        }


        

Returns:
the current table name resolver
        public TableNameResolver getTableNameResolver() {
            return ;
        }

        

Parameters:
value the new table name resolver
        public void setTableNameResolver(TableNameResolver value) {
             = value;
        }

        

Parameters:
value the new table name resolver
Returns:
this builder
        public Builder withTableNameResolver(TableNameResolver value) {
            setTableNameResolver(value);
            return this;
        }


        

Returns:
the current object table name resolver
            return ;
        }

        

Parameters:
value the new object table name resolver
        public void setObjectTableNameResolver(ObjectTableNameResolver value) {
             = value;
        }

        

Parameters:
value the new object table name resolver
Returns:
this builder
            setObjectTableNameResolver(value);
            return this;
        }

        

Returns:
the currently-configured pagination loading strategy
            return ;
        }

        

Parameters:
value the new pagination loading strategy
        public void setPaginationLoadingStrategy(
                PaginationLoadingStrategy value) {
             = value;
        }

        

Parameters:
value the new pagination loading strategy
Returns:
this builder
        public Builder withPaginationLoadingStrategy(
                PaginationLoadingStrategy value) {
            setPaginationLoadingStrategy(value);
            return this;
        }


        

Returns:
the currently-configured request metric collector
            return ;
        }

        

Parameters:
value the new request metric collector
        public void setRequestMetricCollector(RequestMetricCollector value) {
             = value;
        }

        

Parameters:
value the new request metric collector
Returns:
this builder
            setRequestMetricCollector(value);
            return this;
        }


        

Returns:
the current conversion schema
        public ConversionSchema getConversionSchema() {
            return ;
        }

        

Parameters:
value the new conversion schema
        public void setConversionSchema(ConversionSchema value) {
             = value;
        }

        

Parameters:
value the new conversion schema
Returns:
this builder
        public Builder withConversionSchema(ConversionSchema value) {
            setConversionSchema(value);
            return this;
        }


        
Builds a new DynamoDBMapperConfig object.

Returns:
the new, immutable config object
        public DynamoDBMapperConfig build() {
            return new DynamoDBMapperConfig(
                    ,
                    ,
                    ,
                    ,
                    ,
                    ,
                    ,
                    );
        }
    }

    
Enumeration of behaviors for the save operation.
    public static enum SaveBehavior {
        
UPDATE will not affect unmodeled attributes on a save operation and a null value for the modeled attribute will remove it from that item in DynamoDB.

Because of the limitation of updateItem request, the implementation of UPDATE will send a putItem request when a key-only object is being saved, and it will send another updateItem request if the given key(s) already exists in the table.

By default, the mapper uses UPDATE.

        UPDATE,

        
UPDATE_SKIP_NULL_ATTRIBUTES is similar to UPDATE, except that it ignores any null value attribute(s) and will NOT remove them from that item in DynamoDB. It also guarantees to send only one single updateItem request, no matter the object is key-only or not.
        UPDATE_SKIP_NULL_ATTRIBUTES,

        
CLOBBER will clear and replace all attributes, included unmodeled ones, (delete and recreate) on save. Versioned field constraints will also be disregarded.
        CLOBBER,

        
APPEND_SET treats scalar attributes (String, Number, Binary) the same as UPDATE_SKIP_NULL_ATTRIBUTES does. However, for set attributes, it will append to the existing attribute value, instead of overriding it. Caller needs to make sure that the modeled attribute type matches the existing set type, otherwise it would result in a service exception.
        APPEND_SET
    };

    
Enumeration of consistent read behavior.

CONSISTENT uses consistent reads, EVENTUAL does not. Consistent reads have implications for performance and billing; see the service documentation for details.

By default, the mapper uses eventual consistency.

    public static enum ConsistentReads {
        CONSISTENT,
        EVENTUAL
    };

    
Enumeration of pagination loading strategy.
    public static enum PaginationLoadingStrategy {
        
Paginated list is lazily loaded when possible, and all loaded results are kept in the memory.

By default, the mapper uses LAZY_LOADING.

        LAZY_LOADING,

        
Only supports using iterator to read from the paginated list. All other list operations will return UnsupportedOperationException immediately. During the iteration, the list will clear all the previous results before loading the next page, so that the list will keep at most one page of the loaded results in memory. This also means the list could only be iterated once.

Use this configuration to reduce the memory overhead when handling large DynamoDB items.

        ITERATION_ONLY,

        
Paginated list will eagerly load all the paginated results from DynamoDB as soon as the list is initialized.
        EAGER_LOADING
    }

    
Allows overriding the table name declared on a domain class by the DynamoDBTable annotation.
    public static final class TableNameOverride {
        private final String tableNameOverride;
        private final String tableNamePrefix;

        
Returns a new DynamoDBMapperConfig.TableNameOverride object that will prepend the given string to every table name.
        public static TableNameOverride withTableNamePrefix(
                String tableNamePrefix) {
            return new TableNameOverride(nulltableNamePrefix);
        }

        
Returns a new DynamoDBMapperConfig.TableNameOverride object that will replace every table name in requests with the given string.
        public static TableNameOverride withTableNameReplacement(
                String tableNameReplacement) {
            return new TableNameOverride(tableNameReplacementnull);
        }
        private TableNameOverride(
                String tableNameOverride,
                String tableNamePrefix) {
            this. = tableNameOverride;
            this. = tableNamePrefix;
        }

        
        public TableNameOverride(String tableNameOverride) {
            this(tableNameOverridenull);
        }

        
Returns the table name to use for all requests. Exclusive with getTableNamePrefix()

        public String getTableName() {
            return ;
        }

        
Returns the table name prefix to prepend the table name for all requests. Exclusive with getTableName()

        public String getTableNamePrefix() {
            return ;
        }
    }

    
Interface for a strategy used to determine the table name of an object based on it's class. This resolver is used when an object isn't available such as in DynamoDBMapper.query(java.lang.Class,com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression)

    public static interface TableNameResolver {

        
Get the table name for a class. This method is used when an object is not available such as when creating requests for scan or query operations.

Parameters:
clazz The class to get the table name for
config The DynamoDBMapperConfig
Returns:
The table name to use for instances of clazz
        public String getTableName(Class<?> clazzDynamoDBMapperConfig config);
    }

    
Interface for a strategy used to determine the table name of an object based on it's class. This resolver is used when an object is available such as in DynamoDBMapper.batchLoad(java.util.List). If no table name resolver for objects is set, DynamoDBMapper reverts to using the DynamoDBMapperConfig.TableNameResolver on each object's class.

Author(s):
Raniz
See also:
DynamoDBMapperConfig.TableNameResolver
    public static interface ObjectTableNameResolver {

        
Get the table name for an object.

Parameters:
object The object to get the table name for
config The DynamoDBMapperConfig
Returns:
The table name to use for object
        public String getTableName(Object objectDynamoDBMapperConfig config);
    }

    
Default implementation of DynamoDBMapperConfig.TableNameResolver that mimics the behavior of DynamoDBMapper before the addition of DynamoDBMapperConfig.TableNameResolver.

Author(s):
Raniz
    public static class DefaultTableNameResolver implements TableNameResolver {
        public static final DefaultTableNameResolver INSTANCE = new DefaultTableNameResolver();
        private final DynamoDBReflector reflector = new DynamoDBReflector();
        @Override
        public String getTableName(Class<?> clazzDynamoDBMapperConfig config) {
            final TableNameOverride override = config.getTableNameOverride();
            if (override != null) {
                final String tableName = override.getTableName();
                if (tableName != null) {
                    return tableName;
                }
            }
            final String tableName = .getTable(clazz).tableName();
            final String prefix = override == null
                ? null : override.getTableNamePrefix();
            return prefix == null ? tableName : prefix + tableName;
        }
    }
    private final SaveBehavior saveBehavior;
    private final ConsistentReads consistentReads;
    private final TableNameOverride tableNameOverride;
    private final TableNameResolver tableNameResolver;
    private final ConversionSchema conversionSchema;

    
Legacy constructor, using default PaginationLoadingStrategy

Deprecated:
in favor of the fluent DynamoDBMapperConfig.Builder
    @Deprecated
    public DynamoDBMapperConfig(
            SaveBehavior saveBehavior,
            ConsistentReads consistentReads,
            TableNameOverride tableNameOverride) {
        this(saveBehaviorconsistentReadstableNameOverridenullnull);
    }

    
Constructs a new configuration object with the save behavior, consistent read behavior, and table name override given.

Deprecated:
in favor of the fluent Builder
Parameters:
saveBehavior The DynamoDBMapperConfig.SaveBehavior to use, or null for default.
consistentReads The DynamoDBMapperConfig.ConsistentReads to use, or null for default.
tableNameOverride An override for the table name, or null for no override.
paginationLoadingStrategy The pagination loading strategy, or null for default.
    @Deprecated
    public DynamoDBMapperConfig(
            SaveBehavior saveBehavior,
            ConsistentReads consistentReads,
            TableNameOverride tableNameOverride,
            PaginationLoadingStrategy paginationLoadingStrategy) {
        this(saveBehaviorconsistentReadstableNameOverride,
                paginationLoadingStrategynull);
    }

    
Constructs a new configuration object with the save behavior, consistent read behavior, and table name override given.

Deprecated:
in favor of the fluent Builder
Parameters:
saveBehavior The DynamoDBMapperConfig.SaveBehavior to use, or null for default.
consistentReads The DynamoDBMapperConfig.ConsistentReads to use, or null for default.
tableNameOverride An override for the table name, or null for no override.
paginationLoadingStrategy The pagination loading strategy, or null for default.
requestMetricCollector optional request metric collector
    @Deprecated
    public DynamoDBMapperConfig(
            SaveBehavior saveBehavior,
            ConsistentReads consistentReads,
            TableNameOverride tableNameOverride,
            PaginationLoadingStrategy paginationLoadingStrategy,
            RequestMetricCollector requestMetricCollector) {
        this(saveBehavior,
                consistentReads,
                tableNameOverride,
                null,
                null,
                paginationLoadingStrategy,
                requestMetricCollector,
                .);
    }
    private DynamoDBMapperConfig(
            SaveBehavior saveBehavior,
            ConsistentReads consistentReads,
            TableNameOverride tableNameOverride,
            TableNameResolver tableNameResolver,
            ObjectTableNameResolver objectTableNameResolver,
            PaginationLoadingStrategy paginationLoadingStrategy,
            RequestMetricCollector requestMetricCollector,
            ConversionSchema conversionSchema) {
        this. = saveBehavior;
        this. = consistentReads;
        this. = tableNameOverride;
        this. = tableNameResolver;
        this. = objectTableNameResolver;
        this. = paginationLoadingStrategy;
        this. = requestMetricCollector;
        this. = conversionSchema;
    }

    
Constructs a new configuration object with the save behavior given.
    public DynamoDBMapperConfig(SaveBehavior saveBehavior) {
        this(saveBehaviornullnullnullnullnullnull,
                .);
    }

    
Constructs a new configuration object with the consistent read behavior given.
    public DynamoDBMapperConfig(ConsistentReads consistentReads) {
        this(nullconsistentReadsnullnullnullnullnull,
                .);
    }

    
Constructs a new configuration object with the table name override given.
    public DynamoDBMapperConfig(TableNameOverride tableNameOverride) {
        this(nullnulltableNameOverridenullnullnullnull,
                .);
    }

    
Constructs a new configuration object with the table name resolver strategy given.
    public DynamoDBMapperConfig(TableNameResolver tableNameResolver) {
        this(nullnullnulltableNameResolvernullnullnull,
                .);
    }

    
Constructs a new configuration object with the object table name resolver strategy given.
    public DynamoDBMapperConfig(ObjectTableNameResolver objectTableNameResolver) {
        this(nullnullnullnullobjectTableNameResolvernullnull,
                .);
    }

    
Constructs a new configuration object with the table name resolver strategies given.
    public DynamoDBMapperConfig(TableNameResolver tableNameResolverObjectTableNameResolver objectTableNameResolver) {
        this(nullnullnulltableNameResolverobjectTableNameResolvernullnull,
                .);
    }

    
Constructs a new configuration object with the pagination loading strategy given.
    public DynamoDBMapperConfig(
            PaginationLoadingStrategy paginationLoadingStrategy) {
        this(nullnullnullnullnullpaginationLoadingStrategynull,
                .);
    }

    
Constructs a new configuration object with the conversion schema given.
    public DynamoDBMapperConfig(ConversionSchema conversionSchema) {
        this(nullnullnullnullnullnullnullconversionSchema);
    }

    
Constructs a new configuration object from two others: a set of defaults and a set of overrides. Any non-null overrides will be applied to the defaults.

Used internally to merge the DynamoDBMapperConfig provided at construction with an overriding object for a particular operation.

Parameters:
defaults The default mapper configuration values.
overrides The overridden mapper configuration values. Any non-null config settings will be applied to the returned object.
    public DynamoDBMapperConfig(
            DynamoDBMapperConfig defaults,
            DynamoDBMapperConfig overrides) {
        if ( overrides == null ) {
            this. = defaults.getSaveBehavior();
            this. = defaults.getConsistentReads();
            this. = defaults.getTableNameOverride();
            this. = defaults.getTableNameResolver();
            this. = defaults.getObjectTableNameResolver();
            this. =
                    defaults.getPaginationLoadingStrategy();
            this. = defaults.getRequestMetricCollector();
            this. = defaults.getConversionSchema();
        } else {
            this. = (overrides.getSaveBehavior() == null)
                    ? defaults.getSaveBehavior()
                    : overrides.getSaveBehavior();
            this. = (overrides.getConsistentReads() == null)
                    ? defaults.getConsistentReads()
                    : overrides.getConsistentReads();
            this. = (overrides.getTableNameOverride() == null)
                    ? defaults.getTableNameOverride()
                    : overrides.getTableNameOverride();
            this. = (overrides.getTableNameResolver() == null)
                    ? defaults.getTableNameResolver()
                    : overrides.getTableNameResolver();
            this. = (overrides.getObjectTableNameResolver() == null)
                    ? defaults.getObjectTableNameResolver()
                    : overrides.getObjectTableNameResolver();
            this. =
                    (overrides.getPaginationLoadingStrategy() == null)
                    ? defaults.getPaginationLoadingStrategy()
                    : overrides.getPaginationLoadingStrategy();
            this. =
                    (overrides.getRequestMetricCollector() == null)
                    ? defaults.getRequestMetricCollector()
                    : overrides.getRequestMetricCollector();
            this. = (overrides.getConversionSchema() == null)
                    ? defaults.getConversionSchema()
                    : overrides.getConversionSchema();
        }
    }

    
Returns the save behavior for this configuration.
    public SaveBehavior getSaveBehavior() {
        return ;
    }

    
Returns the consistent read behavior for this configuration.
        return ;
    }

    
Returns the table name override for this configuration. This value will override the table name specified in a DynamoDBTable annotation, either by replacing the table name entirely or else by pre-pending a string to each table name. This is useful for partitioning data in multiple tables at runtime.

        return ;
    }

    
Returns the table name resolver for this configuration. This value will be used to determine the table name for classes. It can be used for more powerful customization of table name than is possible using only DynamoDBMapperConfig.TableNameOverride.

        return ;
    }

    
Returns the object table name resolver for this configuration. This value will be used to determine the table name for objects. It can be used for more powerful customization of table name than is possible using only DynamoDBMapperConfig.TableNameOverride.

        return ;
    }

    
Returns the pagination loading strategy for this configuration.
        return ;
    }

    
Returns the request metric collector or null if not specified.
        return ;
    }

    

Returns:
the conversion schema for this config object
        return ;
    }

    
Default configuration uses UPDATE behavior for saves and EVENTUALly consistent reads, with no table name override and lazy-loading strategy.
    public static final DynamoDBMapperConfig DEFAULT = new DynamoDBMapperConfig(
            .,
            .,
            null,  // TableNameOverride
            null// TableNameResolver
            null// ObjectTableNameResolver
            null,  // RequestMetricCollector
            .);
New to GrepCode? Check out our FAQ X