| 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.thindeck.dynamo; |
| 31 | |
|
| 32 | |
import com.amazonaws.services.dynamodbv2.model.Select; |
| 33 | |
import com.google.common.base.Function; |
| 34 | |
import com.google.common.collect.Iterables; |
| 35 | |
import com.jcabi.aspects.Immutable; |
| 36 | |
import com.jcabi.dynamo.Attributes; |
| 37 | |
import com.jcabi.dynamo.Item; |
| 38 | |
import com.jcabi.dynamo.QueryValve; |
| 39 | |
import com.jcabi.dynamo.Region; |
| 40 | |
import com.thindeck.api.Deck; |
| 41 | |
import com.thindeck.api.Decks; |
| 42 | |
import java.io.IOException; |
| 43 | |
import java.util.Iterator; |
| 44 | |
import lombok.EqualsAndHashCode; |
| 45 | |
import lombok.ToString; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | 0 | @ToString |
| 55 | |
@Immutable |
| 56 | 0 | @EqualsAndHashCode(of = { "region", "user" }) |
| 57 | |
final class DyDecks implements Decks { |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
private final transient Region region; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
private final transient String user; |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | 0 | DyDecks(final Region reg, final String usr) { |
| 75 | 0 | this.region = reg; |
| 76 | 0 | this.user = usr; |
| 77 | 0 | } |
| 78 | |
|
| 79 | |
@Override |
| 80 | |
public Deck get(final String name) throws IOException { |
| 81 | 0 | final Iterator<Item> items = this.region |
| 82 | |
.table(DyDeck.TBL) |
| 83 | |
.frame() |
| 84 | |
.through(new QueryValve().withLimit(1)) |
| 85 | |
.where(DyDeck.HASH, this.user) |
| 86 | |
.where(DyDeck.RANGE, name) |
| 87 | |
.iterator(); |
| 88 | 0 | if (!items.hasNext()) { |
| 89 | 0 | throw new IOException( |
| 90 | |
String.format("deck '%s' not found", name) |
| 91 | |
); |
| 92 | |
} |
| 93 | 0 | return new DyDeck( |
| 94 | |
this.region, this.user, name |
| 95 | |
); |
| 96 | |
} |
| 97 | |
|
| 98 | |
@Override |
| 99 | |
public void add(final String name) throws IOException { |
| 100 | 0 | if (!name.matches("[a-z]{3,12}")) { |
| 101 | 0 | throw new IllegalStateException( |
| 102 | |
String.format( |
| 103 | |
"invalid deck name '%s', must be 3-12 English letters", |
| 104 | |
name |
| 105 | |
) |
| 106 | |
); |
| 107 | |
} |
| 108 | 0 | final Iterator<Item> items = this.region.table(DyDeck.TBL) |
| 109 | |
.frame() |
| 110 | |
.through(new QueryValve().withLimit(1)) |
| 111 | |
.where(DyDeck.HASH, this.user) |
| 112 | |
.where(DyDeck.RANGE, name) |
| 113 | |
.iterator(); |
| 114 | 0 | if (items.hasNext()) { |
| 115 | 0 | throw new IllegalStateException( |
| 116 | |
String.format( |
| 117 | |
"deck '%s' already exists, can't add it again", |
| 118 | |
name |
| 119 | |
) |
| 120 | |
); |
| 121 | |
} |
| 122 | 0 | this.region.table(DyDeck.TBL).put( |
| 123 | |
new Attributes() |
| 124 | |
.with(DyDeck.HASH, this.user) |
| 125 | |
.with(DyDeck.RANGE, name) |
| 126 | |
.with(DyDeck.ATTR_UPDATED, System.currentTimeMillis()) |
| 127 | |
.with( |
| 128 | |
DyDeck.ATTR_MEMO, |
| 129 | |
String.format("<deck name='%s/%s'/>", this.user, name) |
| 130 | |
) |
| 131 | |
); |
| 132 | 0 | } |
| 133 | |
|
| 134 | |
@Override |
| 135 | |
public void delete(final String name) { |
| 136 | 0 | final Iterator<Item> items = this.region.table(DyDeck.TBL) |
| 137 | |
.frame() |
| 138 | |
.through(new QueryValve().withLimit(1)) |
| 139 | |
.where(DyDeck.HASH, this.user) |
| 140 | |
.where(DyDeck.RANGE, name) |
| 141 | |
.iterator(); |
| 142 | 0 | if (!items.hasNext()) { |
| 143 | 0 | throw new IllegalStateException( |
| 144 | |
String.format( |
| 145 | |
"deck '%s' not found, can't delete", |
| 146 | |
name |
| 147 | |
) |
| 148 | |
); |
| 149 | |
} |
| 150 | 0 | items.next(); |
| 151 | 0 | items.remove(); |
| 152 | 0 | } |
| 153 | |
|
| 154 | |
@Override |
| 155 | |
public Iterable<Deck> iterate() { |
| 156 | 0 | return Iterables.transform( |
| 157 | |
this.region.table(DyDeck.TBL) |
| 158 | |
.frame() |
| 159 | |
.through(new QueryValve().withSelect(Select.ALL_ATTRIBUTES)) |
| 160 | |
.where(DyDeck.HASH, this.user), |
| 161 | 0 | new Function<Item, Deck>() { |
| 162 | |
@Override |
| 163 | |
public Deck apply(final Item input) { |
| 164 | |
try { |
| 165 | 0 | return DyDecks.this.get(input.get(DyDeck.RANGE).getS()); |
| 166 | 0 | } catch (final IOException ex) { |
| 167 | 0 | throw new IllegalStateException(ex); |
| 168 | |
} |
| 169 | |
} |
| 170 | |
} |
| 171 | |
); |
| 172 | |
} |
| 173 | |
|
| 174 | |
} |
| 175 | |
|