summaryrefslogtreecommitdiffstats
path: root/java/com/google/gerrit/server/notedb/NotesMigrationState.java
blob: c682aed8c2b5c46db645f33ff44fe1558f8ba5d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (C) 2017 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.notedb;

import com.google.gerrit.server.notedb.NoteDbChangeState.PrimaryStorage;
import com.google.gerrit.server.notedb.NotesMigration.Snapshot;
import java.util.Optional;
import java.util.stream.Stream;
import org.eclipse.jgit.lib.Config;

/**
 * Possible high-level states of the NoteDb migration for changes.
 *
 * <p>This class describes the series of states required to migrate a site from ReviewDb-only to
 * NoteDb-only. This process has several steps, and covers only a small subset of the theoretically
 * possible combinations of {@link NotesMigration} return values.
 *
 * <p>These states are ordered: a one-way migration from ReviewDb to NoteDb will pass through states
 * in the order in which they are defined.
 */
public enum NotesMigrationState {
  REVIEW_DB(false, false, false, PrimaryStorage.REVIEW_DB, false),

  WRITE(false, true, false, PrimaryStorage.REVIEW_DB, false),

  READ_WRITE_NO_SEQUENCE(true, true, false, PrimaryStorage.REVIEW_DB, false),

  READ_WRITE_WITH_SEQUENCE_REVIEW_DB_PRIMARY(true, true, true, PrimaryStorage.REVIEW_DB, false),

  READ_WRITE_WITH_SEQUENCE_NOTE_DB_PRIMARY(true, true, true, PrimaryStorage.NOTE_DB, false),

  NOTE_DB(true, true, true, PrimaryStorage.NOTE_DB, true);

  public static final NotesMigrationState FINAL = NOTE_DB;

  public static Optional<NotesMigrationState> forConfig(Config cfg) {
    return forSnapshot(Snapshot.create(cfg));
  }

  public static Optional<NotesMigrationState> forNotesMigration(NotesMigration migration) {
    return forSnapshot(migration.snapshot());
  }

  private static Optional<NotesMigrationState> forSnapshot(Snapshot s) {
    return Stream.of(values()).filter(v -> v.snapshot.equals(s)).findFirst();
  }

  private final Snapshot snapshot;

  NotesMigrationState(
      // Arguments match abstract methods in NotesMigration.
      boolean readChanges,
      boolean rawWriteChangesSetting,
      boolean readChangeSequence,
      PrimaryStorage changePrimaryStorage,
      boolean disableChangeReviewDb) {
    this.snapshot =
        Snapshot.builder()
            .setReadChanges(readChanges)
            .setWriteChanges(rawWriteChangesSetting)
            .setReadChangeSequence(readChangeSequence)
            .setChangePrimaryStorage(changePrimaryStorage)
            .setDisableChangeReviewDb(disableChangeReviewDb)
            .build();
  }

  public void setConfigValues(Config cfg) {
    snapshot.setConfigValues(cfg);
  }

  public String toText() {
    Config cfg = new Config();
    setConfigValues(cfg);
    return cfg.toText();
  }

  Snapshot snapshot() {
    return snapshot;
  }
}