Unverified Commit 615fc5bb authored by Vasili Novikov's avatar Vasili Novikov
Browse files

Change _limit behavior

parent eda37164
Pipeline #4444 failed with stages
in 2 minutes and 46 seconds
......@@ -258,8 +258,8 @@ Mark an item as deleted:
"dateServerModified>=": 1234567890, // date filter
"dateServerModified<": 1234567890,
"deleted": false, // deleted filter
"_sortOrder": "DESC", // sort by server modification either "ASC" (by default) or "DESC"
"_limit": 100, // limit the result set
"_sortOrder": "Asc", // sort by server modification date, either "Asc" (by default) or "Desc"
"_limit": 100, // minimum number of items to return, see below
"[[edges]]": {}, // include all forward edges in the response
"~[[edges]]": {}, // include all backward edges in the response
}
......@@ -272,6 +272,10 @@ The endpoint will return an array of items which have all the properties above.
As a first step of the 2021-03 Pod rewrite, only the above properties are supported.
In the future, any item properties will be available.
If `_limit` is specified, response will include the first `_limit` number of items
from the database, plus also all other items
with exactly the same `dateServerModified` as the last one.
The properties of `"[[edges]]"` and `"~[[edges]]"` are
["magic constants"](https://en.wikipedia.org/wiki/Magic_number_(programming)) for now.
This will change in the future when we'll [continuously expand](./WIP_QueryRedesign.md) our API
......
......@@ -110,6 +110,7 @@ fn parse_item_base(row: &Row) -> Result<ItemBase> {
})
}
#[allow(clippy::comparison_chain)]
pub fn search_items(tx: &Tx, query: &DatabaseSearch) -> Result<Vec<ItemBase>> {
let mut sql_query = "\
SELECT \
......@@ -170,13 +171,22 @@ pub fn search_items(tx: &Tx, query: &DatabaseSearch) -> Result<Vec<ItemBase>> {
let mut result = Vec::new();
let mut num_left = query._limit;
let mut last_date: Option<DbTime> = None;
while let Some(row) = rows.next()? {
if num_left == 0 {
break;
} else {
let item = parse_item_base(row)?;
if num_left > 1 {
num_left -= 1;
} else if num_left == 1 {
num_left = 0;
last_date = Some(item.date_server_modified);
} else if let Some(last_date) = last_date {
if last_date != item.date_server_modified {
break;
}
} else {
break;
}
result.push(parse_item_base(row)?);
result.push(item);
}
Ok(result)
}
......@@ -761,6 +771,29 @@ pub mod tests {
Ok(())
}
#[test]
fn test_limiting() -> Result<()> {
let mut conn = new_conn();
let tx = conn.transaction()?;
let date = Utc::now().timestamp_millis();
let _item1 = insert_item_base(&tx, "one", "Person", date, date, date, false)?;
let _item2 = insert_item_base(&tx, "two", "Book", date, date, date, false)?;
let _item3 = insert_item_base(&tx, "three", "Street", date, date, date + 1, false)?;
let query = DatabaseSearch {
rowid: None,
id: None,
_type: None,
date_server_modified_gte: Some(date),
date_server_modified_lt: None,
deleted: None,
sort_order: SortOrder::Asc,
_limit: 1,
};
// 1 main item + 1 other item with identical `dateServerModified`
assert_eq!(search_items(&tx, &query)?.len(), 2);
Ok(())
}
#[test]
fn test_property_checks() -> Result<()> {
let mut conn = new_conn();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment