Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 48d32f5749c59ab4216641d20852206ece043db6 (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
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.core.variants;

import java.util.*;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.Assert;
import org.eclipse.team.core.TeamException;

/**
 * A <code>ResourceVariantByteStore</code> that caches the variant bytes in
 * memory and does not persist them over workbench invocations.
 * 
 * @since 3.0
 * @noextend This class is not intended to be subclassed by clients.
 */
public class SessionResourceVariantByteStore extends ResourceVariantByteStore {

	private static final byte[] NO_REMOTE = new byte[0];
	private Map membersCache = new HashMap();
	
	private Map syncBytesCache = new HashMap();

	/* (non-Javadoc)
	 * @see org.eclipse.team.core.variants.ResourceVariantByteStore#deleteBytes(org.eclipse.core.resources.IResource)
	 */
	public boolean deleteBytes(IResource resource) throws TeamException {
		return flushBytes(resource, IResource.DEPTH_ZERO);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.team.core.variants.ResourceVariantByteStore#dispose()
	 */
	public void dispose() {
		syncBytesCache.clear();
		membersCache.clear();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.core.variants.ResourceVariantByteStore#flushBytes(org.eclipse.core.resources.IResource, int)
	 */
	public boolean flushBytes(IResource resource, int depth) throws TeamException {
		if (getSyncBytesCache().containsKey(resource)) {
			if (depth != IResource.DEPTH_ZERO) {
				IResource[] members = members(resource);
				for (int i = 0; i < members.length; i++) {
					IResource child = members[i];
					flushBytes(child, (depth == IResource.DEPTH_INFINITE) ? IResource.DEPTH_INFINITE: IResource.DEPTH_ZERO);
				}
			}
			getSyncBytesCache().remove(resource);
			internalRemoveFromParent(resource);
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.core.variants.ResourceVariantByteStore#getBytes(org.eclipse.core.resources.IResource)
	 */
	public byte[] getBytes(IResource resource) throws TeamException {
		byte[] syncBytes = internalGetSyncBytes(resource);
		if (syncBytes != null && equals(syncBytes, NO_REMOTE)) {
			// If it is known that there is no remote, return null
			return null;
		}
		return syncBytes;
	}

	/**
	 * Return <code>true</code> if no bytes are contained in this tree.
	 * @return <code>true</code> if no bytes are contained in this tree.
	 */
	public boolean isEmpty() {
		return syncBytesCache.isEmpty();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.core.variants.ResourceVariantByteStore#members(org.eclipse.core.resources.IResource)
	 */
	public IResource[] members(IResource resource) {
		List members = (List)membersCache.get(resource);
		if (members == null) {
			return new IResource[0];
		}
		return (IResource[]) members.toArray(new IResource[members.size()]);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.core.variants.ResourceVariantByteStore#setBytes(org.eclipse.core.resources.IResource, byte[])
	 */
	public boolean setBytes(IResource resource, byte[] bytes) throws TeamException {
		Assert.isNotNull(bytes);
		byte[] oldBytes = internalGetSyncBytes(resource);
		if (oldBytes != null && equals(oldBytes, bytes)) return false;
		internalSetSyncInfo(resource, bytes);
		return true;
	}
	
	private Map getSyncBytesCache() {
		return syncBytesCache;
	}
	
	private void internalAddToParent(IResource resource) {
		IContainer parent = resource.getParent();
		if (parent == null) return;
		List members = (List)membersCache.get(parent);
		if (members == null) {
			members = new ArrayList();
			membersCache.put(parent, members);
		}
		members.add(resource);
	}
	
	private byte[] internalGetSyncBytes(IResource resource) {
		return (byte[])getSyncBytesCache().get(resource);
	}

	private void internalRemoveFromParent(IResource resource) {
		IContainer parent = resource.getParent();
		List members = (List)membersCache.get(parent);
		if (members != null) {
			members.remove(resource);
			if (members.isEmpty()) {
				membersCache.remove(parent);
			}
		}
	}
	
	private void internalSetSyncInfo(IResource resource, byte[] bytes) {
		getSyncBytesCache().put(resource, bytes);
		internalAddToParent(resource);
	}
}

Back to the top