Move add_version and get_child_version to server module
..and add some tests specifically for those functions, in the absence of all the HTTP nonsense.
This commit is contained in:
@@ -2,12 +2,9 @@ use crate::api::{
|
||||
failure_to_ise, ServerState, HISTORY_SEGMENT_CONTENT_TYPE, PARENT_VERSION_ID_HEADER,
|
||||
VERSION_ID_HEADER,
|
||||
};
|
||||
use crate::server::{AddVersionResult, ClientId, HistorySegment, VersionId, NO_VERSION_ID};
|
||||
use crate::storage::{Client, StorageTxn};
|
||||
use crate::server::{add_version, AddVersionResult, ClientId, VersionId};
|
||||
use actix_web::{error, post, web, HttpMessage, HttpRequest, HttpResponse, Result};
|
||||
use failure::Fallible;
|
||||
use futures::StreamExt;
|
||||
use taskchampion::Uuid;
|
||||
|
||||
/// Max history segment size: 100MB
|
||||
const MAX_SIZE: usize = 100 * 1024 * 1024;
|
||||
@@ -71,31 +68,6 @@ pub(crate) async fn service(
|
||||
})
|
||||
}
|
||||
|
||||
fn add_version<'a>(
|
||||
mut txn: Box<dyn StorageTxn + 'a>,
|
||||
client_id: ClientId,
|
||||
client: Client,
|
||||
parent_version_id: VersionId,
|
||||
history_segment: HistorySegment,
|
||||
) -> Fallible<AddVersionResult> {
|
||||
// check if this version is acceptable, under the protection of the transaction
|
||||
if client.latest_version_id != NO_VERSION_ID && parent_version_id != client.latest_version_id {
|
||||
return Ok(AddVersionResult::ExpectedParentVersion(
|
||||
client.latest_version_id,
|
||||
));
|
||||
}
|
||||
|
||||
// invent a version ID
|
||||
let version_id = Uuid::new_v4();
|
||||
|
||||
// update the DB
|
||||
txn.add_version(client_id, version_id, parent_version_id, history_segment)?;
|
||||
txn.set_client_latest_version_id(client_id, version_id)?;
|
||||
txn.commit()?;
|
||||
|
||||
Ok(AddVersionResult::Ok(version_id))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::api::ServerState;
|
||||
|
||||
@@ -2,10 +2,8 @@ use crate::api::{
|
||||
failure_to_ise, ServerState, HISTORY_SEGMENT_CONTENT_TYPE, PARENT_VERSION_ID_HEADER,
|
||||
VERSION_ID_HEADER,
|
||||
};
|
||||
use crate::server::{ClientId, GetVersionResult, VersionId};
|
||||
use crate::storage::StorageTxn;
|
||||
use crate::server::{get_child_version, ClientId, VersionId};
|
||||
use actix_web::{error, get, web, HttpResponse, Result};
|
||||
use failure::Fallible;
|
||||
|
||||
/// Get a child version.
|
||||
///
|
||||
@@ -41,20 +39,6 @@ pub(crate) async fn service(
|
||||
}
|
||||
}
|
||||
|
||||
fn get_child_version<'a>(
|
||||
mut txn: Box<dyn StorageTxn + 'a>,
|
||||
client_id: ClientId,
|
||||
parent_version_id: VersionId,
|
||||
) -> Fallible<Option<GetVersionResult>> {
|
||||
Ok(txn
|
||||
.get_version_by_parent(client_id, parent_version_id)?
|
||||
.map(|version| GetVersionResult {
|
||||
version_id: version.version_id,
|
||||
parent_version_id: version.parent_version_id,
|
||||
history_segment: version.history_segment,
|
||||
}))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::api::ServerState;
|
||||
|
||||
Reference in New Issue
Block a user