Implementing ** for list.

This commit is contained in:
Patrick MARIE 2021-02-22 23:16:10 +01:00
parent 98b799bae0
commit 5b96e608ad
3 changed files with 112 additions and 21 deletions

View File

@ -155,7 +155,7 @@ ARGS:
* command: read * command: read
- human timestamps (but unix timestamps are ok) - human timestamps (but unix timestamps are ok)
* command: list * command: list
- Missing pattern matching like {}, ** - Missing pattern matching like {abc,def}, **, [0-99]
* command: write * command: write
- Arguments handling - Arguments handling
* command: delete * command: delete

View File

@ -14,7 +14,7 @@ use crate::timerange::TimeRange;
use cassandra_cpp::Session as CassSession; use cassandra_cpp::Session as CassSession;
use cassandra_cpp::Uuid as CassUuid; use cassandra_cpp::Uuid as CassUuid;
use cassandra_cpp::{Batch,BatchType,BindRustType,CassCollection,Cluster,Consistency,Error,LogLevel,Map}; use cassandra_cpp::{Batch,BatchType,BindRustType,CassCollection,Cluster,Error,LogLevel,Map};
use cassandra_cpp::{set_level,stmt}; use cassandra_cpp::{set_level,stmt};
use uuid::Uuid; use uuid::Uuid;

View File

@ -7,7 +7,7 @@
use std::convert::TryFrom; use std::convert::TryFrom;
use std::error; use std::error;
use cassandra_cpp::{BindRustType,CassResult,Consistency,Error,Statement}; use cassandra_cpp::{BindRustType,CassResult,Error,Statement};
use cassandra_cpp::{stmt}; use cassandra_cpp::{stmt};
use chrono::Utc; use chrono::Utc;
use clap::{App,AppSettings,Arg,SubCommand}; use clap::{App,AppSettings,Arg,SubCommand};
@ -87,22 +87,112 @@ fn prepare_component_query(table_name: &str, arguments: &Vec<&str>) -> Result<St
Ok(query) Ok(query)
} }
fn prepare_component_query_globstar(table_name: &str, arguments: &Vec<&str>) -> Result<Vec<Statement>, Error> {
let _q = format!("SELECT parent, name FROM biggraphite_metadata.{} WHERE ", table_name);
let _component_number = 0;
let mut out = vec![];
let pos_globstar = arguments.iter().enumerate().filter(|(_, &x)| x == "**").map(|(id, _)| id).collect::<Vec<usize>>();
if pos_globstar.len() != 1 {
// XXX return error
return Ok(vec![prepare_component_query(table_name, arguments)?]);
}
let pos_globstar = pos_globstar[0];
let mut queries = vec![];
let mut init_args = vec![];
let mut end_args = arguments[pos_globstar+1..].to_vec();
end_args.push("__END__");
for (id, el) in arguments[0..pos_globstar].iter().enumerate() {
if *el == "*" {
continue;
}
if el.ends_with("*") {
init_args.push((id, "LIKE", el.replace("*", "%")));
} else {
init_args.push((id, "=", el.to_string()));
}
}
let components = 16;
for id in init_args.len()..(components-end_args.len()+1) {
let mut current_query = init_args.to_vec();
for (sub_id, el) in end_args.iter().enumerate() {
if *el == "*" {
continue;
}
if el.ends_with("*") {
current_query.push((sub_id + id, "LIKE", el.replace("*", "%")));
} else {
current_query.push((sub_id + id, "=", el.to_string()));
}
}
queries.push(current_query);
}
for query in &queries {
let mut current_query = _q.to_string();
for el in query {
if el.0 != 0 {
current_query.push_str("AND ");
}
current_query.push_str(&format!("component_{} {} ? ", el.0, el.1));
}
current_query.push_str(&String::from("ALLOW FILTERING;"));
let mut statement = stmt!(&current_query);
for (id, el) in query.iter().enumerate() {
statement.bind(id, el.2.as_str())?;
}
out.push(statement);
}
Ok(out)
}
fn metric_list(session: &Session, glob: &str) -> Result<(), Error> { fn metric_list(session: &Session, glob: &str) -> Result<(), Error> {
let components = glob.split(".").collect::<Vec<&str>>(); let components = glob.split(".").collect::<Vec<&str>>();
let mut query_directories = prepare_component_query("directories", &components)?; let query_directories = prepare_component_query_globstar("directories", &components)?;
query_directories.set_consistency(session.read_consistency())?; let mut results = vec![];
let result = session.metadata_session().execute(&query_directories).wait()?;
for row in result.iter() { for mut q in query_directories {
let name = row.get_column_by_name("name".to_string()).unwrap().to_string(); q.set_consistency(session.read_consistency())?;
println!("d {}", name); results.push(session.metadata_session().execute(&q));
} }
let mut query = prepare_component_query("metrics", &components)?; for result in results {
query.set_consistency(session.read_consistency())?; let rows = result.wait()?;
let result = session.metadata_session().execute(&query).wait()?; for row in rows.iter() {
let name = row.get_column_by_name("name".to_string()).unwrap().to_string();
println!("d {}", name);
let names = result }
}
let query_metrics = prepare_component_query_globstar("metrics", &components)?;
let mut results = vec![];
for mut q in query_metrics {
q.set_consistency(session.read_consistency())?;
results.push(session.metadata_session().execute(&q));
}
for result in results {
let rows = result.wait()?;
let names = rows
.iter() .iter()
.map(|x| { .map(|x| {
x.get_column_by_name("name".to_string()).unwrap().to_string() x.get_column_by_name("name".to_string()).unwrap().to_string()
@ -113,6 +203,7 @@ fn metric_list(session: &Session, glob: &str) -> Result<(), Error> {
for metric in metrics { for metric in metrics {
println!("m {}", metric); println!("m {}", metric);
} }
}
Ok(()) Ok(())
} }