Skip to content

Commit f8fb965

Browse files
committed
Optimize Queries
1 parent 0100700 commit f8fb965

22 files changed

+1665
-827
lines changed

.gitignore

Lines changed: 114 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,139 @@
11
*.py[cod]
22
.vscode
33
.empty
4+
.idea
45
benchmark
56
htmlcov
7+
.DS_Store
8+
.vscode
9+
geckodriver.log
10+
11+
# Byte-compiled / optimized / DLL files
12+
__pycache__/
13+
*.py[cod]
14+
*$py.class
615

716
# C extensions
817
*.so
918

10-
# Packages
11-
*.egg
12-
*.egg-info
13-
dist
14-
build
15-
eggs
16-
parts
17-
bin
18-
var
19-
sdist
20-
develop-eggs
19+
# Distribution / packaging
20+
.Python
21+
build/
22+
develop-eggs/
23+
dist/
24+
downloads/
25+
eggs/
26+
.eggs/
27+
lib/
28+
lib64/
29+
parts/
30+
sdist/
31+
var/
32+
wheels/
33+
pip-wheel-metadata/
34+
share/python-wheels/
35+
*.egg-info/
2136
.installed.cfg
22-
lib
23-
lib64
24-
__pycache__
37+
*.egg
38+
MANIFEST
39+
40+
# PyInstaller
41+
# Usually these files are written by a python script from a template
42+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
43+
*.manifest
44+
*.spec
2545

2646
# Installer logs
2747
pip-log.txt
48+
pip-delete-this-directory.txt
2849

2950
# Unit test / coverage reports
51+
htmlcov/
52+
.tox/
53+
.nox/
3054
.coverage
31-
.tox
55+
.coverage.*
56+
.cache
3257
nosetests.xml
58+
coverage.xml
59+
*.cover
60+
*.py,cover
61+
.hypothesis/
3362
.pytest_cache/
3463

3564
# Translations
3665
*.mo
66+
*.pot
3767

38-
# Mr Developer
39-
.mr.developer.cfg
40-
.project
41-
.pydevproject
68+
# Django stuff:
69+
*.log
70+
local_settings.py
71+
db.sqlite3
72+
db.sqlite3-journal
4273

43-
# Pycharm
44-
.idea
74+
# Flask stuff:
75+
instance/
76+
.webassets-cache
77+
78+
# Scrapy stuff:
79+
.scrapy
80+
81+
# Sphinx documentation
82+
docs/_build/
83+
84+
# PyBuilder
85+
target/
86+
87+
# Jupyter Notebook
88+
.ipynb_checkpoints
89+
90+
# IPython
91+
profile_default/
92+
ipython_config.py
93+
94+
# pyenv
95+
.python-version
96+
97+
# pipenv
98+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
99+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
100+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
101+
# install all needed dependencies.
102+
#Pipfile.lock
103+
104+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
105+
__pypackages__/
106+
107+
# Celery stuff
108+
celerybeat-schedule
109+
celerybeat.pid
110+
111+
# SageMath parsed files
112+
*.sage.py
113+
114+
# Environments
115+
.env
116+
.venv
117+
env/
118+
venv/
119+
ENV/
120+
env.bak/
121+
venv.bak/
122+
123+
# Spyder project settings
124+
.spyderproject
125+
.spyproject
126+
127+
# Rope project settings
128+
.ropeproject
129+
130+
# mkdocs documentation
131+
/site
45132

46-
*.db.yml
133+
# mypy
134+
.mypy_cache/
135+
.dmypy.json
136+
dmypy.json
47137

48-
.DS_Store
138+
# Pyre type checker
139+
.pyre/

MANIFEST.in

Lines changed: 0 additions & 2 deletions
This file was deleted.

README.md

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
# What's This?
44

5-
An asynchronous version of `TinyDB`.
5+
An asynchronous version of `TinyDB` with extended capabilities.
66

