rainbowadn/rainbowadn/data/collection/mapping/querymapping.py
2022-05-08 21:56:17 +03:00

45 lines
1.8 KiB
Python

from typing import Generic, TypeVar
from rainbowadn.data.collection.collection_interface.querycollectioninterface import QueryCollectionInterface
from rainbowadn.data.collection.keyvalue import KeyValue
from rainbowadn.hashing.hashpoint import HashPoint
from rainbowadn.hashing.hashresolver import HashResolver
from rainbowadn.hashing.nullability.notnull import NotNull
from rainbowadn.hashing.nullability.null import Null
from rainbowadn.hashing.nullability.nullable import Nullable
__all__ = ('QueryMapping',)
KVKeyType = TypeVar('KVKeyType')
KVValueType = TypeVar('KVValueType')
class QueryMapping(Generic[KVKeyType, KVValueType]):
def __init__(
self,
collection: QueryCollectionInterface[KeyValue[KVKeyType, KVValueType]],
empty_value: HashPoint[KVValueType],
resoler: HashResolver
):
assert isinstance(collection, QueryCollectionInterface)
assert isinstance(empty_value, HashPoint)
assert isinstance(resoler, HashResolver)
self.collection = collection
self.empty_value = empty_value
self.resolver = resoler
def query(self, key: HashPoint[KVKeyType]) -> Nullable[HashPoint[KVValueType]]:
assert isinstance(key, HashPoint)
query_result: Nullable[
HashPoint[KeyValue[KVKeyType, KVValueType]]
] = self.collection.query(HashPoint.of(KeyValue(key, self.empty_value)))
assert isinstance(query_result, Nullable)
if isinstance(query_result, Null):
return Null()
elif isinstance(query_result, NotNull):
key_value: KeyValue[KVKeyType, KVValueType] = self.resolver.resolve(query_result.value)
assert isinstance(key_value, KeyValue)
return NotNull(key_value.value)
else:
raise TypeError