Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 71 additions & 5 deletions src/index/src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,79 @@ use serde::{Deserialize, Serialize};
use snafu::{Snafu, ensure};
use store_api::storage::ColumnId;

/// Describes an index target. Column ids are the only supported variant for now.
/// Describes an index target.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum IndexTarget {
ColumnId(ColumnId),
ColumnNestedPath {
column_id: ColumnId,
path: Vec<String>,
},
}

impl Display for IndexTarget {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
IndexTarget::ColumnId(id) => write!(f, "{}", id),
IndexTarget::ColumnNestedPath { column_id, path } => {
write!(f, "{}:{}", column_id, path.join("."))
}
}
}
}

impl IndexTarget {
/// Parse a target key string back into an index target description.
pub fn decode(key: &str) -> Result<Self, TargetKeyError> {
validate_column_key(key)?;
let id = key
ensure!(!key.is_empty(), EmptySnafu);

let (col_id, nested_path) = match key.split_once(':') {
Some((col_id, path)) => (col_id, Some(path)),
None => (key, None),
};

validate_column_key(col_id)?;

let col_id = col_id
.parse::<ColumnId>()
.map_err(|_| InvalidColumnIdSnafu { value: key }.build())?;
Ok(IndexTarget::ColumnId(id))
.map_err(|_| InvalidColumnIdSnafu { value: col_id }.build())?;

let Some(nested_path) = nested_path else {
return Ok(IndexTarget::ColumnId(col_id));
};

let nested_path_str = nested_path.trim();
ensure!(!nested_path_str.is_empty(), InvalidPathSnafu { key });
// FIXME(fys): do we need to handle special characters in here and encode method?
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The use of . and : as delimiters in the encoded IndexTarget string without escaping is problematic. JSON keys frequently contain these characters. For example, a nested path like ["a.b", "c"] would be encoded as col_id:a.b.c, which the decode method would incorrectly parse as ["a", "b", "c"]. Consider implementing an escaping mechanism or using a more robust serialization format for the target key to avoid collisions.

let nested_path = nested_path_str
.split('.')
.map(str::trim)
.map(ToString::to_string)
.collect::<Vec<_>>();
ensure!(
nested_path
.iter()
.all(|seg| !seg.is_empty() && !seg.contains(':')),
InvalidPathSnafu { key }
);
Ok(IndexTarget::ColumnNestedPath {
column_id: col_id,
path: nested_path,
})
}

pub fn column_id(&self) -> ColumnId {
match self {
IndexTarget::ColumnId(id) => *id,
IndexTarget::ColumnNestedPath { column_id, .. } => *column_id,
}
}

pub fn path(&self) -> Option<&[String]> {
match self {
IndexTarget::ColumnId(_) => None,
IndexTarget::ColumnNestedPath { path, .. } => Some(path),
}
}
}

Expand All @@ -59,6 +110,9 @@ pub enum TargetKeyError {

#[snafu(display("failed to parse column id from '{value}'"))]
InvalidColumnId { value: String },

#[snafu(display("invalid target path in key '{key}'"))]
InvalidPath { key: String },
}

impl ErrorExt for TargetKeyError {
Expand Down Expand Up @@ -104,4 +158,16 @@ mod tests {
let err = IndexTarget::decode("1a2").unwrap_err();
assert!(matches!(err, TargetKeyError::InvalidCharacters { .. }));
}

#[test]
fn encode_decode_column_path() {
let target = IndexTarget::ColumnNestedPath {
column_id: 42,
path: vec!["a".to_string(), "b".to_string()],
};
let key = format!("{}", target);
assert_eq!(key, "42:a.b");
let decoded = IndexTarget::decode(&key).unwrap();
assert_eq!(decoded, target);
}
}
Loading
Loading