Skip to content
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

Make records hashable #107

Merged
merged 4 commits into from
Mar 7, 2024

Conversation

JSCU-CNI
Copy link
Contributor

This PR implements the __hash__ functions for the Record class, enabling grouping records in sets. This enables set operations on collections of records, e.g:

records_a = set(target_one.users())
records_b = set(target_two.users())

diff = records_a.symmetric_difference(records_b)

For some record types though, there will be fields that you might want to ignore, such as _generated and possibly _source when comparing record output from two different targets. Preferably you want the fields to ignore to be configurable at runtime. Therefore, we opted to add an environment variable FLOW_RECORD_IGNORE which can contain a list of comma-separated field names to ignore.

We implemented a check where hashed records are considered 'immutable' and a ValueError is raised if a property is changed after __hash__ has been called on a Record instance. This may have some performance impact though. Feel free to suggest alternative approaches.

Copy link

codecov bot commented Feb 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.46%. Comparing base (523b96c) to head (71e7a16).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #107      +/-   ##
==========================================
+ Coverage   80.41%   80.46%   +0.05%     
==========================================
  Files          34       34              
  Lines        3165     3174       +9     
==========================================
+ Hits         2545     2554       +9     
  Misses        620      620              
Flag Coverage Δ
unittests 80.46% <100.00%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@yunzheng yunzheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the _mutable flag/field, I think it's best if we just treat it as how dataclasses works when using unsafe_hash=True, hashable but still mutable:

In [1]: from dataclasses import dataclass
   ...:
   ...: @dataclass(unsafe_hash=True)
   ...: class MyRecord:
   ...:     foo: str
   ...:     bar: int
   ...:

In [2]: a = MyRecord("a", 1337)

In [3]: b = MyRecord("b", 1337)

In [4]: set([a, b])
Out[4]: {MyRecord(foo='a', bar=1337), MyRecord(foo='b', bar=1337)}

In [5]: b.foo = "a"

In [6]: set([a, b])
Out[6]: {MyRecord(foo='a', bar=1337)}

I think the _mutable flag/field introduces unnecessary hand holding and extra code complexity. So it's best if we just not implement this.

Ideally we want to make flow.record compatible with dataclasses which could solve most of these features more nicely. It's something we want to explore in the future.

flow/record/base.py Outdated Show resolved Hide resolved
flow/record/base.py Outdated Show resolved Hide resolved
flow/record/adapter/sqlite.py Outdated Show resolved Hide resolved
tests/test_regression.py Outdated Show resolved Hide resolved
flow/record/base.py Outdated Show resolved Hide resolved
flow/record/base.py Outdated Show resolved Hide resolved
flow/record/base.py Outdated Show resolved Hide resolved
flow/record/base.py Outdated Show resolved Hide resolved
flow/record/base.py Outdated Show resolved Hide resolved
tests/test_record.py Outdated Show resolved Hide resolved
flow/record/base.py Outdated Show resolved Hide resolved
flow/record/base.py Outdated Show resolved Hide resolved
flow/record/base.py Outdated Show resolved Hide resolved
flow/record/__init__.py Outdated Show resolved Hide resolved
flow/record/__init__.py Outdated Show resolved Hide resolved
tests/test_record.py Outdated Show resolved Hide resolved
tests/test_record.py Outdated Show resolved Hide resolved
Copy link
Member

@yunzheng yunzheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the nice feature and implementation, LGTM

@yunzheng yunzheng merged commit a3b5310 into fox-it:main Mar 7, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants