From 9ce834822ed625512913e14b83c50487d4b2c12b Mon Sep 17 00:00:00 2001 From: Kun Yi Date: Thu, 14 Nov 2019 13:08:39 -0800 Subject: cleanup: Operate on members directly and allow empty paths in the lookup table Instead of using private methods to change data, directly operate on it. Currently no test depends on these methods. There is also a minor bug fix: the previous code will not increment or decrement the open session count if the blobId is an empty string, however there is no check on the manager side. If empty blobIds are disallowed they should be checked earlier. Signed-off-by: Kun Yi Change-Id: I8d97e098bed2d5c27a204a86ed688d0d9a98f0a2 --- manager.cpp | 55 +++++++++---------------------------------------------- manager.hpp | 4 ---- 2 files changed, 9 insertions(+), 50 deletions(-) diff --git a/manager.cpp b/manager.cpp index 286b81a..15ff083 100644 --- a/manager.cpp +++ b/manager.cpp @@ -25,55 +25,18 @@ namespace blobs { -void BlobManager::incrementOpen(const std::string& path) -{ - if (path.empty()) - { - return; - } - - openFiles[path] += 1; -} - -void BlobManager::decrementOpen(const std::string& path) -{ - if (path.empty()) - { - return; - } - - /* TODO(venture): Check into the iterator from find, does it makes sense - * to just update it directly? */ - auto entry = openFiles.find(path); - if (entry != openFiles.end()) - { - /* found it, decrement it and remove it if 0. */ - openFiles[path] -= 1; - if (openFiles[path] == 0) - { - openFiles.erase(path); - } - } -} - -int BlobManager::getOpen(const std::string& path) const -{ - /* No need to input check on the read-only call. */ - auto entry = openFiles.find(path); - if (entry != openFiles.end()) - { - return entry->second; - } - - return 0; -} - void BlobManager::eraseSession(GenericBlobInterface* handler, uint16_t session) { sessions.erase(session); /* Ok for openSessions[handler] to be an empty set */ openSessions[handler].erase(session); - decrementOpen(getPath(session)); + + auto path = getPath(session); + openFiles[path]--; + if (openFiles[path] == 0) + { + openFiles.erase(path); + } } void BlobManager::cleanUpStaleSessions(GenericBlobInterface* handler) @@ -189,7 +152,7 @@ bool BlobManager::open(uint16_t flags, const std::string& path, /* Associate session with handler */ sessions[*session] = SessionInfo(path, handler, flags); openSessions[handler].insert(*session); - incrementOpen(path); + openFiles[path]++; return true; } @@ -310,7 +273,7 @@ bool BlobManager::deleteBlob(const std::string& path) } /* Check if the file has any open handles. */ - if (getOpen(path) > 0) + if (openFiles[path] > 0) { return false; } diff --git a/manager.hpp b/manager.hpp index 011fe88..cba7935 100644 --- a/manager.hpp +++ b/manager.hpp @@ -276,10 +276,6 @@ class BlobManager : public ManagerInterface std::string getPath(uint16_t session) const; private: - void incrementOpen(const std::string& path); - void decrementOpen(const std::string& path); - int getOpen(const std::string& path) const; - /* Helper method to erase a session from all maps */ void eraseSession(GenericBlobInterface* handler, uint16_t session); /* For each session owned by this handler, call expire if it is stale */ -- cgit v1.2.3