[blam] Item: Add reference counting



commit 779882ec23bc9f4d304c7c5aac18cb3b21182d81
Author: Carlos Martín Nieto <carlos cmartin tk>
Date:   Mon Oct 11 20:16:23 2010 +0100

    Item: Add reference counting

 src/Channel.cs   |   15 +++++++++++++--
 src/Item.cs      |   28 ++++++++++++++++++++++++++++
 src/ItemStore.cs |   12 ++++++++----
 3 files changed, 49 insertions(+), 6 deletions(-)
---
diff --git a/src/Channel.cs b/src/Channel.cs
index 619f941..fe9284c 100644
--- a/src/Channel.cs
+++ b/src/Channel.cs
@@ -42,6 +42,7 @@ namespace Imendio.Blam {
 
         ItemStore store = null;
         ArrayList item_list = null;
+        Object obj = new Object();
 
         public event ChannelEventHandler Updated;
 
@@ -142,8 +143,15 @@ namespace Imendio.Blam {
 
         public void Setup()
         {
-            foreach(string id in item_list){
-                (store.Items[id] as Item).Updated += ItemUpdated;
+            for(int i = item_list.Count - 1; i >= 0; --i){
+                string id = item_list[i] as string;
+                Item item = store.Items[id] as Item;
+                /* Maybe it got lost or deleted */
+                if(item == null){
+                    item_list.Remove(id);
+                } else {
+                    item.Updated += ItemUpdated;
+                }
             }
         }
 
@@ -189,6 +197,9 @@ namespace Imendio.Blam {
 		{
 			// Remove old items
 			foreach (Item item in mUnupdatedItems) {
+                if(item == null){ /* No idea how it happens, but it does */
+                    continue;
+                }
 				if (item.Permanent) {
 					// Don't remove permanent items
 					continue;
diff --git a/src/Item.cs b/src/Item.cs
index 2b67240..916a47e 100644
--- a/src/Item.cs
+++ b/src/Item.cs
@@ -16,6 +16,8 @@ namespace Imendio.Blam {
 
 		public string   keywords = "";
 
+        private int ref_cnt = 0;
+        private Object obj = new Object();
         public SyndicationContent exposed_text;
 
         public delegate void UpdateHandler(Item item);
@@ -43,6 +45,29 @@ namespace Imendio.Blam {
             ElementExtensions.Add("old", "blam", Old);
             ElementExtensions.Add("permanent", "blam", Permanent);
             ElementExtensions.Add("keywords", "blam", keywords);
+            ElementExtensions.Add("refcount", "blam", ref_cnt);
+        }
+
+        public int RefCount {
+            get {
+                lock(obj){
+                    return ref_cnt;
+                }
+            }
+        }
+
+        public void Grab()
+        {
+            lock(obj){
+                ++ref_cnt;
+            }
+        }
+
+        public void Release()
+        {
+            lock(obj){
+                --ref_cnt;
+            }
         }
 
 		public Item ()
@@ -80,6 +105,9 @@ namespace Imendio.Blam {
                     case "keywords":
                         Keywords = ext.GetObject<string>();
                         break;
+                    case "refcount":
+                        ref_cnt = ext.GetObject<int>();
+                        break;
                     }
                     break;
                 }
diff --git a/src/ItemStore.cs b/src/ItemStore.cs
index 234ff48..e917f3f 100644
--- a/src/ItemStore.cs
+++ b/src/ItemStore.cs
@@ -26,16 +26,20 @@ namespace Imendio.Blam
         public void Add(Item item)
         {
             if(items.ContainsKey(item.Id)){
-                /* FIXME: See if we can try to update or something */
+                (items[item.Id] as Item).Grab();
                 return;
+            } else {
+                item.Grab();
+                items.Add(item.Id, item);
             }
-
-            items.Add(item.Id, item);
         }
 
         public void Remove(Item item)
         {
-            items.Remove(item.Id);
+            item.Release();
+            if(item.RefCount == 0){
+                items.Remove(item.Id);
+            }
         }
 
         public static ItemStore GetInstance()



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