api: rewrite some comments on synchronization algorithm
This commit is contained in:
parent
0277d2c122
commit
908c62b0a1
21
src/api.rs
21
src/api.rs
|
@ -225,14 +225,11 @@ fn items_sync(db: DbConn, u: user::User, params: Json<SyncParams>) -> Custom<Jso
|
||||||
},
|
},
|
||||||
Ok(items) => {
|
Ok(items) => {
|
||||||
if !items.is_empty() {
|
if !items.is_empty() {
|
||||||
// If we fetched something
|
// If we fetched something, and the length is right at limit
|
||||||
// we should record the current last id to *some* token
|
// we may have more to fetch. In this case, we need to
|
||||||
// this may be `cursor_token` or `sync_token`
|
// inform the client to continue fetching, until there is
|
||||||
// if we have more to fetch, we set `cursor_token` to this id
|
// nothing more to fetch
|
||||||
// so that the client knows to continue
|
// (i.e. until cursor_token is equal to sync_token)
|
||||||
// and we set `sync_token` to this id regardless, for the
|
|
||||||
// client to remember where we were even if
|
|
||||||
// it doesn't need to continue
|
|
||||||
let next_from = items.last().unwrap().id;
|
let next_from = items.last().unwrap().id;
|
||||||
if let Some(limit) = inner_params.limit {
|
if let Some(limit) = inner_params.limit {
|
||||||
if items.len() as i64 == limit {
|
if items.len() as i64 == limit {
|
||||||
|
@ -265,6 +262,10 @@ fn items_sync(db: DbConn, u: user::User, params: Json<SyncParams>) -> Custom<Jso
|
||||||
let mut last_id: i64 = -1;
|
let mut last_id: i64 = -1;
|
||||||
for mut it in inner_params.items.into_iter() {
|
for mut it in inner_params.items.into_iter() {
|
||||||
// Handle conflicts
|
// Handle conflicts
|
||||||
|
// Anything that we just retrieved but need to save immediately
|
||||||
|
// is potentially a conflict
|
||||||
|
// TODO: how do we handle this when the sync needs multiple requests
|
||||||
|
// to finish?
|
||||||
let mut conflicted = false;
|
let mut conflicted = false;
|
||||||
for y in resp.retrieved_items.iter() {
|
for y in resp.retrieved_items.iter() {
|
||||||
if it.uuid == y.uuid {
|
if it.uuid == y.uuid {
|
||||||
|
@ -310,6 +311,10 @@ fn items_sync(db: DbConn, u: user::User, params: Json<SyncParams>) -> Custom<Jso
|
||||||
|
|
||||||
if last_id > -1 {
|
if last_id > -1 {
|
||||||
// Update sync_token to the latest one of our saved items
|
// Update sync_token to the latest one of our saved items
|
||||||
|
// This is ALWAYS the case. `sync_token` indicates the
|
||||||
|
// LATEST known state of the system by the client,
|
||||||
|
// but it MAY still need to fill in a bit of history
|
||||||
|
// (that's where `cursor_token` comes into play)
|
||||||
resp.sync_token = Some(last_id.to_string());
|
resp.sync_token = Some(last_id.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue