summaryrefslogtreecommitdiffstats
path: root/java/com/google/gerrit/server/notedb/rebuild/EventList.java
blob: e83814df8198e7d452bb99bf91b63969136fe111 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// 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.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;

import com.google.common.collect.Lists;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.PatchSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Objects;

class EventList<E extends Event> implements Iterable<E> {
  private final ArrayList<E> list = new ArrayList<>();
  private boolean isSubmit;

  @Override
  public Iterator<E> iterator() {
    return list.iterator();
  }

  void add(E e) {
    list.add(e);
    if (e.isSubmit()) {
      isSubmit = true;
    }
  }

  void clear() {
    list.clear();
    isSubmit = false;
  }

  boolean isEmpty() {
    return list.isEmpty();
  }

  boolean canAdd(E e) {
    if (isEmpty()) {
      return true;
    }
    if (e instanceof FinalUpdatesEvent) {
      return false; // FinalUpdatesEvent always gets its own update.
    }

    Event last = getLast();
    if (!Objects.equals(e.user, last.user)
        || !Objects.equals(e.realUser, last.realUser)
        || !e.psId.equals(last.psId)) {
      return false; // Different patch set or author.
    }
    if (e.canHaveTag() && canHaveTag() && !Objects.equals(e.tag, getTag())) {
      // We should trust the tag field, and it doesn't match.
      return false;
    }
    if (e.isPostSubmitApproval() && isSubmit) {
      // Post-submit approvals must come after the update that submits.
      return false;
    }

    long t = e.when.getTime();
    long tFirst = getFirstTime();
    long tLast = getLastTime();
    checkArgument(t >= tLast, "event %s is before previous event in list %s", e, last);
    if (t - tLast > ChangeRebuilderImpl.MAX_DELTA_MS
        || t - tFirst > ChangeRebuilderImpl.MAX_WINDOW_MS) {
      return false; // Too much time elapsed.
    }

    if (!e.uniquePerUpdate()) {
      return true;
    }
    for (Event o : this) {
      if (e.getClass() == o.getClass()) {
        return false; // Only one event of this type allowed per update.
      }
    }

    // TODO(dborowitz): Additional heuristics, like keeping events separate if
    // they affect overlapping fields within a single entity.

    return true;
  }

  Timestamp getWhen() {
    return get(0).when;
  }

  PatchSet.Id getPatchSetId() {
    PatchSet.Id id = requireNonNull(get(0).psId);
    for (int i = 1; i < size(); i++) {
      checkState(
          get(i).psId.equals(id), "mismatched patch sets in EventList: %s != %s", id, get(i).psId);
    }
    return id;
  }

  Account.Id getAccountId() {
    Account.Id id = get(0).user;
    for (int i = 1; i < size(); i++) {
      checkState(
          Objects.equals(id, get(i).user),
          "mismatched users in EventList: %s != %s",
          id,
          get(i).user);
    }
    return id;
  }

  Account.Id getRealAccountId() {
    Account.Id id = get(0).realUser;
    for (int i = 1; i < size(); i++) {
      checkState(
          Objects.equals(id, get(i).realUser),
          "mismatched real users in EventList: %s != %s",
          id,
          get(i).realUser);
    }
    return id;
  }

  String getTag() {
    for (E e : Lists.reverse(list)) {
      if (e.tag != null) {
        return e.tag;
      }
    }
    return null;
  }

  private boolean canHaveTag() {
    return list.stream().anyMatch(Event::canHaveTag);
  }

  private E get(int i) {
    return list.get(i);
  }

  private int size() {
    return list.size();
  }

  private E getLast() {
    return list.get(list.size() - 1);
  }

  private long getLastTime() {
    return getLast().when.getTime();
  }

  private long getFirstTime() {
    return list.get(0).when.getTime();
  }
}