summaryrefslogtreecommitdiff
path: root/platform/vcs-log/graph/src/com/intellij/vcs/log/graph/impl/facade/bek/BekSorter.java
blob: 4650cd1a11463898593c1170536342abd196d432 (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
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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.intellij.vcs.log.graph.impl.facade.bek;

import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.vcs.log.graph.api.LinearGraph;
import com.intellij.vcs.log.graph.impl.permanent.GraphLayoutImpl;
import com.intellij.vcs.log.graph.utils.IntList;
import com.intellij.vcs.log.graph.utils.TimestampGetter;
import com.intellij.vcs.log.graph.utils.impl.CompressedIntList;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class BekSorter {

  public static boolean isBekEnabled() { // todo drop later
    boolean isInternal = Boolean.valueOf(System.getProperty("idea.is.internal"));
    boolean isBekEnabled = Registry.is("vcs.log.bek.sort");
    return isBekEnabled || isInternal;
  }

  @NotNull
  public static BekIntMap createBekMap(@NotNull LinearGraph permanentGraph,
                                       @NotNull GraphLayoutImpl graphLayout,
                                       @NotNull TimestampGetter timestampGetter) {
    BekSorter bekSorter = new BekSorter(permanentGraph, graphLayout, timestampGetter);

    List<Integer> result = bekSorter.getResult();
    assert result.size() == permanentGraph.nodesCount();
    return createBekIntMap(result);
  }

  private static BekIntMap createBekIntMap(final List<Integer> result) {

    final int[] reverseMap = new int[result.size()];
    for (int i = 0; i < result.size(); i++) {
      reverseMap[result.get(i)] = i;
    }

    final IntList compressedBekMap = CompressedIntList.newInstance(new IntList() {
      @Override
      public int size() {
        return result.size();
      }

      @Override
      public int get(int index) {
        return result.get(index);
      }
    }, CompressedIntList.DEFAULT_BLOCK_SIZE);

    final IntList compressedReverseMap = CompressedIntList.newInstance(reverseMap);
    return new BekIntMap() {
      @Override
      public int size() {
        return compressedBekMap.size();
      }

      @Override
      public int getBekIndex(int usualIndex) {
        if (usualIndex == LinearGraph.NOT_LOAD_COMMIT)
          return LinearGraph.NOT_LOAD_COMMIT;
        return compressedReverseMap.get(usualIndex);
      }

      @Override
      public int getUsualIndex(int bekIndex) {
        if (bekIndex == LinearGraph.NOT_LOAD_COMMIT)
          return LinearGraph.NOT_LOAD_COMMIT;
        return compressedBekMap.get(bekIndex);
      }
    };
  }

  @NotNull
  private final LinearGraph myPermanentGraph;

  @NotNull
  private final GraphLayoutImpl myGraphLayout;

  @NotNull
  private final TimestampGetter myTimestampGetter;

  private BekSorter(@NotNull LinearGraph permanentGraph, @NotNull GraphLayoutImpl graphLayout, @NotNull TimestampGetter timestampGetter) {
    myPermanentGraph = permanentGraph;
    myGraphLayout = graphLayout;
    myTimestampGetter = timestampGetter;
  }

  public List<Integer> getResult() {
    BekBranchCreator bekBranchCreator = new BekBranchCreator(myPermanentGraph, myGraphLayout);
    Pair<List<BekBranch>, BekEdgeRestrictions> branches = bekBranchCreator.getResult();

    BekBranchMerger bekBranchMerger = new BekBranchMerger(branches.first, branches.second, myTimestampGetter);
    return bekBranchMerger.getResult();
  }

}