-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Transforming Content Push Command (#1064)
Motivation: The current push functionality in the Central Dogma server lacks the ability to update content programmatically during the push operation. While this can be achieved by reading the existing file and modifying it before pushing, this approach does not address potential race conditions. To resolve this, a new mechanism is introduced to dynamically transform content during the push operation. Modifications: - Introduced `TransformingContentPushCommand` that includes a transformer `Function`. - The `TransformingContentPushCommand` is converted to a `PushAsIsCommand` for replication, similar to `NormalizingPushCommand`. - Refactored the commit logic in `GitRepository` into `CommitExecutor` and `ChangesApplier`. Result: - Central Dogma server can now perform dynamic transformations on content directly.
- Loading branch information
Showing
20 changed files
with
1,227 additions
and
556 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
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
75 changes: 75 additions & 0 deletions
75
server/src/main/java/com/linecorp/centraldogma/server/command/ContentTransformer.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,75 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE Corporation 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: | ||
* | ||
* https://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 com.linecorp.centraldogma.server.command; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.function.Function; | ||
|
||
import com.google.common.base.MoreObjects; | ||
|
||
import com.linecorp.centraldogma.common.EntryType; | ||
|
||
/** | ||
* A {@link Function} which is used for transforming the content at the specified path of the repository. | ||
*/ | ||
public final class ContentTransformer<T> { | ||
|
||
private final String path; | ||
private final EntryType entryType; | ||
private final Function<T, T> transformer; | ||
|
||
/** | ||
* Creates a new instance. | ||
*/ | ||
public ContentTransformer(String path, EntryType entryType, Function<T, T> transformer) { | ||
this.path = requireNonNull(path, "path"); | ||
checkArgument(entryType == EntryType.JSON, "entryType: %s (expected: %s)", entryType, EntryType.JSON); | ||
this.entryType = requireNonNull(entryType, "entryType"); | ||
this.transformer = requireNonNull(transformer, "transformer"); | ||
} | ||
|
||
/** | ||
* Returns the path of the content to be transformed. | ||
*/ | ||
public String path() { | ||
return path; | ||
} | ||
|
||
/** | ||
* Returns the {@link EntryType} of the content to be transformed. | ||
*/ | ||
public EntryType entryType() { | ||
return entryType; | ||
} | ||
|
||
/** | ||
* Returns the {@link Function} which transforms the content. | ||
*/ | ||
public Function<T, T> transformer() { | ||
return transformer; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("path", path) | ||
.add("entryType", entryType) | ||
.add("transformer", transformer) | ||
.toString(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
server/src/main/java/com/linecorp/centraldogma/server/command/NormalizableCommit.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,29 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE Corporation 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: | ||
* | ||
* https://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 com.linecorp.centraldogma.server.command; | ||
|
||
/** | ||
* A {@link Command} that can be transformed to a {@link PushAsIsCommand} via {@link #asIs(CommitResult)}. | ||
*/ | ||
@FunctionalInterface | ||
public interface NormalizableCommit { | ||
|
||
/** | ||
* Returns a new {@link PushAsIsCommand} which is converted using {@link CommitResult} | ||
* for replicating to other replicas. | ||
*/ | ||
PushAsIsCommand asIs(CommitResult commitResult); | ||
} |
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
112 changes: 112 additions & 0 deletions
112
server/src/main/java/com/linecorp/centraldogma/server/command/TransformCommand.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,112 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE Corporation 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: | ||
* | ||
* https://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 com.linecorp.centraldogma.server.command; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import com.linecorp.centraldogma.common.Author; | ||
import com.linecorp.centraldogma.common.Markup; | ||
import com.linecorp.centraldogma.common.Revision; | ||
|
||
/** | ||
* A {@link Command} that transforms the content at the base revision with | ||
* the specified {@link ContentTransformer} and pushed the result of transformation. | ||
* You can find the result of transformation from {@link CommitResult#changes()}. | ||
* Note that this command is not serialized and deserialized. | ||
*/ | ||
public final class TransformCommand extends RepositoryCommand<CommitResult> implements NormalizableCommit { | ||
|
||
/** | ||
* Creates a new instance. | ||
*/ | ||
public static TransformCommand of(@Nullable Long timestamp, | ||
@Nullable Author author, String projectName, | ||
String repositoryName, Revision baseRevision, | ||
String summary, String detail, Markup markup, | ||
ContentTransformer<?> transformer) { | ||
return new TransformCommand(timestamp, author, projectName, repositoryName, | ||
baseRevision, summary, detail, markup, transformer); | ||
} | ||
|
||
private final Revision baseRevision; | ||
private final String summary; | ||
private final String detail; | ||
private final Markup markup; | ||
private final ContentTransformer<?> transformer; | ||
|
||
private TransformCommand(@Nullable Long timestamp, @Nullable Author author, | ||
String projectName, String repositoryName, Revision baseRevision, | ||
String summary, String detail, Markup markup, | ||
ContentTransformer<?> transformer) { | ||
super(CommandType.TRANSFORM, timestamp, author, projectName, repositoryName); | ||
this.baseRevision = baseRevision; | ||
this.summary = summary; | ||
this.detail = detail; | ||
this.markup = markup; | ||
this.transformer = transformer; | ||
} | ||
|
||
/** | ||
* Returns the base {@link Revision}. | ||
*/ | ||
@JsonProperty | ||
public Revision baseRevision() { | ||
return baseRevision; | ||
} | ||
|
||
/** | ||
* Returns the human-readable summary of the commit. | ||
*/ | ||
@JsonProperty | ||
public String summary() { | ||
return summary; | ||
} | ||
|
||
/** | ||
* Returns the human-readable detail of the commit. | ||
*/ | ||
@JsonProperty | ||
public String detail() { | ||
return detail; | ||
} | ||
|
||
/** | ||
* Returns the {@link Markup} of the {@link #detail()}. | ||
*/ | ||
@JsonProperty | ||
public Markup markup() { | ||
return markup; | ||
} | ||
|
||
/** | ||
* Returns the {@link ContentTransformer} which is used for transforming the content. | ||
*/ | ||
public ContentTransformer<?> transformer() { | ||
return transformer; | ||
} | ||
|
||
@Override | ||
public PushAsIsCommand asIs(CommitResult commitResult) { | ||
requireNonNull(commitResult, "commitResult"); | ||
return new PushAsIsCommand(timestamp(), author(), projectName(), repositoryName(), | ||
commitResult.revision().backward(1), summary(), detail(), | ||
markup(), commitResult.changes()); | ||
} | ||
} |
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.