Skip to content

Commit

Permalink
Add NormalizableCommit
Browse files Browse the repository at this point in the history
  • Loading branch information
minwoox committed Dec 2, 2024
1 parent c2b246e commit bb04f7c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
* You can find the normalized changes from the {@link CommitResult#changes()} that is the result of
* {@link CommandExecutor#execute(Command)}.
*/
public final class NormalizingPushCommand extends AbstractPushCommand<CommitResult> {
public final class NormalizingPushCommand extends AbstractPushCommand<CommitResult>
implements NormalizableCommit {

@JsonCreator
NormalizingPushCommand(@JsonProperty("timestamp") @Nullable Long timestamp,
Expand All @@ -50,11 +51,7 @@ public final class NormalizingPushCommand extends AbstractPushCommand<CommitResu
baseRevision, summary, detail, markup, changes);
}

/**
* Returns a new {@link PushAsIsCommand} which is converted from this {@link NormalizingPushCommand}
* for replicating to other replicas. Unlike the {@link NormalizingPushCommand},
* the changes of this {@link Command} are not normalized and applied as they are.
*/
@Override
public PushAsIsCommand asIs(CommitResult commitResult) {
requireNonNull(commitResult, "commitResult");
return new PushAsIsCommand(timestamp(), author(), projectName(), repositoryName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* 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> {
public final class TransformCommand extends RepositoryCommand<CommitResult> implements NormalizableCommit {

/**
* Creates a new instance.
Expand Down Expand Up @@ -102,11 +102,7 @@ public ContentTransformer<?> transformer() {
return transformer;
}

/**
* Returns a new {@link PushAsIsCommand} which is converted from this {@link TransformCommand}
* for replicating to other replicas. Unlike the {@link TransformCommand},
* the changes of this {@link Command} are not normalized and applied as they are.
*/
@Override
public PushAsIsCommand asIs(CommitResult commitResult) {
requireNonNull(commitResult, "commitResult");
return new PushAsIsCommand(timestamp(), author(), projectName(), repositoryName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@
import com.linecorp.centraldogma.server.command.CommandType;
import com.linecorp.centraldogma.server.command.CommitResult;
import com.linecorp.centraldogma.server.command.ForcePushCommand;
import com.linecorp.centraldogma.server.command.NormalizableCommit;
import com.linecorp.centraldogma.server.command.NormalizingPushCommand;
import com.linecorp.centraldogma.server.command.RemoveRepositoryCommand;
import com.linecorp.centraldogma.server.command.TransformCommand;
import com.linecorp.centraldogma.server.command.UpdateServerStatusCommand;
import com.linecorp.centraldogma.server.metadata.MetadataService;
import com.linecorp.centraldogma.server.metadata.RepositoryMetadata;
Expand Down Expand Up @@ -1319,20 +1319,13 @@ private <T> T blockingExecute(Command<T> command) throws Exception {
final T result = delegate.execute(command).get();
final ReplicationLog<?> log;
final Command<?> maybeUnwrapped = unwrapForcePush(command);
if (maybeUnwrapped.type() == CommandType.NORMALIZING_PUSH) {
final NormalizingPushCommand normalizingPushCommand = (NormalizingPushCommand) maybeUnwrapped;
if (maybeUnwrapped instanceof NormalizableCommit) {
final NormalizableCommit normalizingPushCommand = (NormalizableCommit) maybeUnwrapped;
assert result instanceof CommitResult : result;
final CommitResult commitResult = (CommitResult) result;
final Command<Revision> pushAsIsCommand = normalizingPushCommand.asIs(commitResult);
log = new ReplicationLog<>(replicaId(),
maybeWrap(command, pushAsIsCommand), commitResult.revision());
} else if (maybeUnwrapped.type() == CommandType.TRANSFORM) {
final TransformCommand transformCommand = (TransformCommand) maybeUnwrapped;
assert result instanceof CommitResult : result;
final CommitResult commitResult = (CommitResult) result;
final Command<Revision> pushAsIsCommand = transformCommand.asIs(commitResult);
log = new ReplicationLog<>(replicaId(),
maybeWrap(command, pushAsIsCommand), commitResult.revision());
} else {
log = new ReplicationLog<>(replicaId(), command, result);
}
Expand Down

0 comments on commit bb04f7c

Please sign in to comment.