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
Switch to explicit atomic_batch() database handle #1304
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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,12 +1,25 @@ | ||
from contextlib import contextmanager | ||
import logging | ||
from pathlib import Path | ||
from typing import Generator | ||
from typing import ( | ||
Generator, | ||
TYPE_CHECKING, | ||
) | ||
|
||
from eth_utils import ValidationError | ||
|
||
from eth.db.diff import ( | ||
DBDiffTracker, | ||
DiffMissingError, | ||
) | ||
from .base import ( | ||
BaseAtomicDB, | ||
BaseDB, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
import plyvel # noqa: F401 | ||
|
||
|
||
class LevelDB(BaseAtomicDB): | ||
logger = logging.getLogger("eth.db.backends.LevelDB") | ||
|
@@ -16,10 +29,11 @@ def __init__(self, db_path: Path = None) -> None: | |
if not db_path: | ||
raise TypeError("Please specifiy a valid path for your database.") | ||
try: | ||
import plyvel | ||
import plyvel # noqa: F811 | ||
except ImportError: | ||
raise ImportError("LevelDB requires the plyvel \ | ||
library which is not available for import.") | ||
raise ImportError( | ||
"LevelDB requires the plyvel library which is not available for import." | ||
) | ||
self.db_path = db_path | ||
self.db = plyvel.DB(str(db_path), create_if_missing=True, error_if_exists=False) | ||
|
||
|
@@ -39,10 +53,73 @@ def __delitem__(self, key: bytes) -> None: | |
self.db.delete(key) | ||
|
||
@contextmanager | ||
def atomic_batch(self) -> Generator[None, None, None]: | ||
with self.db.write_batch(transaction=True): | ||
def atomic_batch(self) -> Generator['LevelDBWriteBatch', None, None]: | ||
with self.db.write_batch(transaction=True) as atomic_batch: | ||
readable_batch = LevelDBWriteBatch(self, atomic_batch) | ||
try: | ||
yield | ||
except Exception: | ||
self.logger.exception("Unexpected error occurred during atomic database write") | ||
raise | ||
yield readable_batch | ||
finally: | ||
readable_batch.shutdown() | ||
|
||
|
||
class LevelDBWriteBatch(BaseDB): | ||
""" | ||
A native leveldb write batch does not permit reads on the in-progress data. | ||
This class fills that gap, by tracking the in-progress diff, and adding | ||
a read interface. | ||
""" | ||
logger = logging.getLogger("eth.db.backends.LevelDBWriteBatch") | ||
|
||
def __init__(self, original_read_db: BaseDB, write_batch: 'plyvel.WriteBatch') -> None: | ||
self._original_read_db = original_read_db | ||
self._write_batch = write_batch | ||
# keep track of the temporary changes made | ||
self._track_diff = DBDiffTracker() | ||
|
||
def __getitem__(self, key: bytes) -> bytes: | ||
if self._track_diff is None: | ||
raise ValidationError("Cannot get data from a write batch, out of context") | ||
|
||
try: | ||
changed_value = self._track_diff[key] | ||
except DiffMissingError as missing: | ||
if missing.is_deleted: | ||
raise KeyError(key) | ||
else: | ||
return self._original_read_db[key] | ||
else: | ||
return changed_value | ||
|
||
def __setitem__(self, key: bytes, value: bytes) -> None: | ||
if self._track_diff is None: | ||
raise ValidationError("Cannot set data from a write batch, out of context") | ||
|
||
self._write_batch.put(key, value) | ||
self._track_diff[key] = value | ||
|
||
def _exists(self, key: bytes) -> bool: | ||
if self._track_diff is None: | ||
raise ValidationError("Cannot test data existance from a write batch, out of context") | ||
|
||
try: | ||
self._track_diff[key] | ||
except DiffMissingError as missing: | ||
if missing.is_deleted: | ||
return False | ||
else: | ||
return key in self._original_read_db | ||
else: | ||
return True | ||
|
||
def __delitem__(self, key: bytes) -> None: | ||
if self._track_diff is None: | ||
raise ValidationError("Cannot delete data from a write batch, out of context") | ||
|
||
self._write_batch.delete(key) | ||
del self._track_diff[key] | ||
|
||
def shutdown(self) -> None: | ||
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. Maybe rename to |
||
""" | ||
Prevent any further actions to be taken on this write batch, called after leaving context | ||
""" | ||
self._track_diff = None |
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.
This name is odd, but am I correct that it's an internal API. If so, then I don't really care that much other than maybe prefixing with an
_
to make that clear.