| 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.dynamo.mock; |
| 31 | |
|
| 32 | |
import com.amazonaws.services.dynamodbv2.model.AttributeValue; |
| 33 | |
import com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate; |
| 34 | |
import com.google.common.base.Function; |
| 35 | |
import com.google.common.collect.ImmutableMap; |
| 36 | |
import com.google.common.collect.Maps; |
| 37 | |
import com.jcabi.aspects.Immutable; |
| 38 | |
import com.jcabi.aspects.Loggable; |
| 39 | |
import com.jcabi.dynamo.AttributeUpdates; |
| 40 | |
import com.jcabi.dynamo.Attributes; |
| 41 | |
import com.jcabi.dynamo.Conditions; |
| 42 | |
import com.jcabi.dynamo.Frame; |
| 43 | |
import com.jcabi.dynamo.Item; |
| 44 | |
import java.io.IOException; |
| 45 | |
import java.util.HashMap; |
| 46 | |
import java.util.Locale; |
| 47 | |
import java.util.Map; |
| 48 | |
import lombok.EqualsAndHashCode; |
| 49 | |
import lombok.ToString; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
@Immutable |
| 59 | 2 | @ToString |
| 60 | |
@Loggable(Loggable.DEBUG) |
| 61 | 0 | @EqualsAndHashCode(of = { "data", "table", "attributes" }) |
| 62 | |
final class MkItem implements Item { |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
private final transient MkData data; |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
private final transient String table; |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
private final transient Attributes attributes; |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | 4 | MkItem(final MkData dta, final String tbl, final Attributes attribs) { |
| 86 | 4 | this.data = dta; |
| 87 | 4 | this.table = tbl; |
| 88 | 4 | this.attributes = attribs; |
| 89 | 4 | } |
| 90 | |
|
| 91 | |
@Override |
| 92 | |
public AttributeValue get(final String name) throws IOException { |
| 93 | 8 | return this.data.iterate( |
| 94 | |
this.table, |
| 95 | |
new Conditions().withAttributes( |
| 96 | |
this.attributes.only(this.data.keys(this.table)) |
| 97 | |
) |
| 98 | |
).iterator().next().get(name); |
| 99 | |
} |
| 100 | |
|
| 101 | |
@Override |
| 102 | |
public boolean has(final String name) throws IOException { |
| 103 | 2 | return this.data.iterate( |
| 104 | |
this.table, |
| 105 | |
new Conditions().withAttributes( |
| 106 | |
this.attributes.only(this.data.keys(this.table)) |
| 107 | |
) |
| 108 | |
).iterator().next().containsKey(name.toLowerCase(Locale.ENGLISH)); |
| 109 | |
} |
| 110 | |
|
| 111 | |
@Override |
| 112 | |
public Map<String, AttributeValue> put( |
| 113 | |
final String name, final AttributeValueUpdate value) |
| 114 | |
throws IOException { |
| 115 | 4 | return this.put( |
| 116 | |
new ImmutableMap.Builder<String, AttributeValueUpdate>() |
| 117 | |
.put(name, value).build() |
| 118 | |
); |
| 119 | |
} |
| 120 | |
|
| 121 | |
@Override |
| 122 | |
public Map<String, AttributeValue> put( |
| 123 | |
final Map<String, AttributeValueUpdate> attrs) throws IOException { |
| 124 | 4 | final Map<String, AttributeValue> keys = |
| 125 | |
new HashMap<String, AttributeValue>(0); |
| 126 | 2 | for (final String attr : this.data.keys(this.table)) { |
| 127 | 2 | keys.put(attr, this.attributes.get(attr)); |
| 128 | 2 | } |
| 129 | |
try { |
| 130 | 2 | this.data.update( |
| 131 | |
this.table, |
| 132 | |
new Attributes(keys), |
| 133 | |
new AttributeUpdates(attrs) |
| 134 | |
); |
| 135 | 0 | } catch (final IOException ex) { |
| 136 | 0 | throw new IllegalStateException(ex); |
| 137 | 2 | } |
| 138 | 2 | return Maps.transformValues( |
| 139 | |
attrs, |
| 140 | 6 | new Function<AttributeValueUpdate, AttributeValue>() { |
| 141 | |
@Override |
| 142 | |
public AttributeValue apply(final AttributeValueUpdate update) { |
| 143 | 4 | return update.getValue(); |
| 144 | |
} |
| 145 | |
} |
| 146 | |
); |
| 147 | |
} |
| 148 | |
|
| 149 | |
@Override |
| 150 | |
public Frame frame() { |
| 151 | 0 | return new MkFrame(this.data, this.table); |
| 152 | |
} |
| 153 | |
} |