No Description
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 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Codice;
  5. using Codice.Client.Commands;
  6. using Codice.Client.Commands.Mount;
  7. using Codice.Client.Commands.WkTree;
  8. using Codice.Client.Common;
  9. using Codice.Client.Common.Locks;
  10. using Codice.Client.Common.Threading;
  11. using Codice.Client.Common.WkTree;
  12. using Codice.CM.Common;
  13. using Codice.Utils;
  14. using PlasticGui.WorkspaceWindow;
  15. namespace Unity.PlasticSCM.Editor.AssetsOverlays.Cache
  16. {
  17. internal class LockStatusCache
  18. {
  19. internal LockStatusCache(
  20. WorkspaceInfo wkInfo,
  21. Action repaintProjectWindow,
  22. Action repaintInspector)
  23. {
  24. mWkInfo = wkInfo;
  25. mRepaintProjectWindow = repaintProjectWindow;
  26. mRepaintInspector = repaintInspector;
  27. }
  28. internal AssetStatus GetStatus(string fullPath)
  29. {
  30. LockStatusData lockStatusData = GetLockStatusData(fullPath);
  31. if (lockStatusData == null)
  32. return AssetStatus.None;
  33. return lockStatusData.Status;
  34. }
  35. internal LockStatusData GetLockStatusData(string fullPath)
  36. {
  37. lock (mLock)
  38. {
  39. if (mStatusByPathCache == null)
  40. {
  41. mStatusByPathCache = BuildPathDictionary.ForPlatform<LockStatusData>();
  42. mCurrentCancelToken.Cancel();
  43. mCurrentCancelToken = new CancelToken();
  44. AsyncCalculateStatus(mCurrentCancelToken);
  45. return null;
  46. }
  47. LockStatusData result;
  48. if (mStatusByPathCache.TryGetValue(fullPath, out result))
  49. return result;
  50. return null;
  51. }
  52. }
  53. internal void Clear()
  54. {
  55. lock (mLock)
  56. {
  57. mCurrentCancelToken.Cancel();
  58. mStatusByPathCache = null;
  59. }
  60. }
  61. void AsyncCalculateStatus(CancelToken cancelToken)
  62. {
  63. Dictionary<string, LockStatusData> statusByPathCache = null;
  64. IThreadWaiter waiter = ThreadWaiter.GetWaiter(50);
  65. waiter.Execute(
  66. /*threadOperationDelegate*/ delegate
  67. {
  68. Dictionary<MountPointWithPath, List<WorkspaceTreeNode>> lockCandidates =
  69. new Dictionary<MountPointWithPath, List<WorkspaceTreeNode>>();
  70. FillLockCandidates.ForTree(mWkInfo, lockCandidates);
  71. if (cancelToken.IsCancelled())
  72. return;
  73. Dictionary<WorkspaceTreeNode, LockInfo> lockInfoByNode =
  74. SearchLocks.GetLocksInfo(mWkInfo, lockCandidates);
  75. if (cancelToken.IsCancelled())
  76. return;
  77. statusByPathCache = BuildStatusByNodeCache.
  78. ForLocks(mWkInfo.ClientPath, lockInfoByNode);
  79. },
  80. /*afterOperationDelegate*/ delegate
  81. {
  82. if (waiter.Exception != null)
  83. {
  84. ExceptionsHandler.LogException(
  85. "LockStatusCache",
  86. waiter.Exception);
  87. return;
  88. }
  89. if (cancelToken.IsCancelled())
  90. return;
  91. lock (mLock)
  92. {
  93. mStatusByPathCache = statusByPathCache;
  94. }
  95. mRepaintProjectWindow();
  96. mRepaintInspector();
  97. });
  98. }
  99. static class FillLockCandidates
  100. {
  101. internal static void ForTree(
  102. WorkspaceInfo wkInfo,
  103. Dictionary<MountPointWithPath, List<WorkspaceTreeNode>> lockCandidates)
  104. {
  105. WorkspaceTreeNode rootNode = CmConnection.Get().GetWorkspaceTreeHandler().
  106. GetWorkspaceTree(wkInfo, wkInfo.ClientPath, true);
  107. Queue<NodeWithPath> pendingDirectories = new Queue<NodeWithPath>();
  108. pendingDirectories.Enqueue(new NodeWithPath(
  109. MountPointWithPath.BuildWorkspaceRootMountPoint(rootNode.RepSpec),
  110. rootNode, wkInfo.ClientPath));
  111. while (pendingDirectories.Count > 0)
  112. {
  113. NodeWithPath directoryNode = pendingDirectories.Dequeue();
  114. ForChildren(
  115. wkInfo.ClientPath,
  116. directoryNode.Mount,
  117. directoryNode.Path,
  118. directoryNode.Node,
  119. pendingDirectories,
  120. lockCandidates);
  121. }
  122. }
  123. static void ForChildren(
  124. string wkPath,
  125. MountPointWithPath parentMount,
  126. string dirPath,
  127. WorkspaceTreeNode dirNode,
  128. Queue<NodeWithPath> pendingDirectories,
  129. Dictionary<MountPointWithPath, List<WorkspaceTreeNode>> lockCandidates)
  130. {
  131. if (!dirNode.HasChildren)
  132. return;
  133. foreach (WorkspaceTreeNode child in dirNode.Children)
  134. {
  135. string childPath = Path.Combine(dirPath, child.Name);
  136. if (CheckWorkspaceTreeNodeStatus.IsDirectory(child))
  137. {
  138. MountPointWithPath mount = XlinkWorkspaceTreeNode.IsXlinkWkNode(child) ?
  139. new MountPointWithPath(
  140. MountPointId.BuildForXlink(
  141. ((XlinkWorkspaceTreeNode)child).Xlink.GUID, parentMount.Id),
  142. child.RepSpec,
  143. WorkspacePath.CmPathFromWorkspacePath(childPath, wkPath)) :
  144. parentMount;
  145. pendingDirectories.Enqueue(
  146. new NodeWithPath(mount, child, childPath));
  147. continue;
  148. }
  149. if (CheckWorkspaceTreeNodeStatus.IsAdded(child))
  150. continue;
  151. List<WorkspaceTreeNode> nodes = null;
  152. if (!lockCandidates.TryGetValue(parentMount, out nodes))
  153. {
  154. nodes = new List<WorkspaceTreeNode>();
  155. lockCandidates.Add(parentMount, nodes);
  156. }
  157. nodes.Add(child);
  158. }
  159. }
  160. class NodeWithPath
  161. {
  162. internal readonly MountPointWithPath Mount;
  163. internal readonly WorkspaceTreeNode Node;
  164. internal readonly string Path;
  165. internal NodeWithPath(
  166. MountPointWithPath mount,
  167. WorkspaceTreeNode node,
  168. string path)
  169. {
  170. Mount = mount;
  171. Node = node;
  172. Path = path;
  173. }
  174. }
  175. }
  176. static class BuildStatusByNodeCache
  177. {
  178. internal static Dictionary<string, LockStatusData> ForLocks(
  179. string wkPath,
  180. Dictionary<WorkspaceTreeNode, LockInfo> lockInfoByNode)
  181. {
  182. Dictionary<string, LockStatusData> result =
  183. BuildPathDictionary.ForPlatform<LockStatusData>();
  184. LockOwnerNameResolver nameResolver = new LockOwnerNameResolver();
  185. foreach (WorkspaceTreeNode node in lockInfoByNode.Keys)
  186. {
  187. LockStatusData lockStatusData = BuildLockStatusData(
  188. node, lockInfoByNode[node], nameResolver);
  189. string nodeWkPath = WorkspacePath.GetWorkspacePathFromCmPath(
  190. wkPath,
  191. WorkspaceNodeOperations.GetCmPath(node),
  192. PathHelper.GetDirectorySeparatorChar(wkPath));
  193. result.Add(nodeWkPath, lockStatusData);
  194. }
  195. return result;
  196. }
  197. static LockStatusData BuildLockStatusData(
  198. WorkspaceTreeNode node,
  199. LockInfo lockInfo,
  200. LockOwnerNameResolver nameResolver)
  201. {
  202. return new LockStatusData(
  203. GetAssetStatus(node, lockInfo),
  204. nameResolver.GetSeidName(lockInfo.SEIDData),
  205. BranchInfoCache.GetProtectedBranchName(
  206. node.RepSpec, lockInfo.HolderBranchId));
  207. }
  208. static AssetStatus GetAssetStatus(
  209. WorkspaceTreeNode node,
  210. LockInfo lockInfo)
  211. {
  212. if (lockInfo.Status == LockInfo.LockStatus.Retained)
  213. return AssetStatus.Retained;
  214. return CheckWorkspaceTreeNodeStatus.IsCheckedOut(node) ?
  215. AssetStatus.Locked : AssetStatus.LockedRemote;
  216. }
  217. }
  218. CancelToken mCurrentCancelToken = new CancelToken();
  219. Dictionary<string, LockStatusData> mStatusByPathCache;
  220. readonly Action mRepaintInspector;
  221. readonly Action mRepaintProjectWindow;
  222. readonly WorkspaceInfo mWkInfo;
  223. static object mLock = new object();
  224. }
  225. }