Do

Merge lp:~jassmith/do/clear-universe into lp:do

Proposed by Jason Smith
Status: Merged
Merged at revision: not available
Proposed branch: lp:~jassmith/do/clear-universe
Merge into: lp:do
Diff against target: None lines
To merge this branch: bzr merge lp:~jassmith/do/clear-universe
Reviewer Review Type Date Requested Status
Alex Launi (community) Approve
Robert Dyer (community) Needs Information
Review via email: mp+7863@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Robert Dyer (psybers) wrote :

On line 117 of the diff, use 'source' instead of 's' in the anon method.

Proof-read those 2 huge comment blocks for grammar (missing paren in one, 'so too will the old universe be' => no 'be').

Revision history for this message
Robert Dyer (psybers) wrote :

Don't we have a similar clearing problem with UniverseUpdateLoop()? If so, it needs a temporary universe too.

Then ReloadAction() and ReloadSource() don't need to have for loops checking if actions/items are in the universe because a (temporary) empty one will always be passed in.

review: Needs Information
Revision history for this message
Jason Smith (jassmith) wrote :

> Don't we have a similar clearing problem with UniverseUpdateLoop()? If so, it
> needs a temporary universe too.
>
> Then ReloadAction() and ReloadSource() don't need to have for loops checking
> if actions/items are in the universe because a (temporary) empty one will
> always be passed in.

We do not need to check in UniverseUpdateLoop because it will not happen there. And if it does it is self rectifying in a sense. Reload gets called after any plugin enable/disable so this is not a problem.

As for the other things, grammer and naming, will fix

lp:~jassmith/do/clear-universe updated
1256. By Jason Smith <jason@t500>

Clean up merge request items

