{"action":"created","comment":{"url":"https://api.github.com/repos/lsst/qserv/pulls/comments/189135585","pull_request_review_id":121257280,"id":189135585,"diff_hunk":"@@ -0,0 +1,183 @@\n+/*\n+ * LSST Data Management System\n+ * Copyright 2017 LSST Corporation.\n+ *\n+ * This product includes software developed by the\n+ * LSST Project (http://www.lsst.org/).\n+ *\n+ * This program is free software: you can redistribute it and/or modify\n+ * it under the terms of the GNU General Public License as published by\n+ * the Free Software Foundation, either version 3 of the License, or\n+ * (at your option) any later version.\n+ *\n+ * This program is distributed in the hope that it will be useful,\n+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\n+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+ * GNU General Public License for more details.\n+ *\n+ * You should have received a copy of the LSST License Statement and\n+ * the GNU General Public License along with this program.  If not,\n+ * see <http://www.lsstcorp.org/LegalNotices/>.\n+ */\n+\n+// Class header\n+#include \"replica/ChunkLocker.h\"\n+\n+// System headers\n+#include <stdexcept>\n+#include <tuple>        // std::tie\n+\n+// This macro to appear witin each block which requires thread safety\n+#define LOCK_GUARD std::lock_guard<std::mutex> lock(_mtx)\n+\n+// Qserv headers\n+\n+namespace lsst {\n+namespace qserv {\n+namespace replica {\n+\n+///////////////////////////////////////\n+//                Chunk              //\n+///////////////////////////////////////\n+\n+bool operator==(Chunk const& lhs,\n+                Chunk const& rhs) {\n+    return  std::tie(lhs.databaseFamily, lhs.number) ==\n+            std::tie(rhs.databaseFamily, rhs.number);\n+}\n+\n+bool operator<(Chunk const& lhs,\n+               Chunk const& rhs) {\n+    return  std::tie(lhs.databaseFamily, lhs.number) <\n+            std::tie(rhs.databaseFamily, rhs.number);\n+}\n+\n+std::ostream& operator<<(std::ostream& os, Chunk const& chunk) {\n+    os  << \"Chunk (\" << chunk.databaseFamily << \":\" << chunk.number << \")\";\n+    return os;\n+}\n+\n+/////////////////////////////////////////////\n+//                ChunkLocker              //\n+/////////////////////////////////////////////\n+\n+bool ChunkLocker::isLocked(Chunk const& chunk) const {\n+    LOCK_GUARD;\n+    return _chunk2owner.count(chunk);\n+}\n+\n+bool ChunkLocker::isLocked(Chunk const& chunk,\n+                           std::string& owner) const {\n+\n+    LOCK_GUARD;\n+\n+    if (_chunk2owner.count(chunk)) {\n+        owner = _chunk2owner.at(chunk);\n+        return true;\n+    }\n+    return false;\n+}\n+\n+ChunkLocker::ChunksByOwners ChunkLocker::locked(std::string const& owner) const {\n+\n+    if (owner.empty()) { return _owner2chunks; }\n+\n+    ChunksByOwners owner2chunks;\n+    if (_owner2chunks.count(owner)) {\n+        owner2chunks[owner] = _owner2chunks.at(owner);\n+    }\n+    return owner2chunks;\n+}\n+\n+bool ChunkLocker::lock(Chunk const&       chunk,\n+                       std::string const& owner) {\n+    LOCK_GUARD;\n+\n+    if (owner.empty()) {\n+        throw std::invalid_argument(\"ChunkLocker::lock  empty owner\");\n+    }\n+    if (_chunk2owner.count(chunk)) {\n+        return owner == _chunk2owner.at(chunk);\n+    }\n+    _chunk2owner [chunk] = owner;\n+    _owner2chunks[owner].push_back(chunk);\n+\n+    return true;\n+}\n+\n+bool ChunkLocker::release(Chunk const& chunk) {\n+\n+    LOCK_GUARD;\n+\n+    // An owner (if set) will be ignored by the current method\n+    std::string owner;\n+    return releaseImpl(chunk, owner);\n+}\n+\n+bool ChunkLocker::release(Chunk const& chunk,\n+                          std::string& owner) {\n+    LOCK_GUARD;\n+    return releaseImpl(chunk, owner);\n+}\n+\n+bool ChunkLocker::releaseImpl(Chunk const& chunk,\n+                              std::string& owner) {\n+\n+    if (!_chunk2owner.count(chunk)) { return false; }\n+\n+    // Remove the chunk from this map _only_ after getting its owner\n+    owner = _chunk2owner.at(chunk);\n+    _chunk2owner.erase(chunk);\n+\n+    // Remove the chunk from the list of all chunks claimed by that particular\n+    // owner as well.\n+    std::list<Chunk>& chunks = _owner2chunks.at(owner);\n+    chunks.remove(chunk);\n+\n+    // This last step is needed to avoid building up empty lists\n+    // of non-existing owners\n+    if (chunks.empty()) { _owner2chunks.erase(owner); }\n+\n+    return true;\n+}\n+\n+std::vector<Chunk> ChunkLocker::release(std::string const& owner) {\n+\n+    LOCK_GUARD;\n+\n+    if (owner.empty()) {\n+        throw std::invalid_argument(\"ChunkLocker::release  empty owner\");\n+    }\n+\n+    // First get all chunks claimed by the specified owner.\n+    // This list s also going to be returned o teh cller.\n+    std::vector<Chunk> chunks;\n+    if (_owner2chunks.count(owner)) {\n+        for (auto&& chunk: _owner2chunks.at(owner)) {\n+            chunks.push_back(chunk);\n+        }\n+    }\n+\n+    // Then release the select chunks\n+    for (auto&& chunk: chunks) {\n+        release(chunk);\n+    }\n+    return chunks;\n+}\n+\n+//////////////////////////////////////\n+//               misc               //\n+//////////////////////////////////////\n+\n+std::ostream& operator <<(std::ostream& os, ChunkLocker::ChunksByOwners const& chunks) {","path":"core/modules/replica/ChunkLocker.cc","position":172,"original_position":172,"commit_id":"73970cbe018f340305bf09c072155e5af3a94159","original_commit_id":"c2de524632a6d49eab756f26eddd9a42546e32d5","user":{"login":"n8pease","id":13370548,"avatar_url":"https://avatars0.githubusercontent.com/u/13370548?v=4","gravatar_id":"","url":"https://api.github.com/users/n8pease","html_url":"https://github.com/n8pease","followers_url":"https://api.github.com/users/n8pease/followers","following_url":"https://api.github.com/users/n8pease/following{/other_user}","gists_url":"https://api.github.com/users/n8pease/gists{/gist_id}","starred_url":"https://api.github.com/users/n8pease/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/n8pease/subscriptions","organizations_url":"https://api.github.com/users/n8pease/orgs","repos_url":"https://api.github.com/users/n8pease/repos","events_url":"https://api.github.com/users/n8pease/events{/privacy}","received_events_url":"https://api.github.com/users/n8pease/received_events","type":"User","site_admin":false},"body":"I don't see where this is used...?","created_at":"2018-05-18T00:15:38Z","updated_at":"2018-05-18T00:15:38Z","html_url":"https://github.com/lsst/qserv/pull/393#discussion_r189135585","pull_request_url":"https://api.github.com/repos/lsst/qserv/pulls/393","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/lsst/qserv/pulls/comments/189135585"},"html":{"href":"https://github.com/lsst/qserv/pull/393#discussion_r189135585"},"pull_request":{"href":"https://api.github.com/repos/lsst/qserv/pulls/393"}},"in_reply_to_id":185366393},"pull_request":{"url":"https://api.github.com/repos/lsst/qserv/pulls/393","id":185042944,"html_url":"https://github.com/lsst/qserv/pull/393","diff_url":"https://github.com/lsst/qserv/pull/393.diff","patch_url":"https://github.com/lsst/qserv/pull/393.patch","issue_url":"https://api.github.com/repos/lsst/qserv/issues/393","number":393,"state":"open","locked":false,"title":"Tickets/dm 10424","user":{"login":"iagaponenko","id":14188390,"avatar_url":"https://avatars3.githubusercontent.com/u/14188390?v=4","gravatar_id":"","url":"https://api.github.com/users/iagaponenko","html_url":"https://github.com/iagaponenko","followers_url":"https://api.github.com/users/iagaponenko/followers","following_url":"https://api.github.com/users/iagaponenko/following{/other_user}","gists_url":"https://api.github.com/users/iagaponenko/gists{/gist_id}","starred_url":"https://api.github.com/users/iagaponenko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iagaponenko/subscriptions","organizations_url":"https://api.github.com/users/iagaponenko/orgs","repos_url":"https://api.github.com/users/iagaponenko/repos","events_url":"https://api.github.com/users/iagaponenko/events{/privacy}","received_events_url":"https://api.github.com/users/iagaponenko/received_events","type":"User","site_admin":false},"body":"","created_at":"2018-04-30T19:29:59Z","updated_at":"2018-05-18T00:15:38Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e70956fc6e24bee54721b2d45f287f6da1f30b6b","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/lsst/qserv/pulls/393/commits","review_comments_url":"https://api.github.com/repos/lsst/qserv/pulls/393/comments","review_comment_url":"https://api.github.com/repos/lsst/qserv/pulls/comments{/number}","comments_url":"https://api.github.com/repos/lsst/qserv/issues/393/comments","statuses_url":"https://api.github.com/repos/lsst/qserv/statuses/73970cbe018f340305bf09c072155e5af3a94159","head":{"label":"lsst:tickets/DM-10424","ref":"tickets/DM-10424","sha":"73970cbe018f340305bf09c072155e5af3a94159","user":{"login":"lsst","id":1134504,"avatar_url":"https://avatars1.githubusercontent.com/u/1134504?v=4","gravatar_id":"","url":"https://api.github.com/users/lsst","html_url":"https://github.com/lsst","followers_url":"https://api.github.com/users/lsst/followers","following_url":"https://api.github.com/users/lsst/following{/other_user}","gists_url":"https://api.github.com/users/lsst/gists{/gist_id}","starred_url":"https://api.github.com/users/lsst/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lsst/subscriptions","organizations_url":"https://api.github.com/users/lsst/orgs","repos_url":"https://api.github.com/users/lsst/repos","events_url":"https://api.github.com/users/lsst/events{/privacy}","received_events_url":"https://api.github.com/users/lsst/received_events","type":"Organization","site_admin":false},"repo":{"id":23013460,"name":"qserv","full_name":"lsst/qserv","owner":{"login":"lsst","id":1134504,"avatar_url":"https://avatars1.githubusercontent.com/u/1134504?v=4","gravatar_id":"","url":"https://api.github.com/users/lsst","html_url":"https://github.com/lsst","followers_url":"https://api.github.com/users/lsst/followers","following_url":"https://api.github.com/users/lsst/following{/other_user}","gists_url":"https://api.github.com/users/lsst/gists{/gist_id}","starred_url":"https://api.github.com/users/lsst/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lsst/subscriptions","organizations_url":"https://api.github.com/users/lsst/orgs","repos_url":"https://api.github.com/users/lsst/repos","events_url":"https://api.github.com/users/lsst/events{/privacy}","received_events_url":"https://api.github.com/users/lsst/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/lsst/qserv","description":"LSST Query Services","fork":false,"url":"https://api.github.com/repos/lsst/qserv","forks_url":"https://api.github.com/repos/lsst/qserv/forks","keys_url":"https://api.github.com/repos/lsst/qserv/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lsst/qserv/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lsst/qserv/teams","hooks_url":"https://api.github.com/repos/lsst/qserv/hooks","issue_events_url":"https://api.github.com/repos/lsst/qserv/issues/events{/number}","events_url":"https://api.github.com/repos/lsst/qserv/events","assignees_url":"https://api.github.com/repos/lsst/qserv/assignees{/user}","branches_url":"https://api.github.com/repos/lsst/qserv/branches{/branch}","tags_url":"https://api.github.com/repos/lsst/qserv/tags","blobs_url":"https://api.github.com/repos/lsst/qserv/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lsst/qserv/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lsst/qserv/git/refs{/sha}","trees_url":"https://api.github.com/repos/lsst/qserv/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lsst/qserv/statuses/{sha}","languages_url":"https://api.github.com/repos/lsst/qserv/languages","stargazers_url":"https://api.github.com/repos/lsst/qserv/stargazers","contributors_url":"https://api.github.com/repos/lsst/qserv/contributors","subscribers_url":"https://api.github.com/repos/lsst/qserv/subscribers","subscription_url":"https://api.github.com/repos/lsst/qserv/subscription","commits_url":"https://api.github.com/repos/lsst/qserv/commits{/sha}","git_commits_url":"https://api.github.com/repos/lsst/qserv/git/commits{/sha}","comments_url":"https://api.github.com/repos/lsst/qserv/comments{/number}","issue_comment_url":"https://api.github.com/repos/lsst/qserv/issues/comments{/number}","contents_url":"https://api.github.com/repos/lsst/qserv/contents/{+path}","compare_url":"https://api.github.com/repos/lsst/qserv/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lsst/qserv/merges","archive_url":"https://api.github.com/repos/lsst/qserv/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lsst/qserv/downloads","issues_url":"https://api.github.com/repos/lsst/qserv/issues{/number}","pulls_url":"https://api.github.com/repos/lsst/qserv/pulls{/number}","milestones_url":"https://api.github.com/repos/lsst/qserv/milestones{/number}","notifications_url":"https://api.github.com/repos/lsst/qserv/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lsst/qserv/labels{/name}","releases_url":"https://api.github.com/repos/lsst/qserv/releases{/id}","deployments_url":"https://api.github.com/repos/lsst/qserv/deployments","created_at":"2014-08-16T07:50:36Z","updated_at":"2018-05-14T20:25:15Z","pushed_at":"2018-05-17T22:15:11Z","git_url":"git://github.com/lsst/qserv.git","ssh_url":"git@github.com:lsst/qserv.git","clone_url":"https://github.com/lsst/qserv.git","svn_url":"https://github.com/lsst/qserv","homepage":"http://dm.lsst.org/","size":19161,"stargazers_count":18,"watchers_count":18,"language":"C++","has_issues":false,"has_projects":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":13,"mirror_url":null,"archived":false,"open_issues_count":10,"license":null,"forks":13,"open_issues":10,"watchers":18,"default_branch":"master"}},"base":{"label":"lsst:master","ref":"master","sha":"010f04c04afaea73c0f182b0d65dd981896f8898","user":{"login":"lsst","id":1134504,"avatar_url":"https://avatars1.githubusercontent.com/u/1134504?v=4","gravatar_id":"","url":"https://api.github.com/users/lsst","html_url":"https://github.com/lsst","followers_url":"https://api.github.com/users/lsst/followers","following_url":"https://api.github.com/users/lsst/following{/other_user}","gists_url":"https://api.github.com/users/lsst/gists{/gist_id}","starred_url":"https://api.github.com/users/lsst/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lsst/subscriptions","organizations_url":"https://api.github.com/users/lsst/orgs","repos_url":"https://api.github.com/users/lsst/repos","events_url":"https://api.github.com/users/lsst/events{/privacy}","received_events_url":"https://api.github.com/users/lsst/received_events","type":"Organization","site_admin":false},"repo":{"id":23013460,"name":"qserv","full_name":"lsst/qserv","owner":{"login":"lsst","id":1134504,"avatar_url":"https://avatars1.githubusercontent.com/u/1134504?v=4","gravatar_id":"","url":"https://api.github.com/users/lsst","html_url":"https://github.com/lsst","followers_url":"https://api.github.com/users/lsst/followers","following_url":"https://api.github.com/users/lsst/following{/other_user}","gists_url":"https://api.github.com/users/lsst/gists{/gist_id}","starred_url":"https://api.github.com/users/lsst/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lsst/subscriptions","organizations_url":"https://api.github.com/users/lsst/orgs","repos_url":"https://api.github.com/users/lsst/repos","events_url":"https://api.github.com/users/lsst/events{/privacy}","received_events_url":"https://api.github.com/users/lsst/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/lsst/qserv","description":"LSST Query Services","fork":false,"url":"https://api.github.com/repos/lsst/qserv","forks_url":"https://api.github.com/repos/lsst/qserv/forks","keys_url":"https://api.github.com/repos/lsst/qserv/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lsst/qserv/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lsst/qserv/teams","hooks_url":"https://api.github.com/repos/lsst/qserv/hooks","issue_events_url":"https://api.github.com/repos/lsst/qserv/issues/events{/number}","events_url":"https://api.github.com/repos/lsst/qserv/events","assignees_url":"https://api.github.com/repos/lsst/qserv/assignees{/user}","branches_url":"https://api.github.com/repos/lsst/qserv/branches{/branch}","tags_url":"https://api.github.com/repos/lsst/qserv/tags","blobs_url":"https://api.github.com/repos/lsst/qserv/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lsst/qserv/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lsst/qserv/git/refs{/sha}","trees_url":"https://api.github.com/repos/lsst/qserv/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lsst/qserv/statuses/{sha}","languages_url":"https://api.github.com/repos/lsst/qserv/languages","stargazers_url":"https://api.github.com/repos/lsst/qserv/stargazers","contributors_url":"https://api.github.com/repos/lsst/qserv/contributors","subscribers_url":"https://api.github.com/repos/lsst/qserv/subscribers","subscription_url":"https://api.github.com/repos/lsst/qserv/subscription","commits_url":"https://api.github.com/repos/lsst/qserv/commits{/sha}","git_commits_url":"https://api.github.com/repos/lsst/qserv/git/commits{/sha}","comments_url":"https://api.github.com/repos/lsst/qserv/comments{/number}","issue_comment_url":"https://api.github.com/repos/lsst/qserv/issues/comments{/number}","contents_url":"https://api.github.com/repos/lsst/qserv/contents/{+path}","compare_url":"https://api.github.com/repos/lsst/qserv/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lsst/qserv/merges","archive_url":"https://api.github.com/repos/lsst/qserv/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lsst/qserv/downloads","issues_url":"https://api.github.com/repos/lsst/qserv/issues{/number}","pulls_url":"https://api.github.com/repos/lsst/qserv/pulls{/number}","milestones_url":"https://api.github.com/repos/lsst/qserv/milestones{/number}","notifications_url":"https://api.github.com/repos/lsst/qserv/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lsst/qserv/labels{/name}","releases_url":"https://api.github.com/repos/lsst/qserv/releases{/id}","deployments_url":"https://api.github.com/repos/lsst/qserv/deployments","created_at":"2014-08-16T07:50:36Z","updated_at":"2018-05-14T20:25:15Z","pushed_at":"2018-05-17T22:15:11Z","git_url":"git://github.com/lsst/qserv.git","ssh_url":"git@github.com:lsst/qserv.git","clone_url":"https://github.com/lsst/qserv.git","svn_url":"https://github.com/lsst/qserv","homepage":"http://dm.lsst.org/","size":19161,"stargazers_count":18,"watchers_count":18,"language":"C++","has_issues":false,"has_projects":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":13,"mirror_url":null,"archived":false,"open_issues_count":10,"license":null,"forks":13,"open_issues":10,"watchers":18,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/lsst/qserv/pulls/393"},"html":{"href":"https://github.com/lsst/qserv/pull/393"},"issue":{"href":"https://api.github.com/repos/lsst/qserv/issues/393"},"comments":{"href":"https://api.github.com/repos/lsst/qserv/issues/393/comments"},"review_comments":{"href":"https://api.github.com/repos/lsst/qserv/pulls/393/comments"},"review_comment":{"href":"https://api.github.com/repos/lsst/qserv/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/lsst/qserv/pulls/393/commits"},"statuses":{"href":"https://api.github.com/repos/lsst/qserv/statuses/73970cbe018f340305bf09c072155e5af3a94159"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"n8pease"}}