Expose detection of duplicate insertions.
authorScott Cantor <cantor.2@osu.edu>
Thu, 28 Jun 2007 18:01:15 +0000 (18:01 +0000)
committerScott Cantor <cantor.2@osu.edu>
Thu, 28 Jun 2007 18:01:15 +0000 (18:01 +0000)
xmltooling/impl/MemoryStorageService.cpp
xmltooling/util/StorageService.h

index fbed029..3d2f11d 100644 (file)
@@ -40,12 +40,12 @@ namespace xmltooling {
         MemoryStorageService(const DOMElement* e);
         virtual ~MemoryStorageService();
         
-        void createString(const char* context, const char* key, const char* value, time_t expiration);
+        bool createString(const char* context, const char* key, const char* value, time_t expiration);
         int readString(const char* context, const char* key, string* pvalue=NULL, time_t* pexpiration=NULL, int version=0);
         int updateString(const char* context, const char* key, const char* value=NULL, time_t expiration=0, int version=0);
         bool deleteString(const char* context, const char* key);
         
-        void createText(const char* context, const char* key, const char* value, time_t expiration) {
+        bool createText(const char* context, const char* key, const char* value, time_t expiration) {
             return createString(context, key, value, expiration);
         }
         int readText(const char* context, const char* key, string* pvalue=NULL, time_t* pexpiration=NULL, int version=0) {
@@ -219,7 +219,7 @@ unsigned long MemoryStorageService::Context::reap(time_t exp)
     return count;
 }
 
-void MemoryStorageService::createString(const char* context, const char* key, const char* value, time_t expiration)
+bool MemoryStorageService::createString(const char* context, const char* key, const char* value, time_t expiration)
 {
     Context& ctx = writeContext(context);
     SharedLock locker(m_lock, false);
@@ -229,7 +229,7 @@ void MemoryStorageService::createString(const char* context, const char* key, co
     if (i!=ctx.m_dataMap.end()) {
         // Not yet expired?
         if (time(NULL) < i->second.expiration)
-            throw IOException("attempted to insert a record with duplicate key ($1)", params(1,key));
+            return false;
         // It's dead, so we can just remove it now and create the new record.
         ctx.m_dataMap.erase(i);
     }
@@ -237,6 +237,7 @@ void MemoryStorageService::createString(const char* context, const char* key, co
     ctx.m_dataMap[key]=Record(value,expiration);
     
     m_log.debug("inserted record (%s) in context (%s)", key, context);
+    return true;
 }
 
 int MemoryStorageService::readString(const char* context, const char* key, string* pvalue, time_t* pexpiration, int version)
index 6dd8d8e..3c82ea7 100644 (file)
@@ -52,10 +52,11 @@ namespace xmltooling {
          * @param key           null-terminated unique key of up to 255 bytes
          * @param value         null-terminated value of up to 255 bytes to store
          * @param expiration    an expiration timestamp, after which the record can be purged
+         * @return  true iff record was inserted, false iff a duplicate was found
          * 
-         * @throws IOException  raised if errors occur in the insertion process 
+         * @throws IOException  raised if fatal errors occur in the insertion process 
          */
-        virtual void createString(const char* context, const char* key, const char* value, time_t expiration)=0;
+        virtual bool createString(const char* context, const char* key, const char* value, time_t expiration)=0;
         
         /**
          * Returns an existing "short" record from the storage service.
@@ -110,10 +111,11 @@ namespace xmltooling {
          * @param key           null-terminated unique key of up to 255 bytes
          * @param value         null-terminated value of arbitrary length
          * @param expiration    an expiration timestamp, after which the record can be purged
+         * @return  true iff record was inserted, false iff a duplicate was found
          * 
          * @throws IOException  raised if errors occur in the insertion process 
          */
-        virtual void createText(const char* context, const char* key, const char* value, time_t expiration)=0;
+        virtual bool createText(const char* context, const char* key, const char* value, time_t expiration)=0;
         
         /**
          * Returns an existing "long" record from the storage service.