Skip to content

Commit

Permalink
[db] Openapi does not handle type alias DbKey correctly #929 (#930)
Browse files Browse the repository at this point in the history
* remove DbKey type

* update docs
  • Loading branch information
michaelvlach authored Dec 25, 2023
1 parent 26dae5c commit a996850
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 50 deletions.
7 changes: 3 additions & 4 deletions agdb/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use crate::storage::StorageIndex;
use crate::utilities::serialize::Serialize;
use crate::utilities::serialize::SerializeStatic;
use crate::DbId;
use crate::DbKey;
use crate::DbKeyValue;
use crate::DbValue;
use crate::QueryError;
Expand Down Expand Up @@ -762,7 +761,7 @@ impl<Store: StorageData> DbImpl<Store> {
pub(crate) fn values_by_keys(
&self,
db_id: DbId,
keys: &[DbKey],
keys: &[DbValue],
) -> Result<Vec<DbKeyValue>, DbError> {
Ok(self
.values
Expand Down Expand Up @@ -857,7 +856,7 @@ impl<Store: StorageData> DbImpl<Store> {
Ok(())
}

pub(crate) fn remove_keys(&mut self, db_id: DbId, keys: &[DbKey]) -> Result<i64, QueryError> {
pub(crate) fn remove_keys(&mut self, db_id: DbId, keys: &[DbValue]) -> Result<i64, QueryError> {
let mut result = 0;

for key_value in self.values.values(&self.storage, &db_id)? {
Expand Down Expand Up @@ -987,7 +986,7 @@ impl<Store: StorageData> DbImpl<Store> {
.values
.iter_key(&self.storage, &DbId(index.0))
.map(|(_, kv)| kv.key)
.collect::<Vec<DbKey>>();
.collect::<Vec<DbValue>>();
Ok(SearchControl::Continue(
values.iter().all(|k| keys.contains(k)),
))
Expand Down
13 changes: 5 additions & 8 deletions agdb/src/db/db_key.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use crate::DbValue;

/// Alias to [`DbValue`]
pub type DbKey = DbValue;

/// Ordering for search queries
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub enum DbKeyOrder {
/// Ascending order (from smallest)
Asc(DbKey),
Asc(DbValue),

/// Descending order (from largest)
Desc(DbKey),
Desc(DbValue),
}

#[cfg(test)]
Expand All @@ -21,7 +18,7 @@ mod tests {

#[test]
fn derived_from_debug() {
format!("{:?}", DbKeyOrder::Asc(DbKey::default()));
format!("{:?}", DbKeyOrder::Asc(DbValue::default()));
}

#[test]
Expand All @@ -36,8 +33,8 @@ mod tests {
#[test]
fn derived_from_partial_eq() {
assert_eq!(
DbKeyOrder::Asc(DbKey::default()),
DbKeyOrder::Asc(DbKey::default())
DbKeyOrder::Asc(DbValue::default()),
DbKeyOrder::Asc(DbValue::default())
);
}
}
17 changes: 8 additions & 9 deletions agdb/src/db/db_key_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::storage::StorageIndex;
use crate::utilities::serialize::Serialize;
use crate::utilities::serialize::SerializeStatic;
use crate::DbError;
use crate::DbKey;
use crate::DbValue;
use crate::StorageData;

Expand All @@ -17,15 +16,15 @@ use crate::StorageData;
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct DbKeyValue {
/// Key of the property
pub key: DbKey,
pub key: DbValue,

/// Value of the property
pub value: DbValue,
}

impl<K, T> From<(K, T)> for DbKeyValue
where
K: Into<DbKey>,
K: Into<DbValue>,
T: Into<DbValue>,
{
fn from(value: (K, T)) -> Self {
Expand Down Expand Up @@ -81,21 +80,21 @@ mod tests {
format!(
"{:?}",
DbKeyValue {
key: DbKey::I64(0),
value: DbKey::I64(0)
key: DbValue::I64(0),
value: DbValue::I64(0)
}
);
}
#[test]
fn derived_from_partial_eq() {
assert_eq!(
DbKeyValue {
key: DbKey::I64(0),
value: DbKey::I64(0)
key: DbValue::I64(0),
value: DbValue::I64(0)
},
DbKeyValue {
key: DbKey::I64(0),
value: DbKey::I64(0)
key: DbValue::I64(0),
value: DbValue::I64(0)
}
);
}
Expand Down
4 changes: 2 additions & 2 deletions agdb/src/db/db_user_value.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::DbElement;
use crate::DbError;
use crate::DbId;
use crate::DbKey;
use crate::DbKeyValue;
use crate::DbValue;

/// Trait that allows use of user defined values
/// directly by facilitating translation from/to
Expand Down Expand Up @@ -40,7 +40,7 @@ pub trait DbUserValue: Sized {

/// Returns the list of database keys of
/// this type.
fn db_keys() -> Vec<DbKey>;
fn db_keys() -> Vec<DbValue>;

/// Constructs the user value from the `element`
/// extracting the values from element `values`
Expand Down
1 change: 0 additions & 1 deletion agdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ pub use db::db_element::DbElement;
pub use db::db_error::DbError;
pub use db::db_f64::DbF64;
pub use db::db_id::DbId;
pub use db::db_key::DbKey;
pub use db::db_key::DbKeyOrder;
pub use db::db_key_value::DbKeyValue;
pub use db::db_user_value::DbUserValue;
Expand Down
5 changes: 2 additions & 3 deletions agdb/src/query/query_condition.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::graph_search::SearchControl;
use crate::DbKey;
use crate::DbValue;
use crate::QueryId;

Expand Down Expand Up @@ -70,14 +69,14 @@ pub enum QueryConditionData {
/// with a value that evaluates true against `comparison`.
KeyValue {
/// Property key
key: DbKey,
key: DbValue,

/// Comparison operator (e.g. Equal, GreaterThan etc.)
value: Comparison,
},

/// Test if the current element has **all** of the keys listed.
Keys(Vec<DbKey>),
Keys(Vec<DbValue>),

/// Is the current element a node? I.e. `0 < id`.
Node,
Expand Down
8 changes: 4 additions & 4 deletions agdb/src/query/query_values.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::DbKey;
use crate::DbKeyValue;
use crate::DbUserValue;
use crate::DbValue;

/// Helper type distinguishing uniform (`Single`) values
/// and multiple (`Multi`) values in database queries.
Expand Down Expand Up @@ -29,7 +29,7 @@ pub struct MultiValues(pub Vec<Vec<DbKeyValue>>);
/// Convenient wrapper for the [`QueryBuilder`] to
/// allow properties conversions. Represents list
/// of property keys.
pub struct QueryKeys(pub Vec<DbKey>);
pub struct QueryKeys(pub Vec<DbValue>);

impl From<Vec<DbKeyValue>> for SingleValues {
fn from(values: Vec<DbKeyValue>) -> Self {
Expand All @@ -43,8 +43,8 @@ impl From<Vec<Vec<DbKeyValue>>> for MultiValues {
}
}

impl From<Vec<DbKey>> for QueryKeys {
fn from(value: Vec<DbKey>) -> Self {
impl From<Vec<DbValue>> for QueryKeys {
fn from(value: Vec<DbValue>) -> Self {
QueryKeys(value)
}
}
Expand Down
4 changes: 2 additions & 2 deletions agdb/src/query/search_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::db::db_key::DbKeyOrder;
use crate::DbElement;
use crate::DbId;
use crate::DbImpl;
use crate::DbKey;
use crate::DbValue;
use crate::Query;
use crate::QueryCondition;
use crate::QueryError;
Expand Down Expand Up @@ -136,7 +136,7 @@ impl SearchQuery {
.map(|key_order| match key_order {
DbKeyOrder::Asc(key) | DbKeyOrder::Desc(key) => key.clone(),
})
.collect::<Vec<DbKey>>();
.collect::<Vec<DbValue>>();

ids.sort_by(|left, right| {
let left_values = db.values_by_keys(*left, &keys).unwrap_or_default();
Expand Down
4 changes: 2 additions & 2 deletions agdb/src/query/select_values_query.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::DbElement;
use crate::DbImpl;
use crate::DbKey;
use crate::DbValue;
use crate::Query;
use crate::QueryError;
use crate::QueryIds;
Expand All @@ -16,7 +16,7 @@ use crate::StorageData;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct SelectValuesQuery {
pub keys: Vec<DbKey>,
pub keys: Vec<DbValue>,
pub ids: QueryIds,
}

Expand Down
6 changes: 3 additions & 3 deletions agdb/src/query_builder/where_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::query::query_condition::QueryConditionModifier;
use crate::query::query_values::QueryKeys;
use crate::query::search_query::SearchQuery;
use crate::Comparison;
use crate::DbKey;
use crate::DbValue;
use crate::QueryIds;

/// Condition builder
Expand All @@ -19,7 +19,7 @@ pub struct Where {

/// Condition builder for `key` condition.
pub struct WhereKey {
key: DbKey,
key: DbValue,
where_: Where,
}

Expand Down Expand Up @@ -196,7 +196,7 @@ impl Where {
/// // Includes only elements with property `String("k") == 1_i64`
/// QueryBuilder::search().from(1).where_().key("k").value(Comparison::Equal(1.into())).query();
/// ```
pub fn key<T: Into<DbKey>>(self, key: T) -> WhereKey {
pub fn key<T: Into<DbValue>>(self, key: T) -> WhereKey {
WhereKey {
key: key.into(),
where_: self,
Expand Down
1 change: 0 additions & 1 deletion agdb/tests/db_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ fn public_types() {
use agdb::DbFileTransaction;
use agdb::DbFileTransactionMut;
use agdb::DbId;
use agdb::DbKey;
use agdb::DbKeyOrder;
use agdb::DbKeyValue;
use agdb::DbTransaction;
Expand Down
3 changes: 1 addition & 2 deletions agdb/tests/db_user_value_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod test_db;
use agdb::DbElement;
use agdb::DbError;
use agdb::DbId;
use agdb::DbKey;
use agdb::DbUserValue;
use agdb::DbValue;
use agdb::QueryBuilder;
Expand Down Expand Up @@ -85,7 +84,7 @@ fn db_user_value() {
vec_string: vec!["world".to_string()],
custom_enum: Status::Active,
};
let keys: Vec<DbKey> = vec![
let keys: Vec<DbValue> = vec![
"bytes".into(),
"u64".into(),
"u32".into(),
Expand Down
2 changes: 1 addition & 1 deletion agdb_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn db_user_value_derive(item: TokenStream) -> TokenStream {
#db_id
}

fn db_keys() -> Vec<agdb::DbKey> {
fn db_keys() -> Vec<agdb::DbValue> {
vec![#(#db_keys.into()),*]
}

Expand Down
16 changes: 8 additions & 8 deletions docs/queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ The `DbUserValue` trait is an interface that can be implemented for user defined
```Rust
pub trait DbUserValue: Sized {
fn db_id(&self) -> Option<DbId>;
fn db_keys() -> Vec<DbKey>;
fn db_keys() -> Vec<DbValue>;
fn from_db_element(element: &DbElement) -> Result<Self, DbError>;
fn to_db_values(&self) -> Vec<DbKeyValue>;
}
Expand Down Expand Up @@ -131,12 +131,12 @@ The values are `key-value` pairs (properties) associated with the given element:

```Rust
pub struct DbKeyValue {
pub key: DbKey,
pub key: DbValue,
pub value: DbValue,
}
```

The `DbKey` is an alias of `DbValue` and the value itself is an enum of valid types:
Where `DbValue` is:

```Rust
pub enum DbValue {
Expand Down Expand Up @@ -485,7 +485,7 @@ The result will contain:

```Rust
pub struct SelectValuesQuery {
pub keys: Vec<DbKey>,
pub keys: Vec<DbValue>,
pub ids: QueryIds,
}
```
Expand Down Expand Up @@ -607,8 +607,8 @@ pub enum SearchQueryAlgorithm {
}

pub enum DbKeyOrder {
Asc(DbKey),
Desc(DbKey),
Asc(DbValue),
Desc(DbValue),
}
```

Expand Down Expand Up @@ -691,8 +691,8 @@ pub enum QueryConditionData {
EdgeCountFrom(CountComparison),
EdgeCountTo(CountComparison),
Ids(Vec<QueryId>),
KeyValue { key: DbKey, value: Comparison },
Keys(Vec<DbKey>),
KeyValue { key: DbValue, value: Comparison },
Keys(Vec<DbValue>),
Node,
Where(Vec<QueryCondition>),
}
Expand Down

0 comments on commit a996850

Please sign in to comment.