-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Command.java
187 lines (165 loc) · 6.02 KB
/
Command.java
1
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright 2018 Confluent Inc.
*
* Licensed under the Confluent Community License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.confluent.io/confluent-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package io.confluent.ksql.rest.server.computation;
import static java.util.Objects.requireNonNull;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.confluent.ksql.config.SessionConfig;
import io.confluent.ksql.engine.KsqlPlan;
import io.confluent.ksql.planner.plan.ConfiguredKsqlPlan;
import io.confluent.ksql.properties.PropertiesUtil;
import io.confluent.ksql.rest.server.resources.IncompatibleKsqlCommandVersionException;
import io.confluent.ksql.statement.ConfiguredStatement;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@JsonSubTypes({})
public class Command {
@VisibleForTesting
public static final int VERSION = 18;
private final String statement;
private final Map<String, Object> overwriteProperties;
private final Map<String, String> originalProperties;
private final Optional<KsqlPlan> plan;
private final Optional<Integer> version;
@JsonCreator
public Command(
@JsonProperty(value = "statement", required = true) final String statement,
@JsonProperty("streamsProperties") final Optional<Map<String, Object>> overwriteProperties,
@JsonProperty("originalProperties") final Optional<Map<String, String>> originalProperties,
@JsonProperty("plan") final Optional<KsqlPlan> plan,
@JsonProperty("version") final Optional<Integer> version
) {
this(
statement,
overwriteProperties.orElseGet(ImmutableMap::of),
originalProperties.orElseGet(ImmutableMap::of),
plan,
version,
VERSION
);
}
@VisibleForTesting
public Command(
final String statement,
final Map<String, Object> overwriteProperties,
final Map<String, String> originalProperties,
final Optional<KsqlPlan> plan
) {
this(
statement,
overwriteProperties,
originalProperties,
plan,
Optional.of(VERSION),
VERSION
);
}
@VisibleForTesting
public Command(
final String statement,
final Map<String, Object> overwriteProperties,
final Map<String, String> originalProperties,
final Optional<KsqlPlan> plan,
final Optional<Integer> version,
final int expectedVersion
) {
this.statement = requireNonNull(statement, "statement");
// cannot use ImmutableMap, because we need to handle null-values
this.overwriteProperties = Collections.unmodifiableMap(
requireNonNull(overwriteProperties, "overwriteProperties"));
// cannot use ImmutableMap, because we need to handle null-values
this.originalProperties = Collections.unmodifiableMap(
requireNonNull(originalProperties, "originalProperties"));
this.plan = requireNonNull(plan, "plan");
this.version = requireNonNull(version, "version");
if (expectedVersion < version.orElse(0)) {
throw new IncompatibleKsqlCommandVersionException(
"Received a command from an incompatible command topic version. "
+ "Expected version less than or equal to " + expectedVersion
+ " but got " + version.orElse(0));
}
}
public String getStatement() {
return statement;
}
@JsonProperty("streamsProperties")
@SuppressFBWarnings(
value = "EI_EXPOSE_REP",
justification = "overwriteProperties is unmodifiableMap()"
)
public Map<String, Object> getOverwriteProperties() {
return PropertiesUtil.coerceTypes(overwriteProperties, true);
}
@SuppressFBWarnings(
value = "EI_EXPOSE_REP",
justification = "originalProperties is unmodifiableMap()"
)
public Map<String, String> getOriginalProperties() {
return originalProperties;
}
public Optional<KsqlPlan> getPlan() {
return plan;
}
public Optional<Integer> getVersion() {
return version;
}
public static Command of(final ConfiguredKsqlPlan configuredPlan) {
return new Command(
configuredPlan.getPlan().getStatementText(),
configuredPlan.getConfig().getOverrides(),
configuredPlan.getConfig().getConfig(false).getAllConfigPropsWithSecretsObfuscated(),
Optional.of(configuredPlan.getPlan()),
Optional.of(VERSION),
VERSION
);
}
public static Command of(final ConfiguredStatement<?> configuredStatement) {
final SessionConfig sessionConfig = configuredStatement.getSessionConfig();
return new Command(
configuredStatement.getUnMaskedStatementText(),
sessionConfig.getOverrides(),
sessionConfig.getConfig(false).getAllConfigPropsWithSecretsObfuscated(),
Optional.empty(),
Optional.of(VERSION),
VERSION
);
}
@Override
public boolean equals(final Object o) {
return
o instanceof Command
&& Objects.equals(statement, ((Command)o).statement)
&& Objects.equals(overwriteProperties, ((Command)o).overwriteProperties)
&& Objects.equals(originalProperties, ((Command)o).originalProperties);
}
@Override
public int hashCode() {
return Objects.hash(statement, overwriteProperties, originalProperties);
}
@Override
public String toString() {
return "Command{"
+ "statement='" + statement + '\''
+ ", overwriteProperties=" + overwriteProperties
+ ", version=" + version
+ '}';
}
}