| 1 | 0 | |
| 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 | |
package com.jcabi.simpledb; |
| 31 | |
|
| 32 | |
import com.amazonaws.services.simpledb.model.Attribute; |
| 33 | |
import com.amazonaws.services.simpledb.model.DeleteAttributesRequest; |
| 34 | |
import com.amazonaws.services.simpledb.model.GetAttributesRequest; |
| 35 | |
import com.amazonaws.services.simpledb.model.GetAttributesResult; |
| 36 | |
import com.amazonaws.services.simpledb.model.PutAttributesRequest; |
| 37 | |
import com.amazonaws.services.simpledb.model.ReplaceableAttribute; |
| 38 | |
import com.jcabi.aspects.Immutable; |
| 39 | |
import com.jcabi.aspects.Loggable; |
| 40 | |
import java.util.AbstractMap; |
| 41 | |
import java.util.ArrayList; |
| 42 | |
import java.util.Collection; |
| 43 | |
import java.util.HashSet; |
| 44 | |
import java.util.Map; |
| 45 | |
import java.util.Set; |
| 46 | |
import java.util.concurrent.ConcurrentHashMap; |
| 47 | |
import java.util.concurrent.ConcurrentMap; |
| 48 | |
import lombok.EqualsAndHashCode; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | 1 | @Immutable |
| 59 | |
@Loggable(Loggable.DEBUG) |
| 60 | 0 | @EqualsAndHashCode(of = { "credentials", "table", "label" }) |
| 61 | |
@SuppressWarnings("PMD.TooManyMethods") |
| 62 | |
final class AwsItem implements Item { |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
private final transient Credentials credentials; |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
private final transient String table; |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
private final transient String label; |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
protected AwsItem(final Credentials creds, final String tbl, |
| 86 | 2 | final String item) { |
| 87 | 2 | this.credentials = creds; |
| 88 | 2 | this.table = tbl; |
| 89 | 2 | this.label = item; |
| 90 | 2 | } |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
protected AwsItem(final Credentials creds, final String tbl, |
| 99 | |
final com.amazonaws.services.simpledb.model.Item item) { |
| 100 | 0 | this(creds, tbl, item.getName()); |
| 101 | 0 | } |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
@Override |
| 107 | |
public String toString() { |
| 108 | 1 | return String.format("%s in %s", this.label, this.table); |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
@Override |
| 115 | |
public String name() { |
| 116 | 0 | return this.label; |
| 117 | |
} |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
@Override |
| 123 | |
public int size() { |
| 124 | 0 | return this.entrySet().size(); |
| 125 | |
} |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
@Override |
| 131 | |
public boolean isEmpty() { |
| 132 | 0 | return this.entrySet().isEmpty(); |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
@Override |
| 139 | |
public boolean containsKey(final Object key) { |
| 140 | 0 | return this.keySet().contains(key); |
| 141 | |
} |
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
@Override |
| 147 | |
public boolean containsValue(final Object value) { |
| 148 | 0 | return this.values().contains(value); |
| 149 | |
} |
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
@Override |
| 155 | |
public String get(final Object key) { |
| 156 | 2 | final Set<Entry<String, String>> entries = this.entrySet(); |
| 157 | 1 | String value = null; |
| 158 | 1 | for (final Entry<String, String> entry : entries) { |
| 159 | 1 | if (entry.getKey().equals(key)) { |
| 160 | 0 | value = entry.getValue(); |
| 161 | |
} |
| 162 | 1 | } |
| 163 | 1 | return value; |
| 164 | |
} |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
@Override |
| 170 | |
public String put(final String key, final String value) { |
| 171 | 0 | final String before = this.get(key); |
| 172 | 0 | final ConcurrentMap<String, String> map = |
| 173 | |
new ConcurrentHashMap<String, String>(0); |
| 174 | 0 | map.put(key, value); |
| 175 | 0 | this.putAll(map); |
| 176 | 0 | return before; |
| 177 | |
} |
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
@Override |
| 183 | |
public String remove(final Object key) { |
| 184 | 0 | final String before = this.get(key); |
| 185 | 0 | this.credentials.aws().deleteAttributes( |
| 186 | |
new DeleteAttributesRequest() |
| 187 | |
.withDomainName(this.table) |
| 188 | |
.withItemName(this.label) |
| 189 | |
.withAttributes(new Attribute().withName(key.toString())) |
| 190 | |
); |
| 191 | 0 | return before; |
| 192 | |
} |
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
@Override |
| 198 | |
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") |
| 199 | |
public void putAll(final Map<? extends String, ? extends String> map) { |
| 200 | 0 | final Collection<ReplaceableAttribute> attrs = |
| 201 | |
new ArrayList<ReplaceableAttribute>(map.size()); |
| 202 | 0 | for (final Map.Entry<?, ?> entry : map.entrySet()) { |
| 203 | 0 | attrs.add( |
| 204 | |
new ReplaceableAttribute() |
| 205 | |
.withName(entry.getKey().toString()) |
| 206 | |
.withValue(entry.getValue().toString()) |
| 207 | |
.withReplace(true) |
| 208 | |
); |
| 209 | 0 | } |
| 210 | 0 | this.credentials.aws().putAttributes( |
| 211 | |
new PutAttributesRequest() |
| 212 | |
.withDomainName(this.table) |
| 213 | |
.withItemName(this.label) |
| 214 | |
.withAttributes(attrs) |
| 215 | |
); |
| 216 | 0 | } |
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
@Override |
| 222 | |
public void clear() { |
| 223 | 0 | this.credentials.aws().deleteAttributes( |
| 224 | |
new DeleteAttributesRequest() |
| 225 | |
.withDomainName(this.table) |
| 226 | |
.withItemName(this.label) |
| 227 | |
); |
| 228 | 0 | } |
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
@Override |
| 234 | |
public Set<String> keySet() { |
| 235 | 0 | final Set<Entry<String, String>> entries = this.entrySet(); |
| 236 | 0 | final Set<String> keys = new HashSet<String>(entries.size()); |
| 237 | 0 | for (final Entry<String, String> entry : entries) { |
| 238 | 0 | keys.add(entry.getValue()); |
| 239 | 0 | } |
| 240 | 0 | return keys; |
| 241 | |
} |
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
@Override |
| 247 | |
public Collection<String> values() { |
| 248 | 0 | final Set<Entry<String, String>> entries = this.entrySet(); |
| 249 | 0 | final Collection<String> values = new ArrayList<String>(entries.size()); |
| 250 | 0 | for (final Entry<String, String> entry : entries) { |
| 251 | 0 | values.add(entry.getValue()); |
| 252 | 0 | } |
| 253 | 0 | return values; |
| 254 | |
} |
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
@Override |
| 260 | |
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") |
| 261 | |
public Set<Entry<String, String>> entrySet() { |
| 262 | 2 | final GetAttributesResult result = this.credentials.aws().getAttributes( |
| 263 | |
new GetAttributesRequest() |
| 264 | |
.withConsistentRead(true) |
| 265 | |
.withDomainName(this.table) |
| 266 | |
.withItemName(this.label) |
| 267 | |
); |
| 268 | 1 | final Set<Entry<String, String>> entries = |
| 269 | |
new HashSet<Entry<String, String>>(0); |
| 270 | 1 | for (final Attribute attr : result.getAttributes()) { |
| 271 | 1 | entries.add( |
| 272 | |
new AbstractMap.SimpleImmutableEntry<String, String>( |
| 273 | |
attr.getName(), attr.getValue() |
| 274 | |
) |
| 275 | |
); |
| 276 | 1 | } |
| 277 | 1 | return entries; |
| 278 | |
} |
| 279 | |
|
| 280 | |
} |