-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: simplify how code builds value Serde (#3148)
Simplifies how client code builds `Serde<GenericRow>`. Previously, client code has to build a `KsqlSerdeFactory` and then pass this to `GenericRowSerDe` to get a `Serde<GenericRow>`. Now, client code only needs one call to `GenericRowSerDe.from`. `GenericRowSerDe` also supports dependency injection via the new `ValueSerdeFactory` interface. `KsqlSerdeFactory` is now an implementation detail, encapsulated in the serde module. This code is highly in flux and will likely change again.
- Loading branch information
1 parent
268710a
commit d5d2791
Showing
41 changed files
with
1,053 additions
and
606 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
ksql-common/src/main/java/io/confluent/ksql/serde/WindowInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2019 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.serde; | ||
|
||
import com.google.errorprone.annotations.Immutable; | ||
import io.confluent.ksql.model.WindowType; | ||
import java.time.Duration; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Immutable pojo for storing info about a window. | ||
*/ | ||
@Immutable | ||
public final class WindowInfo { | ||
|
||
private final WindowType type; | ||
private final Optional<Duration> size; | ||
|
||
public static WindowInfo of(final WindowType type, final Optional<Duration> size) { | ||
return new WindowInfo(type, size); | ||
} | ||
|
||
private WindowInfo(final WindowType type, final Optional<Duration> size) { | ||
this.type = Objects.requireNonNull(type, "type"); | ||
this.size = Objects.requireNonNull(size, "size"); | ||
|
||
if (type.requiresWindowSize() && !size.isPresent()) { | ||
throw new IllegalArgumentException("Size required"); | ||
} | ||
|
||
if (!type.requiresWindowSize() && size.isPresent()) { | ||
throw new IllegalArgumentException("Size not required"); | ||
} | ||
|
||
if (size.isPresent() && (size.get().isZero() || size.get().isNegative())) { | ||
throw new IllegalArgumentException("Size must be positive"); | ||
} | ||
} | ||
|
||
public WindowType getType() { | ||
return type; | ||
} | ||
|
||
public Optional<Duration> getSize() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
final WindowInfo that = (WindowInfo) o; | ||
return type == that.type | ||
&& Objects.equals(size, that.size); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(type, size); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "WindowInfo{" | ||
+ "type=" + type | ||
+ ", size=" + size.map(Duration::toMillis) | ||
+ '}'; | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
ksql-common/src/test/java/io/confluent/ksql/serde/WindowInfoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright 2019 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.serde; | ||
|
||
import static io.confluent.ksql.model.WindowType.HOPPING; | ||
import static io.confluent.ksql.model.WindowType.SESSION; | ||
import static io.confluent.ksql.model.WindowType.TUMBLING; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import com.google.common.testing.EqualsTester; | ||
import com.google.common.testing.NullPointerTester; | ||
import io.confluent.ksql.model.WindowType; | ||
import java.time.Duration; | ||
import java.util.Optional; | ||
import org.junit.Test; | ||
|
||
public class WindowInfoTest { | ||
@Test | ||
public void shouldThrowNPEs() { | ||
new NullPointerTester() | ||
.testAllPublicStaticMethods(WindowInfo.class); | ||
} | ||
|
||
@Test | ||
public void shouldImplementEquals() { | ||
new EqualsTester() | ||
.addEqualityGroup( | ||
WindowInfo.of(SESSION, Optional.empty()), | ||
WindowInfo.of(SESSION, Optional.empty()) | ||
) | ||
.addEqualityGroup( | ||
WindowInfo.of(TUMBLING, Optional.of(Duration.ofMillis(19))), | ||
WindowInfo.of(TUMBLING, Optional.of(Duration.ofMillis(19))) | ||
) | ||
.addEqualityGroup( | ||
WindowInfo.of(HOPPING, Optional.of(Duration.ofMillis(19))), | ||
WindowInfo.of(HOPPING, Optional.of(Duration.ofMillis(19))) | ||
) | ||
.addEqualityGroup( | ||
WindowInfo.of(TUMBLING, Optional.of(Duration.ofMillis(1010))) | ||
) | ||
.testEquals(); | ||
} | ||
|
||
@Test | ||
public void shouldImplementToString() { | ||
// Given: | ||
final WindowInfo windowInfo = WindowInfo.of(TUMBLING, Optional.of(Duration.ofMillis(19))); | ||
|
||
// When: | ||
final String result = windowInfo.toString(); | ||
|
||
// Then: | ||
assertThat(result, containsString("TUMBLING")); | ||
assertThat(result, containsString("19")); | ||
} | ||
|
||
@Test | ||
public void shouldGetType() { | ||
// Given: | ||
final WindowInfo windowInfo = WindowInfo.of(SESSION, Optional.empty()); | ||
|
||
// When: | ||
final WindowType result = windowInfo.getType(); | ||
|
||
// Then: | ||
assertThat(result, is(SESSION)); | ||
} | ||
|
||
@Test | ||
public void shouldGetFormatInfo() { | ||
// Given: | ||
final WindowInfo windowInfo = WindowInfo.of(HOPPING, Optional.of(Duration.ofSeconds(10))); | ||
|
||
// When: | ||
final Optional<Duration> result = windowInfo.getSize(); | ||
|
||
// Then: | ||
assertThat(result, is(Optional.of(Duration.ofSeconds(10)))); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void shouldThrowIfSizeProvidedButNotRequired() { | ||
WindowInfo.of(SESSION, Optional.of(Duration.ofSeconds(10))); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void shouldThrowIfSizeRequiredButNotProvided() { | ||
WindowInfo.of(TUMBLING, Optional.empty()); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void shouldThrowIfSizeZero() { | ||
WindowInfo.of(TUMBLING, Optional.of(Duration.ZERO)); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void shouldThrowIfSizeNegative() { | ||
WindowInfo.of(TUMBLING, Optional.of(Duration.ofSeconds(-1))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.