77
Almost every method is asynchronous. And it's based on `TinyDB 4.7.0+`.
88

9-
I will try to keep up with the latest version of `TinyDB`.
10-
11-
9+
Unlike `TinyDB` which has a minimal core, `Async-TinyDB` is designed to have max flexibility and performance.
1210

1311
# Incompatible Changes
1412

@@ -18,23 +16,23 @@ I will try to keep up with the latest version of `TinyDB`.
1816

1917
* **`ujson`:** Using `ujson` instead of `json`. Some arguments aren't compatible with `json`[^1]
2018

21-
* **Storage `closed` property**: Original `TinyDB` won't raise exceptions when operating on a closed file. Now the property `closed` of `Storage` classes is required to be implemented[^why-closed][^operating-on-closed].
19+
* **[Dev-Changes](#dev-changes)**: Changes that only matter to developers (Who customise `Storage`, `Query`, etc).
2220

2321
* **[Miscellaneous](#misc)**: Differences that only matter in edge cases.
2422

2523
# New Features
2624

27-
* **Event Hooks**: You can now use event hooks to hook into an operation. See [Event Hooks](#event-hooks) for more details.
25+
* **Event Hooks**: You can now use event hooks to hook into an operation. See [Event Hooks](./docs/EventHooks.md) for more details.
2826

2927
* **Redesigned ID & Doc Class**: You can [replace](#replacing-id-&-document-class) and [customise them](#customise-id-class) more pleasingly.
3028

31-
* **DB-level Caching**: This significantly improves the performance of all operations. However, the responsibility of converting the data to the correct type is transferred to the Storage[^2][^disable-db-level].
29+
* **DB-level Caching**: This significantly improves the performance of all operations. However, it may cause dirty reads with some types of storage [^disable-db-level].
3230

33-
* **Built-in `Modifier`**: Use `Modifier` to easily [encrypt](#encryption), [compress](./docs/Modifier.md#Compression) and [extend types](./docs/Modifier.md#Conversion) of your database. Sure you can do much more than these. _(See [Modifier](./docs/Modifier.md))_
31+
* **Built-in `Modifier`**: Use `Modifier` to easily [compress](./docs/Modifier.md#Compression), [encrypt](#encryption) and [extend types](./docs/Modifier.md#Conversion) of your database. Sure you can do much more than these. _(See [Modifier](./docs/Modifier.md))_
3432

35-
* **Isolation Level**: Performance or ACID? It's up to you[^isolevel].
33+
* **Isolation Level**: Performance or thread-safe or even ACID? It's up to you[^isolevel].
3634

37-
* **Atomic Write**: **A**CID!
35+
* **Atomic Write**: Shipped with `JSONStorage`
3836

3937
* **Batch Search By IDs**: `search` method now takes an extra `doc_ids` argument (works like an additional condition)
4038

@@ -50,7 +48,7 @@ I will try to keep up with the latest version of `TinyDB`.
5048
## Importing
5149

5250
```Python
53-
from asynctinydb import TinyDB, where
51+
import asynctinydb
5452
```
5553

5654
## Using
@@ -72,7 +70,7 @@ That's it.
7270

7371
**NOTICE: Mixing classes in one table may cause errors!**
7472

75-
When a table exists in a file, `Async-TinyDB` won't determine classes by itself, it is your duty to make sure classes are matching.
73+
When a table exists in a file, `Async-TinyDB` won't determine its classes by itself, it is your duty to make sure classes are matching.
7674

7775
### ID Classes
7876

@@ -88,7 +86,7 @@ from asynctinydb import TinyDB, UUID, IncreID, Document
8886

8987
db = TinyDB("database.db")
9088

91-
# Setting ID class to UUID, document class to Document
89+
# Setting ID class to `UUID`, document class to `Document`
9290
tab = db.table("table1", document_id_class=UUID, document_class=Document)
9391
```
9492

@@ -116,26 +114,19 @@ _See [Encryption](./docs/Modifier.md#Encryption)_
116114

117115
## Isolation Level
118116

119-
To avoid blocking codes, Async-TinyDB puts CPU-bound tasks into another thread (Useful with interpreters without GIL)
120-
121-
Unfortunately, this introduces chances of data being modified when:
117+
When operating the TinyDB concurrently, there might be racing conditions.
122118

123-
* Manipulating mutable objects within `Document` instances in another coroutine
124-
* Performing updating/saving/searching operations (These operations are run in a different thread/process)
125-
* The conditions above are satisfied in the same `Table`
126-
* The conditions above are satisfied simultaneously.
127-
128-
Avoid these operations or set a higher isolation level to mitigate this problem.
119+
Set a higher isolation level to mitigate this problem.
129120

130121
```Python
131-
db.isolevel = 2
122+
db.isolevel = 1
132123
```
133124

134125
`isolevel`:
135126

136-
0. No isolation
137-
1. Serialised(Atomic) CRUD operations (Also ensures thread safety) (default)
138-
2. Deepcopy documents on insertion and searching (**CR**UD) (Ensures `Index` & `Query Cache` consistency)
127+
0. No isolation, best performance.
128+
1. Serialised(Atomic) CRUD operations. (Also ensures thread safety) (default)
129+
2. Deepcopy documents on insertion and retrieving. (**CR**UD) (Ensures `Index` & `Query Cache` consistency)
139130

140131

141132

@@ -258,6 +249,12 @@ class BaseDocument(Mapping[IDVar, Any]):
258249

259250
Make sure you have implemented all the methods required by `BaseDocument` class.
260251

252+
# Dev-Changes
253+
254+
* Storage `closed` property: Original `TinyDB` won't raise exceptions when operating on a closed file. Now the property `closed` of `Storage` classes is required to be implemented[^why-closed][^operating-on-closed].
255+
* Storage data converting: The responsibility of converting the data to the correct type is transferred to the Storage[^2]
256+
* `is_cacheable` method in `QueryInstance` is changed to `cacheable` property and will be deprecated.
257+
261258
# Misc
262259

263260
* Lazy-load: File loading & dirs creating are delayed to the first IO operation.
@@ -266,7 +263,7 @@ Make sure you have implemented all the methods required by `BaseDocument` class
266263
* `search` accepts optional `cond`, returns all docs if no arguments are provided
267264
* `get` and `contains` raises `ValueError` instead of `RuntimeError` when `cond` and `doc_id` are both `None`
268265
* `LRUCache` stores `tuple`s of ids instead of `list`s of docs
269-
* `search` and `get` treat `doc_id` and `doc_ids` as extra conditions instead of ignoring conditions when they are provided. That is to say, when `cond` and `doc_id(s)` are passed, they return docs satisfies both `cond` and `doc_id(s)`.
266+
* `search` and `get` treat `doc_id` and `doc_ids` as extra conditions instead of ignoring conditions when IDs are provided. That is to say, when `cond` and `doc_id(s)` are passed, they return docs satisfies `cond` and is in `doc_id(s)`.
270267

271268

272269

@@ -275,6 +272,6 @@ Make sure you have implemented all the methods required by `BaseDocument` class
275272
[^UUID-version]:Currently using UUID4
276273
[^disable-db-level]: See [DB-level caching](#db-level-caching) to learn how to disable this feature if it causes dirty reads.
277274
[^isolevel]: See [isolevel](#isolation-level)
278-
[^why-closed]: This is for `Middileware` classes to reliably determine whether the `Storage` is closed, so they can raise `IOError`
279-
[^operating-on-closed]: An `IOError` should be raised when operating on a closed storage.
275+
[^why-closed]: This is for `Middleware` classes to reliably determine whether the `Storage` is closed, so they can raise `IOError`
276+
[^operating-on-closed]: An `IOError` should be raised when operating on closed storage.
280277

0 commit comments

Comments
 (0)