KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > subscribers > DescendantResourceVariantByteStore


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.core.subscribers;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.IWorkspaceRunnable;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.team.core.TeamException;
20 import org.eclipse.team.core.variants.*;
21
22 /**
23  * A <code>ResourceVariantByteStore</code> that optimizes the memory footprint
24  * of a remote resource variant tree by only storing those bytes that
25  * differ from a base resource variant tree. This class should only be used
26  * for cases where the base and remote are on the same line-of-descent.
27  * For example, when the remote tree represents the current state of a branch
28  * and the base represents the state of the same branch when the local workspace
29  * as last refreshed.
30  * <p>
31  * This class also contains the logic that allows subclasses to determine if
32  * bytes stored in the remote tree are on a different line-of-descent than the base.
33  * This is necessary because it is possible for the base tree to change in ways that
34  * invalidate the stored remote variants. For example, if the local resources are moved
35  * from the main trunck to a branch, any cached remote resource variants would be stale.
36  *
37  * @since 3.0
38  */

39 public abstract class DescendantResourceVariantByteStore extends ResourceVariantByteStore {
40     
41     ResourceVariantByteStore baseStore, remoteStore;
42
43     public DescendantResourceVariantByteStore(ResourceVariantByteStore baseCache, ResourceVariantByteStore remoteCache) {
44         this.baseStore = baseCache;
45         this.remoteStore = remoteCache;
46     }
47     
48     /**
49      * This method will dispose the remote cache but not the base cache.
50      * @see org.eclipse.team.core.variants.ResourceVariantByteStore#dispose()
51      */

52     public void dispose() {
53         remoteStore.dispose();
54     }
55
56     /* (non-Javadoc)
57      * @see org.eclipse.team.internal.core.subscribers.caches.ResourceVariantByteStore#getBytes(org.eclipse.core.resources.IResource)
58      */

59     public byte[] getBytes(IResource resource) throws TeamException {
60         byte[] remoteBytes = remoteStore.getBytes(resource);
61         byte[] baseBytes = baseStore.getBytes(resource);
62         if (baseBytes == null) {
63             // There is no base so use the remote bytes
64
return remoteBytes;
65         }
66         if (remoteBytes == null) {
67             if (isVariantKnown(resource)) {
68                 // The remote is known to not exist
69
// TODO: The check for NO_REMOTE does not take into consideration the line-of-descent
70
return remoteBytes;
71             } else {
72                 // The remote was either never queried or was the same as the base.
73
// In either of these cases, the base bytes are used.
74
return baseBytes;
75             }
76         }
77         if (isDescendant(resource, baseBytes, remoteBytes)) {
78             // Only use the remote bytes if they are later on the same line-of-descent as the base
79
return remoteBytes;
80         }
81         // Use the base sbytes since the remote bytes must be stale (i.e. are
82
// not on the same line-of-descent
83
return baseBytes;
84     }
85
86     /* (non-Javadoc)
87      * @see org.eclipse.team.internal.core.subscribers.caches.ResourceVariantByteStore#setBytes(org.eclipse.core.resources.IResource, byte[])
88      */

89     public boolean setBytes(IResource resource, byte[] bytes) throws TeamException {
90         byte[] baseBytes = baseStore.getBytes(resource);
91         if (baseBytes != null && equals(baseBytes, bytes)) {
92             // Remove the existing bytes so the base will be used (thus saving space)
93
return remoteStore.flushBytes(resource, IResource.DEPTH_ZERO);
94         } else {
95             return remoteStore.setBytes(resource, bytes);
96         }
97     }
98
99     /* (non-Javadoc)
100      * @see org.eclipse.team.internal.core.subscribers.caches.ResourceVariantByteStore#removeBytes(org.eclipse.core.resources.IResource, int)
101      */

102     public boolean flushBytes(IResource resource, int depth) throws TeamException {
103         return remoteStore.flushBytes(resource, depth);
104     }
105
106     /**
107      * Return <code>true</code> if the variant associated with the given local
108      * resource has been cached. This method is useful for those cases when
109      * there are no bytes for a resource variant and the client wants to
110      * know if this means that the remote does exist (i.e. this method returns
111      * <code>true</code>) or the remote has not been fetched (i.e. this method returns
112      * <code>false</code>).
113      * @param resource the local resource
114      * @return <code>true</code> if the variant associated with the given local
115      * resource has been cached.
116      * @throws TeamException
117      */

118     public abstract boolean isVariantKnown(IResource resource) throws TeamException;
119
120     /**
121      * This method indicates whether the remote bytes are a later revision or version
122      * on the same line-of-descent as the base. A line of descent may be a branch or a fork
123      * (depending on the terminology used by the versioing server). If this method returns
124      * <code>false</code> then the remote bytes will be ignored by this tree.
125      * @param resource the local resource
126      * @param baseBytes the base bytes for the local resoource
127      * @param remoteBytes the remote bytes for the local resoource
128      * @return whether the remote bytes are later on the same line-of-descent as the base bytes
129      */

130     protected abstract boolean isDescendant(IResource resource, byte[] baseBytes, byte[] remoteBytes) throws TeamException;
131     
132     /* (non-Javadoc)
133      * @see org.eclipse.team.internal.core.subscribers.caches.ResourceVariantByteStore#setVariantDoesNotExist(org.eclipse.core.resources.IResource)
134      */

135     public boolean deleteBytes(IResource resource) throws TeamException {
136         return remoteStore.deleteBytes(resource);
137     }
138
139     /**
140      * Return the base tree from which the remote is descendant.
141      * @return Returns the base tree.
142      */

143     protected ResourceVariantByteStore getBaseStore() {
144         return baseStore;
145     }
146
147     /**
148      * Return the remote tree which contains bytes only for the resource variants
149      * that differ from those in the base tree.
150      * @return Returns the remote tree.
151      */

152     protected ResourceVariantByteStore getRemoteStore() {
153         return remoteStore;
154     }
155     
156     /* (non-Javadoc)
157      * @see org.eclipse.team.internal.core.subscribers.caches.ResourceVariantByteStore#members(org.eclipse.core.resources.IResource)
158      */

159     public IResource[] members(IResource resource) throws TeamException {
160         IResource[] remoteMembers = getRemoteStore().members(resource);
161         IResource[] baseMembers = getBaseStore().members(resource);
162         Set JavaDoc members = new HashSet JavaDoc();
163         for (int i = 0; i < remoteMembers.length; i++) {
164             members.add(remoteMembers[i]);
165         }
166         for (int i = 0; i < baseMembers.length; i++) {
167             IResource member = baseMembers[i];
168             // Add the base only if the remote does not know about it
169
// (i.e. hasn't marked it as deleted
170
if (!isVariantKnown(member)) {
171                 members.add(member);
172             }
173         }
174         return (IResource[]) members.toArray(new IResource[members.size()]);
175     }
176     
177     /* (non-Javadoc)
178      * @see org.eclipse.team.core.variants.ResourceVariantByteStore#run(org.eclipse.core.resources.IResource, org.eclipse.core.resources.IWorkspaceRunnable, org.eclipse.core.runtime.IProgressMonitor)
179      */

180     public void run(IResource root, IWorkspaceRunnable runnable, IProgressMonitor monitor) throws TeamException {
181         remoteStore.run(root, runnable, monitor);
182     }
183 }
184
Popular Tags