-
Notifications
You must be signed in to change notification settings - Fork 0
Optimize search #634
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
Merged
Merged
Optimize search #634
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
50ce5cc
refactor: remove pool_pre_ping from database connection settings
Mastermind-U 02d2b59
add: indexes to Directory and Attributes tables for improved query pe…
Mastermind-U 77f863d
refactor: enhance filter condition handling in LDAP query functions
Mastermind-U 39b57f2
add: EXPLAIN statement support for PostgreSQL in SQLAlchemy
Mastermind-U eddb12f
refactor: optimize query loading and adjust path filtering in SearchR…
Mastermind-U ebb98f7
add: implement async explain_query function for SQLAlchemy queries
Mastermind-U 4115527
fix: correct spelling of 'MultiDirectory' in module docstrings
Mastermind-U 82010f5
add: create __main__.py with module documentation and licensing infor…
Mastermind-U dbbd4bb
add: include .zed/ directory in .gitignore
Mastermind-U ec04b62
misc: Apply suggestion from @Copilot
Mastermind-U a1dc75f
fix: remove unnecessary DROP EXTENSION command from downgrade function
Mastermind-U 54bbc8f
fix: streamline DAO provision by removing redundant methods in HTTPPr…
Mastermind-U 29bcdec
fix: update index creation and removal for Users, Directory, and Attr…
Mastermind-U e28d4f5
refactor: _get_filter_condition unification
Mastermind-U 916b5fa
fix: enforce size limit on query results and optimize joins in Search…
Mastermind-U 58784fb
fix: standardize index creation syntax in alembic migration
Mastermind-U b7d4432
fix: remove debug query explanation in SearchRequest
Mastermind-U 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ downloads/ | |
| .idea/ | ||
| .DS_Store | ||
| .vscode/ | ||
| .zed/ | ||
| eggs/ | ||
| .eggs/ | ||
| lib/ | ||
|
|
||
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,5 @@ | ||
| """MultiDirectory. | ||
|
|
||
| Copyright (c) 2024 MultiFactor | ||
| License: https://github.com/MultiDirectoryLab/MultiDirectory/blob/main/LICENSE | ||
| """ |
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,96 @@ | ||
| """index_single_level. | ||
|
|
||
| Revision ID: a7971f00ba4d | ||
| Revises: 35d1542d2505 | ||
| Create Date: 2025-07-22 11:13:48.397808 | ||
|
|
||
| """ | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "a7971f00ba4d" | ||
| down_revision = "35d1542d2505" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| """Create index for Directory depth field.""" | ||
| op.execute( | ||
| sa.text( | ||
| 'CREATE INDEX "idx_User_san_gin" ' | ||
| 'ON "Users" USING gin ("sAMAccountName" gin_trgm_ops);' | ||
| ) | ||
| ) | ||
| op.execute( | ||
| sa.text( | ||
| 'CREATE INDEX "idx_User_upn_gin" ' | ||
| 'ON "Users" USING gin ("userPrincipalName" gin_trgm_ops);' | ||
| ) | ||
| ) | ||
| op.execute( | ||
| sa.text( | ||
| 'CREATE INDEX "idx_User_display_name_gin" ' | ||
| 'ON "Users" USING gin ("displayName" gin_trgm_ops);' | ||
| ) | ||
| ) | ||
| op.execute( | ||
| sa.text( | ||
| 'CREATE INDEX "idx_user_hash_dir_id" ' | ||
| 'ON "Users" USING hash ("directoryId");' | ||
| ) | ||
| ) | ||
| op.execute( | ||
| sa.text( | ||
| 'CREATE INDEX "idx_entity_type_dir_id" ' | ||
| 'ON "Directory" USING hash ("entity_type_id");' | ||
| ) | ||
| ) | ||
| op.execute( | ||
| sa.text( | ||
| 'CREATE INDEX "idx_group_dir_id" ' | ||
| 'ON "Groups" USING hash ("directoryId");' | ||
| ) | ||
| ) | ||
| op.execute( | ||
| sa.text( | ||
| 'CREATE INDEX "idx_Directory_depth_hash" ' | ||
| 'ON "Directory" ' | ||
| "USING hash (depth);" | ||
| ) | ||
| ) | ||
| op.execute( | ||
| sa.text( | ||
| 'CREATE INDEX "idx_composite_attributes_directory_id_name" ' | ||
| 'ON "Attributes" ("directoryId", lower("name"));' | ||
| ) | ||
| ) | ||
| op.execute( | ||
| sa.text( | ||
| 'CREATE INDEX "idx_attributes_value" ' | ||
| 'ON "Attributes" USING gin ("value" gin_trgm_ops);' | ||
| ) | ||
| ) | ||
| op.execute( | ||
| sa.text( | ||
| 'CREATE INDEX "idx_attributes_name_value_trgm" ' | ||
| 'ON "Attributes" USING gin ' | ||
| '("name" gin_trgm_ops, "value" gin_trgm_ops);' | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| """Remove indexes for Directory depth field and Attributes table.""" | ||
| op.drop_index("idx_User_san_gin", "Users") | ||
| op.drop_index("idx_User_upn_gin", "Users") | ||
| op.drop_index("idx_User_display_name_gin", "Users") | ||
| op.drop_index("idx_user_hash_dir_id", "Users") | ||
| op.drop_index("idx_entity_type_dir_id", "Directory") | ||
| op.drop_index("idx_group_dir_id", "Groups") | ||
| op.drop_index("idx_Directory_depth_hash", "Directory") | ||
| op.drop_index("idx_composite_attributes_directory_id_name", "Attributes") | ||
| op.drop_index("idx_attributes_value", "Attributes") | ||
| op.drop_index("idx_attributes_name_value_trgm", "Attributes") |
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
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
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -15,7 +15,7 @@ | |||||
| from sqlalchemy import func, or_ | ||||||
| from sqlalchemy.ext.asyncio import AsyncSession | ||||||
| from sqlalchemy.future import select | ||||||
| from sqlalchemy.orm import joinedload, selectinload, with_loader_criteria | ||||||
| from sqlalchemy.orm import selectinload, with_loader_criteria | ||||||
| from sqlalchemy.sql.elements import ColumnElement, UnaryExpression | ||||||
| from sqlalchemy.sql.expression import Select | ||||||
|
|
||||||
|
|
@@ -307,6 +307,9 @@ async def get_result( | |||||
|
|
||||||
| query, pages_total, count = await self.paginate_query(query, session) | ||||||
|
|
||||||
| if self.size_limit != 0: | ||||||
| query = query.limit(self.size_limit) | ||||||
|
|
||||||
| async for response in self.tree_view(query, session): | ||||||
| yield response | ||||||
|
|
||||||
|
|
@@ -342,7 +345,10 @@ def _mutate_query_with_attributes_to_load( | |||||
| ) -> Select: | ||||||
| """Get attributes to load.""" | ||||||
| if self.entity_type_name: | ||||||
| query = query.options(selectinload(Directory.entity_type)) | ||||||
| query = ( | ||||||
| query.join(Directory.entity_type) | ||||||
| .options(selectinload(Directory.entity_type)) | ||||||
| ) # fmt: skip | ||||||
|
|
||||||
| if self.all_attrs: | ||||||
| return query.options(selectinload(Directory.attributes)) | ||||||
|
|
@@ -370,9 +376,8 @@ def build_query( | |||||
| query = ( | ||||||
| select(Directory) | ||||||
| .join(Directory.user, isouter=True) | ||||||
| .join(Directory.group, isouter=True) | ||||||
| .join(Directory.entity_type, isouter=True) | ||||||
| ) # fmt: skip | ||||||
| .options(selectinload(Directory.group)) | ||||||
| ) | ||||||
|
|
||||||
| query = self._mutate_query_with_attributes_to_load(query) | ||||||
| query = mutate_ap(query, user) | ||||||
|
|
@@ -402,9 +407,9 @@ def build_query( | |||||
|
|
||||||
| elif self.scope == Scope.SINGLE_LEVEL: | ||||||
| query = query.filter( | ||||||
| func.cardinality(Directory.path) == len(search_path) + 1, | ||||||
| Directory.depth == len(search_path) + 1, | ||||||
| get_path_filter( | ||||||
| column=Directory.path[0 : len(search_path)], | ||||||
| column=Directory.path[1 : len(search_path)], | ||||||
|
||||||
| column=Directory.path[1 : len(search_path)], | |
| column=Directory.path[0 : len(search_path)], |
Mastermind-U marked this conversation as resolved.
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.