Ejemplo n.º 1
0
        public void AddFile(string LocalPath)
        {
            lock (cache)
            {
                if (Preferences.LocalSettings.GetBool("TreatProjectsAsFolders", true))
                {
                    string directoryPath = Path.GetDirectoryName(LocalPath);
                    if (directoryPath != null && ((CachedDirectories != null) &&
                                                  (CachedDirectories.ContainsKey(GetKey(directoryPath))) &&
                                                  (CachedDirectories[GetKey(directoryPath)] != DateTime.MinValue)))
                    {
                        cache[GetKey(LocalPath)] = null;
                        logger.Trace("AddFile: {0}", LocalPath);
                        return;
                    }
                    else if (directoryPath != null && ((CachedDirectories == null) ||
                                                       !CachedDirectories.ContainsKey(GetKey(directoryPath))))
                    {
                        AddDirectoryFilesToCache(directoryPath, false);
                        logger.Trace("AddDir: {0}", directoryPath);
                        return;
                    }
                }
                P4.Options opts = new P4.Options();
                opts["-Olhp"] = null;
                IList <P4.FileMetaData> files = _repository.GetFileMetaData(opts, P4.FileSpec.LocalSpec(LocalPath));

                if ((files != null) && (files.Count > 0))
                {
                    cache[GetKey(LocalPath)] = new CachedFile(files[0]);
                    logger.Trace("AddDirMeta: {0}", LocalPath);
                }
            }
        }
Ejemplo n.º 2
0
 public void Set(string key, CachedFile value)
 {
     lock (cache)
     {
         if (string.IsNullOrEmpty(key))
         {
             throw new ArgumentException("Key can't be null or blank", "key");
         }
         if ((key.EndsWith("\\")) || (key.EndsWith("\\*")) || (key.EndsWith("\\...")))
         {
             throw new ArgumentException("Key can't be a folder", "key");
         }
         var k = GetKey(key);
         if (cache.ContainsKey(k))
         {
             cache[k] = value;
         }
         else
         {
             cache.Add(k, value);
         }
     }
 }
