|
| 1 | +from hazelcast.serialization.api import IdentifiedDataSerializable |
| 2 | + |
| 3 | +_PROJECTIONS_FACTORY_ID = -30 |
| 4 | + |
| 5 | + |
| 6 | +class Projection(object): |
| 7 | + """Marker base class for all projections. |
| 8 | +
|
| 9 | + Projections allow the client to transform (strip down) each query result |
| 10 | + object in order to avoid redundant network traffic. |
| 11 | + """ |
| 12 | + |
| 13 | + pass |
| 14 | + |
| 15 | + |
| 16 | +class _AbstractProjection(Projection, IdentifiedDataSerializable): |
| 17 | + def write_data(self, object_data_output): |
| 18 | + raise NotImplementedError("write_data") |
| 19 | + |
| 20 | + def read_data(self, object_data_input): |
| 21 | + pass |
| 22 | + |
| 23 | + def get_factory_id(self): |
| 24 | + return _PROJECTIONS_FACTORY_ID |
| 25 | + |
| 26 | + def get_class_id(self): |
| 27 | + raise NotImplementedError("get_class_id") |
| 28 | + |
| 29 | + |
| 30 | +def _validate_attribute_path(attribute_path): |
| 31 | + if not attribute_path: |
| 32 | + raise ValueError("attribute_path must not be None or empty") |
| 33 | + |
| 34 | + if "[any]" in attribute_path: |
| 35 | + raise ValueError("attribute_path must not contain [any] operators") |
| 36 | + |
| 37 | + |
| 38 | +class _SingleAttributeProjection(_AbstractProjection): |
| 39 | + def __init__(self, attribute_path): |
| 40 | + _validate_attribute_path(attribute_path) |
| 41 | + self._attribute_path = attribute_path |
| 42 | + |
| 43 | + def write_data(self, object_data_output): |
| 44 | + object_data_output.write_string(self._attribute_path) |
| 45 | + |
| 46 | + def get_class_id(self): |
| 47 | + return 0 |
| 48 | + |
| 49 | + |
| 50 | +class _MultiAttributeProjection(_AbstractProjection): |
| 51 | + def __init__(self, *attribute_paths): |
| 52 | + if not attribute_paths: |
| 53 | + raise ValueError("Specify at least one attribute path") |
| 54 | + |
| 55 | + for attribute_path in attribute_paths: |
| 56 | + _validate_attribute_path(attribute_path) |
| 57 | + |
| 58 | + self.attribute_paths = attribute_paths |
| 59 | + |
| 60 | + def write_data(self, object_data_output): |
| 61 | + object_data_output.write_string_array(self.attribute_paths) |
| 62 | + |
| 63 | + def get_class_id(self): |
| 64 | + return 1 |
| 65 | + |
| 66 | + |
| 67 | +class _IdentityProjection(_AbstractProjection): |
| 68 | + def write_data(self, object_data_output): |
| 69 | + pass |
| 70 | + |
| 71 | + def get_class_id(self): |
| 72 | + return 2 |
| 73 | + |
| 74 | + |
| 75 | +def single_attribute(attribute_path): |
| 76 | + """Creates a projection that extracts the value of |
| 77 | + the given attribute path. |
| 78 | +
|
| 79 | + Args: |
| 80 | + attribute_path (str): Path to extract the attribute from. |
| 81 | +
|
| 82 | + Returns: |
| 83 | + Projection[any]: A projection that extracts the value of the given |
| 84 | + attribute path. |
| 85 | + """ |
| 86 | + return _SingleAttributeProjection(attribute_path) |
| 87 | + |
| 88 | + |
| 89 | +def multi_attribute(*attribute_paths): |
| 90 | + """Creates a projection that extracts the values of |
| 91 | + one or more attribute paths. |
| 92 | +
|
| 93 | + Args: |
| 94 | + *attribute_paths (str): Paths to extract the attributes from. |
| 95 | +
|
| 96 | + Returns: |
| 97 | + Projection[list]: A projection that extracts the values of the given |
| 98 | + attribute paths. |
| 99 | + """ |
| 100 | + return _MultiAttributeProjection(*attribute_paths) |
| 101 | + |
| 102 | + |
| 103 | +def identity(): |
| 104 | + """Creates a projection that does no transformation. |
| 105 | +
|
| 106 | + Returns: |
| 107 | + Projection[hazelcast.core.MapEntry]: A projection that does no |
| 108 | + transformation. |
| 109 | + """ |
| 110 | + return _IdentityProjection() |
0 commit comments