diff --git a/swh/storage/vault/api/cooking_tasks.py b/swh/storage/vault/api/cooking_tasks.py --- a/swh/storage/vault/api/cooking_tasks.py +++ b/swh/storage/vault/api/cooking_tasks.py @@ -21,6 +21,7 @@ storage = get_storage(**storage_args) cache = VaultCache(**cache_args) # Initialize cooker - cooker = COOKER_TYPES[type](storage, cache) + obj_id = hashutil.hash_to_bytes(hex_id) + cooker = COOKER_TYPES[type](storage, cache, obj_id) # Perform the cooking - cooker.cook(obj_id=hashutil.hash_to_bytes(hex_id)) + cooker.cook() diff --git a/swh/storage/vault/cookers/base.py b/swh/storage/vault/cookers/base.py --- a/swh/storage/vault/cookers/base.py +++ b/swh/storage/vault/cookers/base.py @@ -40,53 +40,56 @@ To define a new cooker, inherit from this class and override: - CACHE_TYPE_KEY: key to use for the bundle to reference in cache - - def cook(obj_id): cook the object into a bundle - - def notify_bundle_ready(notif_data, bundle_id): notify the + - def cook(): cook the object into a bundle + - def notify_bundle_ready(notif_data): notify the bundle is ready. """ CACHE_TYPE_KEY = None - def __init__(self, storage, cache): + def __init__(self, storage, cache, obj_id): + """Initialize the cooker. + + The type of the object represented by the id depends on the + concrete class. Very likely, each type of bundle will have its + own cooker class. + + Args: + storage: the storage object + cache: the cache where to store the bundle + obj_id: id of the object to be cooked into a bundle. + """ self.storage = storage self.cache = cache + self.obj_id = obj_id @abc.abstractmethod - def prepare_bundle(self, obj_id): + def prepare_bundle(self): """Implementation of the cooker. Returns the bundle bytes. Override this with the cooker implementation. """ raise NotImplemented - def cook(self, obj_id): + def cook(self): """Cook the requested object into a bundle - - The type of the object represented by the id depends on the - concrete class. Very likely, each type of bundle will have its - own cooker class. - - Args: - obj_id: id of the object to be cooked into a bundle. - """ - bundle_content = self.prepare_bundle(obj_id) + bundle_content = self.prepare_bundle() # Cache the bundle - self.update_cache(obj_id, bundle_content) + self.update_cache(bundle_content) # Make a notification that the bundle have been cooked # NOT YET IMPLEMENTED see TODO in function. self.notify_bundle_ready( - notif_data='Bundle %s ready' % hashutil.hash_to_hex(obj_id), - bundle_id=obj_id) + notif_data='Bundle %s ready' % hashutil.hash_to_hex(self.obj_id)) - def update_cache(self, id, bundle_content): + def update_cache(self, bundle_content): """Update the cache with id and bundle_content. """ - self.cache.add(self.CACHE_TYPE_KEY, id, bundle_content) + self.cache.add(self.CACHE_TYPE_KEY, self.obj_id, bundle_content) - def notify_bundle_ready(self, notif_data, bundle_id): + def notify_bundle_ready(self, notif_data): # TODO plug this method with the notification method once # done. pass diff --git a/swh/storage/vault/cookers/directory.py b/swh/storage/vault/cookers/directory.py --- a/swh/storage/vault/cookers/directory.py +++ b/swh/storage/vault/cookers/directory.py @@ -10,7 +10,7 @@ """Cooker to create a directory bundle """ CACHE_TYPE_KEY = 'directory' - def prepare_bundle(self, obj_id): + def prepare_bundle(self): """Cook the requested directory into a Bundle Args: @@ -21,4 +21,4 @@ """ directory_builder = DirectoryBuilder(self.storage) - return directory_builder.get_directory_bytes(obj_id) + return directory_builder.get_directory_bytes(self.obj_id) diff --git a/swh/storage/vault/cookers/revision_flat.py b/swh/storage/vault/cookers/revision_flat.py --- a/swh/storage/vault/cookers/revision_flat.py +++ b/swh/storage/vault/cookers/revision_flat.py @@ -15,12 +15,9 @@ """Cooker to create a directory bundle """ CACHE_TYPE_KEY = 'revision_flat' - def prepare_bundle(self, obj_id): + def prepare_bundle(self): """Cook the requested revision into a Bundle - Args: - obj_id (bytes): the id of the revision to be cooked. - Returns: bytes that correspond to the bundle @@ -28,9 +25,9 @@ directory_builder = DirectoryBuilder(self.storage) with tempfile.TemporaryDirectory(suffix='.cook') as root_tmp: root = Path(root_tmp) - for revision in self.storage.revision_log([obj_id]): + for revision in self.storage.revision_log([self.obj_id]): revdir = root / hashutil.hash_to_hex(revision['id']) revdir.mkdir() directory_builder.build_directory(revision['directory'], str(revdir).encode()) - return get_tar_bytes(root_tmp, hashutil.hash_to_hex(obj_id)) + return get_tar_bytes(root_tmp, hashutil.hash_to_hex(self.obj_id)) diff --git a/swh/storage/vault/cookers/revision_git.py b/swh/storage/vault/cookers/revision_git.py --- a/swh/storage/vault/cookers/revision_git.py +++ b/swh/storage/vault/cookers/revision_git.py @@ -13,8 +13,8 @@ """Cooker to create a git fast-import bundle """ CACHE_TYPE_KEY = 'revision_git' - def prepare_bundle(self, obj_id): - commands = self.fastexport(self.storage.revision_log([obj_id])) + def prepare_bundle(self): + commands = self.fastexport(self.storage.revision_log([self.obj_id])) bundle_content = b'\n'.join(bytes(command) for command in commands) return bundle_content