This repository was archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Add support for defining indexes on models #13
Open
soderluk
wants to merge
6
commits into
ioxiocom:main
Choose a base branch
from
soderluk:ensure_indexes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
adadd10
Add support for defining indexes on models
soderluk 778b36a
Add index classes to manage different index types
soderluk 53ac53e
Update README
soderluk ef730a1
Remove return value typing
soderluk 5f8cd69
Move index creating to tests from conftest
soderluk 0dca83b
Ensure Indexes: Fix tests
soderluk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,172 @@ | ||
| from abc import ABC, abstractmethod | ||
| from typing import List, Optional | ||
|
|
||
| import pydantic | ||
| from aioarangodb.collection import StandardCollection | ||
| from pydantic import Field | ||
|
|
||
|
|
||
| class BaseIndex(ABC, pydantic.BaseModel): | ||
| """ | ||
| Base index model. | ||
|
|
||
| Define fields common to most of the indexes. The index class should be derived | ||
| from this base class, and add the add_index() function which actually creates | ||
| the index. | ||
| """ | ||
|
|
||
| fields: List = Field(..., description="List of fields to index") | ||
| name: Optional[str] = Field(None, description="Optional name for the index.") | ||
| in_background: Optional[bool] = Field( | ||
| None, description="Do not hold the collection lock." | ||
| ) | ||
|
|
||
| @abstractmethod | ||
| async def add_index(self, collection: StandardCollection): | ||
| """ | ||
| Creates the index on the collection. | ||
|
|
||
| Override this function in the subclass. | ||
|
|
||
| :param collection: The collection to create the index on. | ||
| :return: Dictionary with the index information. | ||
| """ | ||
| raise NotImplementedError() | ||
|
|
||
|
|
||
| class HashIndex(BaseIndex): | ||
| """ | ||
| Creates a hash index on the collection. | ||
| """ | ||
|
|
||
| unique: Optional[bool] = Field( | ||
| None, description="Whether the index is unique or not." | ||
| ) | ||
| sparse: Optional[bool] = Field( | ||
| None, | ||
| description="If set to True, documents with None in the field are also indexed," | ||
| " otherwise they're skipped", | ||
| ) | ||
| deduplicate: Optional[bool] = Field( | ||
| None, | ||
| description="If set to True, inserting duplicate index values from the same " | ||
| "document triggers unique constraint errors.", | ||
| ) | ||
|
|
||
| async def add_index(self, collection: StandardCollection): | ||
| return await collection.add_hash_index( | ||
| fields=self.fields, | ||
| unique=self.unique, | ||
| sparse=self.sparse, | ||
| deduplicate=self.deduplicate, | ||
| name=self.name, | ||
| in_background=self.in_background, | ||
| ) | ||
|
|
||
|
|
||
| class SkiplistIndex(BaseIndex): | ||
| """ | ||
| Creates a skiplist index on the collection. | ||
| """ | ||
|
|
||
| unique: Optional[bool] = Field( | ||
| None, description="Whether the index is unique or not." | ||
| ) | ||
| sparse: Optional[bool] = Field( | ||
| None, | ||
| description="If set to True, documents with None in the field are also indexed," | ||
| " otherwise they're skipped", | ||
| ) | ||
| deduplicate: Optional[bool] = Field( | ||
| None, | ||
| description="If set to True, inserting duplicate index values from the same " | ||
| "document triggers unique constraint errors.", | ||
| ) | ||
|
|
||
| async def add_index(self, collection: StandardCollection): | ||
| return await collection.add_skiplist_index( | ||
| fields=self.fields, | ||
| unique=self.unique, | ||
| sparse=self.sparse, | ||
| deduplicate=self.deduplicate, | ||
| name=self.name, | ||
| in_background=self.in_background, | ||
| ) | ||
|
|
||
|
|
||
| class GeoIndex(BaseIndex): | ||
| """ | ||
| Creates a geo-spatial index on the collection. | ||
| """ | ||
|
|
||
| ordered: Optional[bool] = Field( | ||
| None, description="Whether the order is longitude, then latitude." | ||
| ) | ||
|
|
||
| async def add_index(self, collection: StandardCollection): | ||
| return await collection.add_geo_index( | ||
| fields=self.fields, | ||
| ordered=self.ordered, | ||
| name=self.name, | ||
| in_background=self.in_background, | ||
| ) | ||
|
|
||
|
|
||
| class FulltextIndex(BaseIndex): | ||
| """ | ||
| Creates a fulltext index on the collection. | ||
| """ | ||
|
|
||
| min_length: Optional[int] = Field( | ||
| None, description="Minimum number of characters to index." | ||
| ) | ||
|
|
||
| async def add_index(self, collection: StandardCollection): | ||
| return await collection.add_fulltext_index( | ||
| fields=self.fields, | ||
| min_length=self.min_length, | ||
| name=self.name, | ||
| in_background=self.in_background, | ||
| ) | ||
|
|
||
|
|
||
| class PersistentIndex(BaseIndex): | ||
| """ | ||
| Creates a persistent index on the collection. | ||
| """ | ||
|
|
||
| unique: Optional[bool] = Field( | ||
| None, description="Whether the index is unique or not." | ||
| ) | ||
| sparse: Optional[bool] = Field( | ||
| None, | ||
| description="If set to True, documents with None in the field are also indexed," | ||
| " otherwise they're skipped", | ||
| ) | ||
|
|
||
| async def add_index(self, collection: StandardCollection): | ||
| return await collection.add_persistent_index( | ||
| fields=self.fields, | ||
| unique=self.unique, | ||
| sparse=self.sparse, | ||
| name=self.name, | ||
| in_background=self.in_background, | ||
| ) | ||
|
|
||
|
|
||
| class TTLIndex(BaseIndex): | ||
| """ | ||
| Creates a TTL (time-to-live) index. | ||
| """ | ||
|
|
||
| expiry_time: int = Field( | ||
| ..., description="Time of expiry in seconds after document creation." | ||
| ) | ||
|
|
||
| async def add_index(self, collection: StandardCollection): | ||
| return await collection.add_ttl_index( | ||
| fields=self.fields, | ||
| expiry_time=self.expiry_time, | ||
| name=self.name, | ||
| in_background=self.in_background, | ||
| ) |
This file contains hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.