Skip to content

Commit

Permalink
use lock in mutikey getOne and get methods (#361)
Browse files Browse the repository at this point in the history
<!--- Provide a general summary of your changes in the title above -->
<!--- Link the corresponding issues after you created the pull request
-->

## Types of changes
<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->
- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)

## Checklist:
<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
- [x] I have updated the [changelog](../CHANGELOG.md) accordingly.
- [ ] I have added tests to cover my changes.
  • Loading branch information
deichmab-draeger authored May 14, 2024
1 parent 7cb13b6 commit 6473828
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ jobs:
retention-days: 5

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- accessing a multikey may lead to IndexError [#359](https://github.com/Draegerwerk/sdc11073/issues/359)

## [1.3.2] - 2024-03-18

### Fixed
Expand Down
35 changes: 26 additions & 9 deletions src/sdc11073/multikey.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
from collections import defaultdict, namedtuple
from threading import RLock

Expand Down Expand Up @@ -43,17 +44,32 @@ def __init__(self, getKeyFunc, indexNoneValues=True):
super(IndexDefinition, self).__init__()
self._getKeyFunc = getKeyFunc
self._indexNoneValues = indexNoneValues
self._lock: RLock | None = None

def getOne(self, key, allowNone=False):
try:
result = self[key]
if len(result) > 1:
raise RuntimeError('getOne: key "{}" has {} objects'.format(key, len(result)))
return result[0]
except KeyError:
if allowNone:
return
raise RuntimeError('key "{}" not found'.format(key))
with self._lock:
try:
result = self[key]
if len(result) > 1:
raise RuntimeError('getOne: key "{}" has {} objects'.format(key, len(result)))
return result[0]
except KeyError:
if allowNone:
return
raise RuntimeError('key "{}" not found'.format(key))

def get(self, *args, **kwargs):
"""Overwritten get method that uses lock."""
with self._lock:
return super().get(*args, **kwargs)

def __getitem__(self, key):
"""Overwritten __getitem__ method that uses lock."""
with self._lock:
return super().__getitem__(key)

def set_lock(self, lock):
self._lock = lock

def _mkKeys(self, obj):
key = self._getKeyFunc(obj)
Expand Down Expand Up @@ -161,6 +177,7 @@ def __getattr__(self, name):

def addIndex(self, indexName, indexDefinition):
self._idxDefs[indexName] = indexDefinition
indexDefinition.set_lock(self._lock)
# add existing objects to new lookup
for obj in self._objects:
keys = indexDefinition._mkKeys(obj)
Expand Down

0 comments on commit 6473828

Please sign in to comment.