Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Region |
|
| 1.0;1 | ||||
Region$Prefixed |
|
| 1.0;1 | ||||
Region$Prefixed$AjcClosure1 |
|
| 1.0;1 | ||||
Region$Prefixed$AjcClosure3 |
|
| 1.0;1 | ||||
Region$Simple |
|
| 1.0;1 | ||||
Region$Simple$AjcClosure1 |
|
| 1.0;1 | ||||
Region$Simple$AjcClosure3 |
|
| 1.0;1 |
1 | 0 | /** |
2 | * Copyright (c) 2012-2016, jcabi.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 jcabi.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.jcabi.dynamo; | |
31 | ||
32 | import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; | |
33 | import com.jcabi.aspects.Immutable; | |
34 | import com.jcabi.aspects.Loggable; | |
35 | import lombok.EqualsAndHashCode; | |
36 | import lombok.ToString; | |
37 | ||
38 | /** | |
39 | * Amazon DynamoDB region. | |
40 | * | |
41 | * <p>It is recommended to use {@link Region.Simple} in most cases. | |
42 | * | |
43 | * <p>You can use {@link #aws()} method to get access to Amazon DynamoDB | |
44 | * client directly. | |
45 | * | |
46 | * <p>Since version 0.9 it is strongly recommended to wrap your region | |
47 | * in {@link com.jcabi.dynamo.retry.ReRegion} before use, for example: | |
48 | * | |
49 | * <pre> Region region = new ReRegion(new Region.Simple(credentials));</pre> | |
50 | * | |
51 | * <p>After all operations with the region are finished, it can be optionally | |
52 | * shutdown invoking {@link AmazonDynamoDB#shutdown()}. Callers are not expected | |
53 | * to call it, but can if they want to explicitly release any open resources and | |
54 | * forcibly terminate all pending asynchronous service calls. Once a client has | |
55 | * been shutdown, it should not be used to make any more requests. | |
56 | * | |
57 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
58 | * @version $Id: 8a9456f02bb77b305597ca45dd12d6e9cc37f1ef $ | |
59 | * @since 0.1 | |
60 | */ | |
61 | @Immutable | |
62 | public interface Region { | |
63 | ||
64 | /** | |
65 | * Get DynamoDB client. | |
66 | * @return The client | |
67 | */ | |
68 | AmazonDynamoDB aws(); | |
69 | ||
70 | /** | |
71 | * Get one table. | |
72 | * @param name Table name | |
73 | * @return Table | |
74 | */ | |
75 | Table table(String name); | |
76 | ||
77 | /** | |
78 | * Simple region, basic implementation. | |
79 | */ | |
80 | @Immutable | |
81 | @Loggable(Loggable.DEBUG) | |
82 | 0 | @ToString |
83 | 0 | @EqualsAndHashCode(of = "credentials") |
84 | final class Simple implements Region { | |
85 | /** | |
86 | * Credentials. | |
87 | */ | |
88 | private final transient Credentials credentials; | |
89 | /** | |
90 | * Public ctor. | |
91 | * @param creds Credentials | |
92 | */ | |
93 | 1 | public Simple(final Credentials creds) { |
94 | 1 | this.credentials = creds; |
95 | 1 | } |
96 | @Override | |
97 | public AmazonDynamoDB aws() { | |
98 | 0 | return this.credentials.aws(); |
99 | } | |
100 | @Override | |
101 | public Table table(final String name) { | |
102 | 0 | return new AwsTable(this.credentials, this, name); |
103 | } | |
104 | } | |
105 | ||
106 | /** | |
107 | * All tables have a prefix in front of their names. | |
108 | * | |
109 | * <p>The region has to be used in combination with another region, | |
110 | * for example {@link Region.Simple}: | |
111 | * | |
112 | * <pre>Region region = new Region.Prefixed( | |
113 | * new Region.Simple(creds), | |
114 | * "foo-" | |
115 | * );</pre> | |
116 | * | |
117 | * <p>Now, {@code region.table("test")} will return a {@link Table} | |
118 | * instance pointing to the Dynamo DB table named {@code "foo-test"}. Could | |
119 | * be a convenient mechanism when you have many tables for different | |
120 | * projects in the same region. | |
121 | */ | |
122 | @Immutable | |
123 | @Loggable(Loggable.DEBUG) | |
124 | 0 | @ToString |
125 | 0 | @EqualsAndHashCode(of = { "origin", "prefix" }) |
126 | final class Prefixed implements Region { | |
127 | /** | |
128 | * Original region. | |
129 | */ | |
130 | private final transient Region origin; | |
131 | /** | |
132 | * Prefix to add. | |
133 | */ | |
134 | private final transient String prefix; | |
135 | /** | |
136 | * Public ctor. | |
137 | * @param region Original region | |
138 | * @param pfx Prefix to add to all tables | |
139 | */ | |
140 | 1 | public Prefixed(final Region region, final String pfx) { |
141 | 1 | this.origin = region; |
142 | 1 | this.prefix = pfx; |
143 | 1 | } |
144 | @Override | |
145 | public AmazonDynamoDB aws() { | |
146 | 0 | return this.origin.aws(); |
147 | } | |
148 | @Override | |
149 | public Table table(final String name) { | |
150 | 2 | return this.origin.table( |
151 | new StringBuilder(this.prefix).append(name).toString() | |
152 | ); | |
153 | } | |
154 | } | |
155 | ||
156 | } |