-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dvc.data: save and try loading raw dir objects #7597
Merged
efiop
merged 1 commit into
iterative:main
from
dtrifiro:fix/7390-status-recalculating-hashes
May 12, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import errno | ||
import hashlib | ||
import logging | ||
import os | ||
|
@@ -19,6 +20,9 @@ | |
from dvc.fs.base import AnyFSPath, FileSystem | ||
from dvc.objects.db import ObjectDB | ||
|
||
from .tree import Tree | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
|
@@ -233,7 +237,7 @@ def _make_staging_url( | |
return url | ||
|
||
|
||
def _get_staging(odb: "ObjectDB") -> "ObjectDB": | ||
def _get_staging(odb: "ObjectDB") -> "ReferenceObjectDB": | ||
"""Return an ODB that can be used for staging objects. | ||
|
||
Staging will be a reference ODB stored in the the global memfs. | ||
|
@@ -247,27 +251,90 @@ def _get_staging(odb: "ObjectDB") -> "ObjectDB": | |
return ReferenceObjectDB(fs, fs_path, state=state) | ||
|
||
|
||
def _load_from_state(odb, staging, fs_path, fs, name): | ||
def _load_raw_dir_obj(odb: "ObjectDB", hash_info: "HashInfo") -> "Tree": | ||
from dvc.objects.errors import ObjectFormatError | ||
|
||
from .tree import Tree | ||
|
||
try: | ||
tree = Tree.load(odb, hash_info.as_raw()) | ||
tree.check(odb) | ||
tree.hash_info = hash_info | ||
except ObjectFormatError as exc: | ||
raise FileNotFoundError( | ||
errno.ENOENT, | ||
"No such object", | ||
odb.hash_to_path(hash_info.as_raw().value), | ||
) from exc | ||
|
||
return tree | ||
|
||
|
||
def _load_from_state( | ||
odb: "ObjectDB", | ||
staging: "ReferenceObjectDB", | ||
fs_path: "AnyFSPath", | ||
fs: "FileSystem", | ||
name: str, | ||
dry_run: bool, | ||
) -> Tuple["ObjectDB", "Meta", "HashFile"]: | ||
from dvc.objects.errors import ObjectFormatError | ||
|
||
from . import check, load | ||
from .tree import Tree | ||
|
||
state = odb.state | ||
meta, hash_info = state.get(fs_path, fs) | ||
if hash_info: | ||
for odb_ in (odb, staging): | ||
if odb_.exists(hash_info): | ||
try: | ||
obj = load(odb_, hash_info) | ||
check(odb_, obj, check_hash=False) | ||
if isinstance(obj, Tree): | ||
meta.nfiles = len(obj) | ||
assert obj.hash_info.name == name | ||
return odb_, meta, obj | ||
except (ObjectFormatError, FileNotFoundError): | ||
pass | ||
raise FileNotFoundError | ||
if not hash_info: | ||
raise FileNotFoundError | ||
|
||
for odb_ in (odb, staging): | ||
if not odb_.exists(hash_info): | ||
continue | ||
|
||
try: | ||
obj = load(odb, hash_info) | ||
check(odb, obj, check_hash=False) | ||
Comment on lines
+296
to
+297
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw, this PR introduced a typo: it should be |
||
except (ObjectFormatError, FileNotFoundError): | ||
continue | ||
|
||
if isinstance(obj, Tree): | ||
meta.nfiles = len(obj) | ||
assert obj.hash_info.name == name | ||
return odb_, meta, obj | ||
|
||
if not hash_info.isdir: | ||
raise FileNotFoundError | ||
|
||
# Try loading the raw dir object saved by `stage`, see below and #7390 | ||
tree = _load_raw_dir_obj(odb, hash_info) | ||
meta.nfiles = len(tree) | ||
assert tree.hash_info.name == name | ||
|
||
if not dry_run: | ||
assert tree.fs | ||
for key, _, oid in tree: | ||
staging.add( | ||
fs.path.join(fs_path, *key), | ||
fs, | ||
oid, | ||
hardlink=False, | ||
verify=False, | ||
) | ||
|
||
staging.add( | ||
tree.fs_path, | ||
tree.fs, | ||
hash_info, | ||
hardlink=False, | ||
) | ||
|
||
raw = staging.get(hash_info) | ||
tree.fs = raw.fs | ||
tree.fs_path = raw.fs_path | ||
|
||
logger.debug("loaded tree '%s' from raw dir obj", tree) | ||
return staging, meta, tree | ||
|
||
|
||
def _stage_external_tree_info(odb, tree, name): | ||
|
@@ -318,7 +385,7 @@ def stage( | |
staging = _get_staging(odb) | ||
if odb: | ||
try: | ||
return _load_from_state(odb, staging, fs_path, fs, name) | ||
return _load_from_state(odb, staging, fs_path, fs, name, dry_run) | ||
except FileNotFoundError: | ||
pass | ||
|
||
|
@@ -336,6 +403,12 @@ def stage( | |
logger.debug("staged tree '%s'", obj) | ||
if name != "md5": | ||
obj = _stage_external_tree_info(odb, obj, name) | ||
|
||
# In order to avoid re-building the tree when it is not committed to | ||
# the local odb (e.g. for a status call), we save it as a raw object. | ||
# Loading this instead of building the tree can speed up `dvc status` | ||
# for modified directories, see #7390 | ||
odb.add(obj.fs_path, obj.fs, obj.hash_info.as_raw()) | ||
efiop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else: | ||
_, meta, obj = _stage_file( | ||
fs_path, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from dvc.objects.hash_info import HashInfo | ||
|
||
|
||
def test_as_raw(): | ||
hash_info = HashInfo( | ||
"md5", "a1d0c6e83f027327d8461063f4ac58a6.dir", "objname" | ||
) | ||
|
||
raw = hash_info.as_raw() | ||
|
||
assert hash_info.name == "md5" | ||
assert hash_info.value == "a1d0c6e83f027327d8461063f4ac58a6.dir" | ||
assert hash_info.obj_name == "objname" | ||
|
||
assert raw.name == "md5" | ||
assert raw.value == "a1d0c6e83f027327d8461063f4ac58a6" | ||
assert raw.obj_name == "objname" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this causing mypy complaints? Seems like this change is unrelated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep: it is required because since I added type hints to
_load_from_state()