Skip to content
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

Apply hint option to query of MongoDB model #154

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/spaceone/core/model/mongo_model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ def query(
unwind=None,
reference_filter=None,
target=None,
hint=None,
**kwargs,
):
filter = filter or []
Expand Down Expand Up @@ -706,6 +707,9 @@ def query(
try:
vos = cls._get_target_objects(target).filter(_filter)

if hint:
vos = vos.hint(hint)

if len(_order_by) > 0:
vos = vos.order_by(*_order_by)

Expand Down Expand Up @@ -1119,7 +1123,7 @@ def _make_aggregate_rules(cls, aggregate):
return _aggregate_rules

@classmethod
def _stat_aggregate(cls, vos, aggregate, page, allow_disk_use, return_type):
def _stat_aggregate(cls, vos, aggregate, page, hint, allow_disk_use, return_type):
result = {}
pipeline = []
_aggregate_rules = cls._make_aggregate_rules(aggregate)
Expand All @@ -1143,11 +1147,15 @@ def _stat_aggregate(cls, vos, aggregate, page, allow_disk_use, return_type):

pipeline.append({"$limit": limit})

options = {}
if allow_disk_use:
_LOGGER.debug(f"[_stat_aggregate] allow_disk_use: {allow_disk_use}")
cursor = vos.aggregate(pipeline, allowDiskUse=True)
else:
cursor = vos.aggregate(pipeline)
options["allowDiskUse"] = True

if hint:
options["hint"] = hint

cursor = vos.aggregate(pipeline, **options)

if return_type == "cursor":
return cursor
Expand All @@ -1158,6 +1166,7 @@ def _stat_aggregate(cls, vos, aggregate, page, allow_disk_use, return_type):
@classmethod
def _stat_distinct(cls, vos, distinct, page):
result = {}

values = vos.distinct(distinct)

try:
Expand Down Expand Up @@ -1187,6 +1196,7 @@ def stat(
page=None,
reference_filter=None,
target="SECONDARY_PREFERRED",
hint=None,
allow_disk_use=False,
return_type="dict",
**kwargs,
Expand All @@ -1205,7 +1215,7 @@ def stat(

if aggregate:
return cls._stat_aggregate(
vos, aggregate, page, allow_disk_use, return_type
vos, aggregate, page, hint, allow_disk_use, return_type
)

elif distinct:
Expand Down Expand Up @@ -1505,6 +1515,7 @@ def analyze(
date_field_format="%Y-%m-%d",
reference_filter=None,
target="SECONDARY_PREFERRED",
hint=None,
allow_disk_use=False,
return_type="dict",
**kwargs,
Expand Down Expand Up @@ -1547,6 +1558,7 @@ def analyze(
"filter_or": filter_or,
"aggregate": aggregate,
"target": target,
"hint": hint,
"allow_disk_use": allow_disk_use,
"return_type": return_type,
"reference_filter": reference_filter,
Expand Down
Loading