You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by js...@apache.org on 2010/04/19 08:26:31 UTC

svn commit: r935461 - /tuscany/sca-cpp/trunk/components/nosqldb/tinycdb.hpp

Author: jsdelfino
Date: Mon Apr 19 06:26:31 2010
New Revision: 935461

URL: http://svn.apache.org/viewvc?rev=935461&view=rev
Log:
Reuse the same file descriptor when reopening a tinycdb database.

Modified:
    tuscany/sca-cpp/trunk/components/nosqldb/tinycdb.hpp

Modified: tuscany/sca-cpp/trunk/components/nosqldb/tinycdb.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/nosqldb/tinycdb.hpp?rev=935461&r1=935460&r2=935461&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/nosqldb/tinycdb.hpp (original)
+++ tuscany/sca-cpp/trunk/components/nosqldb/tinycdb.hpp Mon Apr 19 06:26:31 2010
@@ -143,19 +143,50 @@ const string dbname(const TinyCDB& cdb) 
  * Open a database.
  */
 const failable<int> cdbopen(TinyCDB& cdb) {
+
+    // Get database file serial number
     struct stat st;
-    int s = stat(c_str(cdb.name), &st);
+    const int s = stat(c_str(cdb.name), &st);
     if (s == -1)
         return mkfailure<int>(string("Couldn't read database stat ") + cdb.name);
-    if (st.st_ino != cdb.st.st_ino || cdb.fd == -1) {
-        if (cdb.fd != -1)
-            close(cdb.fd);
+
+    // Open database for the first time
+    if (cdb.fd == -1) {
         cdb.fd = open(c_str(cdb.name), O_RDONLY);
         if (cdb.fd == -1)
             return mkfailure<int>(string("Couldn't open database file ") + cdb.name);
         debug(cdb.fd, "tinycdb::open::fd");
         cdb.st = st;
+        return cdb.fd;
     }
+
+    // Close and reopen database after a change
+    if (st.st_ino != cdb.st.st_ino) {
+
+        // Close current fd
+        close(cdb.fd);
+
+        // Reopen database
+        const int newfd = open(c_str(cdb.name), O_RDONLY);
+        if (newfd == -1)
+            return mkfailure<int>(string("Couldn't open database file ") + cdb.name);
+        if (newfd == cdb.fd) {
+            debug(cdb.fd, "tinycdb::open::fd");
+            cdb.st = st;
+            return cdb.fd;
+        }
+
+        // We got a different fd, dup it to the current fd then close it
+        if (fcntl(newfd, F_DUPFD, cdb.fd) == -1)
+            return mkfailure<int>(string("Couldn't dup database file handle ") + cdb.name);
+        close(newfd);
+
+        debug(cdb.fd, "tinycdb::open::fd");
+        cdb.st = st;
+        return cdb.fd;
+    }
+
+    // No change, just return the current fd
     return cdb.fd;
 }