| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DyBase |
|
| 2.3333333333333335;2.333 | ||||
| DyBase$1 |
|
| 2.3333333333333335;2.333 |
| 1 | 0 | /** |
| 2 | * Copyright (c) 2014-2015, Thindeck.com | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: 1) Redistributions of source code must retain the above | |
| 8 | * copyright notice, this list of conditions and the following | |
| 9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
| 10 | * copyright notice, this list of conditions and the following | |
| 11 | * disclaimer in the documentation and/or other materials provided | |
| 12 | * with the distribution. 3) Neither the name of the thindeck.com nor | |
| 13 | * the names of its contributors may be used to endorse or promote | |
| 14 | * products derived from this software without specific prior written | |
| 15 | * permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
| 19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
| 21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | */ | |
| 30 | package com.thindeck.dynamo; | |
| 31 | ||
| 32 | import com.google.common.base.Function; | |
| 33 | import com.google.common.collect.Iterables; | |
| 34 | import com.jcabi.aspects.Immutable; | |
| 35 | import com.jcabi.dynamo.Credentials; | |
| 36 | import com.jcabi.dynamo.Item; | |
| 37 | import com.jcabi.dynamo.Region; | |
| 38 | import com.jcabi.dynamo.retry.ReRegion; | |
| 39 | import com.jcabi.manifests.Manifests; | |
| 40 | import com.thindeck.api.Base; | |
| 41 | import com.thindeck.api.Deck; | |
| 42 | import com.thindeck.api.User; | |
| 43 | import java.io.IOException; | |
| 44 | import lombok.EqualsAndHashCode; | |
| 45 | import lombok.ToString; | |
| 46 | ||
| 47 | /** | |
| 48 | * Dynamo implementation of the {@link Base}. | |
| 49 | * | |
| 50 | * @author Krzyszof Krason (Krzysztof.Krason@gmail.com) | |
| 51 | * @author Yegor Bugayenko (yegor@teamed.io) | |
| 52 | * @version $Id$ | |
| 53 | * @since 0.3 | |
| 54 | * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) | |
| 55 | */ | |
| 56 | 0 | @ToString |
| 57 | @Immutable | |
| 58 | 0 | @EqualsAndHashCode(of = "region") |
| 59 | 0 | public final class DyBase implements Base { |
| 60 | ||
| 61 | /** | |
| 62 | * Region we're in. | |
| 63 | */ | |
| 64 | private final transient Region region; | |
| 65 | ||
| 66 | /** | |
| 67 | * Constructor. | |
| 68 | */ | |
| 69 | public DyBase() { | |
| 70 | 0 | this(DyBase.dynamo()); |
| 71 | 0 | } |
| 72 | ||
| 73 | /** | |
| 74 | * Constructor. | |
| 75 | * @param rgn Region | |
| 76 | */ | |
| 77 | 0 | public DyBase(final Region rgn) { |
| 78 | 0 | this.region = rgn; |
| 79 | 0 | Drain.INSTANCE.fetch(); |
| 80 | 0 | } |
| 81 | ||
| 82 | @Override | |
| 83 | public User user(final String name) { | |
| 84 | 0 | if (!name.matches("[a-zA-Z\\-0-9]+")) { |
| 85 | 0 | throw new IllegalStateException( |
| 86 | String.format( | |
| 87 | "invalid user name '%s'", | |
| 88 | name | |
| 89 | ) | |
| 90 | ); | |
| 91 | } | |
| 92 | 0 | return new DyUser(this.region, name); |
| 93 | } | |
| 94 | ||
| 95 | @Override | |
| 96 | public Iterable<Deck> active() { | |
| 97 | 0 | return Iterables.transform( |
| 98 | this.region.table(DyDeck.TBL).frame(), | |
| 99 | 0 | new Function<Item, Deck>() { |
| 100 | @Override | |
| 101 | public Deck apply(final Item item) { | |
| 102 | try { | |
| 103 | 0 | return new DyDeck( |
| 104 | DyBase.this.region, | |
| 105 | item.get(DyDeck.HASH).getS(), | |
| 106 | item.get(DyDeck.RANGE).getS() | |
| 107 | ); | |
| 108 | 0 | } catch (final IOException ex) { |
| 109 | 0 | throw new IllegalStateException(ex); |
| 110 | } | |
| 111 | } | |
| 112 | } | |
| 113 | ); | |
| 114 | } | |
| 115 | ||
| 116 | /** | |
| 117 | * Dynamo DB region. | |
| 118 | * @return Region | |
| 119 | */ | |
| 120 | private static Region dynamo() { | |
| 121 | 0 | final String key = Manifests.read("Thindeck-DynamoKey"); |
| 122 | 0 | Credentials creds = new Credentials.Simple( |
| 123 | key, Manifests.read("Thindeck-DynamoSecret") | |
| 124 | ); | |
| 125 | 0 | if (key.startsWith("AAAAA")) { |
| 126 | 0 | final String port = System.getProperty("dynamo.port"); |
| 127 | 0 | if (port == null) { |
| 128 | 0 | throw new IllegalStateException( |
| 129 | "dynamo.port system property is not set, check pom.xml" | |
| 130 | ); | |
| 131 | } | |
| 132 | 0 | creds = new Credentials.Direct( |
| 133 | creds, | |
| 134 | Integer.parseInt(port) | |
| 135 | ); | |
| 136 | } | |
| 137 | 0 | return new Region.Prefixed( |
| 138 | new ReRegion(new Region.Simple(creds)), "td-" | |
| 139 | ); | |
| 140 | } | |
| 141 | ||
| 142 | } |