暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LockStatusCache.cs 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Collections.Generic;
  3. using Codice;
  4. using Codice.Client.BaseCommands;
  5. using Codice.Client.Commands.WkTree;
  6. using Codice.Client.Common;
  7. using Codice.Client.Common.Locks;
  8. using Codice.Client.Common.Threading;
  9. using Codice.Client.Common.WkTree;
  10. using Codice.CM.Common;
  11. using Codice.Utils;
  12. namespace Unity.PlasticSCM.Editor.AssetsOverlays.Cache
  13. {
  14. internal class LockStatusCache
  15. {
  16. internal LockStatusCache(
  17. WorkspaceInfo wkInfo,
  18. Action repaintProjectWindow)
  19. {
  20. mWkInfo = wkInfo;
  21. mRepaintProjectWindow = repaintProjectWindow;
  22. }
  23. internal AssetStatus GetStatus(string fullPath)
  24. {
  25. LockStatusData lockStatusData = GetLockStatusData(fullPath);
  26. if (lockStatusData == null)
  27. return AssetStatus.None;
  28. return lockStatusData.Status;
  29. }
  30. internal LockStatusData GetLockStatusData(string fullPath)
  31. {
  32. lock (mLock)
  33. {
  34. if (mStatusByPathCache == null)
  35. {
  36. mStatusByPathCache = BuildPathDictionary.ForPlatform<LockStatusData>();
  37. mCurrentCancelToken.Cancel();
  38. mCurrentCancelToken = new CancelToken();
  39. AsyncCalculateStatus(mCurrentCancelToken);
  40. return null;
  41. }
  42. LockStatusData result;
  43. if (mStatusByPathCache.TryGetValue(fullPath, out result))
  44. return result;
  45. return null;
  46. }
  47. }
  48. internal void Clear()
  49. {
  50. lock (mLock)
  51. {
  52. mCurrentCancelToken.Cancel();
  53. mStatusByPathCache = null;
  54. }
  55. }
  56. void AsyncCalculateStatus(CancelToken cancelToken)
  57. {
  58. Dictionary<string, LockStatusData> statusByPathCache = null;
  59. IThreadWaiter waiter = ThreadWaiter.GetWaiter(50);
  60. waiter.Execute(
  61. /*threadOperationDelegate*/ delegate
  62. {
  63. Dictionary<RepositorySpec, List<WorkspaceTreeNode>> lockCandidates =
  64. new Dictionary<RepositorySpec, List<WorkspaceTreeNode>>();
  65. FillLockCandidates.ForTree(mWkInfo, lockCandidates);
  66. if (cancelToken.IsCancelled())
  67. return;
  68. Dictionary<WorkspaceTreeNode, LockInfo> lockInfoByNode =
  69. SearchLocks.GetLocksInfo(mWkInfo, lockCandidates);
  70. if (cancelToken.IsCancelled())
  71. return;
  72. statusByPathCache = BuildStatusByNodeCache.
  73. ForLocks(mWkInfo.ClientPath, lockInfoByNode);
  74. },
  75. /*afterOperationDelegate*/ delegate
  76. {
  77. if (waiter.Exception != null)
  78. {
  79. ExceptionsHandler.LogException(
  80. "LockStatusCache",
  81. waiter.Exception);
  82. return;
  83. }
  84. if (cancelToken.IsCancelled())
  85. return;
  86. lock (mLock)
  87. {
  88. mStatusByPathCache = statusByPathCache;
  89. }
  90. mRepaintProjectWindow();
  91. });
  92. }
  93. static class FillLockCandidates
  94. {
  95. internal static void ForTree(
  96. WorkspaceInfo wkInfo,
  97. Dictionary<RepositorySpec, List<WorkspaceTreeNode>> lockCandidates)
  98. {
  99. WorkspaceTreeNode rootNode = CmConnection.Get().GetWorkspaceTreeHandler().
  100. GetWorkspaceTree(wkInfo, wkInfo.ClientPath, true);
  101. Queue<WorkspaceTreeNode> pendingDirectories = new Queue<WorkspaceTreeNode>();
  102. pendingDirectories.Enqueue(rootNode);
  103. while (pendingDirectories.Count > 0)
  104. {
  105. WorkspaceTreeNode directoryNode = pendingDirectories.Dequeue();
  106. ForChildren(directoryNode, pendingDirectories, lockCandidates);
  107. }
  108. }
  109. static void ForChildren(
  110. WorkspaceTreeNode directoryNode,
  111. Queue<WorkspaceTreeNode> pendingDirectories,
  112. Dictionary<RepositorySpec, List<WorkspaceTreeNode>> lockCandidates)
  113. {
  114. if (!directoryNode.HasChildren)
  115. return;
  116. foreach (WorkspaceTreeNode child in directoryNode.Children)
  117. {
  118. if (CheckWorkspaceTreeNodeStatus.IsDirectory(child))
  119. {
  120. pendingDirectories.Enqueue(child);
  121. continue;
  122. }
  123. if (CheckWorkspaceTreeNodeStatus.IsAdded(child))
  124. continue;
  125. List<WorkspaceTreeNode> nodes = null;
  126. if (!lockCandidates.TryGetValue(child.RepSpec, out nodes))
  127. {
  128. nodes = new List<WorkspaceTreeNode>();
  129. lockCandidates.Add(child.RepSpec, nodes);
  130. }
  131. nodes.Add(child);
  132. }
  133. }
  134. }
  135. static class BuildStatusByNodeCache
  136. {
  137. internal static Dictionary<string, LockStatusData> ForLocks(
  138. string wkPath,
  139. Dictionary<WorkspaceTreeNode, LockInfo> lockInfoByNode)
  140. {
  141. Dictionary<string, LockStatusData> result =
  142. BuildPathDictionary.ForPlatform<LockStatusData>();
  143. LockOwnerNameResolver nameResolver = new LockOwnerNameResolver();
  144. foreach (WorkspaceTreeNode node in lockInfoByNode.Keys)
  145. {
  146. LockStatusData lockStatusData = BuildLockStatusData(
  147. node, lockInfoByNode[node], nameResolver);
  148. string nodeWkPath = WorkspacePath.GetWorkspacePathFromCmPath(
  149. wkPath,
  150. WorkspaceNodeOperations.GetCmPath(node),
  151. PathHelper.GetDirectorySeparatorChar(wkPath));
  152. result.Add(nodeWkPath, lockStatusData);
  153. }
  154. return result;
  155. }
  156. static LockStatusData BuildLockStatusData(
  157. WorkspaceTreeNode node,
  158. LockInfo lockInfo,
  159. LockOwnerNameResolver nameResolver)
  160. {
  161. AssetStatus status = CheckWorkspaceTreeNodeStatus.IsCheckedOut(node) ?
  162. AssetStatus.Locked : AssetStatus.LockedRemote;
  163. return new LockStatusData(
  164. status,
  165. nameResolver.GetSeidName(lockInfo.SEIDData),
  166. LockWkInfo.GetWkCleanName(lockInfo));
  167. }
  168. }
  169. CancelToken mCurrentCancelToken = new CancelToken();
  170. Dictionary<string, LockStatusData> mStatusByPathCache;
  171. readonly WorkspaceInfo mWkInfo;
  172. readonly Action mRepaintProjectWindow;
  173. static object mLock = new object();
  174. }
  175. }