Skip to content

Commit

Permalink
Address the comments from @ikhoon
Browse files Browse the repository at this point in the history
  • Loading branch information
minwoox committed Dec 13, 2024
1 parent 1fe2f94 commit fe31752
Show file tree
Hide file tree
Showing 20 changed files with 530 additions and 460 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,27 @@
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;

import java.util.function.BiFunction;
import java.util.function.Function;

import com.google.common.base.MoreObjects;

import com.linecorp.centraldogma.common.EntryType;
import com.linecorp.centraldogma.common.Revision;

/**
* A {@link Function} which is used for transforming the content at the specified path of the repository.
*/
public final class ContentTransformer<T> {
public class ContentTransformer<T> {

private final String path;
private final EntryType entryType;
private final Function<T, T> transformer;
private final BiFunction<Revision, T, T> transformer;

/**
* Creates a new instance.
*/
public ContentTransformer(String path, EntryType entryType, Function<T, T> transformer) {
public ContentTransformer(String path, EntryType entryType, BiFunction<Revision, T, T> transformer) {
this.path = requireNonNull(path, "path");
checkArgument(entryType == EntryType.JSON, "entryType: %s (expected: %s)", entryType, EntryType.JSON);
this.entryType = requireNonNull(entryType, "entryType");
Expand All @@ -60,7 +62,7 @@ public EntryType entryType() {
/**
* Returns the {@link Function} which transforms the content.
*/
public Function<T, T> transformer() {
public BiFunction<Revision, T, T> transformer() {
return transformer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@
import com.linecorp.centraldogma.common.RepositoryNotFoundException;
import com.linecorp.centraldogma.common.RevisionNotFoundException;
import com.linecorp.centraldogma.common.TooManyRequestsException;
import com.linecorp.centraldogma.server.internal.admin.service.TokenNotFoundException;
import com.linecorp.centraldogma.server.internal.storage.RequestAlreadyTimedOutException;
import com.linecorp.centraldogma.server.internal.storage.repository.RepositoryMetadataException;
import com.linecorp.centraldogma.server.metadata.MemberNotFoundException;
import com.linecorp.centraldogma.server.metadata.TokenNotFoundException;

/**
* A default {@link ExceptionHandlerFunction} of HTTP API.
Expand Down Expand Up @@ -96,8 +97,9 @@ public final class HttpApiExceptionHandler implements ServerErrorHandler {
(ctx, cause) -> newResponse(ctx, HttpStatus.NOT_FOUND, cause,
"Revision %s does not exist.", cause.getMessage()))
.put(TokenNotFoundException.class,
(ctx, cause) -> newResponse(ctx, HttpStatus.NOT_FOUND, cause,
"Token '%s' does not exist.", cause.getMessage()))
(ctx, cause) -> newResponse(ctx, HttpStatus.NOT_FOUND, cause, cause.getMessage()))
.put(MemberNotFoundException.class,
(ctx, cause) -> newResponse(ctx, HttpStatus.NOT_FOUND, cause, cause.getMessage()))
.put(QueryExecutionException.class,
(ctx, cause) -> newResponse(ctx, HttpStatus.BAD_REQUEST, cause))
.put(UnsupportedOperationException.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
import com.linecorp.armeria.server.auth.AuthTokenExtractors;
import com.linecorp.armeria.server.auth.Authorizer;
import com.linecorp.centraldogma.server.internal.admin.auth.AuthUtil;
import com.linecorp.centraldogma.server.internal.admin.service.TokenNotFoundException;
import com.linecorp.centraldogma.server.internal.api.HttpApiUtil;
import com.linecorp.centraldogma.server.metadata.Token;
import com.linecorp.centraldogma.server.metadata.TokenNotFoundException;
import com.linecorp.centraldogma.server.metadata.Tokens;
import com.linecorp.centraldogma.server.metadata.UserWithToken;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@
import com.linecorp.centraldogma.common.CentralDogmaException;
import com.linecorp.centraldogma.common.Revision;
import com.linecorp.centraldogma.internal.Jackson;
import com.linecorp.centraldogma.server.internal.admin.service.TokenNotFoundException;
import com.linecorp.centraldogma.server.storage.StorageException;

abstract class AbstractChangesApplier {

private static final byte[] EMPTY_BYTE = new byte[0];

int apply(Repository jGitRepository, @Nullable Revision baseRevision,
int apply(Repository jGitRepository, Revision headRevision,
@Nullable ObjectId baseTreeId, DirCache dirCache) {
try (ObjectInserter inserter = jGitRepository.newObjectInserter();
ObjectReader reader = jGitRepository.newObjectReader()) {
Expand All @@ -59,16 +58,16 @@ int apply(Repository jGitRepository, @Nullable Revision baseRevision,
builder.finish();
}

return doApply(dirCache, reader, inserter);
} catch (CentralDogmaException | TokenNotFoundException | IllegalArgumentException e) {
return doApply(headRevision, dirCache, reader, inserter);
} catch (CentralDogmaException e) {
throw e;
} catch (Exception e) {
throw new StorageException("failed to apply changes on revision: " +
(baseRevision != null ? baseRevision.major() : 0), e);
throw new StorageException("failed to apply changes on revision: " + headRevision.major(), e);
}
}

abstract int doApply(DirCache dirCache, ObjectReader reader, ObjectInserter inserter) throws IOException;
abstract int doApply(Revision headRevision, DirCache dirCache,
ObjectReader reader, ObjectInserter inserter) throws IOException;

static void applyPathEdit(DirCache dirCache, PathEdit edit) {
final DirCacheEditor e = dirCache.editor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.eclipse.jgit.treewalk.CanonicalTreeParser;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;

import com.linecorp.centraldogma.common.Author;
import com.linecorp.centraldogma.common.CentralDogmaException;
Expand Down Expand Up @@ -81,8 +82,8 @@ String summary() {
return summary;
}

void executeInitialCommit(Iterable<Change<?>> changes) {
commit(null, Revision.INIT, changes);
void executeInitialCommit() {
commit(null, Revision.INIT, ImmutableList.of());
}

CommitResult execute(Revision baseRevision,
Expand Down Expand Up @@ -112,58 +113,57 @@ CommitResult execute(Revision baseRevision,
return CommitResult.of(res.revision, applyingChanges);
}

RevisionAndEntries commit(@Nullable Revision prevRevision, Revision nextRevision,
RevisionAndEntries commit(@Nullable Revision headRevision, Revision nextRevision,
Iterable<Change<?>> changes) {
requireNonNull(nextRevision, "nextRevision");
requireNonNull(changes, "changes");

assert prevRevision == null || prevRevision.major() > 0;
assert (headRevision == null && Iterables.isEmpty(changes)) ||
(headRevision != null && headRevision.major() > 0);
assert nextRevision.major() > 0;

final Repository jGitRepository = gitRepository.jGitRepository();
try (ObjectInserter inserter = jGitRepository.newObjectInserter();
ObjectReader reader = jGitRepository.newObjectReader();
RevWalk revWalk = newRevWalk(reader)) {

final CommitIdDatabase commitIdDatabase = gitRepository.commitIdDatabase();

final ObjectId prevTreeId =
prevRevision != null ? toTree(commitIdDatabase, revWalk, prevRevision) : null;

// The staging area that keeps the entries of the new tree.
// It starts with the entries of the tree at the prevRevision (or with no entries if the
// prevRevision is the initial commit), and then this method will apply the requested changes
// It starts with the entries of the tree at the headRevision (or with no entries if the
// headRevision is the initial commit), and then this method will apply the requested changes
// to build the new tree.
final DirCache dirCache = DirCache.newInCore();

// Apply the changes and retrieve the list of the affected files.
final int numEdits = new DefaultChangesApplier(changes)
.apply(jGitRepository, prevRevision, prevTreeId, dirCache);

// Reject empty commit if necessary.
final List<DiffEntry> diffEntries;
boolean isEmpty = numEdits == 0;
if (!isEmpty) {
// Even if there are edits, the resulting tree might be identical with the previous tree.
final CanonicalTreeParser p = new CanonicalTreeParser();
p.reset(reader, prevTreeId);
final DiffFormatter diffFormatter = new DiffFormatter(null);
diffFormatter.setRepository(jGitRepository);
diffEntries = diffFormatter.scan(p, new DirCacheIterator(dirCache));
isEmpty = diffEntries.isEmpty();

if (headRevision != null) {
final ObjectId prevTreeId = toTree(commitIdDatabase, revWalk, headRevision);
// Apply the changes and retrieve the list of the affected files.
final int numEdits = new DefaultChangesApplier(changes)
.apply(jGitRepository, headRevision, prevTreeId, dirCache);
// Reject empty commit if necessary.
boolean isEmpty = numEdits == 0;
if (!isEmpty) {
// Even if there are edits, the resulting tree might be identical with the previous tree.
final CanonicalTreeParser p = new CanonicalTreeParser();
p.reset(reader, prevTreeId);
final DiffFormatter diffFormatter = new DiffFormatter(null);
diffFormatter.setRepository(jGitRepository);
diffEntries = diffFormatter.scan(p, new DirCacheIterator(dirCache));
isEmpty = diffEntries.isEmpty();
} else {
diffEntries = ImmutableList.of();
}
if (!allowEmptyCommit && isEmpty) {
throw new RedundantChangeException(
headRevision,
"changes did not change anything in " + gitRepository.parent().name() + '/' +
gitRepository.name() + " at revision " + headRevision.major() + ": " + changes);
}
} else {
// initial commit.
diffEntries = ImmutableList.of();
}

if (!allowEmptyCommit && isEmpty) {
// prevRevision is not null when allowEmptyCommit is false.
assert prevRevision != null;
throw new RedundantChangeException(
prevRevision,
"changes did not change anything in " + gitRepository.parent().name() + '/' +
gitRepository.name() + " at revision " + prevRevision.major() + ": " + changes);
}

// flush the current index to repository and get the result tree object id.
final ObjectId nextTreeId = dirCache.writeTree(inserter);

Expand All @@ -182,8 +182,8 @@ RevisionAndEntries commit(@Nullable Revision prevRevision, Revision nextRevision
commitBuilder.setMessage(CommitUtil.toJsonString(summary, detail, markup, nextRevision));

// if the head commit exists, use it as the parent commit.
if (prevRevision != null) {
commitBuilder.setParentId(commitIdDatabase.get(prevRevision));
if (headRevision != null) {
commitBuilder.setParentId(commitIdDatabase.get(headRevision));
}

final ObjectId nextCommitId = inserter.insert(commitBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import com.linecorp.centraldogma.common.Change;
import com.linecorp.centraldogma.common.ChangeConflictException;
import com.linecorp.centraldogma.common.Revision;
import com.linecorp.centraldogma.internal.Jackson;
import com.linecorp.centraldogma.internal.Util;
import com.linecorp.centraldogma.internal.jsonpatch.JsonPatch;
Expand All @@ -58,7 +59,8 @@ final class DefaultChangesApplier extends AbstractChangesApplier {
}

@Override
int doApply(DirCache dirCache, ObjectReader reader, ObjectInserter inserter) throws IOException {
int doApply(Revision unused, DirCache dirCache,
ObjectReader reader, ObjectInserter inserter) throws IOException {
int numEdits = 0;
// loop over the specified changes.
for (Change<?> change : changes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class GitRepository implements Repository {

new CommitExecutor(this, creationTimeMillis, author, "Create a new repository", "",
Markup.PLAINTEXT, true)
.executeInitialCommit(Collections.emptyList());
.executeInitialCommit();

headRevision = Revision.INIT;
success = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
import com.linecorp.centraldogma.common.CentralDogmaException;
import com.linecorp.centraldogma.common.ChangeConflictException;
import com.linecorp.centraldogma.common.EntryType;
import com.linecorp.centraldogma.common.Revision;
import com.linecorp.centraldogma.internal.Jackson;
import com.linecorp.centraldogma.server.command.ContentTransformer;
import com.linecorp.centraldogma.server.internal.admin.service.TokenNotFoundException;

final class TransformingChangesApplier extends AbstractChangesApplier {

Expand All @@ -49,21 +49,22 @@ final class TransformingChangesApplier extends AbstractChangesApplier {
}

@Override
int doApply(DirCache dirCache, ObjectReader reader, ObjectInserter inserter) throws IOException {
int doApply(Revision headRevision, DirCache dirCache,
ObjectReader reader, ObjectInserter inserter) throws IOException {
final String changePath = transformer.path().substring(1); // Strip the leading '/'.
final DirCacheEntry oldEntry = dirCache.getEntry(changePath);
final byte[] oldContent = oldEntry != null ? reader.open(oldEntry.getObjectId()).getBytes()
: null;
final JsonNode oldJsonNode = oldContent != null ? Jackson.readTree(oldContent)
: JsonNodeFactory.instance.nullNode();
try {
final JsonNode newJsonNode = transformer.transformer().apply(oldJsonNode.deepCopy());
final JsonNode newJsonNode = transformer.transformer().apply(headRevision, oldJsonNode.deepCopy());
requireNonNull(newJsonNode, "transformer.transformer().apply() returned null");
if (!Objects.equals(newJsonNode, oldJsonNode)) {
applyPathEdit(dirCache, new InsertJson(changePath, inserter, newJsonNode));
return 1;
}
} catch (CentralDogmaException | TokenNotFoundException | IllegalArgumentException e) {
} catch (CentralDogmaException e) {
throw e;
} catch (Exception e) {
throw new ChangeConflictException("failed to transform the content: " + oldJsonNode +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2019 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.metadata;

import com.linecorp.centraldogma.common.CentralDogmaException;

/**
* A {@link CentralDogmaException} that is raised when failed to find a {@link Member}.
*/
public final class MemberNotFoundException extends CentralDogmaException {

private static final long serialVersionUID = 914551040812058495L;

MemberNotFoundException(String memberId, String projectName) {
super("failed to find member " + memberId + " in '" + projectName + '\'');
}

MemberNotFoundException(String memberId, String projectName, String repoName) {
super("failed to find member " + memberId + " in '" + projectName + '/' + repoName + '\'');
}
}
Loading

0 comments on commit fe31752

Please sign in to comment.