-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Oleg Avdeev <[email protected]>
- Loading branch information
Showing
12 changed files
with
403 additions
and
35 deletions.
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import struct | ||
from typing import List, Tuple | ||
|
||
from feast.types.EntityKey_pb2 import EntityKey as EntityKeyProto | ||
from feast.types.Value_pb2 import Value as ValueProto | ||
from feast.types.Value_pb2 import ValueType | ||
|
||
|
||
def _serialize_val(value_type, v: ValueProto) -> Tuple[bytes, int]: | ||
if value_type == "string_val": | ||
return v.string_val.encode("utf8"), ValueType.STRING | ||
elif value_type == "bytes_val": | ||
return v.bytes_val, ValueType.BYTES | ||
elif value_type == "int32_val": | ||
return struct.pack("<i", v.int32_val), ValueType.INT32 | ||
elif value_type == "int64_val": | ||
return struct.pack("<l", v.int64_val), ValueType.INT64 | ||
else: | ||
raise ValueError(f"Value type not supported for Firestore: {v}") | ||
|
||
|
||
def serialize_entity_key(entity_key: EntityKeyProto) -> bytes: | ||
""" | ||
Serialize entity key to a bytestring so it can be used as a lookup key in a hash table. | ||
We need this encoding to be stable; therefore we cannot just use protobuf serialization | ||
here since it does not guarantee that two proto messages containing the same data will | ||
serialize to the same byte string[1]. | ||
[1] https://developers.google.com/protocol-buffers/docs/encoding | ||
""" | ||
sorted_keys, sorted_values = zip( | ||
*sorted(zip(entity_key.entity_names, entity_key.entity_values)) | ||
) | ||
|
||
output: List[bytes] = [] | ||
for k in sorted_keys: | ||
output.append(struct.pack("<I", ValueType.STRING)) | ||
output.append(k.encode("utf8")) | ||
for v in sorted_values: | ||
val_bytes, value_type = _serialize_val(v.WhichOneof("val"), v) | ||
|
||
output.append(struct.pack("<I", value_type)) | ||
|
||
output.append(struct.pack("<I", len(val_bytes))) | ||
output.append(val_bytes) | ||
|
||
return b"".join(output) |
Oops, something went wrong.