-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23e5cdb
commit 109c024
Showing
15 changed files
with
210 additions
and
139 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
104 changes: 0 additions & 104 deletions
104
api/src/main/java/org/apache/xtable/model/TableSyncMetadata.java
This file was deleted.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
api/src/main/java/org/apache/xtable/model/metadata/TableSyncMetadata.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,93 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.xtable.model.metadata; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Value; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
|
||
import org.apache.xtable.model.exception.ParseException; | ||
|
||
/** | ||
* Metadata representing the state of a table sync process. This metadata is stored in the target | ||
* table's properties and is used to track the status of previous sync operation. | ||
*/ | ||
@Value | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class TableSyncMetadata { | ||
private static final int CURRENT_VERSION = 0; | ||
private static final ObjectMapper MAPPER = | ||
new ObjectMapper() | ||
.registerModule(new JavaTimeModule()) | ||
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) | ||
.setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
|
||
/** Property name for the XTABLE metadata in the table metadata/properties */ | ||
public static final String XTABLE_METADATA = "XTABLE_METADATA"; | ||
|
||
Instant lastInstantSynced; | ||
List<Instant> instantsToConsiderForNextSync; | ||
int version; | ||
|
||
public static TableSyncMetadata of( | ||
Instant lastInstantSynced, List<Instant> instantsToConsiderForNextSync) { | ||
return new TableSyncMetadata(lastInstantSynced, instantsToConsiderForNextSync, CURRENT_VERSION); | ||
} | ||
|
||
public String toJson() { | ||
try { | ||
return MAPPER.writeValueAsString(this); | ||
} catch (IOException e) { | ||
throw new ParseException("Failed to serialize TableSyncMetadata", e); | ||
} | ||
} | ||
|
||
public static Optional<TableSyncMetadata> fromJson(String metadata) { | ||
if (metadata == null || metadata.isEmpty()) { | ||
return Optional.empty(); | ||
} else { | ||
try { | ||
TableSyncMetadata parsedMetadata = MAPPER.readValue(metadata, TableSyncMetadata.class); | ||
if (parsedMetadata.getLastInstantSynced() == null) { | ||
throw new ParseException("LastInstantSynced is required in TableSyncMetadata"); | ||
} | ||
if (parsedMetadata.getVersion() > CURRENT_VERSION) { | ||
throw new ParseException( | ||
"Unable handle metadata version: " | ||
+ parsedMetadata.getVersion() | ||
+ " max supported version: " | ||
+ CURRENT_VERSION); | ||
} | ||
return Optional.of(parsedMetadata); | ||
} catch (IOException e) { | ||
throw new ParseException("Failed to deserialize TableSyncMetadata", e); | ||
} | ||
} | ||
} | ||
} |
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
79 changes: 79 additions & 0 deletions
79
api/src/test/java/org/apache/xtable/model/metadata/TestTableSyncMetadata.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,79 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.xtable.model.metadata; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.time.Instant; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import org.apache.xtable.model.exception.ParseException; | ||
|
||
class TestTableSyncMetadata { | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideMetadataAndJson") | ||
void jsonRoundTrip(TableSyncMetadata metadata, String expectedJson) { | ||
assertEquals(expectedJson, metadata.toJson()); | ||
assertEquals(metadata, TableSyncMetadata.fromJson(expectedJson).get()); | ||
} | ||
|
||
private static Stream<Arguments> provideMetadataAndJson() { | ||
return Stream.of( | ||
Arguments.of( | ||
TableSyncMetadata.of( | ||
Instant.parse("2020-07-04T10:15:30.00Z"), | ||
Arrays.asList( | ||
Instant.parse("2020-08-21T11:15:30.00Z"), | ||
Instant.parse("2024-01-21T12:15:30.00Z"))), | ||
"{\"lastInstantSynced\":\"2020-07-04T10:15:30Z\",\"instantsToConsiderForNextSync\":[\"2020-08-21T11:15:30Z\",\"2024-01-21T12:15:30Z\"],\"version\":0}"), | ||
Arguments.of( | ||
TableSyncMetadata.of(Instant.parse("2020-07-04T10:15:30.00Z"), Collections.emptyList()), | ||
"{\"lastInstantSynced\":\"2020-07-04T10:15:30Z\",\"instantsToConsiderForNextSync\":[],\"version\":0}"), | ||
Arguments.of( | ||
TableSyncMetadata.of(Instant.parse("2020-07-04T10:15:30.00Z"), null), | ||
"{\"lastInstantSynced\":\"2020-07-04T10:15:30Z\",\"version\":0}")); | ||
} | ||
|
||
@Test | ||
void failToParseJsonFromNewerVersion() { | ||
assertThrows( | ||
ParseException.class, | ||
() -> | ||
TableSyncMetadata.fromJson( | ||
"{\"lastInstantSynced\":\"2020-07-04T10:15:30Z\",\"instantsToConsiderForNextSync\":[\"2020-08-21T11:15:30Z\",\"2024-01-21T12:15:30Z\"],\"version\":1}")); | ||
} | ||
|
||
@Test | ||
void failToParseJsonWithMissingLastSyncedInstant() { | ||
assertThrows( | ||
ParseException.class, | ||
() -> | ||
TableSyncMetadata.fromJson( | ||
"{\"instantsToConsiderForNextSync\":[\"2020-08-21T11:15:30Z\",\"2024-01-21T12:15:30Z\"],\"version\":0}")); | ||
} | ||
} |
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
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.