|
1 |
| -using System; |
2 |
| -using System.Collections.Generic; |
3 |
| -using System.IO; |
| 1 | +using System.Collections.Generic; |
4 | 2 |
|
5 |
| -using log4net; |
| 3 | +using Codice.LogWrapper; |
6 | 4 |
|
7 | 5 | namespace TrunkBot
|
8 | 6 | {
|
9 |
| - internal static class BranchesQueueStorage |
| 7 | + internal class BranchesQueueStorage |
10 | 8 | {
|
11 |
| - internal static bool HasQueuedBranches(string filePath) |
| 9 | + internal void AddQueuedBranches(List<Branch> branches) |
12 | 10 | {
|
13 |
| - return GetQueuedBranches(filePath).Count > 0; |
| 11 | + lock(mLock) |
| 12 | + foreach (Branch branch in branches) |
| 13 | + mBranches.AddFirst(branch); |
14 | 14 | }
|
15 | 15 |
|
16 |
| - internal static bool Contains(string repository, string branchId, string filePath) |
| 16 | + internal void EnqueueBranchIfNotAlreadyAdded(Branch branch) |
17 | 17 | {
|
18 |
| - List<Branch> branches = GetQueuedBranches(filePath); |
19 |
| - |
20 |
| - return BranchFinder.IndexOf(branches, repository, branchId) > -1; |
21 |
| - } |
22 |
| - |
23 |
| - internal static void EnqueueBranch(Branch branch, string filePath) |
24 |
| - { |
25 |
| - string line = string.Empty; |
26 |
| - try |
| 18 | + lock (mLock) |
27 | 19 | {
|
28 |
| - line = BranchParser.ToString(branch); |
29 |
| - |
30 |
| - if (Directory.Exists(Path.GetDirectoryName(filePath))) |
31 |
| - Directory.CreateDirectory(Path.GetDirectoryName(filePath)); |
32 |
| - |
33 |
| - using (StreamWriter file = new StreamWriter(filePath, true)) |
34 |
| - { |
35 |
| - file.WriteLine(line); |
36 |
| - } |
| 20 | + if (BranchFinder.Find(mBranches, branch.Repository, branch.Id) != null) |
| 21 | + return; |
| 22 | + mBranches.AddFirst(branch); |
37 | 23 | }
|
38 |
| - catch (Exception ex) |
39 |
| - { |
40 |
| - LogException("Error saving branch '{0}' to '{1}': {2}", ex, branch.FullName, filePath); |
41 |
| - } |
42 |
| - } |
43 |
| - |
44 |
| - internal static Branch DequeueBranch(string filePath) |
45 |
| - { |
46 |
| - List<Branch> queuedBranches = GetQueuedBranches(filePath); |
47 |
| - |
48 |
| - if (queuedBranches.Count == 0) |
49 |
| - return null; |
50 |
| - |
51 |
| - Branch dequeueBranch = queuedBranches[0]; |
52 |
| - queuedBranches.RemoveAt(0); |
53 |
| - |
54 |
| - WriteQueuedBranches(queuedBranches, filePath); |
55 |
| - |
56 |
| - return dequeueBranch; |
57 | 24 | }
|
58 | 25 |
|
59 |
| - internal static void RemoveBranch(string repository, string branchId, string filePath) |
| 26 | + internal void EnqueueBranchOnTop(Branch branch) |
60 | 27 | {
|
61 |
| - List<Branch> queuedBranches = GetQueuedBranches(filePath); |
62 |
| - |
63 |
| - int index = BranchFinder.IndexOf(queuedBranches, repository, branchId); |
64 |
| - if (index == -1) |
65 |
| - return; |
66 |
| - |
67 |
| - queuedBranches.RemoveAt(index); |
68 |
| - WriteQueuedBranches(queuedBranches, filePath); |
| 28 | + lock (mLock) |
| 29 | + mBranches.AddLast(branch); |
69 | 30 | }
|
70 | 31 |
|
71 |
| - internal static void WriteQueuedBranches(IEnumerable<Branch> branches, string filePath) |
| 32 | + internal Branch DequeueBranch() |
72 | 33 | {
|
73 |
| - if (branches == null) |
74 |
| - return; |
75 |
| - |
76 |
| - try |
| 34 | + lock (mLock) |
77 | 35 | {
|
78 |
| - using (StreamWriter file = new StreamWriter(filePath)) |
79 |
| - { |
80 |
| - foreach (Branch branch in branches) |
81 |
| - file.WriteLine(BranchParser.ToString(branch)); |
82 |
| - } |
83 |
| - } |
84 |
| - catch (Exception ex) |
85 |
| - { |
86 |
| - LogException("Error writing the queued branches to '{0}': {1}", ex, filePath); |
| 36 | + if (mBranches.Count == 0) |
| 37 | + return null; |
| 38 | + |
| 39 | + Branch result = mBranches.Last.Value; |
| 40 | + mBranches.RemoveLast(); |
| 41 | + return result; |
87 | 42 | }
|
88 | 43 | }
|
89 | 44 |
|
90 |
| - static List<Branch> GetQueuedBranches(string filePath) |
| 45 | + internal Branch PeekBranch() |
91 | 46 | {
|
92 |
| - List<Branch> result = new List<Branch>(); |
93 |
| - |
94 |
| - try |
| 47 | + lock (mLock) |
95 | 48 | {
|
96 |
| - if (!File.Exists(filePath)) |
97 |
| - return result; |
| 49 | + if (mBranches.Count == 0) |
| 50 | + return null; |
98 | 51 |
|
99 |
| - foreach (string line in File.ReadAllLines(filePath)) |
100 |
| - { |
101 |
| - Branch branch; |
102 |
| - if (!BranchParser.TryParse(line, out branch)) |
103 |
| - { |
104 |
| - mLog.ErrorFormat("Malformed line while reading branches file: {0}", line); |
105 |
| - continue; |
106 |
| - } |
107 |
| - result.Add(branch); |
108 |
| - } |
109 |
| - } |
110 |
| - catch (Exception ex) |
111 |
| - { |
112 |
| - LogException("Error reading the queued branches to '{0}': {1}", ex, filePath); |
| 52 | + return mBranches.Last.Value; |
113 | 53 | }
|
114 |
| - |
115 |
| - return result; |
116 |
| - } |
117 |
| - |
118 |
| - static void LogException(string message, Exception e, params string[] args) |
119 |
| - { |
120 |
| - mLog.ErrorFormat(message, args, e.Message); |
121 |
| - mLog.DebugFormat("StackTrace:{0}{1}", Environment.NewLine, e.StackTrace); |
122 | 54 | }
|
123 | 55 |
|
124 |
| - static class BranchParser |
| 56 | + internal void RemoveBranch(string repository, int branchId) |
125 | 57 | {
|
126 |
| - internal static bool TryParse(string line, out Branch branch) |
127 |
| - { |
128 |
| - branch = null; |
129 |
| - |
130 |
| - if (string.IsNullOrEmpty(line)) |
131 |
| - return false; |
132 |
| - |
133 |
| - string[] fields = ReadFieldsWithFinalComment(line, 5, SEPARATOR); |
134 |
| - if (fields == null) |
135 |
| - return false; |
136 |
| - |
137 |
| - branch = new Branch(fields[0], fields[1], fields[2], fields[3], fields[4]); |
138 |
| - return true; |
139 |
| - } |
140 |
| - |
141 |
| - internal static string ToString(Branch branch) |
142 |
| - { |
143 |
| - string comment = branch.Comment. |
144 |
| - Replace("\r\n", " ").Replace("\r", " ").Replace("\n", " ").Trim(); |
145 |
| - |
146 |
| - return string.Format("{0}|{1}|{2}|{3}|{4}", |
147 |
| - branch.Repository, branch.Id, branch.FullName, branch.Owner, comment); |
148 |
| - } |
149 |
| - |
150 |
| - static string[] ReadFieldsWithFinalComment( |
151 |
| - string line, int count, string separator) |
152 |
| - { |
153 |
| - string pendingToProcess = line; |
154 |
| - |
155 |
| - string[] result = new string[count]; |
156 |
| - |
157 |
| - for (int i = 0; i < count - 1; i++) |
158 |
| - { |
159 |
| - string field = GetNextField(ref pendingToProcess, separator); |
160 |
| - if (field == null) |
161 |
| - return null; |
162 |
| - |
163 |
| - result[i] = field; |
164 |
| - } |
165 |
| - |
166 |
| - result[count - 1] = pendingToProcess; |
167 |
| - return result; |
168 |
| - } |
169 |
| - |
170 |
| - static string GetNextField(ref string contentToProcess, string separator) |
| 58 | + lock (mLock) |
171 | 59 | {
|
172 |
| - int index = contentToProcess.IndexOf(separator); |
173 |
| - if (index == -1) |
174 |
| - { |
175 |
| - contentToProcess = string.Empty; |
176 |
| - return null; |
177 |
| - } |
178 |
| - |
179 |
| - string result = contentToProcess.Substring(0, index); |
180 |
| - if (index == contentToProcess.Length) |
181 |
| - contentToProcess = string.Empty; |
182 |
| - else |
183 |
| - contentToProcess = contentToProcess.Substring(index + 1); |
184 |
| - |
185 |
| - return result; |
| 60 | + Branch branch = BranchFinder.Find(mBranches, repository, branchId); |
| 61 | + if (branch == null) |
| 62 | + return; |
| 63 | + mBranches.Remove(branch); |
186 | 64 | }
|
187 |
| - |
188 |
| - const string SEPARATOR = "|"; |
189 | 65 | }
|
190 | 66 |
|
191 | 67 | static class BranchFinder
|
192 | 68 | {
|
193 |
| - internal static int IndexOf( |
194 |
| - List<Branch> branches, string repository, string branchId) |
| 69 | + internal static Branch Find( |
| 70 | + LinkedList<Branch> branches, string repository, int branchId) |
195 | 71 | {
|
196 |
| - for (int i = 0; i < branches.Count; i++) |
| 72 | + foreach(Branch branch in branches) |
197 | 73 | {
|
198 |
| - if (!RepositoryNameComparer.IsSameName(branches[i].Repository, repository)) |
| 74 | + if (branch.Id != branchId) |
199 | 75 | continue;
|
200 | 76 |
|
201 |
| - if (!branches[i].Id.Equals(branchId)) |
| 77 | + if (!RepositoryNameComparer.IsSameName(branch.Repository, repository)) |
202 | 78 | continue;
|
203 | 79 |
|
204 |
| - return i; |
| 80 | + return branch; |
205 | 81 | }
|
206 | 82 |
|
207 |
| - return -1; |
| 83 | + return null; |
208 | 84 | }
|
209 | 85 | }
|
210 | 86 |
|
211 |
| - static readonly ILog mLog = LogManager.GetLogger("BranchesQueueStorage"); |
| 87 | + LinkedList<Branch> mBranches = new LinkedList<Branch>(); |
| 88 | + object mLock = new object(); |
| 89 | + |
| 90 | + static readonly ILog mLog = LogManager.GetLogger("trunkbot"); |
212 | 91 | }
|
213 | 92 | }
|
0 commit comments