All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gerrit.server.account.externalids.ExternalIdsBatchUpdate Maven / Gradle / Ivy

There is a newer version: 3.10.0-rc5
Show newest version
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed 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 com.google.gerrit.server.account.externalids;

import com.google.common.collect.ImmutableSet;
import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.notes.NoteMap;
import org.eclipse.jgit.revwalk.RevWalk;

/**
 * This class allows to do batch updates to external IDs.
 *
 * 

For NoteDb all updates will result in a single commit to the refs/meta/external-ids branch. * This means callers can prepare many updates by invoking {@link #replace(ExternalId, ExternalId)} * multiple times and when {@link ExternalIdsBatchUpdate#commit(String)} is invoked a single NoteDb * commit is created that contains all the prepared updates. */ public class ExternalIdsBatchUpdate { private final GitRepositoryManager repoManager; private final AllUsersName allUsersName; private final PersonIdent serverIdent; private final ExternalIdCache externalIdCache; private final Set toAdd = new HashSet<>(); private final Set toDelete = new HashSet<>(); @Inject public ExternalIdsBatchUpdate( GitRepositoryManager repoManager, AllUsersName allUsersName, @GerritPersonIdent PersonIdent serverIdent, ExternalIdCache externalIdCache) { this.repoManager = repoManager; this.allUsersName = allUsersName; this.serverIdent = serverIdent; this.externalIdCache = externalIdCache; } /** * Adds an external ID replacement to the batch. * *

The actual replacement is only done when {@link #commit(String)} is invoked. */ public void replace(ExternalId extIdToDelete, ExternalId extIdToAdd) { ExternalIdsUpdate.checkSameAccount(ImmutableSet.of(extIdToDelete, extIdToAdd)); toAdd.add(extIdToAdd); toDelete.add(extIdToDelete); } /** * Commits this batch. * *

This means external ID replacements which were prepared by invoking {@link * #replace(ExternalId, ExternalId)} are now executed. Deletion of external IDs is done before * adding the new external IDs. This means if an external ID is specified for deletion and an * external ID with the same key is specified to be added, the old external ID with that key is * deleted first and then the new external ID is added (so the external ID for that key is * replaced). * *

For NoteDb a single commit is created that contains all the external ID updates. */ public void commit(String commitMessage) throws IOException, OrmException, ConfigInvalidException { if (toDelete.isEmpty() && toAdd.isEmpty()) { return; } try (Repository repo = repoManager.openRepository(allUsersName); RevWalk rw = new RevWalk(repo); ObjectInserter ins = repo.newObjectInserter()) { ObjectId rev = ExternalIdReader.readRevision(repo); NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev); for (ExternalId extId : toDelete) { ExternalIdsUpdate.remove(rw, noteMap, extId); } for (ExternalId extId : toAdd) { ExternalIdsUpdate.insert(rw, ins, noteMap, extId); } ObjectId newRev = ExternalIdsUpdate.commit( repo, rw, ins, rev, noteMap, commitMessage, serverIdent, serverIdent); externalIdCache.onReplace(rev, newRev, toDelete, toAdd); } toAdd.clear(); toDelete.clear(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy