Resume output of original table paths

This commit is contained in:
Josh Hansen 2023-08-30 17:11:53 -07:00
parent 0f605d1fe8
commit d175733379
5 changed files with 39 additions and 13 deletions

View file

@ -33,7 +33,7 @@
-[x] Sensible default destination for `idx`
-[x] Make `get` stop listing nested indices
-[x] Make `get` accelerate traversal using the best available index (a la `del`)
-[ ] Make `get` return paths from original index rather than the one used to accelerate
-[x] Make `get` return paths from original index rather than the one used to accelerate
-[x] Fix output order, making commands reproducible
## 0.5

View file

@ -23,8 +23,8 @@ Initialized ./people/Darrel
Linked ./people:id/5 -> ./people/Darrel
Linked ./people/:state:id/MI/5 -> ./people/Darrel
+ ghee get -a -w state=CA -f name ./people
./people/:state:id/CA/0 user.name Wulfrum
./people/:state:id/CA/2 user.name Sandeep
./people/Wulfrum user.name Wulfrum
./people/Sandeep user.name Sandeep
+ ghee del -v ./people -w name=Sofia
Removed ./people:id/1
Removed ./people/Sofia

View file

@ -29,6 +29,8 @@ pub fn get(
recursive,
all,
&|record| {
let output_path = record.original_path()?;
// Fields that will be output
let projected_fields: Vec<Xattr> = if fields.is_empty() {
record.xattr_values.keys().cloned().collect()
@ -46,7 +48,7 @@ pub fn get(
if !xattrs.is_empty() {
let file_xattrs = FileXattrs {
path: record.path.to_string_lossy().to_string(),
path: output_path.display().to_string(),
xattrs,
};
@ -59,7 +61,7 @@ pub fn get(
} else {
for field in projected_fields.iter() {
if let Some(value) = record.xattr_values.get(field) {
print!("{}\t{}\t", record.path.display(), field);
print!("{}\t{}\t", output_path.display(), field);
{
let mut stdout = std::io::stdout();

View file

@ -128,7 +128,7 @@ pub fn xattr_values_from_path(
Ok(values)
}
pub struct PathVisit<'a, 'b, 'c, 'd> {
pub struct PathVisit<'a, 'b, 'c, 'd, 'e> {
/// The indices of the table to which this path belongs
indices: &'a BTreeMap<Key, PathBuf>,
@ -140,9 +140,12 @@ pub struct PathVisit<'a, 'b, 'c, 'd> {
/// The xattr or field values of the path we're visiting
xattr_values: &'d BTreeMap<Xattr, Value>,
/// The key of the original table targeted
original_table_key: &'e Key,
}
impl<'a, 'b, 'c, 'd> PathVisit<'a, 'b, 'c, 'd> {
impl<'a, 'b, 'c, 'd, 'e> PathVisit<'a, 'b, 'c, 'd, 'e> {
pub fn is_table_root(&self) -> bool {
self.table_path == self.path
}
@ -162,6 +165,21 @@ impl<'a, 'b, 'c, 'd> PathVisit<'a, 'b, 'c, 'd> {
.iter()
.filter(|(k, _v)| !(k.namespace == Namespace::User && k.attr.starts_with("ghee")))
}
/// The path by which this record is reachable in the original table/index targeted
pub fn original_path(&self) -> Result<PathBuf> {
let subkey_values = self
.original_table_key
.value_for_record(self.xattr_values)?;
let mut path = self.indices[self.original_table_key].clone();
for sub in subkey_values {
path.push(sub.to_string());
}
Ok(path)
}
}
/// Return all indices of the table whose path is given
@ -281,14 +299,18 @@ pub fn walk<F: Fn(PathVisit) -> Result<()>>(
None
};
let (path, indices) = match path_or_table_indices {
let (original_table_path, indices) = match path_or_table_indices {
PathOrIndices::Path(path) => (path, loaded_indices.as_ref().unwrap()),
PathOrIndices::PathAndIndices { path, indices } => (path, indices),
};
let key = indices.iter().find(|(_key, p)| *p == path).unwrap().0;
let original_table_key = indices
.iter()
.find(|(_key, p)| *p == original_table_path)
.unwrap()
.0;
let (key, path) = best_index(indices, where_, key);
let (key, path) = best_index(indices, where_, original_table_key);
let path_len = path.components().count();
@ -357,6 +379,7 @@ pub fn walk<F: Fn(PathVisit) -> Result<()>>(
table_path: path,
path: &entry.path().to_path_buf(),
xattr_values: &values,
original_table_key,
};
visitor(visit)?;

View file

@ -125,9 +125,10 @@ impl Key {
let mut values: Vec<Value> = Vec::with_capacity(self.subkeys.len());
for subkey in self.subkeys.iter() {
let json_value = record.get(&subkey).ok_or(KeyErr::SubkeyValueNotFound)?;
let value = Value::try_from(json_value.clone())?;
let value = record
.get(&subkey)
.ok_or(KeyErr::SubkeyValueNotFound)?
.clone();
values.push(value);
}