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

Ensure put returns key #35

Merged
merged 1 commit into from
May 6, 2021
Merged

Ensure put returns key #35

merged 1 commit into from
May 6, 2021

Conversation

mlondschien
Copy link
Member

First step towards #28.

@mlondschien mlondschien requested a review from xhochy April 30, 2021 08:27
@mlondschien mlondschien requested a review from siboehm as a code owner April 30, 2021 08:27
key = store.put(key, value)
assert isinstance(key, str)
new_key = store.put(key, value)
assert key == new_key
Copy link
Member

Choose a reason for hiding this comment

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

I think I have seen some cases (in the redis store?) where you can have new_key != key is key is None. Should we handle that here, too?

Copy link
Member Author

Choose a reason for hiding this comment

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

Here we return the key:

def _put(
self, key: str, value: bytes, ttl_secs: Optional[Union[str, int, float]] = None
) -> str:
assert ttl_secs is not None
if ttl_secs in (NOT_SET, FOREVER):
# if we do not care about ttl, just use set
# in redis, using SET will also clear the timeout
# note that this assumes that there is no way in redis
# to set a default timeout on keys
self.redis.set(key, value)
else:
ittl = None
try:
ittl = int(ttl_secs)
except ValueError:
pass # let it blow up further down
if ittl == ttl_secs:
self.redis.setex(key, ittl, value)
else:
self.redis.psetex(key, int(ttl_secs * 1000), value)
return key

I guess there it the KeyTransformingDecorator (undocumented):

def put(self, key: str, *args, **kwargs): # noqa D
return self._unmap_key(self._dstore.put(self._map_key(key), *args, **kwargs))

and the HashDecorator:

def put(self, key: Optional[str], data: bytes, *args, **kwargs):
"""Store bytestring data at key.
Parameters
----------
key : str or None
The key under which the data is to be stored. If None, the hash of data is
used.
data : bytes
Data to be stored at key, must be of type ``bytes``.
Returns
-------
str
The key under which data was stored.
Raises
------
ValueError
If the key is not valid.
IOError
If storing failed or the file could not be read.
"""
if key is None:
key = self._template.format(self.hashfunc(data).hexdigest())
return self._dstore.put(key, data, *args, **kwargs) # type: ignore

The former is surely untested (else tests would be failing).

@mlondschien mlondschien requested a review from xhochy May 6, 2021 06:18
@mlondschien
Copy link
Member Author

Let's merge this and then worry about other methods later.

@xhochy xhochy merged commit de4804e into main May 6, 2021
@xhochy xhochy deleted the issue-28 branch May 6, 2021 08:24
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