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

Mongo can handle larger databases now #174

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
91 changes: 55 additions & 36 deletions medallion/filters/mongodb_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,22 @@ def process_filter(self, data, allowed, manifest_info):
pipeline = [
{"$match": {"$and": [self.full_query]}},
]

latest_pipeline = list()
# when no filter is provided only latest is considered.
match_spec_version = self.filter_args.get("match[spec_version]")
if not match_spec_version and "spec_version" in allowed:
latest_pipeline = list(pipeline)
latest_pipeline.append({"$sort": {"_manifest.media_type": ASCENDING}})
latest_pipeline.append({"$group": SON([("_id", "$id"), ("media_type", SON([("$last", "$_manifest.media_type")]))])})

query = [
{"id": x["_id"], "_manifest.media_type": x["media_type"]}
for x in list(data.aggregate(latest_pipeline))
spec_versions = data.distinct("_manifest.media_type")
spec_versions.sort()
pipeline = [
{
"$match": {
"$and": [
self.full_query,
{"_manifest.media_type": spec_versions[-1]},
]
}
}
]
if query:
pipeline.append({"$match": {"$or": query}})

# create version filter
if "version" in allowed:
match_version = self.filter_args.get("match[version]")
Expand All @@ -77,28 +78,46 @@ def process_filter(self, data, allowed, manifest_info):
if "all" not in match_version:
actual_dates = [datetime_to_float(string_to_datetime(x)) for x in match_version.split(",") if (x != "first" and x != "last")]

latest_pipeline = list(pipeline)
latest_pipeline.append({"$sort": {"_manifest.version": ASCENDING}})
latest_pipeline.append({"$group": SON([("_id", "$id"), ("versions", SON([("$push", "$_manifest.version")]))])})

latest_pipeline = []
accumulatorOperator = None
# The documents are sorted in ASCENDING order.
version_selector = []
if "last" in match_version:
version_selector.append({"$arrayElemAt": ["$versions", -1]})
accumulatorOperator = "max"
if "first" in match_version:
version_selector.append({"$arrayElemAt": ["$versions", 0]})
for d in actual_dates:
version_selector.append({"$arrayElemAt": ["$versions", {"$indexOfArray": ["$versions", d]}]})
latest_pipeline.append({"$addFields": {"versions": version_selector}})
accumulatorOperator = "min"
if accumulatorOperator:
latest_pipeline.append(
{
"$setWindowFields": {
"partitionBy": "$id",
"output": {
"_picked_version": {
f"${accumulatorOperator}": "$_manifest.version"
}
}
}
})
latest_pipeline.append({
"$match": {
"$expr": {
"$eq": [
"$_manifest.version",
"$_picked_version"
]
}
}
})
latest_pipeline.append(
{
"$unset": [
"_picked_version"
]
})
if actual_dates:
latest_pipeline.append({"$match": {"versions": {"$in": actual_dates}}})
latest_pipeline.append({"$match": {"_manifest.version": {"$in": actual_dates}}})

query = [
{"id": x["_id"], "_manifest.version": {"$in": x["versions"]}}
for x in list(data.aggregate(latest_pipeline))
]
if query:
pipeline.append({"$match": {"$or": query}})
for obj in latest_pipeline:
pipeline.append(obj)

pipeline.append({"$sort": SON([("_manifest.date_added", ASCENDING), ("created", ASCENDING), ("modified", ASCENDING)])})

Expand All @@ -109,19 +128,21 @@ def process_filter(self, data, allowed, manifest_info):

count = self.get_result_count(pipeline, data)
self.add_pagination_operations(pipeline)
results = list(data.aggregate(pipeline))
results = list(data.aggregate(pipeline, allowDiskUse=True))
elif manifest_info == "objects":
# Project the final results
pipeline.append({"$project": {"_id": 0, "_collection_id": 0, "_manifest": 0}})
pipeline.append(
{"$project": {"_id": 0, "_collection_id": 0, "_manifest": 0}}
)

count = self.get_result_count(pipeline, data)
self.add_pagination_operations(pipeline)
results = list(data.aggregate(pipeline))
results = list(data.aggregate(pipeline, allowDiskUse=True))
else:
# Return raw data from Mongodb
count = self.get_result_count(pipeline, data)
self.add_pagination_operations(pipeline)
results = list(data.aggregate(pipeline))
results = list(data.aggregate(pipeline, allowDiskUse=True))

return count, results

Expand All @@ -133,11 +154,9 @@ def add_pagination_operations(self, pipeline):
def get_result_count(self, pipeline, data):
count_pipeline = list(pipeline)
count_pipeline.append({"$count": "total"})
count_result = list(data.aggregate(count_pipeline))
count_result = list(data.aggregate(count_pipeline, allowDiskUse=True))

if len(count_result) == 0:
# No results
return 0

count = count_result[0]["total"]
return count
return count_result[0]["total"]