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

Support Multi Mosaic in DynamoDB Table #127

Merged
merged 18 commits into from
Oct 22, 2020
Merged
43 changes: 20 additions & 23 deletions cogeo_mosaic/backends/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import boto3
import click
import mercantile
from boto3.dynamodb.conditions import Key
from botocore.exceptions import ClientError
from cachetools.keys import hashkey
from rio_tiler.utils import _chunks

from cogeo_mosaic.backends.base import BaseBackend
from cogeo_mosaic.backends.utils import find_quadkeys
Expand Down Expand Up @@ -93,11 +95,8 @@ def info(self, quadkeys: bool = False):
def _quadkeys(self) -> List[str]:
"""Return the list of quadkey tiles."""
resp = self.table.query(
KeyConditionExpression="#mosaicId = :mosaicId",
# allows you to use dyanmodb reserved keywords as field names
ExpressionAttributeNames={"#mosaicId": "mosaicId", "#quadKey": "quadKey"},
ExpressionAttributeValues={":mosaicId": {"S": self.mosaic_name}},
ProjectionExpression="#quadKey",
KeyConditionExpression=Key("mosaicId").eq(self.mosaic_name),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ very readable

ProjectionExpression="quadkey",
)
return [qk["quadkey"] for qk in resp["Items"] if qk["quadkey"] != "-1"]

Expand Down Expand Up @@ -296,27 +295,25 @@ def clean(self):
"""clean MosaicID from dynamoDB Table."""
logger.debug(f"Deleting items for mosaic {self.mosaic_name}...")

# get items
# get quadkeys
resp = self.table.query(
KeyConditionExpression="#mosaicId = :mosaicId",
ExpressionAttributeNames={"#mosaicId": "mosaicId", "#quadKey": "quadKey"},
ExpressionAttributeValues={":mosaicId": {"S": self.mosaic_name}},
ProjectionExpression="#quadKey",
KeyConditionExpression=Key("mosaicId").eq(self.mosaic_name),
ProjectionExpression="quadkey",
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved
)
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved

# delete items
for i in resp["Items"]:
self.client.batch_write_item(
RequestItems={
self.table_name: [
{
"DeleteRequest": {
"Key": {
"mosaicId": {"S": i["mosaicId"]},
"quadkey": {"S": i["quadkey"]},
}
}
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.batch_write_item
max_items = 25
for c in _chunks(resp["Items"], max_items):
items = [
{
"DeleteRequest": {
"Key": {
"mosaicId": {"S": self.mosaic_name},
"quadkey": {"S": item["quadkey"]},
}
]
}
}
)
for item in c
]
self.client.batch_write_item(RequestItems={self.table_name: items})
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved