-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from ErikKalkoken/update-docs
Update docs
- Loading branch information
Showing
11 changed files
with
159 additions
and
107 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Change Log | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](http://keepachangelog.com/) | ||
and this project adheres to [Semantic Versioning](http://semver.org/). | ||
|
||
## [Unreleased] - yyyy-mm-dd | ||
|
||
### Added | ||
|
||
### Changed | ||
|
||
### Fixed | ||
|
||
## [0.1.0] - 2023-06-05 | ||
|
||
### Added | ||
|
||
- Initial release |
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,3 +1,8 @@ | ||
.logo { | ||
overflow-wrap: normal; | ||
} | ||
|
||
div.sphinxsidebar { | ||
max-height: 100%; | ||
overflow-y: auto; | ||
} |
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,20 @@ | ||
.. currentmodule:: aiodiskqueue | ||
|
||
=============== | ||
Customization | ||
=============== | ||
|
||
Storage Engines | ||
=============== | ||
|
||
aiodiskqueue uses the DbmEngine as default, but you can also select a different storage engine. | ||
|
||
Or you can create your own storage engine by inheriting from :class:`.FifoStorageEngine`. | ||
|
||
.. automodule:: aiodiskqueue.engines.dbm | ||
|
||
.. automodule:: aiodiskqueue.engines.simple | ||
|
||
.. automodule:: aiodiskqueue.engines.sqlite | ||
|
||
.. automodule:: aiodiskqueue.engines.base |
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
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,102 +1,96 @@ | ||
"""Engines for storing the queues on disk.""" | ||
"""Engines for storing the queues with DBM.""" | ||
|
||
import dbm | ||
import logging | ||
import pickle | ||
from pathlib import Path | ||
from typing import Any, List, Optional, Union | ||
|
||
try: | ||
import aiodbm | ||
except ImportError: | ||
has_aiodbm = False | ||
else: | ||
has_aiodbm = True | ||
import aiodbm | ||
|
||
from .base import _FifoStorageEngine | ||
from .base import FifoStorageEngine | ||
|
||
logger = logging.getLogger("aiodiskqueue") | ||
|
||
if has_aiodbm: | ||
|
||
class DbmEngine(_FifoStorageEngine): | ||
"""A queue storage engine using DBM.""" | ||
|
||
def __init__(self, data_path: Path) -> None: | ||
super().__init__(data_path) | ||
self._data_path_2 = str(data_path.absolute()) | ||
|
||
HEAD_ID_KEY = "head_id" | ||
TAIL_ID_KEY = "tail_id" | ||
|
||
async def initialize(self): | ||
async with aiodbm.open(self._data_path_2, "c") as db: | ||
await db.set("dummy", "test") | ||
await db.delete("dummy") | ||
|
||
async def fetch_all(self) -> List[Any]: | ||
try: | ||
async with aiodbm.open(self._data_path_2, "r") as db: | ||
head_id = await self._get_obj(db, self.HEAD_ID_KEY) | ||
tail_id = await self._get_obj(db, self.TAIL_ID_KEY) | ||
if not head_id or not tail_id: | ||
return [] | ||
|
||
items = [] | ||
for item_id in range(head_id, tail_id + 1): | ||
item_key = self._make_item_key(item_id) | ||
item = await self._get_obj(db, item_key) | ||
items.append(item) | ||
except dbm.error: | ||
items = [] | ||
|
||
return items | ||
class DbmEngine(FifoStorageEngine): | ||
"""A queue storage engine using DBM.""" | ||
|
||
async def add_item(self, item: Any): | ||
async with aiodbm.open(self._data_path_2, "w") as db: | ||
tail_id = await self._get_obj(db, self.TAIL_ID_KEY) | ||
if tail_id: | ||
item_id = tail_id + 1 | ||
is_first = False | ||
else: | ||
item_id = 1 | ||
is_first = True | ||
def __init__(self, data_path: Path) -> None: | ||
super().__init__(data_path) | ||
self._data_path_2 = str(data_path.absolute()) | ||
|
||
await self._set_obj(db, self._make_item_key(item_id), item) | ||
await self._set_obj(db, self.TAIL_ID_KEY, item_id) | ||
_HEAD_ID_KEY = "head_id" | ||
_TAIL_ID_KEY = "tail_id" | ||
|
||
if is_first: | ||
await self._set_obj(db, self.HEAD_ID_KEY, item_id) | ||
async def initialize(self): | ||
async with aiodbm.open(self._data_path_2, "c") as db: | ||
await db.set("dummy", "test") | ||
await db.delete("dummy") | ||
|
||
async def remove_item(self): | ||
async with aiodbm.open(self._data_path_2, "w") as db: | ||
head_id = await self._get_obj(db, self.HEAD_ID_KEY) | ||
tail_id = await self._get_obj(db, self.TAIL_ID_KEY) | ||
async def fetch_all(self) -> List[Any]: | ||
try: | ||
async with aiodbm.open(self._data_path_2, "r") as db: | ||
head_id = await self._get_obj(db, self._HEAD_ID_KEY) | ||
tail_id = await self._get_obj(db, self._TAIL_ID_KEY) | ||
if not head_id or not tail_id: | ||
raise ValueError("Nothing to remove from an empty database") | ||
item_key = self._make_item_key(head_id) | ||
await db.delete(item_key) | ||
|
||
if head_id != tail_id: | ||
# there are items left | ||
await self._set_obj(db, self.HEAD_ID_KEY, head_id + 1) | ||
else: | ||
# was last item | ||
await db.delete(self.HEAD_ID_KEY) | ||
await db.delete(self.TAIL_ID_KEY) | ||
|
||
@staticmethod | ||
def _make_item_key(item_id: int) -> str: | ||
return f"item-{item_id}" | ||
|
||
@staticmethod | ||
async def _get_obj(db, key: Union[str, bytes]) -> Optional[Any]: | ||
data = await db.get(key) | ||
if not data: | ||
return None | ||
return pickle.loads(data) | ||
|
||
@staticmethod | ||
async def _set_obj(db, key: Union[str, bytes], item: Any): | ||
data = pickle.dumps(item) | ||
await db.set(key, data) | ||
return [] | ||
|
||
items = [] | ||
for item_id in range(head_id, tail_id + 1): | ||
item_key = self._make_item_key(item_id) | ||
item = await self._get_obj(db, item_key) | ||
items.append(item) | ||
except dbm.error: | ||
items = [] | ||
|
||
return items | ||
|
||
async def add_item(self, item: Any): | ||
async with aiodbm.open(self._data_path_2, "w") as db: | ||
tail_id = await self._get_obj(db, self._TAIL_ID_KEY) | ||
if tail_id: | ||
item_id = tail_id + 1 | ||
is_first = False | ||
else: | ||
item_id = 1 | ||
is_first = True | ||
|
||
await self._set_obj(db, self._make_item_key(item_id), item) | ||
await self._set_obj(db, self._TAIL_ID_KEY, item_id) | ||
|
||
if is_first: | ||
await self._set_obj(db, self._HEAD_ID_KEY, item_id) | ||
|
||
async def remove_item(self): | ||
async with aiodbm.open(self._data_path_2, "w") as db: | ||
head_id = await self._get_obj(db, self._HEAD_ID_KEY) | ||
tail_id = await self._get_obj(db, self._TAIL_ID_KEY) | ||
if not head_id or not tail_id: | ||
raise ValueError("Nothing to remove from an empty database") | ||
item_key = self._make_item_key(head_id) | ||
await db.delete(item_key) | ||
|
||
if head_id != tail_id: | ||
# there are items left | ||
await self._set_obj(db, self._HEAD_ID_KEY, head_id + 1) | ||
else: | ||
# was last item | ||
await db.delete(self._HEAD_ID_KEY) | ||
await db.delete(self._TAIL_ID_KEY) | ||
|
||
@staticmethod | ||
def _make_item_key(item_id: int) -> str: | ||
return f"item-{item_id}" | ||
|
||
@staticmethod | ||
async def _get_obj(db, key: Union[str, bytes]) -> Optional[Any]: | ||
data = await db.get(key) | ||
if not data: | ||
return None | ||
return pickle.loads(data) | ||
|
||
@staticmethod | ||
async def _set_obj(db, key: Union[str, bytes], item: Any): | ||
data = pickle.dumps(item) | ||
await db.set(key, data) |
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
Oops, something went wrong.