Prevent create/update from seeing expired records.
authorScott Cantor <cantor.2@osu.edu>
Fri, 29 Sep 2006 06:14:15 +0000 (06:14 +0000)
committerScott Cantor <cantor.2@osu.edu>
Fri, 29 Sep 2006 06:14:15 +0000 (06:14 +0000)
xmltooling/impl/MemoryStorageService.cpp

index e161e65..505eea8 100644 (file)
@@ -218,8 +218,23 @@ void MemoryStorageService::createString(const char* context, const char* key, co
     \r
     // Check for a duplicate.\r
     map<string,Record>::iterator i=ctx.m_dataMap.find(key);\r
-    if (i!=ctx.m_dataMap.end())\r
-        throw IOException("attempted to insert a record with duplicate key ($1)", params(1,key));\r
+    if (i!=ctx.m_dataMap.end()) {\r
+        // Not yet expired?\r
+        if (time(NULL) < i->second.expiration)\r
+            throw IOException("attempted to insert a record with duplicate key ($1)", params(1,key));\r
+        // It's dead, so we can just remove it now and create the new record.\r
+        // Now find the reversed index of expiration to key, so we can clear it.\r
+        pair<multimap<time_t,string>::iterator,multimap<time_t,string>::iterator> range =\r
+            ctx.m_expMap.equal_range(i->second.expiration);\r
+        for (; range.first != range.second; ++range.first) {\r
+            if (range.first->second == i->first) {\r
+                ctx.m_expMap.erase(range.first);\r
+                break;\r
+            }\r
+        }\r
+        // And finally delete the record itself.\r
+        ctx.m_dataMap.erase(i);\r
+    }\r
     \r
     ctx.m_dataMap[key]=Record(value,expiration);\r
     ctx.m_expMap.insert(multimap<time_t,string>::value_type(expiration,key));\r
@@ -255,6 +270,8 @@ bool MemoryStorageService::updateString(const char* context, const char* key, co
     map<string,Record>::iterator i=ctx.m_dataMap.find(key);\r
     if (i==ctx.m_dataMap.end())\r
         return false;\r
+    else if (time(NULL) >= i->second.expiration)\r
+        return false;\r
         \r
     if (value)\r
         i->second.data = value;\r