Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
used signal_type index and generalize hashstore (#398)
* used signal_type index and generalize hashstore * remove store and move it to base class of dto objects
- Loading branch information
1 parent
749cacd
commit 0d0154a
Showing
7 changed files
with
121 additions
and
79 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,39 +1,108 @@ | ||
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved | ||
|
||
import datetime | ||
import typing as t | ||
from dataclasses import dataclass | ||
from mypy_boto3_dynamodb.service_resource import Table | ||
|
||
""" | ||
Not enforceable because named tuples can't have multiple inheritance, but all | ||
DTO classes in this module should implement methods `to_dynamodb_item(self)` and | ||
Data transfer object classes to be used with dynamodbstore | ||
Classes in this module should implement methods `to_dynamodb_item(self)` and | ||
`to_sqs_message(self)` | ||
""" | ||
|
||
class PDQHashRecord(t.NamedTuple): | ||
class DynamoDBItem(): | ||
|
||
def write_to_table(self, table: Table): | ||
table.put_item(Item=self.to_dynamodb_item()) | ||
|
||
def to_dynamo_db_item(self) -> t.Dict: | ||
raise NotImplementedError | ||
|
||
|
||
@dataclass | ||
class PDQRecordBase(DynamoDBItem): | ||
""" | ||
Successful execution at the hasher produces this record. | ||
Abstract Base Record for PDQ releated items. | ||
""" | ||
|
||
SIGNAL_TYPE = "pdq" | ||
|
||
content_key: str | ||
content_hash: str | ||
quality: int | ||
timestamp: datetime.datetime # ISO-8601 formatted | ||
|
||
@staticmethod | ||
def get_dynamodb_pk(key: str): | ||
def get_dynamodb_content_key(key: str): | ||
return f"c#{key}" | ||
|
||
@staticmethod | ||
def get_dynamodb_type_key(key: str): | ||
return f"type#{key}" | ||
|
||
@staticmethod | ||
def get_dynamodb_type_key(key: str): | ||
return f"type#{key}" | ||
|
||
def to_dynamodb_item(self) -> dict: | ||
raise NotImplementedError | ||
|
||
def to_sqs_message(self) -> dict: | ||
raise NotImplementedError | ||
|
||
|
||
@dataclass | ||
class PipelinePDQHashRecord(PDQRecordBase): | ||
""" | ||
Successful execution at the hasher produces this record. | ||
""" | ||
|
||
quality: int | ||
|
||
def to_dynamodb_item(self) -> dict: | ||
return { | ||
"PK": PDQHashRecord.get_dynamodb_pk(self.content_key), | ||
"SK": "type#pdq", | ||
"PK": self.get_dynamodb_content_key(self.content_key), | ||
"SK": self.get_dynamodb_type_key(self.SIGNAL_TYPE), | ||
"ContentHash": self.content_hash, | ||
"Quality": self.quality, | ||
"Timestamp": self.timestamp.isoformat(), | ||
"HashType": "pdq", | ||
"HashType": self.SIGNAL_TYPE, | ||
} | ||
|
||
def to_sqs_message(self) -> dict: | ||
return { | ||
"hash": self.content_hash, | ||
"type": "pdq", | ||
"key": self.content_key | ||
"type": self.SIGNAL_TYPE, | ||
"key": self.content_key, | ||
} | ||
|
||
|
||
@dataclass | ||
class PDQMatchRecord(PDQRecordBase): | ||
""" | ||
Successful execution at the matcher produces this record. | ||
""" | ||
|
||
te_id: int | ||
te_hash: str | ||
|
||
@staticmethod | ||
def get_dynamodb_te_key(key: str): | ||
return f"te#{key}" | ||
|
||
def to_dynamodb_item(self) -> dict: | ||
return { | ||
"PK": self.get_dynamodb_content_key(self.content_key), | ||
"SK": self.get_dynamodb_te_key(self.te_id), | ||
"ContentHash": self.content_hash, | ||
"Timestamp": self.timestamp.isoformat(), | ||
"TEHash": self.te_hash, | ||
"GSI1-PK": self.get_dynamodb_te_key(self.te_id), | ||
"GSI1-SK": self.get_dynamodb_content_key(self.content_key), | ||
"HashType": self.SIGNAL_TYPE, | ||
"GSI2-PK": self.get_dynamodb_type_key(self.SIGNAL_TYPE), | ||
} | ||
|
||
def to_sqs_message(self) -> dict: | ||
# TODO add method for when matches are added to a sqs | ||
raise NotImplementedError |
This file contains 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 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 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
Empty file.
This file was deleted.
Oops, something went wrong.