use crate::models::*;
use serde_json::Value;
use std::collections::HashMap;
pub struct FilterBuilder {
conditions: Vec<FilterCondition>,
}
impl FilterBuilder {
pub fn new() -> Self {
Self {
conditions: Vec::new(),
}
}
pub fn gt(mut self, field: impl Into<String>, value: impl Into<Value>) -> Self {
self.conditions.push(FilterCondition {
field: field.into(),
operator: FilterOperator::GreaterThan,
value: value.into(),
});
self
}
pub fn gte(mut self, field: impl Into<String>, value: impl Into<Value>) -> Self {
self.conditions.push(FilterCondition {
field: field.into(),
operator: FilterOperator::GreaterThanOrEqual,
value: value.into(),
});
self
}
pub fn lt(mut self, field: impl Into<String>, value: impl Into<Value>) -> Self {
self.conditions.push(FilterCondition {
field: field.into(),
operator: FilterOperator::LessThan,
value: value.into(),
});
self
}
pub fn lte(mut self, field: impl Into<String>, value: impl Into<Value>) -> Self {
self.conditions.push(FilterCondition {
field: field.into(),
operator: FilterOperator::LessThanOrEqual,
value: value.into(),
});
self
}
pub fn in_values(mut self, field: impl Into<String>, values: Vec<impl Into<Value>>) -> Self {
let values: Vec<Value> = values.into_iter().map(|v| v.into()).collect();
self.conditions.push(FilterCondition {
field: field.into(),
operator: FilterOperator::In,
value: Value::Array(values),
});
self
}
pub fn not_in(mut self, field: impl Into<String>, values: Vec<impl Into<Value>>) -> Self {
let values: Vec<Value> = values.into_iter().map(|v| v.into()).collect();
self.conditions.push(FilterCondition {
field: field.into(),
operator: FilterOperator::NotIn,
value: Value::Array(values),
});
self
}
pub fn build(self) -> Filter {
Filter::and(self.conditions)
}
}
impl Default for FilterBuilder {
fn default() -> Self {
Self::new()
}
}
pub struct IndexParamsBuilder {
params: IndexParams,
}
impl IndexParamsBuilder {
pub fn new() -> Self {
Self {
params: IndexParams::new(),
}
}
pub fn metric_type(mut self, metric_type: impl Into<String>) -> Self {
self.params.metric_type = Some(metric_type.into());
self
}
pub fn ef_search(mut self, ef_search: i32) -> Self {
self.params.ef_search = Some(ef_search);
self
}
pub fn nprobe(mut self, nprobe: i32) -> Self {
self.params.nprobe = Some(nprobe);
self
}
pub fn parallel_on_queries(mut self, parallel: i32) -> Self {
self.params.parallel_on_queries = Some(parallel);
self
}
pub fn build(self) -> IndexParams {
self.params
}
}
impl Default for IndexParamsBuilder {
fn default() -> Self {
Self::new()
}
}
pub struct SearchRequestBuilder {
request: SearchRequest,
}
impl SearchRequestBuilder {
pub fn new(db_name: impl Into<String>, space_name: impl Into<String>) -> Self {
Self {
request: SearchRequest {
db_name: db_name.into(),
space_name: space_name.into(),
vectors: Vec::new(),
filters: None,
index_params: None,
ranker: None,
is_brute_search: None,
vector_value: None,
load_balance: None,
limit: None,
},
}
}
pub fn with_vector(mut self, vector: VectorField) -> Self {
self.request.vectors.push(vector);
self
}
pub fn with_filters(mut self, filters: Filter) -> Self {
self.request.filters = Some(filters);
self
}
pub fn with_index_params(mut self, params: IndexParams) -> Self {
self.request.index_params = Some(params);
self
}
pub fn with_ranker(mut self, ranker: Ranker) -> Self {
self.request.ranker = Some(ranker);
self
}
pub fn brute_search(mut self, brute: bool) -> Self {
self.request.is_brute_search = Some(if brute { 1 } else { 0 });
self
}
pub fn include_vectors(mut self, include: bool) -> Self {
self.request.vector_value = Some(include);
self
}
pub fn load_balance(mut self, strategy: impl Into<String>) -> Self {
self.request.load_balance = Some(strategy.into());
self
}
pub fn limit(mut self, limit: i32) -> Self {
self.request.limit = Some(limit);
self
}
pub fn build(self) -> SearchRequest {
self.request
}
}
pub struct QueryRequestBuilder {
request: QueryRequest,
}
impl QueryRequestBuilder {
pub fn new(db_name: impl Into<String>, space_name: impl Into<String>) -> Self {
Self {
request: QueryRequest {
db_name: db_name.into(),
space_name: space_name.into(),
document_ids: None,
partition_id: None,
filters: None,
fields: None,
vector_value: None,
limit: None,
},
}
}
pub fn with_ids(mut self, ids: Vec<String>) -> Self {
self.request.document_ids = Some(ids);
self
}
pub fn with_partition(mut self, partition_id: i32) -> Self {
self.request.partition_id = Some(partition_id);
self
}
pub fn with_filters(mut self, filters: Filter) -> Self {
self.request.filters = Some(filters);
self
}
pub fn with_fields(mut self, fields: Vec<String>) -> Self {
self.request.fields = Some(fields);
self
}
pub fn include_vectors(mut self, include: bool) -> Self {
self.request.vector_value = Some(include);
self
}
pub fn limit(mut self, limit: i32) -> Self {
self.request.limit = Some(limit);
self
}
pub fn build(self) -> QueryRequest {
self.request
}
}
pub struct DeleteRequestBuilder {
request: DeleteRequest,
}
impl DeleteRequestBuilder {
pub fn new(db_name: impl Into<String>, space_name: impl Into<String>) -> Self {
Self {
request: DeleteRequest {
db_name: db_name.into(),
space_name: space_name.into(),
document_ids: None,
filters: None,
limit: None,
},
}
}
pub fn with_ids(mut self, ids: Vec<String>) -> Self {
self.request.document_ids = Some(ids);
self
}
pub fn with_filters(mut self, filters: Filter) -> Self {
self.request.filters = Some(filters);
self
}
pub fn limit(mut self, limit: i32) -> Self {
self.request.limit = Some(limit);
self
}
pub fn build(self) -> DeleteRequest {
self.request
}
}
pub mod utils {
use super::*;
pub fn filter(field: impl Into<String>, operator: FilterOperator, value: impl Into<Value>) -> Filter {
Filter::and(vec![FilterCondition {
field: field.into(),
operator,
value: value.into(),
}])
}
pub fn range_filter(
field: impl Into<String> + Clone,
min: Option<impl Into<Value>>,
max: Option<impl Into<Value>>,
) -> Filter {
let mut conditions = Vec::new();
let field_string = field.into();
if let Some(min_val) = min {
conditions.push(FilterCondition {
field: field_string.clone(),
operator: FilterOperator::GreaterThanOrEqual,
value: min_val.into(),
});
}
if let Some(max_val) = max {
conditions.push(FilterCondition {
field: field_string,
operator: FilterOperator::LessThanOrEqual,
value: max_val.into(),
});
}
Filter::and(conditions)
}
pub fn text_filter(field: impl Into<String>, values: Vec<String>) -> Filter {
Filter::and(vec![FilterCondition {
field: field.into(),
operator: FilterOperator::In,
value: Value::Array(values.into_iter().map(Value::String).collect()),
}])
}
}
pub struct DatabaseBuilder {
name: String,
description: Option<String>,
}
impl DatabaseBuilder {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
description: None,
}
}
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub fn build(self) -> crate::models::CreateDatabaseRequest {
crate::models::CreateDatabaseRequest {
name: self.name,
description: self.description,
}
}
}
pub struct FieldBuilder {
name: String,
field_type: crate::models::FieldType,
dimension: Option<usize>,
model_id: Option<String>,
format: Option<String>,
element_type: Option<crate::models::FieldType>,
max_length: Option<usize>,
index: Option<bool>,
store: Option<bool>,
}
impl FieldBuilder {
pub fn new(name: impl Into<String>, field_type: crate::models::FieldType) -> Self {
Self {
name: name.into(),
field_type,
dimension: None,
model_id: None,
format: None,
element_type: None,
max_length: None,
index: None,
store: None,
}
}
pub fn with_dimension(mut self, dimension: usize) -> Self {
self.dimension = Some(dimension);
self
}
pub fn with_model_id(mut self, model_id: impl Into<String>) -> Self {
self.model_id = Some(model_id.into());
self
}
pub fn with_format(mut self, format: impl Into<String>) -> Self {
self.format = Some(format.into());
self
}
pub fn with_element_type(mut self, element_type: crate::models::FieldType) -> Self {
self.element_type = Some(element_type);
self
}
pub fn with_max_length(mut self, max_length: usize) -> Self {
self.max_length = Some(max_length);
self
}
pub fn with_index(mut self, index: bool) -> Self {
self.index = Some(index);
self
}
pub fn with_store(mut self, store: bool) -> Self {
self.store = Some(store);
self
}
pub fn build(self) -> (String, crate::models::FieldDefinition) {
let field_def = crate::models::FieldDefinition {
field_type: self.field_type,
dimension: self.dimension,
model_id: self.model_id,
format: self.format,
element_type: self.element_type,
max_length: self.max_length,
index: self.index,
store: self.store,
};
(self.name, field_def)
}
}
pub struct IndexBuilder {
index_type: String,
nlist: Option<i32>,
m: Option<i32>,
ef_construction: Option<i32>,
ef_search: Option<i32>,
metric_type: Option<String>,
nprobe: Option<i32>,
parallel_on_queries: Option<i32>,
}
impl IndexBuilder {
pub fn new(index_type: impl Into<String>) -> Self {
Self {
index_type: index_type.into(),
nlist: None,
m: None,
ef_construction: None,
ef_search: None,
metric_type: None,
nprobe: None,
parallel_on_queries: None,
}
}
pub fn with_nlist(mut self, nlist: i32) -> Self {
self.nlist = Some(nlist);
self
}
pub fn with_m(mut self, m: i32) -> Self {
self.m = Some(m);
self
}
pub fn with_ef_construction(mut self, ef_construction: i32) -> Self {
self.ef_construction = Some(ef_construction);
self
}
pub fn with_ef_search(mut self, ef_search: i32) -> Self {
self.ef_search = Some(ef_search);
self
}
pub fn with_metric_type(mut self, metric_type: impl Into<String>) -> Self {
self.metric_type = Some(metric_type.into());
self
}
pub fn with_nprobe(mut self, nprobe: i32) -> Self {
self.nprobe = Some(nprobe);
self
}
pub fn with_parallel_on_queries(mut self, parallel: i32) -> Self {
self.parallel_on_queries = Some(parallel);
self
}
pub fn build(self) -> crate::models::IndexConfig {
crate::models::IndexConfig {
index_type: self.index_type,
nlist: self.nlist,
m: self.m,
ef_construction: self.ef_construction,
ef_search: self.ef_search,
metric_type: self.metric_type,
nprobe: self.nprobe,
parallel_on_queries: self.parallel_on_queries,
}
}
}
pub struct SpaceBuilder {
name: String,
fields: HashMap<String, crate::models::FieldDefinition>,
index: Option<crate::models::IndexConfig>,
partition_num: Option<i32>,
replica_num: Option<i32>,
description: Option<String>,
}
impl SpaceBuilder {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
fields: HashMap::new(),
index: None,
partition_num: None,
replica_num: None,
description: None,
}
}
pub fn with_field(mut self, field: (String, crate::models::FieldDefinition)) -> Self {
self.fields.insert(field.0, field.1);
self
}
pub fn with_index(mut self, index: crate::models::IndexConfig) -> Self {
self.index = Some(index);
self
}
pub fn with_partition_num(mut self, partition_num: i32) -> Self {
self.partition_num = Some(partition_num);
self
}
pub fn with_replica_num(mut self, replica_num: i32) -> Self {
self.replica_num = Some(replica_num);
self
}
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub fn build(self) -> crate::models::CreateSpaceRequest {
crate::models::CreateSpaceRequest {
name: self.name,
fields: self.fields,
index: self.index,
partition_num: self.partition_num,
replica_num: self.replica_num,
description: self.description,
}
}
}
pub mod field_utils {
use super::*;
pub fn string_field(name: impl Into<String>) -> FieldBuilder {
FieldBuilder::new(name, crate::models::FieldType::String)
}
pub fn int_field(name: impl Into<String>) -> FieldBuilder {
FieldBuilder::new(name, crate::models::FieldType::Integer)
}
pub fn float_field(name: impl Into<String>) -> FieldBuilder {
FieldBuilder::new(name, crate::models::FieldType::Float)
}
pub fn double_field(name: impl Into<String>) -> FieldBuilder {
FieldBuilder::new(name, crate::models::FieldType::Double)
}
pub fn bool_field(name: impl Into<String>) -> FieldBuilder {
FieldBuilder::new(name, crate::models::FieldType::Boolean)
}
pub fn vector_field(name: impl Into<String>, dimension: usize) -> FieldBuilder {
FieldBuilder::new(name, crate::models::FieldType::Vector)
.with_dimension(dimension)
}
pub fn array_field(name: impl Into<String>, element_type: crate::models::FieldType) -> FieldBuilder {
FieldBuilder::new(name, crate::models::FieldType::Array)
.with_element_type(element_type)
}
pub fn date_field(name: impl Into<String>) -> FieldBuilder {
FieldBuilder::new(name, crate::models::FieldType::Date)
}
pub fn geo_field(name: impl Into<String>) -> FieldBuilder {
FieldBuilder::new(name, crate::models::FieldType::Geo)
}
}
pub struct PrivilegeBuilder {
privileges: crate::models::Privileges,
}
impl PrivilegeBuilder {
pub fn new() -> Self {
Self {
privileges: HashMap::new(),
}
}
pub fn all_resources(mut self, privilege: crate::models::PrivilegeType) -> Self {
self.privileges.insert(crate::models::ResourceType::ResourceAll, privilege);
self
}
pub fn cluster(mut self, privilege: crate::models::PrivilegeType) -> Self {
self.privileges.insert(crate::models::ResourceType::ResourceCluster, privilege);
self
}
pub fn server(mut self, privilege: crate::models::PrivilegeType) -> Self {
self.privileges.insert(crate::models::ResourceType::ResourceServer, privilege);
self
}
pub fn partition(mut self, privilege: crate::models::PrivilegeType) -> Self {
self.privileges.insert(crate::models::ResourceType::ResourcePartition, privilege);
self
}
pub fn database(mut self, privilege: crate::models::PrivilegeType) -> Self {
self.privileges.insert(crate::models::ResourceType::ResourceDB, privilege);
self
}
pub fn space(mut self, privilege: crate::models::PrivilegeType) -> Self {
self.privileges.insert(crate::models::ResourceType::ResourceSpace, privilege);
self
}
pub fn document(mut self, privilege: crate::models::PrivilegeType) -> Self {
self.privileges.insert(crate::models::ResourceType::ResourceDocument, privilege);
self
}
pub fn index(mut self, privilege: crate::models::PrivilegeType) -> Self {
self.privileges.insert(crate::models::ResourceType::ResourceIndex, privilege);
self
}
pub fn alias(mut self, privilege: crate::models::PrivilegeType) -> Self {
self.privileges.insert(crate::models::ResourceType::ResourceAlias, privilege);
self
}
pub fn user(mut self, privilege: crate::models::PrivilegeType) -> Self {
self.privileges.insert(crate::models::ResourceType::ResourceUser, privilege);
self
}
pub fn role(mut self, privilege: crate::models::PrivilegeType) -> Self {
self.privileges.insert(crate::models::ResourceType::ResourceRole, privilege);
self
}
pub fn config(mut self, privilege: crate::models::PrivilegeType) -> Self {
self.privileges.insert(crate::models::ResourceType::ResourceConfig, privilege);
self
}
pub fn cache(mut self, privilege: crate::models::PrivilegeType) -> Self {
self.privileges.insert(crate::models::ResourceType::ResourceCache, privilege);
self
}
pub fn build(self) -> crate::models::Privileges {
self.privileges
}
}
impl Default for PrivilegeBuilder {
fn default() -> Self {
Self::new()
}
}
pub struct RoleBuilder {
name: String,
privileges: crate::models::Privileges,
}
impl RoleBuilder {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
privileges: HashMap::new(),
}
}
pub fn with_privileges(mut self, privileges: crate::models::Privileges) -> Self {
self.privileges = privileges;
self
}
pub fn build(self) -> crate::models::CreateRoleRequest {
crate::models::CreateRoleRequest {
name: self.name,
privileges: self.privileges,
}
}
}
pub struct UserBuilder {
name: String,
password: String,
role_name: String,
}
impl UserBuilder {
pub fn new(name: impl Into<String>, password: impl Into<String>) -> Self {
Self {
name: name.into(),
password: password.into(),
role_name: String::new(),
}
}
pub fn with_role(mut self, role_name: impl Into<String>) -> Self {
self.role_name = role_name.into();
self
}
pub fn build(self) -> crate::models::CreateUserRequest {
crate::models::CreateUserRequest {
name: self.name,
password: self.password,
role_name: self.role_name,
}
}
}
pub struct AuthConfigBuilder {
username: String,
password: String,
role_name: Option<String>,
}
impl AuthConfigBuilder {
pub fn new(username: impl Into<String>, password: impl Into<String>) -> Self {
Self {
username: username.into(),
password: password.into(),
role_name: None,
}
}
pub fn with_role(mut self, role_name: impl Into<String>) -> Self {
self.role_name = Some(role_name.into());
self
}
pub fn build(self) -> crate::models::AuthConfig {
crate::models::AuthConfig {
username: self.username,
password: self.password,
role_name: self.role_name,
}
}
}