use nodedb_types::value::Value;
use crate::error::LiteError;
use crate::query::assignments::resolve_updates_with_excluded;
use crate::query::engine::LiteQueryEngine;
use crate::storage::engine::StorageEngine;
use super::super::reads::row_to_object;
pub(super) async fn find_row<S: StorageEngine>(
engine: &LiteQueryEngine<S>,
collection: &str,
pk: &Value,
) -> Result<Option<Vec<Value>>, LiteError> {
let schema = match engine.columnar.schema(collection) {
Some(s) => s,
None => return Ok(None),
};
let pk_idx = schema
.columns
.iter()
.position(|c| c.primary_key)
.unwrap_or(0);
let rows = engine.columnar.list_rows(collection).await?;
Ok(rows
.into_iter()
.find(|row| row.get(pk_idx).map(|v| v == pk).unwrap_or(false)))
}
type UpdateValue = nodedb_physical::physical_plan::document::types::UpdateValue;
pub(super) fn apply_conflict_updates(
mut existing: Vec<Value>,
incoming: &Value,
updates: &[(String, UpdateValue)],
col_names: &[String],
) -> Result<Vec<Value>, LiteError> {
let current = row_to_object(col_names, &existing);
let resolved = resolve_updates_with_excluded(updates, ¤t, incoming)?;
for (field, value) in resolved {
if let Some(col_idx) = col_names.iter().position(|n| *n == field)
&& let Some(slot) = existing.get_mut(col_idx)
{
*slot = value;
}
}
Ok(existing)
}