summaryrefslogtreecommitdiffstats
path: root/java/com/google/gerrit/server/notedb/rebuild/Event.java
blob: 3957c5c748dfefa3ec1bfba6fcde7b6dc0b9b21b (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
// 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.notedb.rebuild;

import static com.google.common.base.Preconditions.checkState;
import static com.google.gerrit.server.notedb.rebuild.ChangeRebuilderImpl.MAX_WINDOW_MS;

import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.collect.ComparisonChain;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.server.ReviewDbUtil;
import com.google.gerrit.server.notedb.AbstractChangeUpdate;
import com.google.gerrit.server.notedb.ChangeUpdate;
import com.google.gwtorm.server.OrmException;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

abstract class Event implements Comparable<Event> {
  // NOTE: EventList only supports direct subclasses, not an arbitrary
  // hierarchy.

  final Account.Id user;
  final Account.Id realUser;
  final String tag;
  final boolean predatesChange;

  /** Dependencies of this event; other events that must happen before this one. */
  final List<Event> deps;

  Timestamp when;
  PatchSet.Id psId;

  protected Event(
      PatchSet.Id psId,
      Account.Id effectiveUser,
      Account.Id realUser,
      Timestamp when,
      Timestamp changeCreatedOn,
      String tag) {
    this.psId = psId;
    this.user = effectiveUser;
    this.realUser = realUser != null ? realUser : effectiveUser;
    this.tag = tag;
    // Truncate timestamps at the change's createdOn timestamp.
    predatesChange = when.before(changeCreatedOn);
    this.when = predatesChange ? changeCreatedOn : when;
    deps = new ArrayList<>();
  }

  protected void checkUpdate(AbstractChangeUpdate update) {
    checkState(
        Objects.equals(update.getPatchSetId(), psId),
        "cannot apply event for %s to update for %s",
        update.getPatchSetId(),
        psId);
    checkState(
        when.getTime() - update.getWhen().getTime() <= MAX_WINDOW_MS,
        "event at %s outside update window starting at %s",
        when,
        update.getWhen());
    checkState(
        Objects.equals(update.getNullableAccountId(), user),
        "cannot apply event by %s to update by %s",
        user,
        update.getNullableAccountId());
  }

  Event addDep(Event e) {
    deps.add(e);
    return this;
  }

  /**
   * @return whether this event type must be unique per {@link ChangeUpdate}, i.e. there may be at
   *     most one of this type.
   */
  abstract boolean uniquePerUpdate();

  abstract void apply(ChangeUpdate update) throws OrmException, IOException;

  protected boolean isPostSubmitApproval() {
    return false;
  }

  protected boolean isSubmit() {
    return false;
  }

  protected boolean canHaveTag() {
    return false;
  }

  @Override
  public String toString() {
    ToStringHelper helper =
        MoreObjects.toStringHelper(this)
            .add("psId", psId)
            .add("effectiveUser", user)
            .add("realUser", realUser)
            .add("when", when)
            .add("tag", tag);
    addToString(helper);
    return helper.toString();
  }

  /** @param helper toString helper to add fields to */
  protected void addToString(ToStringHelper helper) {}

  @Override
  public int compareTo(Event other) {
    return ComparisonChain.start()
        .compareFalseFirst(this.isFinalUpdates(), other.isFinalUpdates())
        .compare(this.when, other.when)
        .compareTrueFirst(isPatchSet(), isPatchSet())
        .compareTrueFirst(this.predatesChange, other.predatesChange)
        .compare(this.user, other.user, ReviewDbUtil.intKeyOrdering())
        .compare(this.realUser, other.realUser, ReviewDbUtil.intKeyOrdering())
        .compare(this.psId, other.psId, ReviewDbUtil.intKeyOrdering().nullsLast())
        .result();
  }

  private boolean isPatchSet() {
    return this instanceof PatchSetEvent;
  }

  private boolean isFinalUpdates() {
    return this instanceof FinalUpdatesEvent;
  }
}