eda: Simpler approach to Id row caching by storing row directly inside the Table...
authorJerome St-Louis <jerome@ecere.com>
Thu, 26 Jul 2012 03:41:24 +0000 (23:41 -0400)
committerJerome St-Louis <jerome@ecere.com>
Thu, 26 Jul 2012 03:41:24 +0000 (23:41 -0400)
eda/libeda/src/EDA.ec
eda/libeda/src/idList.ec

index 8575505..382b55d 100644 (file)
@@ -4,9 +4,6 @@ public import static "ecere"
 public import "ecere"
 #endif
 
-// For idRowCache connection...
-import "idList"
-
 #include <stdarg.h>
 
 #ifdef _DEBUG
@@ -238,12 +235,15 @@ public struct FieldIndex
    Field memberIdField;
 };
 
+Mutex idRowCacheMutex { };
+
 public class Table
 {
    class_no_expansion;
    Table prev, next;
    Database db;
    OldList listRows { offset = (uint)&((Row)0).prev };
+   Row cachedIdRow;
 public:
    virtual String GetName();
    virtual Field GetFirstField();
@@ -254,19 +254,11 @@ public:
    ~Table()
    {
       Row row;
-      MapIterator<Table, Row> it { map = idRowCache };
 
-      // Remove row from Id row cache
-      idRowCache.mutex.Wait();
-      if(it.Index(this, false))
-      {
-         Row r = it.data;
-         if(_refCount < 2)
-            _refCount = 2; // Removing the Table entry will try to decref the table ('this') again
-         it.Remove(r);
-         delete r;
-      }
-      idRowCache.mutex.Release();
+      // Delete cached Id row
+      idRowCacheMutex.Wait();
+      delete cachedIdRow;
+      idRowCacheMutex.Release();
 
 #ifdef AUTO_DELETE_TABLES
       if(db)
index 658aa1d..13976b9 100644 (file)
@@ -30,32 +30,6 @@ static void UnusedFunction()
 }
 private:
 
-class IdRowCache : Map<Table, Row>
-{
-   Mutex mutex { };
-   Row GetRow(Table tbl)
-   {
-      MapIterator<Table, Row> it { map = this };
-      mutex.Wait();
-      if(it.Index(tbl, false))
-      {
-         Row r = it.data;
-         return it.data;
-      }
-      else
-      {
-         Row r { tbl };
-         this[tbl] = r;
-         return r;
-      }
-   }
-   ~IdRowCache()
-   {
-      Free();
-   }
-}
-IdRowCache idRowCache { };
-
 public class Id : uint
 {
    class_data Table * table;     class_property Table * table     { set { class_data(table) = value; } get { return class_data(table); } };
@@ -200,7 +174,14 @@ public class Id : uint
          // FIXME
          Table tbl = *class_data(table);
          Field idField = tbl.FindField(defaultIdField);
-         Row r = idRowCache.GetRow(tbl);
+         Row r;
+         idRowCacheMutex.Wait();
+         if(!tbl.cachedIdRow)
+         {
+            tbl.cachedIdRow = Row { tbl };
+            incref tbl.cachedIdRow;
+         }
+         r = tbl.cachedIdRow;
 
          if(this)
          {
@@ -246,7 +227,7 @@ public class Id : uint
             sprintf(tempString, $"(Click to add a new %s...)", $"item"/*class_data(addText)*/);
          }
          // delete r;
-         idRowCache.mutex.Release();
+         idRowCacheMutex.Release();
       }
       return tempString;
    }