greenthreads.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # greenthreads.py -- Utility module for querying an ObjectStore with gevent
  2. # Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
  3. #
  4. # Author: Fabien Boucher <fabien.boucher@enovance.com>
  5. #
  6. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  7. # General Public License as public by the Free Software Foundation; version 2.0
  8. # or (at your option) any later version. You can redistribute it and/or
  9. # modify it under the terms of either of these two licenses.
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # You should have received a copy of the licenses; if not, see
  18. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  19. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  20. # License, Version 2.0.
  21. #
  22. """Utility module for querying an ObjectStore with gevent."""
  23. import gevent
  24. from gevent import pool
  25. from dulwich.objects import (
  26. Commit,
  27. Tag,
  28. )
  29. from dulwich.object_store import (
  30. MissingObjectFinder,
  31. _collect_ancestors,
  32. _collect_filetree_revs,
  33. )
  34. def _split_commits_and_tags(obj_store, lst, *, ignore_unknown=False, pool=None):
  35. """Split object id list into two list with commit SHA1s and tag SHA1s.
  36. Same implementation as object_store._split_commits_and_tags
  37. except we use gevent to parallelize object retrieval.
  38. """
  39. commits = set()
  40. tags = set()
  41. def find_commit_type(sha):
  42. try:
  43. o = obj_store[sha]
  44. except KeyError:
  45. if not ignore_unknown:
  46. raise
  47. else:
  48. if isinstance(o, Commit):
  49. commits.add(sha)
  50. elif isinstance(o, Tag):
  51. tags.add(sha)
  52. commits.add(o.object[1])
  53. else:
  54. raise KeyError("Not a commit or a tag: %s" % sha)
  55. jobs = [pool.spawn(find_commit_type, s) for s in lst]
  56. gevent.joinall(jobs)
  57. return (commits, tags)
  58. class GreenThreadsMissingObjectFinder(MissingObjectFinder):
  59. """Find the objects missing from another object store.
  60. Same implementation as object_store.MissingObjectFinder
  61. except we use gevent to parallelize object retrieval.
  62. """
  63. def __init__(
  64. self,
  65. object_store,
  66. haves,
  67. wants,
  68. progress=None,
  69. get_tagged=None,
  70. concurrency=1,
  71. get_parents=None,
  72. ):
  73. def collect_tree_sha(sha):
  74. self.sha_done.add(sha)
  75. cmt = object_store[sha]
  76. _collect_filetree_revs(object_store, cmt.tree, self.sha_done)
  77. self.object_store = object_store
  78. p = pool.Pool(size=concurrency)
  79. have_commits, have_tags = _split_commits_and_tags(object_store, haves, ignore_unknown=True, pool=p)
  80. want_commits, want_tags = _split_commits_and_tags(object_store, wants, ignore_unknown=False, pool=p)
  81. all_ancestors = _collect_ancestors(object_store, have_commits)[0]
  82. missing_commits, common_commits = _collect_ancestors(
  83. object_store, want_commits, all_ancestors
  84. )
  85. self.sha_done = set()
  86. jobs = [p.spawn(collect_tree_sha, c) for c in common_commits]
  87. gevent.joinall(jobs)
  88. for t in have_tags:
  89. self.sha_done.add(t)
  90. missing_tags = want_tags.difference(have_tags)
  91. wants = missing_commits.union(missing_tags)
  92. self.objects_to_send = {(w, None, False) for w in wants}
  93. if progress is None:
  94. self.progress = lambda x: None
  95. else:
  96. self.progress = progress
  97. self._tagged = get_tagged and get_tagged() or {}