Ejemplo n.º 3
0
        private IList <string> UpdateFiles(IList <string> LocalPaths)
        {
            IList <string> updatedFiles = new List <string>();

            lock (cache)
            {
                P4.Options opts = new P4.Options();
                opts["-Olhp"] = null;
                IList <P4.FileMetaData> files = _repository.GetFileMetaData(P4.FileSpec.LocalSpecList(LocalPaths.ToArray()), opts);

                if ((files != null) && (files.Count > 0))
                {
                    foreach (P4.FileMetaData file in files)
                    {
                        var key = GetKey(file.LocalPath.Path);
                        if (cache.ContainsKey(key))
                        {
                            SourceControlStatus oldStat = cache[key].ScmStatus;
                            SourceControlStatus newStat = new SourceControlStatus(file);
                            if (oldStat.Flags != newStat.Flags)
                            {
                                cache[key] = new CachedFile(file);
                                updatedFiles.Add(file.LocalPath.Path);
                                logger.Trace("UpdateFiles: {0}", file.LocalPath.Path);
                            }
                        }
                        else
                        {
                            cache[key] = new CachedFile(file);
                            updatedFiles.Add(file.LocalPath.Path);
                            logger.Trace("UpdateFiles: {0}", file.LocalPath.Path);
                        }
                    }
                }
            }
            return(updatedFiles);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Check if the file has been updated in the specified timespan
        /// </summary>
        /// <param name="key">Local path of the file</param>
        /// <param name="maxAge">Max timespan  since last refresh</param>
        /// <returns>
        /// False: If not in cache or last update was more than maxAge ago.
        /// True: If last update less than maxAge ago.
        /// </returns>
        public bool CacheIsFresh(string key, TimeSpan maxAge)
        {
            lock (cache)
            {
                if (ContainsKey(key) == false)
                {
                    return(false);
                }

                CachedFile c      = cache[GetKey(key)];
                bool       result = ((DateTime.Now - c.LastUpdate) < maxAge);
                if (logger.IsTraceEnabled)
                {
                    var details = "";
                    if (!result)
                    {
                        details = string.Format("({0}) ", (DateTime.Now - c.LastUpdate));
                    }
                    logger.Trace("CacheFresh {0} {1}timespan {2} and file {3}",
                                 result, details, maxAge, key);
                }
                return(result);
            }
        }
Ejemplo n.º 5
0
        private void RefreshFilesThreadProc()
        {
            try
            {
                DateTime updateStarted = DateTime.Now;
                while (_runRefreshThread)
                {
                    int minutes = Preferences.LocalSettings.GetInt("Update_status", 5);
                    logger.Trace("CWD: {0}", _scm.Connection.Repository.Connection.CurrentWorkingDirectory);
                    logger.Trace("Entering loop in RefreshFilesThreadProc, LoadingSolution:{0}, WaitTime: {1} ",
                                 _scm.LoadingSolution, minutes);
                    if ((_scm.LoadingSolution) || (minutes < 1))
                    {
                        minutes = 0;

                        Thread.Sleep(15000);                         //sleep 15 seconds
                        // then check to see if the refresh interval has changed
                        // or the solution has completed loading
                        continue;
                    }
                    logger.Trace("Finished waiting for solution load or postive refresh interval in RefreshFilesThreadProc");

                    _refreshInterval = TimeSpan.FromMinutes(minutes);

                    TimeSpan waitIncrement = TimeSpan.FromMilliseconds(1000);
                    try
                    {
                        TimeSpan timeWaited = DateTime.Now - updateStarted;
                        while (timeWaited < _refreshInterval)
                        {
                            if (_runRefreshThread == false)
                            {
                                logger.Debug("Exiting refresh thread");
                                return;
                            }
                            Thread.Sleep(waitIncrement);
                            timeWaited += waitIncrement;
                        }
                    }
                    catch { }

                    updateStarted = DateTime.Now;

                    try
                    {
                        if (_runRefreshThread == false)
                        {
                            logger.Debug("Exiting refresh thread");
                            return;
                        }
                        if ((minutes > 0) && (cache.Count > 0) && (_onUpdatedFiles != null))
                        {
                            if (_ignoredFilesMap != null)
                            {
                                lock (_ignoredFilesMap)
                                {
                                    _ignoredFilesMap = null;                                        // TODO - RC - Why??
                                }
                            }
                            CachedFile[] files = new CachedFile[cache.Count];
                            logger.Trace("Finished waiting for refresh interval in RefreshFilesThreadProc, {0} files in the cache", cache.Count);
                            lock (cache)
                            {
                                cache.Values.CopyTo(files, 0);
                            }
                            int fileidx = 0;
                            int fileCnt = cache.Count;
                            while (fileidx < fileCnt)
                            {
                                IList <string> filesToRefresh = new List <string>();

                                for (int batchCnt = 0; (fileidx < fileCnt); fileidx++)
                                {
                                    CachedFile file = files[fileidx];
                                    if (_runRefreshThread == false)
                                    {
                                        logger.Debug("Exiting refresh thread");
                                        return;
                                    }
                                    if (DateTime.Now - file.LastUpdate > _refreshInterval)
                                    {
                                        batchCnt++;
                                        if ((file != null) && (((P4.FileMetaData)file) != null) && (((P4.FileMetaData)file).LocalPath != null))
                                        {
                                            filesToRefresh.Add(((P4.FileMetaData)file).LocalPath.Path);
                                        }
                                    }
                                }
                                if ((filesToRefresh != null) && (filesToRefresh.Count > 0))
                                {
                                    logger.Trace("Found {0} files to refresh in RefreshFilesThreadProc, starting with {1}", filesToRefresh.Count, filesToRefresh[0]);
                                    filesToRefresh = UpdateFiles(filesToRefresh);

                                    if ((filesToRefresh != null) && (filesToRefresh.Count > 0))
                                    {
                                        Delegate[] targetList = _onUpdatedFiles.GetInvocationList();
                                        foreach (UpdatedFiles_Delegate dlgRefreshProjectGlyphs in targetList)
                                        {
                                            if (_runRefreshThread == false)
                                            {
                                                logger.Debug("Exiting refresh thread");
                                                return;
                                            }
                                            try
                                            {
                                                dlgRefreshProjectGlyphs(filesToRefresh, false);
                                            }
                                            catch
                                            {
                                                // problem with delegate, so remove from the list
                                                OnUpdatedFiles -= dlgRefreshProjectGlyphs;
                                            }
                                        }
                                    }
                                    if (fileidx < fileCnt)
                                    {
                                        Thread.Sleep(1000);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Warn("Refresh thread exception: {0}", ex.Message);
                        logger.Debug("Refresh thread exception: {0}", ex.StackTrace);
                    }
                }
            }
            catch (ThreadAbortException ex)
            {
                logger.Warn("Refresh thread aborted: {0}", ex.Message);
                logger.Debug("Refresh thread exception: {0}", ex.StackTrace);
                Thread.ResetAbort();
            }
            catch (Exception ex)
            {
                logger.Warn("Refresh thread exception: {0}", ex.Message);
                logger.Debug("Refresh thread exception: {0}", ex.StackTrace);
            }
        }