Skip to main content
summaryrefslogtreecommitdiffstats
blob: 594319ab12f538d3f5e53a77b6e222c90a7331a5 (plain) (blame)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.skynet.core.relation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import org.eclipse.osee.framework.core.data.ArtifactToken;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.data.IRelationType;
import org.eclipse.osee.framework.core.enums.DeletionFlag;
import org.eclipse.osee.framework.core.enums.RelationSide;
import org.eclipse.osee.framework.jdk.core.type.CompositeKeyHashMap;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactKey;
import org.eclipse.osee.framework.skynet.core.internal.Activator;
import org.eclipse.osee.framework.skynet.core.relation.RelationFilterUtil.RelationMatcher;
import org.eclipse.osee.framework.skynet.core.types.IArtifact;

/**
 * @author Roberto E. Escobar
 */
public class RelationCache {

   private static final ThreadLocal<ArtifactKey> THREAD_SHARED_KEY = new ThreadLocal<ArtifactKey>() {

      @Override
      protected ArtifactKey initialValue() {
         return new ArtifactKey();
      }
   };

   // Indexed by ArtifactKey so that map does not hold strong reference to artifact which allows it to be garbage collected
   // the branch is accounted for because artifact key includes the branch uuid
   private final CompositeKeyHashMap<ArtifactKey, IRelationType, List<RelationLink>> relationsByType =
      new CompositeKeyHashMap<ArtifactKey, IRelationType, List<RelationLink>>(1024, true);

   private ArtifactKey getKey(ArtifactToken artifact) {
      ArtifactKey key = THREAD_SHARED_KEY.get();
      return key.setKey(artifact);
   }

   private ArtifactKey getKey(long artId, BranchId branchUuid) {
      ArtifactKey key = THREAD_SHARED_KEY.get();
      return key.setKey(artId, branchUuid);
   }

   public void deCache(IArtifact artifact) {
      ArtifactKey key = getKey(artifact);
      Collection<List<RelationLink>> removeValues = relationsByType.removeValues(key);

      if (removeValues != null) {
         for (List<RelationLink> relations : removeValues) {
            for (RelationLink relation : relations) {
               removeSingleRelation(artifact, relation);
            }
         }
      }
   }

   private void removeSingleRelation(IArtifact otherArtifact, RelationLink relation) {
      int artifactId;
      if (otherArtifact.getArtId() == relation.getBArtifactId()) {
         artifactId = relation.getAArtifactId();
      } else {
         artifactId = relation.getBArtifactId();
      }

      ArtifactKey key = getKey(artifactId, relation.getBranch());
      List<RelationLink> relations = relationsByType.get(key, relation.getRelationType());
      if (relations != null) {
         relations.remove(relation);
      }
   }

   public void cache(IArtifact artifact, RelationLink newRelation) {
      IRelationType relationType = newRelation.getRelationType();
      List<RelationLink> selectedRelations = getAllByType(artifact, relationType);
      if (selectedRelations == null) {
         selectedRelations = new CopyOnWriteArrayList<>();
         relationsByType.put(new ArtifactKey(artifact), relationType, selectedRelations);
      }
      if (selectedRelations.contains(newRelation)) {
         OseeLog.logf(Activator.class, Level.SEVERE,
            "Duplicate relationByType objects for same relation for Relation [%s] Artifact (%s)[%s]", newRelation,
            artifact.getArtId(), artifact.getName());
      }
      selectedRelations.add(newRelation);
   }

   public List<RelationLink> getAll(ArtifactToken artifact) {
      return getRelations(artifact, DeletionFlag.INCLUDE_DELETED);
   }

   public List<RelationLink> getAllByType(IArtifact artifact, IRelationType relationType) {
      ArtifactKey key = getKey(artifact);
      return relationsByType.get(key, relationType);
   }

   public List<RelationLink> getRelations(ArtifactToken artifact, DeletionFlag deletionFlag) {
      ArtifactKey key = getKey(artifact);
      List<RelationLink> linksFound = new ArrayList<>();
      RelationMatcher matcher = RelationFilterUtil.createMatcher(deletionFlag);
      findRelations(linksFound, key, matcher);
      return linksFound;
   }

