about summary refs log tree commit diff
path: root/src/libstore/local-store.cc
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2010-02-18T16·21+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2010-02-18T16·21+0000
commit836e5b6f5741c845da08490a6fd8ad9a3f37726e (patch)
treecd977d8902869b522b9913b716f3db76d1cd611c /src/libstore/local-store.cc
parent77cb9e3fb19005a40724cb42773d2b11e2528858 (diff)
* Implemented queryReferrers().
Diffstat (limited to 'src/libstore/local-store.cc')
-rw-r--r--src/libstore/local-store.cc48
1 files changed, 18 insertions, 30 deletions
diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc
index 76911da67c9c..f1deb2eae270 100644
--- a/src/libstore/local-store.cc
+++ b/src/libstore/local-store.cc
@@ -167,6 +167,9 @@ LocalStore::LocalStore()
 
     if (sqlite3_busy_timeout(db, 60000) != SQLITE_OK)
         throw SQLiteError(db, "setting timeout");
+
+    /* !!! check whether sqlite has been built with foreign key
+       support */
     
     /* Check the current database schema and if necessary do an
        upgrade.  !!! Race condition: several processes could start
@@ -240,6 +243,8 @@ void LocalStore::prepareStatements()
         "select id, hash, registrationTime, deriver from ValidPaths where path = ?;");
     stmtQueryReferences.create(db,
         "select path from Refs join ValidPaths on reference = id where referrer = ?;");
+    stmtQueryReferrers.create(db,
+        "select path from Refs join ValidPaths on referrer = id where reference = (select id from ValidPaths where path = ?);");
 }
 
 
@@ -498,41 +503,24 @@ void LocalStore::queryReferences(const Path & path,
 }
 
 
-bool LocalStore::queryReferrersInternal(const Path & path, PathSet & referrers)
+void LocalStore::queryReferrers(const Path & path, PathSet & referrers)
 {
-#if 0
-    bool allValid = true;
-    
-    if (!isValidPath(path))
-        throw Error(format("path `%1%' is not valid") % path);
-
-    /* No locking is necessary here: updates are only done by
-       appending or by atomically replacing the file.  When appending,
-       there is a possibility that we see a partial entry, but it will
-       just be filtered out below (the partially written path will not
-       be valid, so it will be ignored). */
-
-    Path referrersFile = referrersFileFor(path);
-    if (!pathExists(referrersFile)) return true;
-    
-    AutoCloseFD fd = open(referrersFile.c_str(), O_RDONLY);
-    if (fd == -1) throw SysError(format("opening file `%1%'") % referrersFile);
-
-    Paths refs = tokenizeString(readFile(fd), " ");
+    assertStorePath(path);
 
-    foreach (Paths::iterator, i, refs)
-        /* Referrers can be invalid (see registerValidPath() for the
-           invariant), so we only return one if it is valid. */
-        if (isStorePath(*i) && isValidPath(*i)) referrers.insert(*i); else allValid = false;
+    stmtQueryReferrers.reset();
 
-    return allValid;
-#endif
-}
+    if (sqlite3_bind_text(stmtQueryReferrers, 1, path.c_str(), -1, SQLITE_TRANSIENT) != SQLITE_OK)
+        throw SQLiteError(db, "binding argument");
 
+    int r;
+    while ((r = sqlite3_step(stmtQueryReferrers)) == SQLITE_ROW) {
+        const char * s = (const char *) sqlite3_column_text(stmtQueryReferrers, 0);
+        assert(s);
+        referrers.insert(s);
+    }
 
-void LocalStore::queryReferrers(const Path & path, PathSet & referrers)
-{
-    queryReferrersInternal(path, referrers);
+    if (r != SQLITE_DONE)
+        throw Error(format("error getting references of `%1%'") % path);
 }