Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion crates/gmeta-bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
gmeta-core = { path = "../gmeta-core" }
gmeta-core = { path = "../gmeta-core", features = ["internal"] }
anyhow = { workspace = true }
gix = { workspace = true }
serde_json = { workspace = true }
Expand Down
9 changes: 7 additions & 2 deletions crates/gmeta-bench/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use std::time::{Duration, Instant};

use gmeta_core::types::TargetType;
use gmeta_core::types::{Target, TargetType};
use gmeta_core::Session;

const RESET: &str = "\x1b[0m";
Expand Down Expand Up @@ -40,7 +40,12 @@ pub fn run() -> Result<()> {
continue;
}
};
match db.get(&target_type, target_value, key) {
let target = if target_type == TargetType::Project {
Target::project()
} else {
Target::from_parts(target_type, Some(target_value.clone()))
};
match db.get(&target, key) {
Ok(Some(mv)) => {
let elapsed = t0.elapsed();
let bytes = mv.value.len();
Expand Down
11 changes: 6 additions & 5 deletions crates/gmeta-bench/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use std::io::Write;
use std::time::Instant;

use gmeta_core::db::Store;
use gmeta_core::types::{TargetType, ValueType};
use gmeta_core::tree_paths;
use gmeta_core::types::{Target, TargetType, ValueType};

const RESET: &str = "\x1b[0m";
const BOLD: &str = "\x1b[1m";
Expand Down Expand Up @@ -211,13 +212,13 @@ fn build_bench_tree(
if e.value_type != ValueType::String {
continue;
}
let target = if e.target_type == "project" {
let target = if e.target_type == TargetType::Project {
Target::parse("project")?
} else {
Target::parse(&format!("{}:{}", e.target_type, e.target_value))?
};

let full_path = target.tree_path(&e.key)?;
let full_path = tree_paths::tree_path(&target, &e.key)?;
if e.is_git_ref {
let oid = gix::ObjectId::from_hex(e.value.as_bytes())?;
let blob = oid.attach(repo).object()?.into_blob();
Expand Down Expand Up @@ -304,9 +305,9 @@ pub fn run(rounds: usize) -> Result<()> {
let value = fake_value(&mut rng, 10, 200);
let json_value = serde_json::to_string(&value)?;

let target = Target::from_parts(TargetType::Commit, Some(sha));
db.set(
&TargetType::Commit,
&sha,
&target,
&key,
&json_value,
&ValueType::String,
Expand Down
9 changes: 3 additions & 6 deletions crates/gmeta-cli/src/commands/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{bail, Result};

use crate::context::CommandContext;
use gmeta_core::types::{validate_key, MetaValue, Target, TargetType};
use gmeta_core::types::{validate_key, MetaValue, Target};

const CONFIG_PREFIX: &str = "meta:";

Expand Down Expand Up @@ -41,15 +41,12 @@ fn validate_config_key(key: &str) -> Result<()> {
}

fn project_target() -> Target {
Target {
target_type: TargetType::Project,
value: None,
}
Target::project()
}

fn run_set(handle: &gmeta_core::SessionTargetHandle<'_>, key: &str, value: &str) -> Result<()> {
let meta_value = MetaValue::String(value.to_string());
handle.set_value(key, &meta_value)?;
handle.set(key, meta_value)?;
Ok(())
}

Expand Down
118 changes: 58 additions & 60 deletions crates/gmeta-cli/src/commands/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,55 @@ use serde_json::{json, Map, Value};

use crate::context::CommandContext;
use gmeta_core::db::Store;
use gmeta_core::types::{self, Target, TargetType, ValueType};
use gmeta_core::tree_paths;
use gmeta_core::types::{Target, TargetType, ValueType};

const NODE_VALUE_KEY: &str = "__value";
const SET_VALUE_DIR: &str = "__set";
const TOMBSTONE_ROOT: &str = "__tombstones";

pub fn run(
target_str: &str,
key: Option<&str>,
json_output: bool,
with_authorship: bool,
) -> Result<()> {
let mut target = Target::parse(target_str)?;

let ctx = CommandContext::open(None)?;
ctx.session.resolve_target(&mut target)?;
let target = ctx.session.resolve_target(&Target::parse(target_str)?)?;
let repo = ctx.session.repo();

let include_target_subtree = target.target_type == TargetType::Path;
let mut entries = ctx.session.store().get_all_with_target_prefix(
&target.target_type,
target.value_str(),
include_target_subtree,
key,
)?;
let include_target_subtree = *target.target_type() == TargetType::Path;
let mut entries =
ctx.session
.store()
.get_all_with_target_prefix(&target, include_target_subtree, key)?;

// If no exact match, try prefix expansion for non-commit types
// (commits are already resolved by git, but change-ids/branches may be partial)
if entries.is_empty() && target.target_type != TargetType::Path {
if entries.is_empty() && *target.target_type() != TargetType::Path {
let matches = ctx.session.store().find_target_values_by_prefix(
&target.target_type,
target.value_str(),
target.target_type(),
target.value().unwrap_or(""),
2,
)?;
if matches.len() == 1 {
let expanded = &matches[0];
entries = ctx.session.store().get_all_with_target_prefix(
&target.target_type,
expanded,
false,
key,
)?;
let expanded_target =
Target::from_parts(target.target_type().clone(), Some(expanded.clone()));
entries =
ctx.session
.store()
.get_all_with_target_prefix(&expanded_target, false, key)?;
if !entries.is_empty() {
eprintln!("expanded to {}:{}", target.type_str(), expanded);
eprintln!("expanded to {}:{}", target.target_type().as_str(), expanded);
}
} else if matches.len() > 1 {
eprintln!("ambiguous prefix '{}', matches:", target.value_str());
eprintln!(
"ambiguous prefix '{}', matches:",
target.value().unwrap_or("")
);
for m in &matches {
eprintln!(" {}:{}", target.type_str(), m);
eprintln!(" {}:{}", target.target_type().as_str(), m);
}
return Ok(());
}
Expand All @@ -69,12 +71,11 @@ pub fn run(
.collect();

if !promised.is_empty() {
let hydrated = hydrate_promised_entries(&ctx.session, &target.target_type, &promised)?;
let hydrated = hydrate_promised_entries(&ctx.session, target.target_type(), &promised)?;
if hydrated > 0 {
// Re-query to get the now-resolved values
entries = ctx.session.store().get_all_with_target_prefix(
&target.target_type,
target.value_str(),
&target,
include_target_subtree,
key,
)?;
Expand Down Expand Up @@ -149,7 +150,7 @@ fn hydrate_promised_entries(
};

// Try __value (string) first
if let Ok(path) = parsed_target.tree_path(key) {
if let Ok(path) = tree_paths::tree_path(&parsed_target, key) {
if let Some(oid) =
gmeta_core::git_utils::find_blob_oid_in_tree(repo, tip_tree_id, &path)?
{
Expand All @@ -163,7 +164,7 @@ fn hydrate_promised_entries(
}

// Try __list directory
if let Ok(path) = parsed_target.list_dir_path(key) {
if let Ok(path) = tree_paths::list_dir_path(&parsed_target, key) {
if let Some(dir_oid) =
gmeta_core::git_utils::find_blob_oid_in_tree(repo, tip_tree_id, &path)?
{
Expand All @@ -174,7 +175,7 @@ fn hydrate_promised_entries(
.filter_map(|e| {
let e = e.ok()?;
let name = e.filename().to_str().ok()?;
if name.starts_with("__") || name == types::TOMBSTONE_ROOT {
if name.starts_with("__") || name == TOMBSTONE_ROOT {
return None;
}
if e.mode().is_blob() {
Expand All @@ -196,8 +197,8 @@ fn hydrate_promised_entries(
}

// Try __set directory
if let Ok(key_path) = parsed_target.key_tree_path(key) {
let set_path = format!("{}/{}", key_path, types::SET_VALUE_DIR);
if let Ok(key_path) = tree_paths::key_tree_path(&parsed_target, key) {
let set_path = format!("{}/{}", key_path, SET_VALUE_DIR);
if let Some(dir_oid) =
gmeta_core::git_utils::find_blob_oid_in_tree(repo, tip_tree_id, &set_path)?
{
Expand All @@ -207,7 +208,7 @@ fn hydrate_promised_entries(
.filter_map(|e| {
let e = e.ok()?;
let name = e.filename().to_str().ok()?;
if name.starts_with("__") || name == types::TOMBSTONE_ROOT {
if name.starts_with("__") || name == TOMBSTONE_ROOT {
return None;
}
if e.mode().is_blob() {
Expand All @@ -234,7 +235,12 @@ fn hydrate_promised_entries(
// Clean up entries that no longer exist in the tip
for idx in &not_found {
let (target_value, key) = &entries[*idx];
db.delete_promised(target_type, target_value, key)?;
let entry_target = if *target_type == TargetType::Project {
Target::project()
} else {
Target::from_parts(target_type.clone(), Some(target_value.clone()))
};
db.delete_promised(&entry_target, key)?;
}

if pending.is_empty() {
Expand Down Expand Up @@ -267,6 +273,11 @@ fn hydrate_promised_entries(
let mut hydrated = 0;
for entry in &pending {
let (target_value, key) = &entries[entry.idx];
let entry_target = if *target_type == TargetType::Project {
Target::project()
} else {
Target::from_parts(target_type.clone(), Some(target_value.clone()))
};

match entry.value_type {
ValueType::String => {
Expand All @@ -280,14 +291,7 @@ fn hydrate_promised_entries(
Err(_) => continue,
};
let json_value = serde_json::to_string(content)?;
db.resolve_promised(
target_type,
target_value,
key,
&json_value,
&ValueType::String,
false,
)?;
db.resolve_promised(&entry_target, key, &json_value, &ValueType::String, false)?;
hydrated += 1;
}
ValueType::List => {
Expand All @@ -302,14 +306,7 @@ fn hydrate_promised_entries(
}
}
let json_value = serde_json::to_string(&list_entries)?;
db.resolve_promised(
target_type,
target_value,
key,
&json_value,
&ValueType::List,
false,
)?;
db.resolve_promised(&entry_target, key, &json_value, &ValueType::List, false)?;
hydrated += 1;
}
ValueType::Set => {
Expand All @@ -325,14 +322,7 @@ fn hydrate_promised_entries(
}
set_members.sort();
let json_value = serde_json::to_string(&set_members)?;
db.resolve_promised(
target_type,
target_value,
key,
&json_value,
&ValueType::Set,
false,
)?;
db.resolve_promised(&entry_target, key, &json_value, &ValueType::Set, false)?;
hydrated += 1;
}
_ => anyhow::bail!("unsupported value type"),
Expand Down Expand Up @@ -381,7 +371,7 @@ fn print_plain(
let labels: Vec<String> = entries
.iter()
.map(|(tv, k, _, _)| {
if target.target_type == TargetType::Path {
if *target.target_type() == TargetType::Path {
format!("{};{}", tv, k)
} else {
k.clone()
Expand Down Expand Up @@ -455,7 +445,15 @@ fn print_json(
let parsed_value = parse_stored_value(value, value_type)?;

let leaf_value = if with_authorship {
let authorship = db.get_authorship(&target.target_type, entry_target_value, key)?;
let entry_target = Target::from_parts(
target.target_type().clone(),
if entry_target_value.is_empty() {
None
} else {
Some(entry_target_value.clone())
},
);
let authorship = db.get_authorship(&entry_target, key)?;
let (author, timestamp) = match authorship {
Some(a) => (a.email, a.timestamp),
None => ("unknown".to_string(), 0),
Expand All @@ -469,7 +467,7 @@ fn print_json(
parsed_value
};

if target.target_type == TargetType::Path {
if *target.target_type() == TargetType::Path {
insert_nested(
&mut root,
&[entry_target_value.as_str(), key.as_str()],
Expand Down
Loading
Loading