Line data Source code
1 : // Copyright (C) 2018 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.schema; 16 : 17 : import static com.google.gerrit.entities.RefNames.REFS_VERSION; 18 : 19 : import com.google.common.annotations.VisibleForTesting; 20 : import com.google.gerrit.exceptions.StorageException; 21 : import com.google.gerrit.server.config.AllProjectsName; 22 : import com.google.gerrit.server.extensions.events.GitReferenceUpdated; 23 : import com.google.gerrit.server.git.GitRepositoryManager; 24 : import com.google.gerrit.server.notedb.IntBlob; 25 : import com.google.inject.Inject; 26 : import java.io.IOException; 27 : import java.util.Optional; 28 : import org.eclipse.jgit.lib.ObjectId; 29 : import org.eclipse.jgit.lib.Repository; 30 : import org.eclipse.jgit.revwalk.RevWalk; 31 : 32 : public class NoteDbSchemaVersionManager { 33 : private final AllProjectsName allProjectsName; 34 : private final GitRepositoryManager repoManager; 35 : 36 : @Inject 37 : @VisibleForTesting 38 : public NoteDbSchemaVersionManager( 39 151 : AllProjectsName allProjectsName, GitRepositoryManager repoManager) { 40 : // Can't inject GitReferenceUpdated here because it has dependencies that are not always 41 : // available in this injector (e.g. during init). This is ok for now since no other ref updates 42 : // during init are available to plugins, and there are not any other use cases for listening for 43 : // updates to the version ref. 44 151 : this.allProjectsName = allProjectsName; 45 151 : this.repoManager = repoManager; 46 151 : } 47 : 48 : public int read() { 49 139 : try (Repository repo = repoManager.openRepository(allProjectsName)) { 50 139 : return IntBlob.parse(repo, REFS_VERSION).map(IntBlob::value).orElse(0); 51 0 : } catch (IOException e) { 52 0 : throw new StorageException("Failed to read " + REFS_VERSION, e); 53 : } 54 : } 55 : 56 : public void init() throws IOException { 57 151 : try (Repository repo = repoManager.openRepository(allProjectsName); 58 151 : RevWalk rw = new RevWalk(repo)) { 59 151 : Optional<IntBlob> old = IntBlob.parse(repo, REFS_VERSION, rw); 60 151 : if (old.isPresent()) { 61 0 : throw new StorageException( 62 0 : String.format( 63 0 : "Expected no old version for %s, found %s", REFS_VERSION, old.get().value())); 64 : } 65 151 : IntBlob.store( 66 : repo, 67 : rw, 68 : allProjectsName, 69 : REFS_VERSION, 70 151 : old.map(IntBlob::id).orElse(ObjectId.zeroId()), 71 : NoteDbSchemaVersions.LATEST, 72 : GitReferenceUpdated.DISABLED); 73 : } 74 151 : } 75 : 76 : public void increment(int expectedOldVersion) throws IOException { 77 1 : try (Repository repo = repoManager.openRepository(allProjectsName); 78 1 : RevWalk rw = new RevWalk(repo)) { 79 1 : Optional<IntBlob> old = IntBlob.parse(repo, REFS_VERSION, rw); 80 1 : if (old.isPresent() && old.get().value() != expectedOldVersion) { 81 1 : throw new StorageException( 82 1 : String.format( 83 : "Expected old version %d for %s, found %d", 84 1 : expectedOldVersion, REFS_VERSION, old.get().value())); 85 : } 86 1 : IntBlob.store( 87 : repo, 88 : rw, 89 : allProjectsName, 90 : REFS_VERSION, 91 1 : old.map(IntBlob::id).orElse(ObjectId.zeroId()), 92 : expectedOldVersion + 1, 93 : GitReferenceUpdated.DISABLED); 94 : } 95 1 : } 96 : }