forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamoDBMapperConfig.java
More file actions
225 lines (204 loc) · 8.5 KB
/
DynamoDBMapperConfig.java
File metadata and controls
225 lines (204 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Copyright 2011-2013 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.dynamodb.datamodeling;
/**
* Immutable configuration object for service call behavior. An instance of this
* configuration is supplied to every {@link DynamoDBMapper} at construction; if
* not provided explicitly, {@link DynamoDBMapperConfig#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:
*
* <pre>
* 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));
* </pre>
*/
@Deprecated
public class DynamoDBMapperConfig {
/**
* Enumeration of behaviors for the save operation.
* <p>
* UPDATE will not affect unmodeled attributes on a save operation. CLOBBER
* will clear and replace all attributes, included unmodeled ones, (delete
* and recreate) on save. Versioned field constraints will also be
* disregarded.
* <p>
* By default, the mapper uses UPDATE.
*/
public static enum SaveBehavior {
UPDATE, CLOBBER
};
/**
* Enumeration of consistent read behavior.
* <p>
* CONSISTENT uses consistent reads, EVENTUAL does not. Consistent reads
* have implications for performance and billing; see the service
* documentation for details.
* <p>
* By default, the mapper uses eventual consistency.
*/
public static enum ConsistentReads {
CONSISTENT, EVENTUAL
};
/**
* Allows overriding the table name declared on a domain class by the
* {@link DynamoDBTable} annotation.
*/
public static final class TableNameOverride {
private final String tableNameOverride;
private final String tableNamePrefix;
/**
* Returns a new {@link TableNameOverride} object that will prepend the
* given string to every table name.
*/
public static TableNameOverride withTableNamePrefix(String tableNamePrefix) {
return new TableNameOverride(null, tableNamePrefix);
}
/**
* Returns a new {@link TableNameOverride} object that will replace
* every table name in requests with the given string.
*/
public static TableNameOverride withTableNameReplacement(String tableNameReplacement) {
return new TableNameOverride(tableNameReplacement, null);
}
private TableNameOverride(String tableNameOverride, String tableNamePrefix) {
this.tableNameOverride = tableNameOverride;
this.tableNamePrefix = tableNamePrefix;
}
/**
* @see TableNameOverride#withTableNameReplacement(String)
*/
public TableNameOverride(String tableNameOverride) {
this(tableNameOverride, null);
}
/**
* Returns the table name to use for all requests. Exclusive with
* {@link TableNameOverride#getTableNamePrefix()}
*
* @see DynamoDBMapperConfig#getTableNameOverride()
*/
public String getTableName() {
return tableNameOverride;
}
/**
* Returns the table name prefix to prepend the table name for all
* requests. Exclusive with {@link TableNameOverride#getTableName()}
*
* @see DynamoDBMapperConfig#getTableNameOverride()
*/
public String getTableNamePrefix() {
return tableNamePrefix;
}
}
private final SaveBehavior saveBehavior;
private final ConsistentReads consistentReads;
private final TableNameOverride tableNameOverride;
/**
* Constructs a new configuration object with the save behavior, consistent
* read behavior, and table name override given.
*
* @param saveBehavior
* The {@link SaveBehavior} to use, or null for default.
* @param consistentReads
* The {@link ConsistentReads} to use, or null for default.
* @param tableNameOverride
* An override for the table name, or null for no override.
*/
public DynamoDBMapperConfig(SaveBehavior saveBehavior, ConsistentReads consistentReads,
TableNameOverride tableNameOverride) {
this.saveBehavior = saveBehavior;
this.consistentReads = consistentReads;
this.tableNameOverride = tableNameOverride;
}
/**
* Constructs a new configuration object with the save behavior given.
*/
public DynamoDBMapperConfig(SaveBehavior saveBehavior) {
this(saveBehavior, null, null);
}
/**
* Constructs a new configuration object with the consistent read behavior
* given.
*/
public DynamoDBMapperConfig(ConsistentReads consistentReads) {
this(null, consistentReads, null);
}
/**
* Constructs a new configuration object with the table name override given.
*/
public DynamoDBMapperConfig(TableNameOverride tableNameOverride) {
this(null, null, tableNameOverride);
}
/**
* 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.
* <p>
* Used internally to merge the {@link DynamoDBMapperConfig} provided at
* construction with an overriding object for a particular operation.
*/
DynamoDBMapperConfig(DynamoDBMapperConfig defaults, DynamoDBMapperConfig overrides) {
if ( overrides == null ) {
this.saveBehavior = defaults.getSaveBehavior();
this.consistentReads = defaults.getConsistentReads();
this.tableNameOverride = defaults.getTableNameOverride();
} else {
this.saveBehavior = overrides.getSaveBehavior() == null ? defaults.getSaveBehavior() : overrides
.getSaveBehavior();
this.consistentReads = overrides.getConsistentReads() == null ? defaults.getConsistentReads() : overrides
.getConsistentReads();
this.tableNameOverride = overrides.getTableNameOverride() == null ? defaults.getTableNameOverride()
: overrides.getTableNameOverride();
}
}
/**
* Returns the save behavior for this configuration.
*/
public SaveBehavior getSaveBehavior() {
return saveBehavior;
}
/**
* Returns the consistent read behavior for this configuration.
*/
public ConsistentReads getConsistentReads() {
return consistentReads;
}
/**
* Returns the table name override for this configuration. This value will
* override the table name specified in a {@link 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.
*
* @see TableNameOverride#withTableNamePrefix(String)
* @see TableNameOverride#withTableNameReplacement(String)
*/
public TableNameOverride getTableNameOverride() {
return tableNameOverride;
}
/**
* Default configuration uses UPDATE behavior for saves and EVENTUALly
* consistent reads, with no table name override.
*/
public static final DynamoDBMapperConfig DEFAULT = new DynamoDBMapperConfig(SaveBehavior.UPDATE,
ConsistentReads.EVENTUAL, null);
}