use crate::{
db::access::{AccessPlan, normalize_access_plan_value},
db::query::plan::{PrimaryKeyInputResourceSummary, primary_key_input_resource_from_value_list},
traits::KeyValueCodec,
value::Value,
};
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db::query) enum KeyAccess<K> {
Single(K),
Many(Vec<K>),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(in crate::db::query) enum KeyAccessKind {
Single,
Many,
Only,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db::query) struct KeyAccessState<K> {
pub(in crate::db::query::intent) kind: KeyAccessKind,
pub(in crate::db::query::intent) access: KeyAccess<K>,
}
pub(in crate::db::query) struct KeyAccessPlanningProjection {
access_plan: AccessPlan<Value>,
input_resource: Option<PrimaryKeyInputResourceSummary>,
}
impl KeyAccessPlanningProjection {
#[must_use]
pub(in crate::db::query) fn into_parts(
self,
) -> (AccessPlan<Value>, Option<PrimaryKeyInputResourceSummary>) {
(self.access_plan, self.input_resource)
}
}
pub(in crate::db::query) fn build_access_plan_from_keys<K>(
access: &KeyAccess<K>,
) -> AccessPlan<Value>
where
K: KeyValueCodec,
{
let plan = match access {
KeyAccess::Single(key) => AccessPlan::by_key(key.to_key_value()),
KeyAccess::Many(keys) => {
let mut values = Vec::with_capacity(keys.len());
values.extend(keys.iter().map(KeyValueCodec::to_key_value));
AccessPlan::by_keys(values)
}
};
normalize_access_plan_value(plan)
}
pub(in crate::db::query) fn project_key_access_for_planning<K>(
access: &KeyAccess<K>,
) -> KeyAccessPlanningProjection
where
K: KeyValueCodec,
{
let (plan, input_resource) = match access {
KeyAccess::Single(key) => (AccessPlan::by_key(key.to_key_value()), None),
KeyAccess::Many(keys) => {
let mut values = Vec::with_capacity(keys.len());
values.extend(keys.iter().map(KeyValueCodec::to_key_value));
let input_resource = primary_key_input_resource_from_value_list(&values);
(AccessPlan::by_keys(values), input_resource)
}
};
KeyAccessPlanningProjection {
access_plan: normalize_access_plan_value(plan),
input_resource,
}
}