Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Valenta2003-08-18 17:56:23 +0000
committerMichael Valenta2003-08-18 17:56:23 +0000
commitc54f967d1a3b203c9986b3d3b50dd7f21f68587e (patch)
tree1fc727972f3d9647532518bdcc237ee4259c516a
parentd8483f3e4e2d102366ff5c9156d48e2700fc02bd (diff)
downloadeclipse.platform.team-c54f967d1a3b203c9986b3d3b50dd7f21f68587e.tar.gz
eclipse.platform.team-c54f967d1a3b203c9986b3d3b50dd7f21f68587e.tar.xz
eclipse.platform.team-c54f967d1a3b203c9986b3d3b50dd7f21f68587e.zip
Initial release of Team caching facility
-rw-r--r--bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/CVSProviderPlugin.java103
-rw-r--r--bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/resources/RemoteFile.java160
2 files changed, 34 insertions, 229 deletions
diff --git a/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/CVSProviderPlugin.java b/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/CVSProviderPlugin.java
index 3d8551e5f..d6f3aceb7 100644
--- a/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/CVSProviderPlugin.java
+++ b/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/CVSProviderPlugin.java
@@ -18,10 +18,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Date;
-import java.util.Enumeration;
import java.util.HashMap;
-import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -48,6 +45,7 @@ import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.Team;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.subscribers.TeamProvider;
+import org.eclipse.team.core.sync.RemoteContentsCache;
import org.eclipse.team.internal.ccvs.core.client.Command;
import org.eclipse.team.internal.ccvs.core.client.Command.KSubstOption;
import org.eclipse.team.internal.ccvs.core.client.Command.QuietOption;
@@ -93,16 +91,6 @@ public class CVSProviderPlugin extends Plugin {
public static final String PT_CONNECTIONMETHODS = "connectionmethods"; //$NON-NLS-1$
public static final String PT_FILE_MODIFICATION_VALIDATOR = "filemodificationvalidator"; //$NON-NLS-1$
- // Directory to cache file contents
- private static final String CACHE_DIRECTORY = ".cache"; //$NON-NLS-1$
- // Maximum lifespan of local cache file, in milliseconds
- private static final long CACHE_FILE_LIFESPAN = 60*60*1000; // 1hr
-
- private Hashtable cacheFileNames;
- private Hashtable cacheFileTimes;
- private long lastCacheCleanup;
- private int cacheDirSize;
-
private QuietOption quietness;
private int compressionLevel = DEFAULT_COMPRESSION_LEVEL;
private KSubstOption defaultTextKSubstOption = DEFAULT_TEXT_KSUBST_OPTION;
@@ -339,7 +327,7 @@ public class CVSProviderPlugin extends Plugin {
workspace.addResourceChangeListener(fileModificationManager, IResourceChangeEvent.POST_CHANGE);
fileModificationManager.registerSaveParticipant();
- createCacheDirectory();
+ RemoteContentsCache.enableCaching(ID);
cvsWorkspaceSubscriber = new CVSWorkspaceSubscriber(
CVS_WORKSPACE_SUBSCRIBER_ID,
@@ -368,7 +356,7 @@ public class CVSProviderPlugin extends Plugin {
// each class that added itself as a participant to have to listen to shutdown.
workspace.removeSaveParticipant(this);
- deleteCacheDirectory();
+ RemoteContentsCache.disableCache(ID);
}
/**
@@ -571,91 +559,6 @@ public class CVSProviderPlugin extends Plugin {
public void setReplaceUnmanaged(boolean replaceUnmanaged) {
this.replaceUnmanaged = replaceUnmanaged;
}
-
- private void createCacheDirectory() {
- try {
- IPath cacheLocation = getStateLocation().append(CACHE_DIRECTORY);
- File file = cacheLocation.toFile();
- if (file.exists()) {
- deleteFile(file);
- }
- file.mkdir();
- cacheFileNames = new Hashtable();
- cacheFileTimes = new Hashtable();
- lastCacheCleanup = -1;
- cacheDirSize = 0;
- } catch (IOException e) {
- log(new Status(IStatus.ERROR, ID, 0, Policy.bind("CVSProviderPlugin.errorCreatingCache", e.getMessage()), e)); //$NON-NLS-1$
- }
- }
-
- private void deleteCacheDirectory() {
- try {
- IPath cacheLocation = getStateLocation().append(CACHE_DIRECTORY);
- File file = cacheLocation.toFile();
- if (file.exists()) {
- deleteFile(file);
- }
- cacheFileNames = cacheFileTimes = null;
- lastCacheCleanup = -1;
- cacheDirSize = 0;
- } catch (IOException e) {
- log(new Status(IStatus.ERROR, ID, 0, Policy.bind("CVSProviderPlugin.errorDeletingCache", e.getMessage()), e)); //$NON-NLS-1$
- }
- }
-
- private void deleteFile(File file) throws IOException {
- if (file.isDirectory()) {
- File[] children = file.listFiles();
- for (int i = 0; i < children.length; i++) {
- deleteFile(children[i]);
- }
- }
- file.delete();
- }
-
- public synchronized File getCacheFileFor(String path) throws IOException {
- String physicalPath;
- if (cacheFileNames.containsKey(path)) {
- /*
- * cache hit
- */
- physicalPath = (String)cacheFileNames.get(path);
- registerHit(path);
- } else {
- /*
- * cache miss
- */
- physicalPath = String.valueOf(cacheDirSize++);
- cacheFileNames.put(path, physicalPath);
- registerHit(path);
- clearOldCacheEntries();
- }
- return getCacheFileForPhysicalPath(physicalPath);
- }
- private File getCacheFileForPhysicalPath(String physicalPath) throws IOException {
- return new File(getStateLocation().append(CACHE_DIRECTORY).toFile(), physicalPath);
- }
- private void registerHit(String path) {
- cacheFileTimes.put(path, Long.toString(new Date().getTime()));
- }
- private void clearOldCacheEntries() throws IOException {
- long current = new Date().getTime();
- if ((lastCacheCleanup!=-1) && (current - lastCacheCleanup < CACHE_FILE_LIFESPAN)) return;
- Enumeration e = cacheFileTimes.keys();
- while (e.hasMoreElements()) {
- String f = (String)e.nextElement();
- long lastHit = Long.valueOf((String)cacheFileTimes.get(f)).longValue();
- if ((current - lastHit) > CACHE_FILE_LIFESPAN) purgeCacheFile(f);
- }
-
- }
- private void purgeCacheFile(String path) throws IOException {
- File f = getCacheFileForPhysicalPath((String)cacheFileNames.get(path));
- f.delete();
- cacheFileTimes.remove(path);
- cacheFileNames.remove(path);
- }
/*
* Add the repository location to the cahced locations
diff --git a/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/resources/RemoteFile.java b/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/resources/RemoteFile.java
index b3b4dc34a..b1e9dd926 100644
--- a/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/resources/RemoteFile.java
+++ b/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/resources/RemoteFile.java
@@ -10,17 +10,9 @@
*******************************************************************************/
package org.eclipse.team.internal.ccvs.core.resources;
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
import java.io.InputStream;
-import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -34,6 +26,7 @@ import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.sync.IRemoteResource;
+import org.eclipse.team.core.sync.RemoteContentsCache;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
import org.eclipse.team.internal.ccvs.core.CVSStatus;
@@ -302,18 +295,9 @@ public class RemoteFile extends RemoteResource implements ICVSRemoteFile {
*/
public long getSize() {
if (contents == null) {
- try {
- File ioFile = getCacheFile();
- if (ioFile.exists()) {
- return ioFile.length();
- }
- } catch (IOException e) {
- // Try to purge the cache and continue
- try {
- clearCachedContents();
- } catch (IOException e2) {
- }
- CVSProviderPlugin.log(CVSException.wrapException(e));
+ File ioFile = getRemoteContentsCache().getFile(getCacheRelativePath());
+ if (ioFile.exists()) {
+ return ioFile.length();
}
}
return contents == null ? 0 : contents.length;
@@ -380,52 +364,39 @@ public class RemoteFile extends RemoteResource implements ICVSRemoteFile {
return new ByteArrayInputStream(contents == null ? new byte[0] : contents);
}
+ private InputStream getCachedContents() throws CVSException {
+ try {
+ return getRemoteContentsCache().getContents(getCacheRelativePath());
+ } catch (TeamException e) {
+ throw CVSException.wrapException(e);
+ }
+ }
+
public void setContents(InputStream stream, int responseType, boolean keepLocalHistory, IProgressMonitor monitor) throws CVSException {
try {
- try {
- byte[] buffer = new byte[1024];
- ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
- OutputStream out = byteStream;
- int read;
- try {
- try {
- while ((read = stream.read(buffer)) >= 0) {
- Policy.checkCanceled(monitor);
- out.write(buffer, 0, read);
- // Detect when the file is getting too big to keep in memory
- // and switch to a caching strategy for the contents of the file
- if (out == byteStream && byteStream.size() > CACHING_THRESHOLD) {
- // Switch streams
- byteStream.close();
- out = switchToCacheOutputStream(byteStream);
- // Continue looping until the whole file is read
- }
- }
- } finally {
- out.close();
- }
- } catch (IOException e) {
- // Make sure we don't leave the cache file around as it may not have the right contents
- if (byteStream != out) {
- clearCachedContents();
- }
- throw e;
- }
-
- // Set the contents if we didn't cache them to disk
- if (out instanceof ByteArrayOutputStream) {
- contents = ((ByteArrayOutputStream)out).toByteArray();
- } else {
- contents = null;
- }
- } finally {
- stream.close();
- }
- } catch(IOException e) {
+ getRemoteContentsCache().setContents(getCacheRelativePath(), stream, monitor);
+ } catch (TeamException e) {
throw CVSException.wrapException(e);
}
}
+ private RemoteContentsCache getRemoteContentsCache() {
+ return RemoteContentsCache.getCache(CVSProviderPlugin.ID);
+ }
+
+ /*
+ * Return the cache relative path for the receiver as
+ * host/cvs/root/module/path/.#filename revision
+ */
+ private String getCacheRelativePath() {
+ ICVSRepositoryLocation location = getRepository();
+ IPath path = new Path(location.getHost());
+ path = path.append(location.getRootDirectory());
+ path = path.append(parent.getRepositoryRelativePath());
+ path = path.append(getName() + ' ' + getRevision());
+ return path.toString();
+ }
+
/*
* @see ICVSFile#setReadOnly(boolean)
*/
@@ -512,75 +483,6 @@ public class RemoteFile extends RemoteResource implements ICVSRemoteFile {
RemoteFile remote = (RemoteFile) target;
return super.equals(target) && remote.getRevision().equals(getRevision());
}
-
- /*
- * Return the cache relative path for the receiver as
- * host/cvs/root/module/path/.#filename revision
- */
- private String getCacheRelativePath() {
- ICVSRepositoryLocation location = getRepository();
- IPath path = new Path(location.getHost());
- path = path.append(location.getRootDirectory());
- path = path.append(parent.getRepositoryRelativePath());
- path = path.append(getName() + ' ' + getRevision());
- return path.toString();
- }
-
- private File getCacheFile() throws IOException {
- return CVSProviderPlugin.getPlugin().getCacheFileFor(getCacheRelativePath());
- }
-
- private void clearCachedContents() throws IOException {
- try {
- File ioFile = getCacheFile();
- if (ioFile.exists()) {
- ioFile.delete();
- }
- } catch (IOException e) {
- CVSProviderPlugin.log(CVSException.wrapException(e));
- }
- }
-
- private InputStream getCachedContents() throws CVSException {
- // TODO: Update to use Team caching facility
- try {
- try {
- File ioFile = getCacheFile();
- if (ioFile.exists()) {
- return new BufferedInputStream(new FileInputStream(ioFile));
- }
- } catch (IOException e) {
- // Try to purge the cache and continue
- clearCachedContents();
- throw e;
- }
- } catch (IOException e) {
- // We will end up here if we couldn't read or delete the cache file
- throw new CVSException(new CVSStatus(IStatus.ERROR, 0, Policy.bind("RemoteFile.errorRetrievingFromCache", e.getMessage()), e));//$NON-NLS-1$
- }
- return null;
- }
-
- private OutputStream switchToCacheOutputStream(ByteArrayOutputStream byteStream) throws IOException {
- // Get the cache file and make sure it's parent exists
- File ioFile = getCacheFile();
- if ( ! ioFile.getParentFile().exists()) {
- ioFile.getParentFile().mkdirs();
- }
- // Switch streams
- OutputStream out;
- try {
- out = new BufferedOutputStream(new FileOutputStream(ioFile));
- } catch (FileNotFoundException e) {
- // Could not find the file. Perhaps the name is too long. (bug 20696)
- CVSProviderPlugin.log(IStatus.ERROR, Policy.bind("RemoteFile.Could_not_cache_remote_contents_to_disk._Caching_remote_file_in_memory_instead._1"), e); //$NON-NLS-1$
- // Resort to in-memory storage of the remote file
- out = new ByteArrayOutputStream();
- }
- // Write what we've read so far
- out.write(byteStream.toByteArray());
- return out;
- }
/**
* @see org.eclipse.team.internal.ccvs.core.ICVSFile#checkout(int)

Back to the top