Revision history for this message
Alex Launi (alexlauni) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'Do/src/Do.Core/UniverseManager.cs'
--- Do/src/Do.Core/UniverseManager.cs 2009-06-24 20:45:55 +0000
+++ Do/src/Do.Core/UniverseManager.cs 2009-06-24 20:46:47 +0000
@@ -28,6 +28,8 @@
28using Do.Universe;28using Do.Universe;
29using Do.Universe.Safe;29using Do.Universe.Safe;
3030
31using UniverseCollection = System.Collections.Generic.Dictionary<string, Do.Universe.Item>;
32
31namespace Do.Core33namespace Do.Core
32{34{
33 35
@@ -35,8 +37,9 @@
35 {37 {
3638
37 Thread update_thread;39 Thread update_thread;
38 Dictionary<string, Item> universe;40 UniverseCollection universe;
39 EventHandler initialized;41 EventHandler initialized;
42 object universe_lock;
40 43
41 const float epsilon = 0.00001f;44 const float epsilon = 0.00001f;
42 45
@@ -75,7 +78,8 @@
75 78
76 public UniverseManager ()79 public UniverseManager ()
77 {80 {
78 universe = new Dictionary<string, Item> ();81 universe = new UniverseCollection ();
82 universe_lock = new object ();
7983
80 update_thread = new Thread (new ThreadStart (UniverseUpdateLoop));84 update_thread = new Thread (new ThreadStart (UniverseUpdateLoop));
81 update_thread.IsBackground = true;85 update_thread.IsBackground = true;
@@ -117,7 +121,7 @@
117 121
118 public IEnumerable<Item> Search (string query, IEnumerable<Type> filter, Item other)122 public IEnumerable<Item> Search (string query, IEnumerable<Type> filter, Item other)
119 {123 {
120 lock (universe) 124 lock (universe_lock)
121 return Search (query, filter, universe.Values, other);125 return Search (query, filter, universe.Values, other);
122 }126 }
123 127
@@ -158,22 +162,24 @@
158 void UniverseUpdateLoop ()162 void UniverseUpdateLoop ()
159 {163 {
160 Random rand = new Random ();164 Random rand = new Random ();
161 DateTime startUpdate = DateTime.Now;165 DateTime startUpdate = DateTime.UtcNow;
162166
163 while (true) {167 while (true) {
164 Thread.Sleep (UpdateTimeout);168 Thread.Sleep (UpdateTimeout);
165 if (Do.Controller.IsSummoned) continue;169 if (Do.Controller.IsSummoned) continue;
166 startUpdate = DateTime.Now;170 startUpdate = DateTime.UtcNow;
167 171
168 if (rand.Next (10) == 0) {172 if (rand.Next (10) == 0) {
169 ReloadActions ();173 ReloadActions (universe);
170 }174 }
171 175
172 foreach (ItemSource source in PluginManager.ItemSources) {176 foreach (ItemSource source in PluginManager.ItemSources) {
173 ReloadSource (source);177 ReloadSource (source, universe);
174 if (UpdateRunTime < DateTime.Now - startUpdate) {178
179 if (UpdateRunTime < DateTime.UtcNow - startUpdate) {
175 Thread.Sleep (UpdateTimeout);180 Thread.Sleep (UpdateTimeout);
176 startUpdate = DateTime.Now;181 // sleeping for a bit
182 startUpdate = DateTime.UtcNow;
177 }183 }
178 }184 }
179 }185 }
@@ -182,10 +188,10 @@
182 /// <summary>188 /// <summary>
183 /// Reloads all actions in the universe.189 /// Reloads all actions in the universe.
184 /// </summary>190 /// </summary>
185 void ReloadActions ()191 void ReloadActions (UniverseCollection universe)
186 {192 {
187 Log<UniverseManager>.Debug ("Reloading actions...");193 Log<UniverseManager>.Debug ("Reloading actions...");
188 lock (universe) {194 lock (universe_lock) {
189 foreach (Act action in PluginManager.Actions) {195 foreach (Act action in PluginManager.Actions) {
190 if (universe.ContainsKey (action.UniqueId))196 if (universe.ContainsKey (action.UniqueId))
191 universe.Remove (action.UniqueId);197 universe.Remove (action.UniqueId);
@@ -200,7 +206,7 @@
200 /// not be called on the main thread to avoid blocking the UI if the206 /// not be called on the main thread to avoid blocking the UI if the
201 /// item source takes a long time to update.207 /// item source takes a long time to update.
202 /// </summary>208 /// </summary>
203 void ReloadSource (ItemSource source)209 void ReloadSource (ItemSource source, UniverseCollection universe)
204 {210 {
205 SafeItemSource safeSource;211 SafeItemSource safeSource;
206 IEnumerable<Item> oldItems, newItems;212 IEnumerable<Item> oldItems, newItems;
@@ -215,7 +221,7 @@
215 safeSource.UpdateItems ();221 safeSource.UpdateItems ();
216 newItems = safeSource.Items;222 newItems = safeSource.Items;
217 223
218 lock (universe) {224 lock (universe_lock) {
219 foreach (Item item in oldItems) {225 foreach (Item item in oldItems) {
220 if (universe.ContainsKey (item.UniqueId))226 if (universe.ContainsKey (item.UniqueId))
221 universe.Remove (item.UniqueId);227 universe.Remove (item.UniqueId);
@@ -229,8 +235,18 @@
229 void ReloadUniverse ()235 void ReloadUniverse ()
230 {236 {
231 Log<UniverseManager>.Info ("Reloading universe...");237 Log<UniverseManager>.Info ("Reloading universe...");
232 ReloadActions ();238
233 PluginManager.ItemSources.ForEach (ReloadSource);239 // A new temporary universe is created so that searches made during the reload (as threaded
240 // searches are allowed will not see an interuption in available items. Additionally this
241 // serves to clear out unused items that are orphaned from their item service.
242 UniverseCollection tmpUniverse = new UniverseCollection ();
243 ReloadActions (tmpUniverse);
244 PluginManager.ItemSources.ForEach (s => ReloadSource (s, tmpUniverse));
245
246 // Clearing the old universe is not needed and considered harmful as enumerables in existence
247 // already will be based off the old universe. Clearing it may cause an exception to be thrown.
248 // Once those enumerables are destroyed, so too will the old universe be.
249 universe = tmpUniverse;
234 Log<UniverseManager>.Info ("Universe contains {0} items.", universe.Count);250 Log<UniverseManager>.Info ("Universe contains {0} items.", universe.Count);
235 }251 }
236 252
@@ -242,7 +258,7 @@
242 /// </param>258 /// </param>
243 public void AddItems (IEnumerable<Item> items)259 public void AddItems (IEnumerable<Item> items)
244 {260 {
245 lock (universe) {261 lock (universe_lock) {
246 foreach (Item item in items) {262 foreach (Item item in items) {
247 if (universe.ContainsKey (item.UniqueId)) continue;263 if (universe.ContainsKey (item.UniqueId)) continue;
248 universe [item.UniqueId] = item;264 universe [item.UniqueId] = item;
@@ -259,7 +275,7 @@
259 /// </param>275 /// </param>
260 public void DeleteItems (IEnumerable<Item> items)276 public void DeleteItems (IEnumerable<Item> items)
261 {277 {
262 lock (universe) {278 lock (universe_lock) {
263 foreach (Item item in items) {279 foreach (Item item in items) {
264 universe.Remove (item.UniqueId);280 universe.Remove (item.UniqueId);
265 }281 }
@@ -277,7 +293,7 @@
277 /// </param>293 /// </param>
278 public bool TryGetItemForUniqueId (string uid, out Item element)294 public bool TryGetItemForUniqueId (string uid, out Item element)
279 {295 {
280 lock (universe) {296 lock (universe_lock) {
281 if (universe.ContainsKey (uid)) {297 if (universe.ContainsKey (uid)) {
282 element = universe [uid];298 element = universe [uid];
283 } else {299 } else {