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.AttributeValue; |
33 | |
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator; |
34 | |
import com.amazonaws.services.dynamodbv2.model.Condition; |
35 | |
import com.google.common.base.Function; |
36 | |
import com.google.common.collect.Iterables; |
37 | |
import com.jcabi.aspects.Immutable; |
38 | |
import com.jcabi.dynamo.Attributes; |
39 | |
import com.jcabi.dynamo.Item; |
40 | |
import com.jcabi.dynamo.QueryValve; |
41 | |
import com.jcabi.dynamo.Region; |
42 | |
import com.jcabi.manifests.Manifests; |
43 | |
import com.thindeck.api.Events; |
44 | |
import java.io.IOException; |
45 | |
import lombok.EqualsAndHashCode; |
46 | |
import lombok.ToString; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 0 | @ToString |
56 | |
@Immutable |
57 | 0 | @EqualsAndHashCode(of = { "region", "deck" }) |
58 | |
final class DyEvents implements Events { |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public static final String TBL = "events"; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public static final String HASH = "deck"; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public static final String RANGE = "msec"; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public static final String ATTR_HEAD = "head"; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public static final String ATTR_TEXT = "text"; |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | 0 | private static final String VERSION = String.format( |
89 | |
"%s %s %s", |
90 | |
|
91 | |
Manifests.read("Thindeck-Version"), |
92 | |
Manifests.read("Thindeck-Revision"), |
93 | |
Manifests.read("Thindeck-Date") |
94 | |
); |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
private final transient Region region; |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
private final transient String deck; |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | 0 | DyEvents(final Region reg, final String dck) { |
112 | 0 | this.region = reg; |
113 | 0 | this.deck = dck; |
114 | 0 | } |
115 | |
|
116 | |
@Override |
117 | |
public Iterable<String> iterate(final long since) { |
118 | 0 | return Iterables.transform( |
119 | |
this.region.table(DyEvents.TBL) |
120 | |
.frame() |
121 | |
.through(new QueryValve().withScanIndexForward(false)) |
122 | |
.where(DyEvents.HASH, this.deck) |
123 | |
.where( |
124 | |
DyEvents.RANGE, |
125 | |
new Condition() |
126 | |
.withComparisonOperator(ComparisonOperator.LE) |
127 | |
.withAttributeValueList( |
128 | |
new AttributeValue().withN(Long.toString(since)) |
129 | |
) |
130 | |
), |
131 | 0 | new Function<Item, String>() { |
132 | |
@Override |
133 | |
public String apply(final Item input) { |
134 | |
try { |
135 | 0 | return String.format( |
136 | |
"%s\n%s\n%s", |
137 | |
input.get(DyEvents.ATTR_HEAD).getS(), |
138 | |
input.get(DyEvents.RANGE).getN(), |
139 | |
input.get(DyEvents.ATTR_TEXT).getS() |
140 | |
); |
141 | 0 | } catch (final IOException ex) { |
142 | 0 | throw new IllegalStateException(ex); |
143 | |
} |
144 | |
} |
145 | |
} |
146 | |
); |
147 | |
} |
148 | |
|
149 | |
@Override |
150 | |
public void create(final String text) throws IOException { |
151 | 0 | final StringBuilder log = new StringBuilder(Drain.INSTANCE.fetch()); |
152 | 0 | if (log.length() > 0) { |
153 | 0 | log.append('\n').append(DyEvents.VERSION); |
154 | 0 | this.region.table(DyEvents.TBL).put( |
155 | |
new Attributes() |
156 | |
.with(DyEvents.HASH, this.deck) |
157 | |
.with( |
158 | |
DyEvents.RANGE, |
159 | |
new AttributeValue().withN( |
160 | |
Long.toString(System.currentTimeMillis()) |
161 | |
) |
162 | |
) |
163 | |
.with(DyEvents.ATTR_HEAD, text) |
164 | |
.with(DyEvents.ATTR_TEXT, log) |
165 | |
); |
166 | |
} |
167 | 0 | } |
168 | |
} |