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
1=== modified file 'Do/src/Do.Core/UniverseManager.cs'
2--- Do/src/Do.Core/UniverseManager.cs 2009-06-24 20:45:55 +0000
3+++ Do/src/Do.Core/UniverseManager.cs 2009-06-24 20:46:47 +0000
4@@ -28,6 +28,8 @@
5 using Do.Universe;
6 using Do.Universe.Safe;
7
8+using UniverseCollection = System.Collections.Generic.Dictionary<string, Do.Universe.Item>;
9+
10 namespace Do.Core
11 {
12
13@@ -35,8 +37,9 @@
14 {
15
16 Thread update_thread;
17- Dictionary<string, Item> universe;
18+ UniverseCollection universe;
19 EventHandler initialized;
20+ object universe_lock;
21
22 const float epsilon = 0.00001f;
23
24@@ -75,7 +78,8 @@
25
26 public UniverseManager ()
27 {
28- universe = new Dictionary<string, Item> ();
29+ universe = new UniverseCollection ();
30+ universe_lock = new object ();
31
32 update_thread = new Thread (new ThreadStart (UniverseUpdateLoop));
33 update_thread.IsBackground = true;
34@@ -117,7 +121,7 @@
35
36 public IEnumerable<Item> Search (string query, IEnumerable<Type> filter, Item other)
37 {
38- lock (universe)
39+ lock (universe_lock)
40 return Search (query, filter, universe.Values, other);
41 }
42
43@@ -158,22 +162,24 @@
44 void UniverseUpdateLoop ()
45 {
46 Random rand = new Random ();
47- DateTime startUpdate = DateTime.Now;
48+ DateTime startUpdate = DateTime.UtcNow;
49
50 while (true) {
51 Thread.Sleep (UpdateTimeout);
52 if (Do.Controller.IsSummoned) continue;
53- startUpdate = DateTime.Now;
54+ startUpdate = DateTime.UtcNow;
55
56 if (rand.Next (10) == 0) {
57- ReloadActions ();
58+ ReloadActions (universe);
59 }
60
61 foreach (ItemSource source in PluginManager.ItemSources) {
62- ReloadSource (source);
63- if (UpdateRunTime < DateTime.Now - startUpdate) {
64+ ReloadSource (source, universe);
65+
66+ if (UpdateRunTime < DateTime.UtcNow - startUpdate) {
67 Thread.Sleep (UpdateTimeout);
68- startUpdate = DateTime.Now;
69+ // sleeping for a bit
70+ startUpdate = DateTime.UtcNow;
71 }
72 }
73 }
74@@ -182,10 +188,10 @@
75 /// <summary>
76 /// Reloads all actions in the universe.
77 /// </summary>
78- void ReloadActions ()
79+ void ReloadActions (UniverseCollection universe)
80 {
81 Log<UniverseManager>.Debug ("Reloading actions...");
82- lock (universe) {
83+ lock (universe_lock) {
84 foreach (Act action in PluginManager.Actions) {
85 if (universe.ContainsKey (action.UniqueId))
86 universe.Remove (action.UniqueId);
87@@ -200,7 +206,7 @@
88 /// not be called on the main thread to avoid blocking the UI if the
89 /// item source takes a long time to update.
90 /// </summary>
91- void ReloadSource (ItemSource source)
92+ void ReloadSource (ItemSource source, UniverseCollection universe)
93 {
94 SafeItemSource safeSource;
95 IEnumerable<Item> oldItems, newItems;
96@@ -215,7 +221,7 @@
97 safeSource.UpdateItems ();
98 newItems = safeSource.Items;
99
100- lock (universe) {
101+ lock (universe_lock) {
102 foreach (Item item in oldItems) {
103 if (universe.ContainsKey (item.UniqueId))
104 universe.Remove (item.UniqueId);
105@@ -229,8 +235,18 @@
106 void ReloadUniverse ()
107 {
108 Log<UniverseManager>.Info ("Reloading universe...");
109- ReloadActions ();
110- PluginManager.ItemSources.ForEach (ReloadSource);
111+
112+ // A new temporary universe is created so that searches made during the reload (as threaded
113+ // searches are allowed will not see an interuption in available items. Additionally this
114+ // serves to clear out unused items that are orphaned from their item service.
115+ UniverseCollection tmpUniverse = new UniverseCollection ();
116+ ReloadActions (tmpUniverse);
117+ PluginManager.ItemSources.ForEach (s => ReloadSource (s, tmpUniverse));
118+
119+ // Clearing the old universe is not needed and considered harmful as enumerables in existence
120+ // already will be based off the old universe. Clearing it may cause an exception to be thrown.
121+ // Once those enumerables are destroyed, so too will the old universe be.
122+ universe = tmpUniverse;
123 Log<UniverseManager>.Info ("Universe contains {0} items.", universe.Count);
124 }
125
126@@ -242,7 +258,7 @@
127 /// </param>
128 public void AddItems (IEnumerable<Item> items)
129 {
130- lock (universe) {
131+ lock (universe_lock) {
132 foreach (Item item in items) {
133 if (universe.ContainsKey (item.UniqueId)) continue;
134 universe [item.UniqueId] = item;
135@@ -259,7 +275,7 @@
136 /// </param>
137 public void DeleteItems (IEnumerable<Item> items)
138 {
139- lock (universe) {
140+ lock (universe_lock) {
141 foreach (Item item in items) {
142 universe.Remove (item.UniqueId);
143 }
144@@ -277,7 +293,7 @@
145 /// </param>
146 public bool TryGetItemForUniqueId (string uid, out Item element)
147 {
148- lock (universe) {
149+ lock (universe_lock) {
150 if (universe.ContainsKey (uid)) {
151 element = universe [uid];
152 } else {