summaryrefslogtreecommitdiffstats
path: root/gerrit-pgm/src/main/java/com/google/gerrit/pgm/ProtobufImport.java
blob: 0732b28217d510a0662ce0c606a61e0b4033bcbb (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (C) 2015 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.pgm;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.gerrit.server.schema.DataSourceProvider.Context.SINGLE_USER;

import com.google.auto.value.AutoValue;
import com.google.common.collect.Iterables;
import com.google.gerrit.lifecycle.LifecycleManager;
import com.google.gerrit.pgm.util.RuntimeShutdown;
import com.google.gerrit.pgm.util.SiteProgram;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gwtorm.protobuf.CodecFactory;
import com.google.gwtorm.protobuf.ProtobufCodec;
import com.google.gwtorm.schema.RelationModel;
import com.google.gwtorm.schema.java.JavaSchemaModel;
import com.google.gwtorm.server.Access;
import com.google.gwtorm.server.OrmDuplicateKeyException;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.protobuf.ByteString;
import com.google.protobuf.Parser;
import com.google.protobuf.UnknownFieldSet;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.lib.TextProgressMonitor;
import org.kohsuke.args4j.Option;

/**
 * Import data from a protocol buffer dump into the database.
 *
 * <p>Takes as input a file containing protocol buffers concatenated together with varint length
 * encoding, as in {@link Parser#parseDelimitedFrom(InputStream)}. Each message contains a single
 * field with a tag corresponding to the relation ID in the {@link
 * com.google.gwtorm.server.Relation} annotation.
 *
 * <p><strong>Warning</strong>: This method blindly upserts data into the database. It should only
 * be used to restore a protobuf-formatted backup into a new, empty site.
 */
public class ProtobufImport extends SiteProgram {
  @Option(
      name = "--file",
      aliases = {"-f"},
      required = true,
      metaVar = "FILE",
      usage = "File to import from")
  private File file;

  private final LifecycleManager manager = new LifecycleManager();
  private final Map<Integer, Relation> relations = new HashMap<>();

  @Inject private SchemaFactory<ReviewDb> schemaFactory;

  @Override
  public int run() throws Exception {
    mustHaveValidSite();

    Injector dbInjector = createDbInjector(SINGLE_USER);
    manager.add(dbInjector);
    manager.start();
    RuntimeShutdown.add(manager::stop);
    dbInjector.injectMembers(this);

    ProgressMonitor progress = new TextProgressMonitor();
    progress.beginTask("Importing entities", ProgressMonitor.UNKNOWN);
    try (ReviewDb db = schemaFactory.open()) {
      for (RelationModel model : new JavaSchemaModel(ReviewDb.class).getRelations()) {
        relations.put(model.getRelationID(), Relation.create(model, db));
      }

      Parser<UnknownFieldSet> parser = UnknownFieldSet.getDefaultInstance().getParserForType();
      try (InputStream in = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
        UnknownFieldSet msg;
        while ((msg = parser.parseDelimitedFrom(in)) != null) {
          Map.Entry<Integer, UnknownFieldSet.Field> e =
              Iterables.getOnlyElement(msg.asMap().entrySet());
          Relation rel =
              checkNotNull(
                  relations.get(e.getKey()),
                  "unknown relation ID %s in message: %s",
                  e.getKey(),
                  msg);
          List<ByteString> values = e.getValue().getLengthDelimitedList();
          checkState(values.size() == 1, "expected one string field in message: %s", msg);
          upsert(rel, values.get(0));
          progress.update(1);
        }
      }
      progress.endTask();
    }

    return 0;
  }

  @SuppressWarnings({"rawtypes", "unchecked"})
  private static void upsert(Relation rel, ByteString s) throws OrmException {
    Collection ents = Collections.singleton(rel.codec().decode(s));
    try {
      // Not all relations support update; fall back manually.
      rel.access().insert(ents);
    } catch (OrmDuplicateKeyException e) {
      rel.access().delete(ents);
      rel.access().insert(ents);
    }
  }

  @AutoValue
  abstract static class Relation {
    private static Relation create(RelationModel model, ReviewDb db)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException,
            ClassNotFoundException {
      Method m = db.getClass().getMethod(model.getMethodName());
      Class<?> clazz = Class.forName(model.getEntityTypeClassName());
      return new AutoValue_ProtobufImport_Relation(
          (Access<?, ?>) m.invoke(db), CodecFactory.encoder(clazz));
    }

    abstract Access<?, ?> access();

    abstract ProtobufCodec<?> codec();
  }
}