summaryrefslogtreecommitdiffstats
blob: 698198014b32c0afea8e50aa9ca5172203bc5160 (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
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
202
203
204
205
206
207
208
/*******************************************************************************
 * 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.core.model.cache;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import org.eclipse.osee.framework.core.enums.OseeCacheEnum;
import org.eclipse.osee.framework.core.enums.TransactionVersion;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.TransactionRecord;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.logging.OseeLog;

/**
 * @author Roberto E. Escobar
 */
public class TransactionCache implements IOseeCache<TransactionRecord> {
   private ITransactionDataAccessor accessor;

   private final Map<Integer, TransactionRecord> transactionIdCache =
      new ConcurrentHashMap<Integer, TransactionRecord>();

   private final OseeCacheEnum cacheId;
   private boolean ensurePopulatedRanOnce;
   private long lastLoaded;

   public TransactionCache() {
      this.lastLoaded = 0;
      this.cacheId = OseeCacheEnum.TRANSACTION_CACHE;
      this.ensurePopulatedRanOnce = false;
   }

   public void setAccessor(ITransactionDataAccessor accessor) {
      this.accessor = accessor;
   }

   protected ITransactionDataAccessor getDataAccessor() {
      return accessor;
   }

   @Override
   public void cache(TransactionRecord... types) throws OseeCoreException {
      Conditions.checkNotNull(types, "types to cache");
      for (TransactionRecord type : types) {
         cache(type);
      }
   }

   @Override
   public void cache(TransactionRecord type) throws OseeCoreException {
      Conditions.checkNotNull(type, "type to cache");
      ensurePopulated();
      transactionIdCache.put(type.getId(), type);
   }

   @Override
   public void decache(TransactionRecord... types) throws OseeCoreException {
      Conditions.checkNotNull(types, "types to de-cache");
      for (TransactionRecord type : types) {
         decache(type);
      }
   }

   @Override
   public void decache(TransactionRecord type) throws OseeCoreException {
      Conditions.checkNotNull(type, "type to de-cache");
      ensurePopulated();
      if (type.isIdValid()) {
         transactionIdCache.remove(type.getId());
      }
   }

   @Override
   public Collection<TransactionRecord> getAll() throws OseeCoreException {
      ensurePopulated();
      return new ArrayList<TransactionRecord>(transactionIdCache.values());
   }

   public TransactionRecord getOrLoad(int txId) throws OseeCoreException {
      TransactionRecord transactionRecord = getById(txId);
      if (transactionRecord == null) {
         loadTransactions(Collections.singletonList(txId));
         transactionRecord = getById(txId);
         if (transactionRecord == null) {
            if (txId == 1) { // handle bootstrap case for system root branch creation
               return null;
            }
            throw new OseeStateException("Transaction Record[%s] was not found", txId);
         }
      }
      return transactionRecord;
   }

   @Override
   public TransactionRecord getById(int txId) throws OseeCoreException {
      ensurePopulated();
      return transactionIdCache.get(txId);
   }

   @Override
   public TransactionRecord getByGuid(String guid) throws OseeCoreException {
      throw new OseeStateException("TransactionCache.getByGuid() is not implemented...");
   }

   @Override
   public OseeCacheEnum getCacheId() {
      return cacheId;
   }

   @Override
   public int size() {
      return transactionIdCache.size();
   }

   @Override
   public Collection<TransactionRecord> getAllDirty() {
      Set<TransactionRecord> dirtys = new HashSet<TransactionRecord>();
      for (TransactionRecord record : transactionIdCache.values()) {
         if (record.isDirty()) {
            dirtys.add(record);
         }
      }
      return dirtys;
   }

   @Override
   public void storeAllModified() {
   }

   @Override
   public void storeItems(TransactionRecord... items) {
   }

   @Override
   public void storeItems(Collection<TransactionRecord> toStore) {
   }

   public TransactionRecord getPriorTransaction(TransactionRecord transactionId) throws OseeCoreException {
      return getDataAccessor().getOrLoadPriorTransaction(this, transactionId.getId(), transactionId.getBranchId());
   }

   public TransactionRecord getTransaction(Branch branch, TransactionVersion revision) throws OseeCoreException {
      TransactionRecord toReturn = null;
      if (TransactionVersion.BASE == revision) {
         toReturn = branch.getBaseTransaction();
      }
      if (toReturn == null) {
         toReturn = getDataAccessor().loadTransactionRecord(this, branch, revision);
      }
      return toReturn;
   }

   public Collection<TransactionRecord> getTransactions(Branch branch) {
      return Collections.emptyList();
   }

   public void loadTransactions(Collection<Integer> transactionIds) throws OseeCoreException {
      ensurePopulated();
      getDataAccessor().loadTransactionRecord(this, transactionIds);
   }

   @Override
   public synchronized void ensurePopulated() throws OseeCoreException {
      if (!ensurePopulatedRanOnce) {
         ensurePopulatedRanOnce = true;
         reloadCache();
      }
   }

   @Override
   public long getLastLoaded() {
      return lastLoaded;
   }

   private synchronized void setLastLoaded(long lastLoaded) {
      this.lastLoaded = lastLoaded;
   }

   @Override
   public synchronized boolean reloadCache() throws OseeCoreException {
      getDataAccessor().load(this);
      OseeLog.log(this.getClass(), Level.INFO, "Loaded " + getCacheId().toString().toLowerCase());
      setLastLoaded(System.currentTimeMillis());
      return true;
   }

   @Override
   public void decacheAll() {
      transactionIdCache.clear();
      this.ensurePopulatedRanOnce = false;
   }
}