-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathhelpers.py
More file actions
72 lines (58 loc) · 2.38 KB
/
helpers.py
File metadata and controls
72 lines (58 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import struct
from datetime import datetime, timezone
from typing import Any, List
import mmh3
from feast.importer import import_class
from feast.infra.key_encoding_utils import (
serialize_entity_key,
serialize_entity_key_prefix,
)
from feast.infra.online_stores.online_store import OnlineStore
from feast.protos.feast.types.EntityKey_pb2 import EntityKey as EntityKeyProto
def get_online_store_from_config(online_store_config: Any) -> OnlineStore:
"""Creates an online store corresponding to the given online store config."""
module_name = online_store_config.__module__
qualified_name = type(online_store_config).__name__
class_name = qualified_name.replace("Config", "")
online_store_class = import_class(module_name, class_name, "OnlineStore")
return online_store_class()
def _redis_key(
project: str, entity_key: EntityKeyProto, entity_key_serialization_version=3
) -> bytes:
key: List[bytes] = [
serialize_entity_key(
entity_key,
entity_key_serialization_version=entity_key_serialization_version,
),
project.encode("utf-8"),
]
return b"".join(key)
def _redis_key_prefix(entity_keys: List[str]) -> bytes:
return serialize_entity_key_prefix(entity_keys)
def _mmh3(key: str):
"""
Calculate murmur3_32 hash which is equal to scala version which is using little endian:
https://stackoverflow.com/questions/29932956/murmur3-hash-different-result-between-python-and-java-implementation
https://stackoverflow.com/questions/13141787/convert-decimal-int-to-little-endian-string-x-x
"""
key_hash = mmh3.hash(key, signed=False)
return bytes.fromhex(struct.pack("<Q", key_hash).hex()[:8])
def compute_entity_id(
entity_key: EntityKeyProto, entity_key_serialization_version=3
) -> str:
"""
Compute Entity id given Feast Entity Key for online stores.
Remember that Entity here refers to `EntityKeyProto` which is used in some online stores to encode the keys.
It has nothing to do with the Entity concept we have in Feast.
"""
return mmh3.hash_bytes(
serialize_entity_key(
entity_key,
entity_key_serialization_version=entity_key_serialization_version,
)
).hex()
def _to_naive_utc(ts: datetime) -> datetime:
if ts.tzinfo is None:
return ts
else:
return ts.astimezone(tz=timezone.utc).replace(tzinfo=None)