[blam] ItemStore: protect the item hash table



commit 612031ceb05d3ee39fd748c06e158258fe9e02bd
Author: Carlos Martín Nieto <carlos cmartin tk>
Date:   Sat Mar 26 14:02:14 2011 +0100

    ItemStore: protect the item hash table
    
    In preparation for better locking, don't allow users to access the has
    table directly.
    
    Signed-off-by: Carlos Martín Nieto <carlos cmartin tk>

 src/Channel.cs   |   22 +++++++++++++---------
 src/ItemList.cs  |    2 +-
 src/ItemStore.cs |   17 +++++++++--------
 3 files changed, 23 insertions(+), 18 deletions(-)
---
diff --git a/src/Channel.cs b/src/Channel.cs
index 657966d..7e57c54 100644
--- a/src/Channel.cs
+++ b/src/Channel.cs
@@ -84,7 +84,7 @@ namespace Imendio.Blam {
                 lock(obj){
                     foreach (string id in item_list) {
                         if(id != null){
-                            item = store.Items[id] as Item;
+                            item = store.Get(id);
                             if(item.Unread){
                                 ++unread;
                             }
@@ -105,7 +105,7 @@ namespace Imendio.Blam {
                         if(id == null){ /* TODO: Figure out why this happens */
                             continue;
                         }
-                        item = store.Items[id] as Item;
+                        item = store.Get(id);
                         if(item != null && item.Unread && !item.Old){
                             ++new_items;
                         }
@@ -155,7 +155,7 @@ namespace Imendio.Blam {
 
         public Item GetItem (string id)
         {
-            return store.Items[id] as Item;
+            return store.Get(id);
         }
 
         public void Setup()
@@ -166,7 +166,7 @@ namespace Imendio.Blam {
                     if(id == null){
                         continue;
                     }
-                    Item item = store.Items[id] as Item;
+                    Item item = store.Get(id) as Item;
                     if(item == null){
                     } else {
                         nlist.Add(id);
@@ -194,7 +194,11 @@ namespace Imendio.Blam {
 
             lock(obj){
                 foreach(string id in item_list){
-                    item = store.Items[id] as Item;
+                    if(id == null){
+                        System.Console.WriteLine("null id {0} on {1}", id, Url);
+                        continue;
+                    }
+                    item = store.Get(id);
                     if (item.Unread) {
                         item.SetUnread (false);
                         updated = true;
@@ -242,7 +246,7 @@ namespace Imendio.Blam {
                         continue;
                     }
                     /* We already had it, so just update it */
-                    (store.Items[sitem.Id] as Item).Update(sitem);
+                    store.Get(sitem.Id).Update(sitem);
                 } else {
                     store.Add(new Item(sitem));
                 }
@@ -276,7 +280,7 @@ namespace Imendio.Blam {
         {
             if(item_list.Contains(item.Id)){
                 /* In this case we only need to update */
-                (store.Items[item.Id] as Item).Update(item);
+                store.Get(item.Id).Update(item);
                 return;
             }
 
@@ -284,13 +288,13 @@ namespace Imendio.Blam {
             lock(obj){
                 item_list.Add(item.Id);
             }
-            (store.Items[item.Id] as Item).Updated += ItemUpdated;
+            store.Get(item.Id).Updated += ItemUpdated;
         }
 
         /* Used to cross-mark as read */
         public void MarkItemIdAsRead (string id)
         {
-            Item item = store.Items[id] as Item;
+            Item item = store.Get(id);
             if (item.Unread) {
                 item.Unread = false;
                 EmitUpdated();
diff --git a/src/ItemList.cs b/src/ItemList.cs
index 2488efa..3c9d800 100644
--- a/src/ItemList.cs
+++ b/src/ItemList.cs
@@ -177,7 +177,7 @@ namespace Imendio.Blam {
                 if(id == null){
                     continue;
                 }
-                (Model as ListStore).AppendValues(store.Items[id]);
+                (Model as ListStore).AppendValues(store.Get(id));
             }
 
 	    GLib.Timeout.Add (100, new GLib.TimeoutHandler (IdleScrollCb));
diff --git a/src/ItemStore.cs b/src/ItemStore.cs
index 625d405..bf904e3 100644
--- a/src/ItemStore.cs
+++ b/src/ItemStore.cs
@@ -9,18 +9,19 @@ namespace Imendio.Blam
 {
     public class ItemStore
     {
-        Hashtable items;
+        private Hashtable items;
         static ItemStore instance = null;
         static string itemfile = Defines.APP_HOMEDIR + "/" + Defines.APP_ITEMSTORE_FILE;
+        private object db_lock;
 
-        public Hashtable Items {
-            get {
-                return items;
+        public Item Get(string id)
+        {
+            if(id == null){
+                Console.Error.WriteLine("Tried to access item with null key");
+                return null;
             }
 
-            set {
-                items = value;
-            }
+            return items[id] as Item;
         }
 
         public void Add(Item item)
@@ -75,7 +76,7 @@ namespace Imendio.Blam
             Console.WriteLine("about to format");
             SyndicationFeed feed = SyndicationFeed.Load(reader);
             foreach(SyndicationItem item in feed.Items){
-                instance.Items.Add(item.Id, new Item(item));
+                instance.items.Add(item.Id, new Item(item));
             }
         }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]