greenthreads.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 .object_store import (MissingObjectFinder, _collect_ancestors,
  26. _collect_filetree_revs)
  27. from .objects import Commit, Tag
  28. def _split_commits_and_tags(obj_store, lst, *, ignore_unknown=False, pool=None):
  29. """Split object id list into two list with commit SHA1s and tag SHA1s.
  30. Same implementation as object_store._split_commits_and_tags
  31. except we use gevent to parallelize object retrieval.
  32. """
  33. commits = set()
  34. tags = set()
  35. def find_commit_type(sha):
  36. try:
  37. o = obj_store[sha]
  38. except KeyError:
  39. if not ignore_unknown:
  40. raise
  41. else:
  42. if isinstance(o, Commit):
  43. commits.add(sha)
  44. elif isinstance(o, Tag):
  45. tags.add(sha)
  46. commits.add(o.object[1])
  47. else:
  48. raise KeyError("Not a commit or a tag: %s" % sha)
  49. jobs = [pool.spawn(find_commit_type, s) for s in lst]
  50. gevent.joinall(jobs)
  51. return (commits, tags)
  52. class GreenThreadsMissingObjectFinder(MissingObjectFinder):
  53. """Find the objects missing from another object store.
  54. Same implementation as object_store.MissingObjectFinder
  55. except we use gevent to parallelize object retrieval.
  56. """
  57. def __init__(
  58. self,
  59. object_store,
  60. haves,
  61. wants,
  62. progress=None,
  63. get_tagged=None,
  64. concurrency=1,
  65. get_parents=None,
  66. ):
  67. def collect_tree_sha(sha):
  68. self.sha_done.add(sha)
  69. cmt = object_store[sha]
  70. _collect_filetree_revs(object_store, cmt.tree, self.sha_done)
  71. self.object_store = object_store
  72. p = pool.Pool(size=concurrency)
  73. have_commits, have_tags = _split_commits_and_tags(object_store, haves, ignore_unknown=True, pool=p)
  74. want_commits, want_tags = _split_commits_and_tags(object_store, wants, ignore_unknown=False, pool=p)
  75. all_ancestors = _collect_ancestors(object_store, have_commits)[0]
  76. missing_commits, common_commits = _collect_ancestors(
  77. object_store, want_commits, all_ancestors
  78. )
  79. self.sha_done = set()
  80. jobs = [p.spawn(collect_tree_sha, c) for c in common_commits]
  81. gevent.joinall(jobs)
  82. for t in have_tags:
  83. self.sha_done.add(t)
  84. missing_tags = want_tags.difference(have_tags)
  85. wants = missing_commits.union(missing_tags)
  86. self.objects_to_send = {(w, None, False) for w in wants}
  87. if progress is None:
  88. self.progress = lambda x: None
  89. else:
  90. self.progress = progress
  91. self._tagged = get_tagged and get_tagged() or {}