rainbowadn/rainbowadn/data/collection/keymetadataquerycollection.py

37 lines
1.5 KiB
Python

from typing import Generic, TypeVar
from rainbowadn.data.collection.collection_interface.querycollectioninterface import QueryCollectionInterface
from rainbowadn.data.collection.keymetadata import KeyMetadata
from rainbowadn.hashing.hashpoint import HashPoint
from rainbowadn.hashing.nullability.notnull import NotNull
from rainbowadn.hashing.nullability.null import Null
from rainbowadn.hashing.nullability.nullable import Nullable
__all__ = ('KeyMetadataQueryCollection',)
ActiveKeyType = TypeVar('ActiveKeyType')
MetaDataType = TypeVar('MetaDataType')
class KeyMetadataQueryCollection(QueryCollectionInterface[ActiveKeyType], Generic[ActiveKeyType, MetaDataType]):
def __init__(
self,
metadata: HashPoint[MetaDataType],
collection: QueryCollectionInterface[KeyMetadata[ActiveKeyType, MetaDataType]],
):
assert isinstance(metadata, HashPoint)
assert isinstance(collection, QueryCollectionInterface)
self.metadata = metadata
self.collection = collection
def query(self, key: HashPoint[ActiveKeyType]) -> Nullable[HashPoint[ActiveKeyType]]:
assert isinstance(key, HashPoint)
result: Nullable[
HashPoint[KeyMetadata[ActiveKeyType, MetaDataType]]
] = self.collection.query(HashPoint.of(KeyMetadata(key, self.metadata)))
assert isinstance(result, Nullable)
if result.null():
return Null()
else:
return NotNull(result.resolve().resolve().key)