   private void findRelations(Collection<RelationLink> linksFound, int artId, BranchId branchUuid, IRelationType relationType, RelationMatcher matcher) {
      List<RelationLink> sourceLink = relationsByType.get(getKey(artId, branchUuid), relationType);
      RelationFilterUtil.filter(sourceLink, linksFound, matcher);
   }

   private void findRelations(Collection<RelationLink> linksFound, int artId, BranchId branchUuid, RelationMatcher matcher) {
      ArtifactKey artifactKey = getKey(artId, branchUuid);
      findRelations(linksFound, artifactKey, matcher);
   }

   private void findRelations(Collection<RelationLink> linksFound, ArtifactKey artifactKey, RelationMatcher matcher) {
      List<List<RelationLink>> values = relationsByType.getValues(artifactKey);
      if (values != null) {
         for (List<RelationLink> linksSource : values) {
            RelationFilterUtil.filter(linksSource, linksFound, matcher);
         }
      }
   }

   /**
    * Find RelationById Related On ArtA or ArtB
    */
   public RelationLink getByRelIdOnArtifact(int relLinkId, int aArtifactId, int bArtifactId, BranchId branch) {
      RelationMatcher relIdMatcher = RelationFilterUtil.createFindFirstRelationLinkIdMatcher(relLinkId);
      List<RelationLink> links = new ArrayList<>();
      findRelations(links, aArtifactId, branch, relIdMatcher);
      if (links.isEmpty()) {
         findRelations(links, bArtifactId, branch, relIdMatcher);
      }
      return links.isEmpty() ? null : links.iterator().next();
   }

   public RelationLink getLoadedRelation(IArtifact artifact, int aArtifactId, int bArtifactId, IRelationType relationType, DeletionFlag deletionFlag) {
      Set<RelationLink> itemsFound = new HashSet<>();

      final int artifactId = artifact.getArtId();
      final BranchId branchUuid = artifact.getBranch();
      RelationMatcher artIdMatcher = new RelationMatcher() {

         @Override
         public boolean matches(RelationLink relationLink) {
            return relationLink.getAArtifactId() == artifactId || relationLink.getBArtifactId() == artifactId;
         }

         @Override
         public boolean isFindNextAllowed() {
            return true;
         }
      };

      RelationMatcher matcher = RelationFilterUtil.createMatcher(deletionFlag, artIdMatcher);
      findRelations(itemsFound, artifactId, branchUuid, relationType, matcher);

      List<RelationLink> relations = new ArrayList<>();
      for (RelationLink relation : itemsFound) {
         if (relation.getAArtifactId() == aArtifactId && relation.getBArtifactId() == bArtifactId) {
            relations.add(relation);
         }
      }
      int size = relations.size();
      if (size > 1) {
         OseeLog.logf(Activator.class, Level.SEVERE,
            "Artifact A [%s] has [%d] relations of same type [%s] to Artifact B [%s]", artifact.getArtId(),
            relations.size(), relationType, bArtifactId);
      }
      return size != 0 ? relations.iterator().next() : null;
   }

   public RelationLink getLoadedRelation(IRelationType relationType, int aArtifactId, int bArtifactId, BranchId branch) {
      RelationMatcher bArtIdMatcher =
         RelationFilterUtil.createFindFirstRelatedArtIdMatcher(bArtifactId, RelationSide.SIDE_B);
      List<RelationLink> links = new ArrayList<>();
      findRelations(links, aArtifactId, branch, relationType, bArtIdMatcher);
      if (links.isEmpty()) {
         RelationMatcher aArtIdMatcher =
            RelationFilterUtil.createFindFirstRelatedArtIdMatcher(aArtifactId, RelationSide.SIDE_A);
         findRelations(links, bArtifactId, branch, relationType, aArtIdMatcher);
      }
      return links.isEmpty() ? null : links.iterator().next();
   }

}

Back to the top