Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0af48d684e2eb87dbb352110b1de6312ccff0a4d (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
202
203
204
205
206
207
208
209
210
211
/*******************************************************************************
 * Copyright (c) 2005, 2006 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/**
 * 
 */
package org.eclipse.team.internal.ccvs.core.filesystem;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.ccvs.core.ICVSRemoteFile;
import org.eclipse.team.internal.ccvs.core.ICVSRemoteResource;
import org.eclipse.team.internal.ccvs.core.ILogEntry;
import org.eclipse.team.internal.ccvs.core.client.listeners.ILogEntryListener;
import org.eclipse.team.internal.ccvs.core.util.Util;

class LogEntryCache implements ILogEntryListener {

	/*
	 * Cache of all log entries
	 */
	private Map entries = new HashMap(); /* Map String:remoteFilePath->Map (String:revision -> ILogEntry) */

	Map internalGetLogEntries(String path) {
		return (Map) entries.get(path);
	}

	/**
	 * Return all the log entries at the given path
	 * @param path the file path
	 * @return the log entries for the file
	 */
	public ILogEntry[] getLogEntries(String path) {
		Map map = internalGetLogEntries(path);
		return (ILogEntry[]) map.values().toArray(new ILogEntry[map.values().size()]);
	}

	ILogEntry internalGetLogEntry(String path, String revision) {
		Map fileEntries = internalGetLogEntries(path);
		if (fileEntries != null) {
			return (ILogEntry) fileEntries.get(revision);
		}
		return null;
	}

	public String[] getCachedFilePaths() {
		return (String[]) entries.keySet().toArray(new String[entries.size()]);
	}

	/**
	 * Return the log entry that for the given resource
	 * or <code>null</code> if no entry was fetched or the
	 * resource is not a file.
	 * @param getFullPath(resource) the resource
	 * @return the log entry or <code>null</code>
	 */
	public synchronized ILogEntry getLogEntry(ICVSRemoteResource resource) {
		if (resource instanceof ICVSRemoteFile) {
			try {
				String path = getFullPath(resource);
				String revision = ((ICVSRemoteFile) resource).getRevision();
				return internalGetLogEntry(path, revision);
			} catch (TeamException e) {
				// Log and return null
			}
		}
		return null;
	}

	/**
	 * Return the log entries that were fetched for the given resource
	 * or an empty list if no entry was fetched.
	 * @param getFullPath(resource) the resource
	 * @return the fetched log entries or an empty list is none were found
	 */
	public synchronized ILogEntry[] getLogEntries(ICVSRemoteResource resource) {
		Map fileEntries = internalGetLogEntries(getFullPath(resource));
		if (fileEntries != null) {
			return (ILogEntry[]) fileEntries.values().toArray(new ILogEntry[fileEntries.size()]);
		}
		return new ILogEntry[0];
	}

	/*
	 * Return the full path that uniquely identifies the resource
	 * accross repositories. This path include the repository and
	 * resource path but does not include the revision so that 
	 * all log entries for a file can be retrieved.
	 */
	String getFullPath(ICVSRemoteResource resource) {
		return Util.appendPath(resource.getRepository().getLocation(false), resource.getRepositoryRelativePath());
	}

	public synchronized void clearEntries() {
		entries.clear();
	}

	public synchronized ICVSRemoteFile getImmediatePredecessor(ICVSRemoteFile file) throws TeamException {
		ILogEntry[] allLogs = getLogEntries(file);
		String revision = file.getRevision();
		// First decrement the last digit and see if that revision exists
		String predecessorRevision = getPredecessorRevision(revision);
		ICVSRemoteFile predecessor = findRevison(allLogs, predecessorRevision);
		// If nothing was found, try to fond the base of a branch
		if (predecessor == null && isBrancheRevision(revision)) {
			predecessorRevision = getBaseRevision(revision);
			predecessor = findRevison(allLogs, predecessorRevision);
		}
		// If that fails, it is still possible that there is a revision.
		// This can happen if the revision has been manually set.
		if (predecessor == null) {
			// We don't search in this case since this is costly and would be done
			// for any file that is new as well.
		}
		return predecessor;
	}

	/*
	 * Find the given revision in the list of log entries.
	 * Return null if the revision wasn't found.
	 */
	ICVSRemoteFile findRevison(ILogEntry[] allLogs, String predecessorRevision) throws TeamException {
		for (int i = 0; i < allLogs.length; i++) {
			ILogEntry entry = allLogs[i];
			ICVSRemoteFile file = entry.getRemoteFile();
			if (file.getRevision().equals(predecessorRevision)) {
				return file;
			}
		}
		return null;
	}

	/*
	 * Decrement the trailing digit by one.
	 */
	String getPredecessorRevision(String revision) {
		int digits[] = Util.convertToDigits(revision);
		digits[digits.length - 1]--;
		StringBuffer buffer = new StringBuffer(revision.length());
		for (int i = 0; i < digits.length; i++) {
			buffer.append(Integer.toString(digits[i]));
			if (i < digits.length - 1) {
				buffer.append('.');
			}
		}
		return buffer.toString();
	}

	/*
	 * Return true if there are more than 2 digits in the revision number
	 * (i.e. the revision is on a branch)
	 */
	boolean isBrancheRevision(String revision) {
		return Util.convertToDigits(revision).length > 2;
	}

	/*
	 * Remove the trailing revision digits such that the
	 * returned revision is shorter than the given revision 
	 * and is an even number of digits long
	 */
	String getBaseRevision(String revision) {
		int digits[] = Util.convertToDigits(revision);
		int length = digits.length - 1;
		if (length % 2 == 1) {
			length--;
		}
		StringBuffer buffer = new StringBuffer(revision.length());
		for (int i = 0; i < length; i++) {
			buffer.append(Integer.toString(digits[i]));
			if (i < length - 1) {
				buffer.append('.');
			}
		}
		return buffer.toString();
	}

	/**
	 * Remove any entries for the remote resources
	 * @param resource the remote resource
	 */
	public synchronized void clearEntries(ICVSRemoteResource resource) {
		String remotePath = getFullPath(resource);
		entries.remove(remotePath);
	}

	@Override
	public void handleLogEntryReceived(ILogEntry entry) {
		ICVSRemoteFile file = entry.getRemoteFile();
		String fullPath = getFullPath(file);
		String revision = entry.getRevision();
		Map fileEntries = internalGetLogEntries(fullPath);
		if (fileEntries == null) {
			fileEntries = new HashMap();
			entries.put(fullPath, fileEntries);
		}
		fileEntries.put(revision, entry);
	}
}

Back to the top