-
Notifications
You must be signed in to change notification settings - Fork 2
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
♻️ Move generic hashing and storage utilities from lamindb
into lamindb-setup
#661
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8ec5f5c
:recycle: Move generic hashing utilities into lamindb-setup
bpenteado 393815c
:recycle: Move generic storage utilities from lamindb into lamindb-setup
bpenteado a9928bb
:adhesive_bandage: Fix redundant import
bpenteado 5e6ead7
:white_check_mark: Move hashing tests into lamindb-setup
bpenteado 377c5f5
:construction_worker: Checkout main branch in laminapp-ui
bpenteado 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
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,62 @@ | ||
"""Hashing. | ||
|
||
.. autosummary:: | ||
:toctree: . | ||
|
||
hash_set | ||
hash_file | ||
|
||
""" | ||
|
||
import base64 | ||
import hashlib | ||
from typing import List, Set, Tuple | ||
|
||
|
||
def to_b64_str(bstr: bytes): | ||
b64 = base64.urlsafe_b64encode(bstr).decode().strip("=") | ||
return b64 | ||
|
||
|
||
def b16_to_b64(s: str): | ||
return to_b64_str(base64.b16decode(s.strip('"'), casefold=True)) | ||
|
||
|
||
# a lot to read about this: lamin-notes/2022/hashing | ||
def hash_set(s: Set[str]) -> str: | ||
bstr = ":".join(sorted(s)).encode("utf-8") | ||
# as we're truncating at 20 b64, we choose md5 over sha512 | ||
return to_b64_str(hashlib.md5(bstr).digest())[:20] | ||
|
||
|
||
def hash_md5s_from_dir(etags: List[str]) -> Tuple[str, str]: | ||
# need to sort below because we don't want the order of parsing the dir to | ||
# affect the hash | ||
digests = b"".join( | ||
hashlib.md5(etag.encode("utf-8")).digest() for etag in sorted(etags) | ||
) | ||
digest = hashlib.md5(digests).digest() | ||
return to_b64_str(digest)[:22], "md5-d" | ||
|
||
|
||
def hash_file(file_path, chunk_size=50 * 1024 * 1024) -> Tuple[str, str]: | ||
chunks = [] | ||
with open(file_path, "rb") as fp: | ||
# read first chunk | ||
chunks = [fp.read(chunk_size)] | ||
# try reading the 2nd chunk | ||
data = fp.read(chunk_size) | ||
if data: | ||
# go to end of file | ||
fp.seek(-chunk_size, 2) | ||
# read last chunk | ||
data = fp.read(chunk_size) | ||
chunks.append(data) | ||
if len(chunks) == 1: | ||
digest = hashlib.md5(chunks[0]).digest() | ||
hash_type = "md5" | ||
else: | ||
digests = b"".join(hashlib.sha1(chunk).digest() for chunk in chunks) | ||
digest = hashlib.sha1(digests).digest() | ||
hash_type = "sha1-fl" # sha1 first last chunk | ||
return to_b64_str(digest)[:22], hash_type |
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,35 @@ | ||
import base64 | ||
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. I fear these tests don't executed - see the noxfile. I'll take care of this. 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. Fixed here: #666 |
||
from pathlib import Path | ||
|
||
from lamindb_setup.core.hashing import b16_to_b64, hash_file, to_b64_str | ||
|
||
|
||
def test_compute_hash(): | ||
files = [ | ||
# file_content, hash, chunk_size, hash_type | ||
("a", "DMF1ucDxtqgxw5niaXcmYQ", None, "md5"), | ||
("b", "kutf_uauL-w61xx3dTFXjw", None, "md5"), | ||
("abc", "kAFQmDzST7DWlj99KOF_cg", None, "md5"), | ||
("a", "DMF1ucDxtqgxw5niaXcmYQ", 1, "md5"), | ||
("b", "kutf_uauL-w61xx3dTFXjw", 1, "md5"), | ||
# the last case here triggers multi-chunk compute with sha1 | ||
("abc", "p0EbDbQEP1wS-Tw6TuBjKS", 1, "sha1-fl"), | ||
] | ||
for content, hash, chunk_size, hash_type in files: | ||
filepath = Path("file_1") | ||
filepath.write_text(content) | ||
computed_hash, computed_hash_type = hash_file(filepath, chunk_size=chunk_size) | ||
assert computed_hash == hash | ||
assert computed_hash_type == hash_type | ||
filepath.unlink() | ||
|
||
|
||
def test_base64(): | ||
mytest = b"test" | ||
b64_str = to_b64_str(mytest) | ||
b64_str_padded = f"{b64_str}==" | ||
assert base64.urlsafe_b64decode(b64_str_padded.encode()).hex() == mytest.hex() | ||
|
||
|
||
def test_b16_to_b64(): | ||
assert b16_to_b64("9b89c8c1acf79dba5b5341d1fff9806f") == "m4nIwaz3nbpbU0HR__mAbw" |
Oops, something went wrong.
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.
We need tests for this, @bpenteado!
In lamindb, this was covered, now, I fear, it's uncovered by CI.
My guess is that there were no fine-grained unit tests in lamindb for this, but coverage came through more high-level functionality.
Given this is in lamindb-setup now and the interfaces are used by laminhub and lamindb, we need a more fine-grained test that reflects needs in both downstream repos (they should be the same if we design the code well).
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.
Here is an issue for this: #665