mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-08-20 04:13:07 +02:00
move contents of taskchampion repo to tc/
This commit is contained in:
parent
73baefa0a5
commit
2a92b2a4b9
219 changed files with 0 additions and 0 deletions
27
rust/sync-server/Cargo.toml
Normal file
27
rust/sync-server/Cargo.toml
Normal file
|
@ -0,0 +1,27 @@
|
|||
[package]
|
||||
name = "taskchampion-sync-server"
|
||||
version = "0.4.1"
|
||||
authors = ["Dustin J. Mitchell <dustin@mozilla.com>"]
|
||||
edition = "2018"
|
||||
publish = false
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
uuid = { version = "^0.8.2", features = ["serde", "v4"] }
|
||||
actix-web = "^3.3.2"
|
||||
anyhow = "1.0"
|
||||
thiserror = "1.0"
|
||||
futures = "^0.3.8"
|
||||
serde = "^1.0.125"
|
||||
serde_json = "^1.0"
|
||||
clap = "^2.33.0"
|
||||
log = "^0.4.14"
|
||||
env_logger = "^0.8.3"
|
||||
rusqlite = { version = "0.25", features = ["bundled"] }
|
||||
chrono = { version = "^0.4.10", features = ["serde"] }
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "^1.1.1"
|
||||
tempfile = "3"
|
||||
pretty_assertions = "1"
|
204
rust/sync-server/src/api/add_snapshot.rs
Normal file
204
rust/sync-server/src/api/add_snapshot.rs
Normal file
|
@ -0,0 +1,204 @@
|
|||
use crate::api::{client_key_header, failure_to_ise, ServerState, SNAPSHOT_CONTENT_TYPE};
|
||||
use crate::server::{add_snapshot, VersionId, NIL_VERSION_ID};
|
||||
use actix_web::{error, post, web, HttpMessage, HttpRequest, HttpResponse, Result};
|
||||
use futures::StreamExt;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Max snapshot size: 100MB
|
||||
const MAX_SIZE: usize = 100 * 1024 * 1024;
|
||||
|
||||
/// Add a new snapshot, after checking prerequisites. The snapshot should be transmitted in the
|
||||
/// request entity body and must have content-type `application/vnd.taskchampion.snapshot`. The
|
||||
/// content can be encoded in any of the formats supported by actix-web.
|
||||
///
|
||||
/// On success, the response is a 200 OK. Even in a 200 OK, the snapshot may not appear in a
|
||||
/// subsequent `GetSnapshot` call.
|
||||
///
|
||||
/// Returns other 4xx or 5xx responses on other errors.
|
||||
#[post("/v1/client/add-snapshot/{version_id}")]
|
||||
pub(crate) async fn service(
|
||||
req: HttpRequest,
|
||||
server_state: web::Data<Arc<ServerState>>,
|
||||
web::Path((version_id,)): web::Path<(VersionId,)>,
|
||||
mut payload: web::Payload,
|
||||
) -> Result<HttpResponse> {
|
||||
// check content-type
|
||||
if req.content_type() != SNAPSHOT_CONTENT_TYPE {
|
||||
return Err(error::ErrorBadRequest("Bad content-type"));
|
||||
}
|
||||
|
||||
let client_key = client_key_header(&req)?;
|
||||
|
||||
// read the body in its entirety
|
||||
let mut body = web::BytesMut::new();
|
||||
while let Some(chunk) = payload.next().await {
|
||||
let chunk = chunk?;
|
||||
// limit max size of in-memory payload
|
||||
if (body.len() + chunk.len()) > MAX_SIZE {
|
||||
return Err(error::ErrorBadRequest("Snapshot over maximum allowed size"));
|
||||
}
|
||||
body.extend_from_slice(&chunk);
|
||||
}
|
||||
|
||||
if body.is_empty() {
|
||||
return Err(error::ErrorBadRequest("No snapshot supplied"));
|
||||
}
|
||||
|
||||
// note that we do not open the transaction until the body has been read
|
||||
// completely, to avoid blocking other storage access while that data is
|
||||
// in transit.
|
||||
let mut txn = server_state.storage.txn().map_err(failure_to_ise)?;
|
||||
|
||||
// get, or create, the client
|
||||
let client = match txn.get_client(client_key).map_err(failure_to_ise)? {
|
||||
Some(client) => client,
|
||||
None => {
|
||||
txn.new_client(client_key, NIL_VERSION_ID)
|
||||
.map_err(failure_to_ise)?;
|
||||
txn.get_client(client_key).map_err(failure_to_ise)?.unwrap()
|
||||
}
|
||||
};
|
||||
|
||||
add_snapshot(
|
||||
txn,
|
||||
&server_state.config,
|
||||
client_key,
|
||||
client,
|
||||
version_id,
|
||||
body.to_vec(),
|
||||
)
|
||||
.map_err(failure_to_ise)?;
|
||||
Ok(HttpResponse::Ok().body(""))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::storage::{InMemoryStorage, Storage};
|
||||
use crate::Server;
|
||||
use actix_web::{http::StatusCode, test, App};
|
||||
use pretty_assertions::assert_eq;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_success() -> anyhow::Result<()> {
|
||||
let client_key = Uuid::new_v4();
|
||||
let version_id = Uuid::new_v4();
|
||||
let storage: Box<dyn Storage> = Box::new(InMemoryStorage::new());
|
||||
|
||||
// set up the storage contents..
|
||||
{
|
||||
let mut txn = storage.txn().unwrap();
|
||||
txn.new_client(client_key, version_id).unwrap();
|
||||
txn.add_version(client_key, version_id, NIL_VERSION_ID, vec![])?;
|
||||
}
|
||||
|
||||
let server = Server::new(Default::default(), storage);
|
||||
let app = App::new().configure(|sc| server.config(sc));
|
||||
let mut app = test::init_service(app).await;
|
||||
|
||||
let uri = format!("/v1/client/add-snapshot/{}", version_id);
|
||||
let req = test::TestRequest::post()
|
||||
.uri(&uri)
|
||||
.header("Content-Type", "application/vnd.taskchampion.snapshot")
|
||||
.header("X-Client-Key", client_key.to_string())
|
||||
.set_payload(b"abcd".to_vec())
|
||||
.to_request();
|
||||
let resp = test::call_service(&mut app, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
// read back that snapshot
|
||||
let uri = "/v1/client/snapshot";
|
||||
let req = test::TestRequest::get()
|
||||
.uri(uri)
|
||||
.header("X-Client-Key", client_key.to_string())
|
||||
.to_request();
|
||||
let mut resp = test::call_service(&mut app, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
use futures::StreamExt;
|
||||
let (bytes, _) = resp.take_body().into_future().await;
|
||||
assert_eq!(bytes.unwrap().unwrap().as_ref(), b"abcd");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_not_added_200() {
|
||||
let client_key = Uuid::new_v4();
|
||||
let version_id = Uuid::new_v4();
|
||||
let storage: Box<dyn Storage> = Box::new(InMemoryStorage::new());
|
||||
|
||||
// set up the storage contents..
|
||||
{
|
||||
let mut txn = storage.txn().unwrap();
|
||||
txn.new_client(client_key, NIL_VERSION_ID).unwrap();
|
||||
}
|
||||
|
||||
let server = Server::new(Default::default(), storage);
|
||||
let app = App::new().configure(|sc| server.config(sc));
|
||||
let mut app = test::init_service(app).await;
|
||||
|
||||
// add a snapshot for a nonexistent version
|
||||
let uri = format!("/v1/client/add-snapshot/{}", version_id);
|
||||
let req = test::TestRequest::post()
|
||||
.uri(&uri)
|
||||
.header("Content-Type", "application/vnd.taskchampion.snapshot")
|
||||
.header("X-Client-Key", client_key.to_string())
|
||||
.set_payload(b"abcd".to_vec())
|
||||
.to_request();
|
||||
let resp = test::call_service(&mut app, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
// read back, seeing no snapshot
|
||||
let uri = "/v1/client/snapshot";
|
||||
let req = test::TestRequest::get()
|
||||
.uri(uri)
|
||||
.header("X-Client-Key", client_key.to_string())
|
||||
.to_request();
|
||||
let resp = test::call_service(&mut app, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_bad_content_type() {
|
||||
let client_key = Uuid::new_v4();
|
||||
let version_id = Uuid::new_v4();
|
||||
let storage: Box<dyn Storage> = Box::new(InMemoryStorage::new());
|
||||
let server = Server::new(Default::default(), storage);
|
||||
let app = App::new().configure(|sc| server.config(sc));
|
||||
let mut app = test::init_service(app).await;
|
||||
|
||||
let uri = format!("/v1/client/add-snapshot/{}", version_id);
|
||||
let req = test::TestRequest::post()
|
||||
.uri(&uri)
|
||||
.header("Content-Type", "not/correct")
|
||||
.header("X-Client-Key", client_key.to_string())
|
||||
.set_payload(b"abcd".to_vec())
|
||||
.to_request();
|
||||
let resp = test::call_service(&mut app, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_empty_body() {
|
||||
let client_key = Uuid::new_v4();
|
||||
let version_id = Uuid::new_v4();
|
||||
let storage: Box<dyn Storage> = Box::new(InMemoryStorage::new());
|
||||
let server = Server::new(Default::default(), storage);
|
||||
let app = App::new().configure(|sc| server.config(sc));
|
||||
let mut app = test::init_service(app).await;
|
||||
|
||||
let uri = format!("/v1/client/add-snapshot/{}", version_id);
|
||||
let req = test::TestRequest::post()
|
||||
.uri(&uri)
|
||||
.header(
|
||||
"Content-Type",
|
||||
"application/vnd.taskchampion.history-segment",
|
||||
)
|
||||
.header("X-Client-Key", client_key.to_string())
|
||||
.to_request();
|
||||
let resp = test::call_service(&mut app, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||
}
|
||||
}
|
230
rust/sync-server/src/api/add_version.rs
Normal file
230
rust/sync-server/src/api/add_version.rs
Normal file
|
@ -0,0 +1,230 @@
|
|||
use crate::api::{
|
||||
client_key_header, failure_to_ise, ServerState, HISTORY_SEGMENT_CONTENT_TYPE,
|
||||
PARENT_VERSION_ID_HEADER, SNAPSHOT_REQUEST_HEADER, VERSION_ID_HEADER,
|
||||
};
|
||||
use crate::server::{add_version, AddVersionResult, SnapshotUrgency, VersionId, NIL_VERSION_ID};
|
||||
use actix_web::{error, post, web, HttpMessage, HttpRequest, HttpResponse, Result};
|
||||
use futures::StreamExt;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Max history segment size: 100MB
|
||||
const MAX_SIZE: usize = 100 * 1024 * 1024;
|
||||
|
||||
/// Add a new version, after checking prerequisites. The history segment should be transmitted in
|
||||
/// the request entity body and must have content-type
|
||||
/// `application/vnd.taskchampion.history-segment`. The content can be encoded in any of the
|
||||
/// formats supported by actix-web.
|
||||
///
|
||||
/// On success, the response is a 200 OK with the new version ID in the `X-Version-Id` header. If
|
||||
/// the version cannot be added due to a conflict, the response is a 409 CONFLICT with the expected
|
||||
/// parent version ID in the `X-Parent-Version-Id` header.
|
||||
///
|
||||
/// If included, a snapshot request appears in the `X-Snapshot-Request` header with value
|
||||
/// `urgency=low` or `urgency=high`.
|
||||
///
|
||||
/// Returns other 4xx or 5xx responses on other errors.
|
||||
#[post("/v1/client/add-version/{parent_version_id}")]
|
||||
pub(crate) async fn service(
|
||||
req: HttpRequest,
|
||||
server_state: web::Data<Arc<ServerState>>,
|
||||
web::Path((parent_version_id,)): web::Path<(VersionId,)>,
|
||||
mut payload: web::Payload,
|
||||
) -> Result<HttpResponse> {
|
||||
// check content-type
|
||||
if req.content_type() != HISTORY_SEGMENT_CONTENT_TYPE {
|
||||
return Err(error::ErrorBadRequest("Bad content-type"));
|
||||
}
|
||||
|
||||
let client_key = client_key_header(&req)?;
|
||||
|
||||
// read the body in its entirety
|
||||
let mut body = web::BytesMut::new();
|
||||
while let Some(chunk) = payload.next().await {
|
||||
let chunk = chunk?;
|
||||
// limit max size of in-memory payload
|
||||
if (body.len() + chunk.len()) > MAX_SIZE {
|
||||
return Err(error::ErrorBadRequest("overflow"));
|
||||
}
|
||||
body.extend_from_slice(&chunk);
|
||||
}
|
||||
|
||||
if body.is_empty() {
|
||||
return Err(error::ErrorBadRequest("Empty body"));
|
||||
}
|
||||
|
||||
// note that we do not open the transaction until the body has been read
|
||||
// completely, to avoid blocking other storage access while that data is
|
||||
// in transit.
|
||||
let mut txn = server_state.storage.txn().map_err(failure_to_ise)?;
|
||||
|
||||
// get, or create, the client
|
||||
let client = match txn.get_client(client_key).map_err(failure_to_ise)? {
|
||||
Some(client) => client,
|
||||
None => {
|
||||
txn.new_client(client_key, NIL_VERSION_ID)
|
||||
.map_err(failure_to_ise)?;
|
||||
txn.get_client(client_key).map_err(failure_to_ise)?.unwrap()
|
||||
}
|
||||
};
|
||||
|
||||
let (result, snap_urgency) = add_version(
|
||||
txn,
|
||||
&server_state.config,
|
||||
client_key,
|
||||
client,
|
||||
parent_version_id,
|
||||
body.to_vec(),
|
||||
)
|
||||
.map_err(failure_to_ise)?;
|
||||
|
||||
Ok(match result {
|
||||
AddVersionResult::Ok(version_id) => {
|
||||
let mut rb = HttpResponse::Ok();
|
||||
rb.header(VERSION_ID_HEADER, version_id.to_string());
|
||||
match snap_urgency {
|
||||
SnapshotUrgency::None => {}
|
||||
SnapshotUrgency::Low => {
|
||||
rb.header(SNAPSHOT_REQUEST_HEADER, "urgency=low");
|
||||
}
|
||||
SnapshotUrgency::High => {
|
||||
rb.header(SNAPSHOT_REQUEST_HEADER, "urgency=high");
|
||||
}
|
||||
};
|
||||
rb.finish()
|
||||
}
|
||||
AddVersionResult::ExpectedParentVersion(parent_version_id) => {
|
||||
let mut rb = HttpResponse::Conflict();
|
||||
rb.header(PARENT_VERSION_ID_HEADER, parent_version_id.to_string());
|
||||
rb.finish()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::storage::{InMemoryStorage, Storage};
|
||||
use crate::Server;
|
||||
use actix_web::{http::StatusCode, test, App};
|
||||
use pretty_assertions::assert_eq;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_success() {
|
||||
let client_key = Uuid::new_v4();
|
||||
let version_id = Uuid::new_v4();
|
||||
let parent_version_id = Uuid::new_v4();
|
||||
let storage: Box<dyn Storage> = Box::new(InMemoryStorage::new());
|
||||
|
||||
// set up the storage contents..
|
||||
{
|
||||
let mut txn = storage.txn().unwrap();
|
||||
txn.new_client(client_key, Uuid::nil()).unwrap();
|
||||
}
|
||||
|
||||
let server = Server::new(Default::default(), storage);
|
||||
let app = App::new().configure(|sc| server.config(sc));
|
||||
let mut app = test::init_service(app).await;
|
||||
|
||||
let uri = format!("/v1/client/add-version/{}", parent_version_id);
|
||||
let req = test::TestRequest::post()
|
||||
.uri(&uri)
|
||||
.header(
|
||||
"Content-Type",
|
||||
"application/vnd.taskchampion.history-segment",
|
||||
)
|
||||
.header("X-Client-Key", client_key.to_string())
|
||||
.set_payload(b"abcd".to_vec())
|
||||
.to_request();
|
||||
let resp = test::call_service(&mut app, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
// the returned version ID is random, but let's check that it's not
|
||||
// the passed parent version ID, at least
|
||||
let new_version_id = resp.headers().get("X-Version-Id").unwrap();
|
||||
assert!(new_version_id != &version_id.to_string());
|
||||
|
||||
// Shapshot should be requested, since there is no existing snapshot
|
||||
let snapshot_request = resp.headers().get("X-Snapshot-Request").unwrap();
|
||||
assert_eq!(snapshot_request, "urgency=high");
|
||||
|
||||
assert_eq!(resp.headers().get("X-Parent-Version-Id"), None);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_conflict() {
|
||||
let client_key = Uuid::new_v4();
|
||||
let version_id = Uuid::new_v4();
|
||||
let parent_version_id = Uuid::new_v4();
|
||||
let storage: Box<dyn Storage> = Box::new(InMemoryStorage::new());
|
||||
|
||||
// set up the storage contents..
|
||||
{
|
||||
let mut txn = storage.txn().unwrap();
|
||||
txn.new_client(client_key, version_id).unwrap();
|
||||
}
|
||||
|
||||
let server = Server::new(Default::default(), storage);
|
||||
let app = App::new().configure(|sc| server.config(sc));
|
||||
let mut app = test::init_service(app).await;
|
||||
|
||||
let uri = format!("/v1/client/add-version/{}", parent_version_id);
|
||||
let req = test::TestRequest::post()
|
||||
.uri(&uri)
|
||||
.header(
|
||||
"Content-Type",
|
||||
"application/vnd.taskchampion.history-segment",
|
||||
)
|
||||
.header("X-Client-Key", client_key.to_string())
|
||||
.set_payload(b"abcd".to_vec())
|
||||
.to_request();
|
||||
let resp = test::call_service(&mut app, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::CONFLICT);
|
||||
assert_eq!(resp.headers().get("X-Version-Id"), None);
|
||||
assert_eq!(
|
||||
resp.headers().get("X-Parent-Version-Id").unwrap(),
|
||||
&version_id.to_string()
|
||||
);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_bad_content_type() {
|
||||
let client_key = Uuid::new_v4();
|
||||
let parent_version_id = Uuid::new_v4();
|
||||
let storage: Box<dyn Storage> = Box::new(InMemoryStorage::new());
|
||||
let server = Server::new(Default::default(), storage);
|
||||
let app = App::new().configure(|sc| server.config(sc));
|
||||
let mut app = test::init_service(app).await;
|
||||
|
||||
let uri = format!("/v1/client/add-version/{}", parent_version_id);
|
||||
let req = test::TestRequest::post()
|
||||
.uri(&uri)
|
||||
.header("Content-Type", "not/correct")
|
||||
.header("X-Client-Key", client_key.to_string())
|
||||
.set_payload(b"abcd".to_vec())
|
||||
.to_request();
|
||||
let resp = test::call_service(&mut app, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_empty_body() {
|
||||
let client_key = Uuid::new_v4();
|
||||
let parent_version_id = Uuid::new_v4();
|
||||
let storage: Box<dyn Storage> = Box::new(InMemoryStorage::new());
|
||||
let server = Server::new(Default::default(), storage);
|
||||
let app = App::new().configure(|sc| server.config(sc));
|
||||
let mut app = test::init_service(app).await;
|
||||
|
||||
let uri = format!("/v1/client/add-version/{}", parent_version_id);
|
||||
let req = test::TestRequest::post()
|
||||
.uri(&uri)
|
||||
.header(
|
||||
"Content-Type",
|
||||
"application/vnd.taskchampion.history-segment",
|
||||
)
|
||||
.header("X-Client-Key", client_key.to_string())
|
||||
.to_request();
|
||||
let resp = test::call_service(&mut app, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||
}
|
||||
}
|
167
rust/sync-server/src/api/get_child_version.rs
Normal file
167
rust/sync-server/src/api/get_child_version.rs
Normal file
|
@ -0,0 +1,167 @@
|
|||
use crate::api::{
|
||||
client_key_header, failure_to_ise, ServerState, HISTORY_SEGMENT_CONTENT_TYPE,
|
||||
PARENT_VERSION_ID_HEADER, VERSION_ID_HEADER,
|
||||
};
|
||||
use crate::server::{get_child_version, GetVersionResult, VersionId};
|
||||
use actix_web::{error, get, web, HttpRequest, HttpResponse, Result};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Get a child version.
|
||||
///
|
||||
/// On succcess, the response is the same sequence of bytes originally sent to the server,
|
||||
/// with content-type `application/vnd.taskchampion.history-segment`. The `X-Version-Id` and
|
||||
/// `X-Parent-Version-Id` headers contain the corresponding values.
|
||||
///
|
||||
/// If no such child exists, returns a 404 with no content.
|
||||
/// Returns other 4xx or 5xx responses on other errors.
|
||||
#[get("/v1/client/get-child-version/{parent_version_id}")]
|
||||
pub(crate) async fn service(
|
||||
req: HttpRequest,
|
||||
server_state: web::Data<Arc<ServerState>>,
|
||||
web::Path((parent_version_id,)): web::Path<(VersionId,)>,
|
||||
) -> Result<HttpResponse> {
|
||||
let mut txn = server_state.storage.txn().map_err(failure_to_ise)?;
|
||||
|
||||
let client_key = client_key_header(&req)?;
|
||||
|
||||
let client = txn
|
||||
.get_client(client_key)
|
||||
.map_err(failure_to_ise)?
|
||||
.ok_or_else(|| error::ErrorNotFound("no such client"))?;
|
||||
|
||||
return match get_child_version(
|
||||
txn,
|
||||
&server_state.config,
|
||||
client_key,
|
||||
client,
|
||||
parent_version_id,
|
||||
)
|
||||
.map_err(failure_to_ise)?
|
||||
{
|
||||
GetVersionResult::Success {
|
||||
version_id,
|
||||
parent_version_id,
|
||||
history_segment,
|
||||
} => Ok(HttpResponse::Ok()
|
||||
.content_type(HISTORY_SEGMENT_CONTENT_TYPE)
|
||||
.header(VERSION_ID_HEADER, version_id.to_string())
|
||||
.header(PARENT_VERSION_ID_HEADER, parent_version_id.to_string())
|
||||
.body(history_segment)),
|
||||
GetVersionResult::NotFound => Err(error::ErrorNotFound("no such version")),
|
||||
GetVersionResult::Gone => Err(error::ErrorGone("version has been deleted")),
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::server::NIL_VERSION_ID;
|
||||
use crate::storage::{InMemoryStorage, Storage};
|
||||
use crate::Server;
|
||||
use actix_web::{http::StatusCode, test, App};
|
||||
use pretty_assertions::assert_eq;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_success() {
|
||||
let client_key = Uuid::new_v4();
|
||||
let version_id = Uuid::new_v4();
|
||||
let parent_version_id = Uuid::new_v4();
|
||||
let storage: Box<dyn Storage> = Box::new(InMemoryStorage::new());
|
||||
|
||||
// set up the storage contents..
|
||||
{
|
||||
let mut txn = storage.txn().unwrap();
|
||||
txn.new_client(client_key, Uuid::new_v4()).unwrap();
|
||||
txn.add_version(client_key, version_id, parent_version_id, b"abcd".to_vec())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let server = Server::new(Default::default(), storage);
|
||||
let app = App::new().configure(|sc| server.config(sc));
|
||||
let mut app = test::init_service(app).await;
|
||||
|
||||
let uri = format!("/v1/client/get-child-version/{}", parent_version_id);
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&uri)
|
||||
.header("X-Client-Key", client_key.to_string())
|
||||
.to_request();
|
||||
let mut resp = test::call_service(&mut app, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
resp.headers().get("X-Version-Id").unwrap(),
|
||||
&version_id.to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
resp.headers().get("X-Parent-Version-Id").unwrap(),
|
||||
&parent_version_id.to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
resp.headers().get("Content-Type").unwrap(),
|
||||
&"application/vnd.taskchampion.history-segment".to_string()
|
||||
);
|
||||
|
||||
use futures::StreamExt;
|
||||
let (bytes, _) = resp.take_body().into_future().await;
|
||||
assert_eq!(bytes.unwrap().unwrap().as_ref(), b"abcd");
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_client_not_found() {
|
||||
let client_key = Uuid::new_v4();
|
||||
let parent_version_id = Uuid::new_v4();
|
||||
let storage: Box<dyn Storage> = Box::new(InMemoryStorage::new());
|
||||
let server = Server::new(Default::default(), storage);
|
||||
let app = App::new().configure(|sc| server.config(sc));
|
||||
let mut app = test::init_service(app).await;
|
||||
|
||||
let uri = format!("/v1/client/get-child-version/{}", parent_version_id);
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&uri)
|
||||
.header("X-Client-Key", client_key.to_string())
|
||||
.to_request();
|
||||
let resp = test::call_service(&mut app, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||
assert_eq!(resp.headers().get("X-Version-Id"), None);
|
||||
assert_eq!(resp.headers().get("X-Parent-Version-Id"), None);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_version_not_found_and_gone() {
|
||||
let client_key = Uuid::new_v4();
|
||||
let parent_version_id = Uuid::new_v4();
|
||||
let storage: Box<dyn Storage> = Box::new(InMemoryStorage::new());
|
||||
|
||||
// create the client, but not the version
|
||||
{
|
||||
let mut txn = storage.txn().unwrap();
|
||||
txn.new_client(client_key, Uuid::new_v4()).unwrap();
|
||||
}
|
||||
let server = Server::new(Default::default(), storage);
|
||||
let app = App::new().configure(|sc| server.config(sc));
|
||||
let mut app = test::init_service(app).await;
|
||||
|
||||
// the child of an unknown parent_version_id is GONE
|
||||
let uri = format!("/v1/client/get-child-version/{}", parent_version_id);
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&uri)
|
||||
.header("X-Client-Key", client_key.to_string())
|
||||
.to_request();
|
||||
let resp = test::call_service(&mut app, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::GONE);
|
||||
assert_eq!(resp.headers().get("X-Version-Id"), None);
|
||||
assert_eq!(resp.headers().get("X-Parent-Version-Id"), None);
|
||||
|
||||
// but the child of the nil parent_version_id is NOT FOUND, since
|
||||
// there is no snapshot. The tests in crate::server test more
|
||||
// corner cases.
|
||||
let uri = format!("/v1/client/get-child-version/{}", NIL_VERSION_ID);
|
||||
let req = test::TestRequest::get()
|
||||
.uri(&uri)
|
||||
.header("X-Client-Key", client_key.to_string())
|
||||
.to_request();
|
||||
let resp = test::call_service(&mut app, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||
assert_eq!(resp.headers().get("X-Version-Id"), None);
|
||||
assert_eq!(resp.headers().get("X-Parent-Version-Id"), None);
|
||||
}
|
||||
}
|
114
rust/sync-server/src/api/get_snapshot.rs
Normal file
114
rust/sync-server/src/api/get_snapshot.rs
Normal file
|
@ -0,0 +1,114 @@
|
|||
use crate::api::{
|
||||
client_key_header, failure_to_ise, ServerState, SNAPSHOT_CONTENT_TYPE, VERSION_ID_HEADER,
|
||||
};
|
||||
use crate::server::get_snapshot;
|
||||
use actix_web::{error, get, web, HttpRequest, HttpResponse, Result};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Get a snapshot.
|
||||
///
|
||||
/// If a snapshot for this client exists, it is returned with content-type
|
||||
/// `application/vnd.taskchampion.snapshot`. The `X-Version-Id` header contains the version of the
|
||||
/// snapshot.
|
||||
///
|
||||
/// If no snapshot exists, returns a 404 with no content. Returns other 4xx or 5xx responses on
|
||||
/// other errors.
|
||||
#[get("/v1/client/snapshot")]
|
||||
pub(crate) async fn service(
|
||||
req: HttpRequest,
|
||||
server_state: web::Data<Arc<ServerState>>,
|
||||
) -> Result<HttpResponse> {
|
||||
let mut txn = server_state.storage.txn().map_err(failure_to_ise)?;
|
||||
|
||||
let client_key = client_key_header(&req)?;
|
||||
|
||||
let client = txn
|
||||
.get_client(client_key)
|
||||
.map_err(failure_to_ise)?
|
||||
.ok_or_else(|| error::ErrorNotFound("no such client"))?;
|
||||
|
||||
if let Some((version_id, data)) =
|
||||
get_snapshot(txn, &server_state.config, client_key, client).map_err(failure_to_ise)?
|
||||
{
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type(SNAPSHOT_CONTENT_TYPE)
|
||||
.header(VERSION_ID_HEADER, version_id.to_string())
|
||||
.body(data))
|
||||
} else {
|
||||
Err(error::ErrorNotFound("no snapshot"))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::storage::{InMemoryStorage, Snapshot, Storage};
|
||||
use crate::Server;
|
||||
use actix_web::{http::StatusCode, test, App};
|
||||
use chrono::{TimeZone, Utc};
|
||||
use pretty_assertions::assert_eq;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_not_found() {
|
||||
let client_key = Uuid::new_v4();
|
||||
let storage: Box<dyn Storage> = Box::new(InMemoryStorage::new());
|
||||
|
||||
// set up the storage contents..
|
||||
{
|
||||
let mut txn = storage.txn().unwrap();
|
||||
txn.new_client(client_key, Uuid::new_v4()).unwrap();
|
||||
}
|
||||
|
||||
let server = Server::new(Default::default(), storage);
|
||||
let app = App::new().configure(|sc| server.config(sc));
|
||||
let mut app = test::init_service(app).await;
|
||||
|
||||
let uri = "/v1/client/snapshot";
|
||||
let req = test::TestRequest::get()
|
||||
.uri(uri)
|
||||
.header("X-Client-Key", client_key.to_string())
|
||||
.to_request();
|
||||
let resp = test::call_service(&mut app, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_success() {
|
||||
let client_key = Uuid::new_v4();
|
||||
let version_id = Uuid::new_v4();
|
||||
let snapshot_data = vec![1, 2, 3, 4];
|
||||
let storage: Box<dyn Storage> = Box::new(InMemoryStorage::new());
|
||||
|
||||
// set up the storage contents..
|
||||
{
|
||||
let mut txn = storage.txn().unwrap();
|
||||
txn.new_client(client_key, Uuid::new_v4()).unwrap();
|
||||
txn.set_snapshot(
|
||||
client_key,
|
||||
Snapshot {
|
||||
version_id,
|
||||
versions_since: 3,
|
||||
timestamp: Utc.ymd(2001, 9, 9).and_hms(1, 46, 40),
|
||||
},
|
||||
snapshot_data.clone(),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let server = Server::new(Default::default(), storage);
|
||||
let app = App::new().configure(|sc| server.config(sc));
|
||||
let mut app = test::init_service(app).await;
|
||||
|
||||
let uri = "/v1/client/snapshot";
|
||||
let req = test::TestRequest::get()
|
||||
.uri(uri)
|
||||
.header("X-Client-Key", client_key.to_string())
|
||||
.to_request();
|
||||
let mut resp = test::call_service(&mut app, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
use futures::StreamExt;
|
||||
let (bytes, _) = resp.take_body().into_future().await;
|
||||
assert_eq!(bytes.unwrap().unwrap().as_ref(), snapshot_data);
|
||||
}
|
||||
}
|
61
rust/sync-server/src/api/mod.rs
Normal file
61
rust/sync-server/src/api/mod.rs
Normal file
|
@ -0,0 +1,61 @@
|
|||
use crate::server::ClientKey;
|
||||
use crate::storage::Storage;
|
||||
use crate::ServerConfig;
|
||||
use actix_web::{error, http::StatusCode, web, HttpRequest, Result, Scope};
|
||||
|
||||
mod add_snapshot;
|
||||
mod add_version;
|
||||
mod get_child_version;
|
||||
mod get_snapshot;
|
||||
|
||||
/// The content-type for history segments (opaque blobs of bytes)
|
||||
pub(crate) const HISTORY_SEGMENT_CONTENT_TYPE: &str =
|
||||
"application/vnd.taskchampion.history-segment";
|
||||
|
||||
/// The content-type for snapshots (opaque blobs of bytes)
|
||||
pub(crate) const SNAPSHOT_CONTENT_TYPE: &str = "application/vnd.taskchampion.snapshot";
|
||||
|
||||
/// The header name for version ID
|
||||
pub(crate) const VERSION_ID_HEADER: &str = "X-Version-Id";
|
||||
|
||||
/// The header name for client key
|
||||
pub(crate) const CLIENT_KEY_HEADER: &str = "X-Client-Key";
|
||||
|
||||
/// The header name for parent version ID
|
||||
pub(crate) const PARENT_VERSION_ID_HEADER: &str = "X-Parent-Version-Id";
|
||||
|
||||
/// The header name for parent version ID
|
||||
pub(crate) const SNAPSHOT_REQUEST_HEADER: &str = "X-Snapshot-Request";
|
||||
|
||||
/// The type containing a reference to the persistent state for the server
|
||||
pub(crate) struct ServerState {
|
||||
pub(crate) storage: Box<dyn Storage>,
|
||||
pub(crate) config: ServerConfig,
|
||||
}
|
||||
|
||||
pub(crate) fn api_scope() -> Scope {
|
||||
web::scope("")
|
||||
.service(get_child_version::service)
|
||||
.service(add_version::service)
|
||||
.service(get_snapshot::service)
|
||||
.service(add_snapshot::service)
|
||||
}
|
||||
|
||||
/// Convert a failure::Error to an Actix ISE
|
||||
fn failure_to_ise(err: anyhow::Error) -> impl actix_web::ResponseError {
|
||||
error::InternalError::new(err, StatusCode::INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
|
||||
/// Get the client key
|
||||
fn client_key_header(req: &HttpRequest) -> Result<ClientKey> {
|
||||
fn badrequest() -> error::Error {
|
||||
error::ErrorBadRequest("bad x-client-id")
|
||||
}
|
||||
if let Some(client_key_hdr) = req.headers().get(CLIENT_KEY_HEADER) {
|
||||
let client_key = client_key_hdr.to_str().map_err(|_| badrequest())?;
|
||||
let client_key = ClientKey::parse_str(client_key).map_err(|_| badrequest())?;
|
||||
Ok(client_key)
|
||||
} else {
|
||||
Err(badrequest())
|
||||
}
|
||||
}
|
93
rust/sync-server/src/bin/taskchampion-sync-server.rs
Normal file
93
rust/sync-server/src/bin/taskchampion-sync-server.rs
Normal file
|
@ -0,0 +1,93 @@
|
|||
#![deny(clippy::all)]
|
||||
|
||||
use actix_web::{middleware::Logger, App, HttpServer};
|
||||
use clap::Arg;
|
||||
use taskchampion_sync_server::storage::SqliteStorage;
|
||||
use taskchampion_sync_server::{Server, ServerConfig};
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
env_logger::init();
|
||||
let defaults = ServerConfig::default();
|
||||
let default_snapshot_versions = defaults.snapshot_versions.to_string();
|
||||
let default_snapshot_days = defaults.snapshot_days.to_string();
|
||||
let matches = clap::App::new("taskchampion-sync-server")
|
||||
.version(env!("CARGO_PKG_VERSION"))
|
||||
.about("Server for TaskChampion")
|
||||
.arg(
|
||||
Arg::with_name("port")
|
||||
.short("p")
|
||||
.long("port")
|
||||
.value_name("PORT")
|
||||
.help("Port on which to serve")
|
||||
.default_value("8080")
|
||||
.takes_value(true)
|
||||
.required(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("data-dir")
|
||||
.short("d")
|
||||
.long("data-dir")
|
||||
.value_name("DIR")
|
||||
.help("Directory in which to store data")
|
||||
.default_value("/var/lib/taskchampion-sync-server")
|
||||
.takes_value(true)
|
||||
.required(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("snapshot-versions")
|
||||
.long("snapshot-versions")
|
||||
.value_name("NUM")
|
||||
.help("Target number of versions between snapshots")
|
||||
.default_value(&default_snapshot_versions)
|
||||
.takes_value(true)
|
||||
.required(false),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("snapshot-days")
|
||||
.long("snapshot-days")
|
||||
.value_name("NUM")
|
||||
.help("Target number of days between snapshots")
|
||||
.default_value(&default_snapshot_days)
|
||||
.takes_value(true)
|
||||
.required(false),
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
let data_dir = matches.value_of("data-dir").unwrap();
|
||||
let port = matches.value_of("port").unwrap();
|
||||
let snapshot_versions = matches.value_of("snapshot-versions").unwrap();
|
||||
let snapshot_days = matches.value_of("snapshot-versions").unwrap();
|
||||
|
||||
let config = ServerConfig::from_args(snapshot_days, snapshot_versions)?;
|
||||
let server = Server::new(config, Box::new(SqliteStorage::new(data_dir)?));
|
||||
|
||||
log::warn!("Serving on port {}", port);
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.wrap(Logger::default())
|
||||
.configure(|cfg| server.config(cfg))
|
||||
})
|
||||
.bind(format!("0.0.0.0:{}", port))?
|
||||
.run()
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use actix_web::{test, App};
|
||||
use taskchampion_sync_server::storage::InMemoryStorage;
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_index_get() {
|
||||
let server = Server::new(Default::default(), Box::new(InMemoryStorage::new()));
|
||||
let app = App::new().configure(|sc| server.config(sc));
|
||||
let mut app = test::init_service(app).await;
|
||||
|
||||
let req = test::TestRequest::get().uri("/").to_request();
|
||||
let resp = test::call_service(&mut app, req).await;
|
||||
assert!(resp.status().is_success());
|
||||
}
|
||||
}
|
73
rust/sync-server/src/lib.rs
Normal file
73
rust/sync-server/src/lib.rs
Normal file
|
@ -0,0 +1,73 @@
|
|||
#![deny(clippy::all)]
|
||||
|
||||
mod api;
|
||||
mod server;
|
||||
pub mod storage;
|
||||
|
||||
use crate::storage::Storage;
|
||||
use actix_web::{get, middleware, web, Responder};
|
||||
use api::{api_scope, ServerState};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub use server::ServerConfig;
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> impl Responder {
|
||||
format!("TaskChampion sync server v{}", env!("CARGO_PKG_VERSION"))
|
||||
}
|
||||
|
||||
/// A Server represents a sync server.
|
||||
#[derive(Clone)]
|
||||
pub struct Server {
|
||||
server_state: Arc<ServerState>,
|
||||
}
|
||||
|
||||
impl Server {
|
||||
/// Create a new sync server with the given storage implementation.
|
||||
pub fn new(config: ServerConfig, storage: Box<dyn Storage>) -> Self {
|
||||
Self {
|
||||
server_state: Arc::new(ServerState { config, storage }),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get an Actix-web service for this server.
|
||||
pub fn config(&self, cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(
|
||||
web::scope("")
|
||||
.data(self.server_state.clone())
|
||||
.wrap(
|
||||
middleware::DefaultHeaders::new()
|
||||
.header("Cache-Control", "no-store, max-age=0"),
|
||||
)
|
||||
.service(index)
|
||||
.service(api_scope()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::storage::InMemoryStorage;
|
||||
use actix_web::{test, App};
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
pub(crate) fn init_logging() {
|
||||
let _ = env_logger::builder().is_test(true).try_init();
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_cache_control() {
|
||||
let server = Server::new(Default::default(), Box::new(InMemoryStorage::new()));
|
||||
let app = App::new().configure(|sc| server.config(sc));
|
||||
let mut app = test::init_service(app).await;
|
||||
|
||||
let req = test::TestRequest::get().uri("/").to_request();
|
||||
let resp = test::call_service(&mut app, req).await;
|
||||
assert!(resp.status().is_success());
|
||||
assert_eq!(
|
||||
resp.headers().get("Cache-Control").unwrap(),
|
||||
&"no-store, max-age=0".to_string()
|
||||
)
|
||||
}
|
||||
}
|
1053
rust/sync-server/src/server.rs
Normal file
1053
rust/sync-server/src/server.rs
Normal file
File diff suppressed because it is too large
Load diff
287
rust/sync-server/src/storage/inmemory.rs
Normal file
287
rust/sync-server/src/storage/inmemory.rs
Normal file
|
@ -0,0 +1,287 @@
|
|||
use super::{Client, Snapshot, Storage, StorageTxn, Uuid, Version};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Mutex, MutexGuard};
|
||||
|
||||
struct Inner {
|
||||
/// Clients, indexed by client_key
|
||||
clients: HashMap<Uuid, Client>,
|
||||
|
||||
/// Snapshot data, indexed by client key
|
||||
snapshots: HashMap<Uuid, Vec<u8>>,
|
||||
|
||||
/// Versions, indexed by (client_key, version_id)
|
||||
versions: HashMap<(Uuid, Uuid), Version>,
|
||||
|
||||
/// Child versions, indexed by (client_key, parent_version_id)
|
||||
children: HashMap<(Uuid, Uuid), Uuid>,
|
||||
}
|
||||
|
||||
pub struct InMemoryStorage(Mutex<Inner>);
|
||||
|
||||
impl InMemoryStorage {
|
||||
#[allow(clippy::new_without_default)]
|
||||
pub fn new() -> Self {
|
||||
Self(Mutex::new(Inner {
|
||||
clients: HashMap::new(),
|
||||
snapshots: HashMap::new(),
|
||||
versions: HashMap::new(),
|
||||
children: HashMap::new(),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
struct InnerTxn<'a>(MutexGuard<'a, Inner>);
|
||||
|
||||
/// In-memory storage for testing and experimentation.
|
||||
///
|
||||
/// NOTE: this does not implement transaction rollback.
|
||||
impl Storage for InMemoryStorage {
|
||||
fn txn<'a>(&'a self) -> anyhow::Result<Box<dyn StorageTxn + 'a>> {
|
||||
Ok(Box::new(InnerTxn(self.0.lock().expect("poisoned lock"))))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> StorageTxn for InnerTxn<'a> {
|
||||
fn get_client(&mut self, client_key: Uuid) -> anyhow::Result<Option<Client>> {
|
||||
Ok(self.0.clients.get(&client_key).cloned())
|
||||
}
|
||||
|
||||
fn new_client(&mut self, client_key: Uuid, latest_version_id: Uuid) -> anyhow::Result<()> {
|
||||
if self.0.clients.get(&client_key).is_some() {
|
||||
return Err(anyhow::anyhow!("Client {} already exists", client_key));
|
||||
}
|
||||
self.0.clients.insert(
|
||||
client_key,
|
||||
Client {
|
||||
latest_version_id,
|
||||
snapshot: None,
|
||||
},
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_snapshot(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
snapshot: Snapshot,
|
||||
data: Vec<u8>,
|
||||
) -> anyhow::Result<()> {
|
||||
let mut client = self
|
||||
.0
|
||||
.clients
|
||||
.get_mut(&client_key)
|
||||
.ok_or_else(|| anyhow::anyhow!("no such client"))?;
|
||||
client.snapshot = Some(snapshot);
|
||||
self.0.snapshots.insert(client_key, data);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_snapshot_data(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
version_id: Uuid,
|
||||
) -> anyhow::Result<Option<Vec<u8>>> {
|
||||
// sanity check
|
||||
let client = self.0.clients.get(&client_key);
|
||||
let client = client.ok_or_else(|| anyhow::anyhow!("no such client"))?;
|
||||
if Some(&version_id) != client.snapshot.as_ref().map(|snap| &snap.version_id) {
|
||||
return Err(anyhow::anyhow!("unexpected snapshot_version_id"));
|
||||
}
|
||||
Ok(self.0.snapshots.get(&client_key).cloned())
|
||||
}
|
||||
|
||||
fn get_version_by_parent(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
parent_version_id: Uuid,
|
||||
) -> anyhow::Result<Option<Version>> {
|
||||
if let Some(parent_version_id) = self.0.children.get(&(client_key, parent_version_id)) {
|
||||
Ok(self
|
||||
.0
|
||||
.versions
|
||||
.get(&(client_key, *parent_version_id))
|
||||
.cloned())
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_version(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
version_id: Uuid,
|
||||
) -> anyhow::Result<Option<Version>> {
|
||||
Ok(self.0.versions.get(&(client_key, version_id)).cloned())
|
||||
}
|
||||
|
||||
fn add_version(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
version_id: Uuid,
|
||||
parent_version_id: Uuid,
|
||||
history_segment: Vec<u8>,
|
||||
) -> anyhow::Result<()> {
|
||||
// TODO: verify it doesn't exist (`.entry`?)
|
||||
let version = Version {
|
||||
version_id,
|
||||
parent_version_id,
|
||||
history_segment,
|
||||
};
|
||||
|
||||
if let Some(client) = self.0.clients.get_mut(&client_key) {
|
||||
client.latest_version_id = version_id;
|
||||
if let Some(ref mut snap) = client.snapshot {
|
||||
snap.versions_since += 1;
|
||||
}
|
||||
} else {
|
||||
return Err(anyhow::anyhow!("Client {} does not exist", client_key));
|
||||
}
|
||||
|
||||
self.0
|
||||
.children
|
||||
.insert((client_key, parent_version_id), version_id);
|
||||
self.0.versions.insert((client_key, version_id), version);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn commit(&mut self) -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use chrono::Utc;
|
||||
|
||||
#[test]
|
||||
fn test_get_client_empty() -> anyhow::Result<()> {
|
||||
let storage = InMemoryStorage::new();
|
||||
let mut txn = storage.txn()?;
|
||||
let maybe_client = txn.get_client(Uuid::new_v4())?;
|
||||
assert!(maybe_client.is_none());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_client_storage() -> anyhow::Result<()> {
|
||||
let storage = InMemoryStorage::new();
|
||||
let mut txn = storage.txn()?;
|
||||
|
||||
let client_key = Uuid::new_v4();
|
||||
let latest_version_id = Uuid::new_v4();
|
||||
txn.new_client(client_key, latest_version_id)?;
|
||||
|
||||
let client = txn.get_client(client_key)?.unwrap();
|
||||
assert_eq!(client.latest_version_id, latest_version_id);
|
||||
assert!(client.snapshot.is_none());
|
||||
|
||||
let latest_version_id = Uuid::new_v4();
|
||||
txn.add_version(client_key, latest_version_id, Uuid::new_v4(), vec![1, 1])?;
|
||||
|
||||
let client = txn.get_client(client_key)?.unwrap();
|
||||
assert_eq!(client.latest_version_id, latest_version_id);
|
||||
assert!(client.snapshot.is_none());
|
||||
|
||||
let snap = Snapshot {
|
||||
version_id: Uuid::new_v4(),
|
||||
timestamp: Utc::now(),
|
||||
versions_since: 4,
|
||||
};
|
||||
txn.set_snapshot(client_key, snap.clone(), vec![1, 2, 3])?;
|
||||
|
||||
let client = txn.get_client(client_key)?.unwrap();
|
||||
assert_eq!(client.latest_version_id, latest_version_id);
|
||||
assert_eq!(client.snapshot.unwrap(), snap);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gvbp_empty() -> anyhow::Result<()> {
|
||||
let storage = InMemoryStorage::new();
|
||||
let mut txn = storage.txn()?;
|
||||
let maybe_version = txn.get_version_by_parent(Uuid::new_v4(), Uuid::new_v4())?;
|
||||
assert!(maybe_version.is_none());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_version_and_get_version() -> anyhow::Result<()> {
|
||||
let storage = InMemoryStorage::new();
|
||||
let mut txn = storage.txn()?;
|
||||
|
||||
let client_key = Uuid::new_v4();
|
||||
let version_id = Uuid::new_v4();
|
||||
let parent_version_id = Uuid::new_v4();
|
||||
let history_segment = b"abc".to_vec();
|
||||
|
||||
txn.new_client(client_key, parent_version_id)?;
|
||||
txn.add_version(
|
||||
client_key,
|
||||
version_id,
|
||||
parent_version_id,
|
||||
history_segment.clone(),
|
||||
)?;
|
||||
|
||||
let expected = Version {
|
||||
version_id,
|
||||
parent_version_id,
|
||||
history_segment,
|
||||
};
|
||||
|
||||
let version = txn
|
||||
.get_version_by_parent(client_key, parent_version_id)?
|
||||
.unwrap();
|
||||
assert_eq!(version, expected);
|
||||
|
||||
let version = txn.get_version(client_key, version_id)?.unwrap();
|
||||
assert_eq!(version, expected);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_snapshots() -> anyhow::Result<()> {
|
||||
let storage = InMemoryStorage::new();
|
||||
let mut txn = storage.txn()?;
|
||||
|
||||
let client_key = Uuid::new_v4();
|
||||
|
||||
txn.new_client(client_key, Uuid::new_v4())?;
|
||||
assert!(txn.get_client(client_key)?.unwrap().snapshot.is_none());
|
||||
|
||||
let snap = Snapshot {
|
||||
version_id: Uuid::new_v4(),
|
||||
timestamp: Utc::now(),
|
||||
versions_since: 3,
|
||||
};
|
||||
txn.set_snapshot(client_key, snap.clone(), vec![9, 8, 9])?;
|
||||
|
||||
assert_eq!(
|
||||
txn.get_snapshot_data(client_key, snap.version_id)?.unwrap(),
|
||||
vec![9, 8, 9]
|
||||
);
|
||||
assert_eq!(txn.get_client(client_key)?.unwrap().snapshot, Some(snap));
|
||||
|
||||
let snap2 = Snapshot {
|
||||
version_id: Uuid::new_v4(),
|
||||
timestamp: Utc::now(),
|
||||
versions_since: 10,
|
||||
};
|
||||
txn.set_snapshot(client_key, snap2.clone(), vec![0, 2, 4, 6])?;
|
||||
|
||||
assert_eq!(
|
||||
txn.get_snapshot_data(client_key, snap2.version_id)?
|
||||
.unwrap(),
|
||||
vec![0, 2, 4, 6]
|
||||
);
|
||||
assert_eq!(txn.get_client(client_key)?.unwrap().snapshot, Some(snap2));
|
||||
|
||||
// check that mismatched version is detected
|
||||
assert!(txn.get_snapshot_data(client_key, Uuid::new_v4()).is_err());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
98
rust/sync-server/src/storage/mod.rs
Normal file
98
rust/sync-server/src/storage/mod.rs
Normal file
|
@ -0,0 +1,98 @@
|
|||
use chrono::{DateTime, Utc};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
mod inmemory;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
pub use inmemory::InMemoryStorage;
|
||||
|
||||
mod sqlite;
|
||||
pub use self::sqlite::SqliteStorage;
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub struct Client {
|
||||
/// The latest version for this client (may be the nil version)
|
||||
pub latest_version_id: Uuid,
|
||||
/// Data about the latest snapshot for this client
|
||||
pub snapshot: Option<Snapshot>,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub struct Snapshot {
|
||||
/// ID of the version at which this snapshot was made
|
||||
pub version_id: Uuid,
|
||||
|
||||
/// Timestamp at which this snapshot was set
|
||||
pub timestamp: DateTime<Utc>,
|
||||
|
||||
/// Number of versions since this snapshot was made
|
||||
pub versions_since: u32,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Debug)]
|
||||
pub struct Version {
|
||||
pub version_id: Uuid,
|
||||
pub parent_version_id: Uuid,
|
||||
pub history_segment: Vec<u8>,
|
||||
}
|
||||
|
||||
pub trait StorageTxn {
|
||||
/// Get information about the given client
|
||||
fn get_client(&mut self, client_key: Uuid) -> anyhow::Result<Option<Client>>;
|
||||
|
||||
/// Create a new client with the given latest_version_id
|
||||
fn new_client(&mut self, client_key: Uuid, latest_version_id: Uuid) -> anyhow::Result<()>;
|
||||
|
||||
/// Set the client's most recent snapshot.
|
||||
fn set_snapshot(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
snapshot: Snapshot,
|
||||
data: Vec<u8>,
|
||||
) -> anyhow::Result<()>;
|
||||
|
||||
/// Get the data for the most recent snapshot. The version_id
|
||||
/// is used to verify that the snapshot is for the correct version.
|
||||
fn get_snapshot_data(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
version_id: Uuid,
|
||||
) -> anyhow::Result<Option<Vec<u8>>>;
|
||||
|
||||
/// Get a version, indexed by parent version id
|
||||
fn get_version_by_parent(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
parent_version_id: Uuid,
|
||||
) -> anyhow::Result<Option<Version>>;
|
||||
|
||||
/// Get a version, indexed by its own version id
|
||||
fn get_version(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
version_id: Uuid,
|
||||
) -> anyhow::Result<Option<Version>>;
|
||||
|
||||
/// Add a version (that must not already exist), and
|
||||
/// - update latest_version_id
|
||||
/// - increment snapshot.versions_since
|
||||
fn add_version(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
version_id: Uuid,
|
||||
parent_version_id: Uuid,
|
||||
history_segment: Vec<u8>,
|
||||
) -> anyhow::Result<()>;
|
||||
|
||||
/// Commit any changes made in the transaction. It is an error to call this more than
|
||||
/// once. It is safe to skip this call for read-only operations.
|
||||
fn commit(&mut self) -> anyhow::Result<()>;
|
||||
}
|
||||
|
||||
/// A trait for objects able to act as storage. Most of the interesting behavior is in the
|
||||
/// [`crate::storage::StorageTxn`] trait.
|
||||
pub trait Storage: Send + Sync {
|
||||
/// Begin a transaction
|
||||
fn txn<'a>(&'a self) -> anyhow::Result<Box<dyn StorageTxn + 'a>>;
|
||||
}
|
452
rust/sync-server/src/storage/sqlite.rs
Normal file
452
rust/sync-server/src/storage/sqlite.rs
Normal file
|
@ -0,0 +1,452 @@
|
|||
use super::{Client, Snapshot, Storage, StorageTxn, Uuid, Version};
|
||||
use anyhow::Context;
|
||||
use chrono::{TimeZone, Utc};
|
||||
use rusqlite::types::{FromSql, ToSql};
|
||||
use rusqlite::{params, Connection, OptionalExtension};
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
enum SqliteError {
|
||||
#[error("Failed to create SQLite transaction")]
|
||||
CreateTransactionFailed,
|
||||
}
|
||||
|
||||
/// Newtype to allow implementing `FromSql` for foreign `uuid::Uuid`
|
||||
struct StoredUuid(Uuid);
|
||||
|
||||
/// Conversion from Uuid stored as a string (rusqlite's uuid feature stores as binary blob)
|
||||
impl FromSql for StoredUuid {
|
||||
fn column_result(value: rusqlite::types::ValueRef<'_>) -> rusqlite::types::FromSqlResult<Self> {
|
||||
let u = Uuid::parse_str(value.as_str()?)
|
||||
.map_err(|_| rusqlite::types::FromSqlError::InvalidType)?;
|
||||
Ok(StoredUuid(u))
|
||||
}
|
||||
}
|
||||
|
||||
/// Store Uuid as string in database
|
||||
impl ToSql for StoredUuid {
|
||||
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
|
||||
let s = self.0.to_string();
|
||||
Ok(s.into())
|
||||
}
|
||||
}
|
||||
|
||||
/// An on-disk storage backend which uses SQLite
|
||||
pub struct SqliteStorage {
|
||||
db_file: std::path::PathBuf,
|
||||
}
|
||||
|
||||
impl SqliteStorage {
|
||||
fn new_connection(&self) -> anyhow::Result<Connection> {
|
||||
Ok(Connection::open(&self.db_file)?)
|
||||
}
|
||||
|
||||
pub fn new<P: AsRef<Path>>(directory: P) -> anyhow::Result<SqliteStorage> {
|
||||
std::fs::create_dir_all(&directory)?;
|
||||
let db_file = directory.as_ref().join("taskchampion-sync-server.sqlite3");
|
||||
|
||||
let o = SqliteStorage { db_file };
|
||||
|
||||
{
|
||||
let mut con = o.new_connection()?;
|
||||
let txn = con.transaction()?;
|
||||
|
||||
let queries = vec![
|
||||
"CREATE TABLE IF NOT EXISTS clients (
|
||||
client_key STRING PRIMARY KEY,
|
||||
latest_version_id STRING,
|
||||
snapshot_version_id STRING,
|
||||
versions_since_snapshot INTEGER,
|
||||
snapshot_timestamp INTEGER,
|
||||
snapshot BLOB);",
|
||||
"CREATE TABLE IF NOT EXISTS versions (version_id STRING PRIMARY KEY, client_key STRING, parent_version_id STRING, history_segment BLOB);",
|
||||
"CREATE INDEX IF NOT EXISTS versions_by_parent ON versions (parent_version_id);",
|
||||
];
|
||||
for q in queries {
|
||||
txn.execute(q, [])
|
||||
.context("Error while creating SQLite tables")?;
|
||||
}
|
||||
txn.commit()?;
|
||||
}
|
||||
|
||||
Ok(o)
|
||||
}
|
||||
}
|
||||
|
||||
impl Storage for SqliteStorage {
|
||||
fn txn<'a>(&'a self) -> anyhow::Result<Box<dyn StorageTxn + 'a>> {
|
||||
let con = self.new_connection()?;
|
||||
let t = Txn { con };
|
||||
Ok(Box::new(t))
|
||||
}
|
||||
}
|
||||
|
||||
struct Txn {
|
||||
con: Connection,
|
||||
}
|
||||
|
||||
impl Txn {
|
||||
fn get_txn(&mut self) -> Result<rusqlite::Transaction, SqliteError> {
|
||||
self.con
|
||||
.transaction()
|
||||
.map_err(|_e| SqliteError::CreateTransactionFailed)
|
||||
}
|
||||
|
||||
/// Implementation for queries from the versions table
|
||||
fn get_version_impl(
|
||||
&mut self,
|
||||
query: &'static str,
|
||||
client_key: Uuid,
|
||||
version_id_arg: Uuid,
|
||||
) -> anyhow::Result<Option<Version>> {
|
||||
let t = self.get_txn()?;
|
||||
let r = t
|
||||
.query_row(
|
||||
query,
|
||||
params![&StoredUuid(version_id_arg), &StoredUuid(client_key)],
|
||||
|r| {
|
||||
let version_id: StoredUuid = r.get("version_id")?;
|
||||
let parent_version_id: StoredUuid = r.get("parent_version_id")?;
|
||||
|
||||
Ok(Version {
|
||||
version_id: version_id.0,
|
||||
parent_version_id: parent_version_id.0,
|
||||
history_segment: r.get("history_segment")?,
|
||||
})
|
||||
},
|
||||
)
|
||||
.optional()
|
||||
.context("Error getting version")?;
|
||||
Ok(r)
|
||||
}
|
||||
}
|
||||
|
||||
impl StorageTxn for Txn {
|
||||
fn get_client(&mut self, client_key: Uuid) -> anyhow::Result<Option<Client>> {
|
||||
let t = self.get_txn()?;
|
||||
let result: Option<Client> = t
|
||||
.query_row(
|
||||
"SELECT
|
||||
latest_version_id,
|
||||
snapshot_timestamp,
|
||||
versions_since_snapshot,
|
||||
snapshot_version_id
|
||||
FROM clients
|
||||
WHERE client_key = ?
|
||||
LIMIT 1",
|
||||
[&StoredUuid(client_key)],
|
||||
|r| {
|
||||
let latest_version_id: StoredUuid = r.get(0)?;
|
||||
let snapshot_timestamp: Option<i64> = r.get(1)?;
|
||||
let versions_since_snapshot: Option<u32> = r.get(2)?;
|
||||
let snapshot_version_id: Option<StoredUuid> = r.get(3)?;
|
||||
|
||||
// if all of the relevant fields are non-NULL, return a snapshot
|
||||
let snapshot = match (
|
||||
snapshot_timestamp,
|
||||
versions_since_snapshot,
|
||||
snapshot_version_id,
|
||||
) {
|
||||
(Some(ts), Some(vs), Some(v)) => Some(Snapshot {
|
||||
version_id: v.0,
|
||||
timestamp: Utc.timestamp(ts, 0),
|
||||
versions_since: vs,
|
||||
}),
|
||||
_ => None,
|
||||
};
|
||||
Ok(Client {
|
||||
latest_version_id: latest_version_id.0,
|
||||
snapshot,
|
||||
})
|
||||
},
|
||||
)
|
||||
.optional()
|
||||
.context("Error getting client")?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn new_client(&mut self, client_key: Uuid, latest_version_id: Uuid) -> anyhow::Result<()> {
|
||||
let t = self.get_txn()?;
|
||||
|
||||
t.execute(
|
||||
"INSERT OR REPLACE INTO clients (client_key, latest_version_id) VALUES (?, ?)",
|
||||
params![&StoredUuid(client_key), &StoredUuid(latest_version_id)],
|
||||
)
|
||||
.context("Error creating/updating client")?;
|
||||
t.commit()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_snapshot(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
snapshot: Snapshot,
|
||||
data: Vec<u8>,
|
||||
) -> anyhow::Result<()> {
|
||||
let t = self.get_txn()?;
|
||||
|
||||
t.execute(
|
||||
"UPDATE clients
|
||||
SET
|
||||
snapshot_version_id = ?,
|
||||
snapshot_timestamp = ?,
|
||||
versions_since_snapshot = ?,
|
||||
snapshot = ?
|
||||
WHERE client_key = ?",
|
||||
params![
|
||||
&StoredUuid(snapshot.version_id),
|
||||
snapshot.timestamp.timestamp(),
|
||||
snapshot.versions_since,
|
||||
data,
|
||||
&StoredUuid(client_key),
|
||||
],
|
||||
)
|
||||
.context("Error creating/updating snapshot")?;
|
||||
t.commit()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_snapshot_data(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
version_id: Uuid,
|
||||
) -> anyhow::Result<Option<Vec<u8>>> {
|
||||
let t = self.get_txn()?;
|
||||
let r = t
|
||||
.query_row(
|
||||
"SELECT snapshot, snapshot_version_id FROM clients WHERE client_key = ?",
|
||||
params![&StoredUuid(client_key)],
|
||||
|r| {
|
||||
let v: StoredUuid = r.get("snapshot_version_id")?;
|
||||
let d: Vec<u8> = r.get("snapshot")?;
|
||||
Ok((v.0, d))
|
||||
},
|
||||
)
|
||||
.optional()
|
||||
.context("Error getting snapshot")?;
|
||||
r.map(|(v, d)| {
|
||||
if v != version_id {
|
||||
return Err(anyhow::anyhow!("unexpected snapshot_version_id"));
|
||||
}
|
||||
|
||||
Ok(d)
|
||||
})
|
||||
.transpose()
|
||||
}
|
||||
|
||||
fn get_version_by_parent(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
parent_version_id: Uuid,
|
||||
) -> anyhow::Result<Option<Version>> {
|
||||
self.get_version_impl(
|
||||
"SELECT version_id, parent_version_id, history_segment FROM versions WHERE parent_version_id = ? AND client_key = ?",
|
||||
client_key,
|
||||
parent_version_id)
|
||||
}
|
||||
|
||||
fn get_version(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
version_id: Uuid,
|
||||
) -> anyhow::Result<Option<Version>> {
|
||||
self.get_version_impl(
|
||||
"SELECT version_id, parent_version_id, history_segment FROM versions WHERE version_id = ? AND client_key = ?",
|
||||
client_key,
|
||||
version_id)
|
||||
}
|
||||
|
||||
fn add_version(
|
||||
&mut self,
|
||||
client_key: Uuid,
|
||||
version_id: Uuid,
|
||||
parent_version_id: Uuid,
|
||||
history_segment: Vec<u8>,
|
||||
) -> anyhow::Result<()> {
|
||||
let t = self.get_txn()?;
|
||||
|
||||
t.execute(
|
||||
"INSERT INTO versions (version_id, client_key, parent_version_id, history_segment) VALUES(?, ?, ?, ?)",
|
||||
params![
|
||||
StoredUuid(version_id),
|
||||
StoredUuid(client_key),
|
||||
StoredUuid(parent_version_id),
|
||||
history_segment
|
||||
]
|
||||
)
|
||||
.context("Error adding version")?;
|
||||
t.execute(
|
||||
"UPDATE clients
|
||||
SET
|
||||
latest_version_id = ?,
|
||||
versions_since_snapshot = versions_since_snapshot + 1
|
||||
WHERE client_key = ?",
|
||||
params![StoredUuid(version_id), StoredUuid(client_key),],
|
||||
)
|
||||
.context("Error updating client for new version")?;
|
||||
|
||||
t.commit()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn commit(&mut self) -> anyhow::Result<()> {
|
||||
// FIXME: Note the queries aren't currently run in a
|
||||
// transaction, as storing the transaction object and a pooled
|
||||
// connection in the `Txn` object is complex.
|
||||
// https://github.com/taskchampion/taskchampion/pull/206#issuecomment-860336073
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use chrono::DateTime;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tempfile::TempDir;
|
||||
|
||||
#[test]
|
||||
fn test_emtpy_dir() -> anyhow::Result<()> {
|
||||
let tmp_dir = TempDir::new()?;
|
||||
let non_existant = tmp_dir.path().join("subdir");
|
||||
let storage = SqliteStorage::new(&non_existant)?;
|
||||
let mut txn = storage.txn()?;
|
||||
let maybe_client = txn.get_client(Uuid::new_v4())?;
|
||||
assert!(maybe_client.is_none());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_client_empty() -> anyhow::Result<()> {
|
||||
let tmp_dir = TempDir::new()?;
|
||||
let storage = SqliteStorage::new(&tmp_dir.path())?;
|
||||
let mut txn = storage.txn()?;
|
||||
let maybe_client = txn.get_client(Uuid::new_v4())?;
|
||||
assert!(maybe_client.is_none());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_client_storage() -> anyhow::Result<()> {
|
||||
let tmp_dir = TempDir::new()?;
|
||||
let storage = SqliteStorage::new(&tmp_dir.path())?;
|
||||
let mut txn = storage.txn()?;
|
||||
|
||||
let client_key = Uuid::new_v4();
|
||||
let latest_version_id = Uuid::new_v4();
|
||||
txn.new_client(client_key, latest_version_id)?;
|
||||
|
||||
let client = txn.get_client(client_key)?.unwrap();
|
||||
assert_eq!(client.latest_version_id, latest_version_id);
|
||||
assert!(client.snapshot.is_none());
|
||||
|
||||
let latest_version_id = Uuid::new_v4();
|
||||
txn.add_version(client_key, latest_version_id, Uuid::new_v4(), vec![1, 1])?;
|
||||
|
||||
let client = txn.get_client(client_key)?.unwrap();
|
||||
assert_eq!(client.latest_version_id, latest_version_id);
|
||||
assert!(client.snapshot.is_none());
|
||||
|
||||
let snap = Snapshot {
|
||||
version_id: Uuid::new_v4(),
|
||||
timestamp: "2014-11-28T12:00:09Z".parse::<DateTime<Utc>>().unwrap(),
|
||||
versions_since: 4,
|
||||
};
|
||||
txn.set_snapshot(client_key, snap.clone(), vec![1, 2, 3])?;
|
||||
|
||||
let client = txn.get_client(client_key)?.unwrap();
|
||||
assert_eq!(client.latest_version_id, latest_version_id);
|
||||
assert_eq!(client.snapshot.unwrap(), snap);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gvbp_empty() -> anyhow::Result<()> {
|
||||
let tmp_dir = TempDir::new()?;
|
||||
let storage = SqliteStorage::new(&tmp_dir.path())?;
|
||||
let mut txn = storage.txn()?;
|
||||
let maybe_version = txn.get_version_by_parent(Uuid::new_v4(), Uuid::new_v4())?;
|
||||
assert!(maybe_version.is_none());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_version_and_get_version() -> anyhow::Result<()> {
|
||||
let tmp_dir = TempDir::new()?;
|
||||
let storage = SqliteStorage::new(&tmp_dir.path())?;
|
||||
let mut txn = storage.txn()?;
|
||||
|
||||
let client_key = Uuid::new_v4();
|
||||
let version_id = Uuid::new_v4();
|
||||
let parent_version_id = Uuid::new_v4();
|
||||
let history_segment = b"abc".to_vec();
|
||||
txn.add_version(
|
||||
client_key,
|
||||
version_id,
|
||||
parent_version_id,
|
||||
history_segment.clone(),
|
||||
)?;
|
||||
|
||||
let expected = Version {
|
||||
version_id,
|
||||
parent_version_id,
|
||||
history_segment,
|
||||
};
|
||||
|
||||
let version = txn
|
||||
.get_version_by_parent(client_key, parent_version_id)?
|
||||
.unwrap();
|
||||
assert_eq!(version, expected);
|
||||
|
||||
let version = txn.get_version(client_key, version_id)?.unwrap();
|
||||
assert_eq!(version, expected);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_snapshots() -> anyhow::Result<()> {
|
||||
let tmp_dir = TempDir::new()?;
|
||||
let storage = SqliteStorage::new(&tmp_dir.path())?;
|
||||
let mut txn = storage.txn()?;
|
||||
|
||||
let client_key = Uuid::new_v4();
|
||||
|
||||
txn.new_client(client_key, Uuid::new_v4())?;
|
||||
assert!(txn.get_client(client_key)?.unwrap().snapshot.is_none());
|
||||
|
||||
let snap = Snapshot {
|
||||
version_id: Uuid::new_v4(),
|
||||
timestamp: "2013-10-08T12:00:09Z".parse::<DateTime<Utc>>().unwrap(),
|
||||
versions_since: 3,
|
||||
};
|
||||
txn.set_snapshot(client_key, snap.clone(), vec![9, 8, 9])?;
|
||||
|
||||
assert_eq!(
|
||||
txn.get_snapshot_data(client_key, snap.version_id)?.unwrap(),
|
||||
vec![9, 8, 9]
|
||||
);
|
||||
assert_eq!(txn.get_client(client_key)?.unwrap().snapshot, Some(snap));
|
||||
|
||||
let snap2 = Snapshot {
|
||||
version_id: Uuid::new_v4(),
|
||||
timestamp: "2014-11-28T12:00:09Z".parse::<DateTime<Utc>>().unwrap(),
|
||||
versions_since: 10,
|
||||
};
|
||||
txn.set_snapshot(client_key, snap2.clone(), vec![0, 2, 4, 6])?;
|
||||
|
||||
assert_eq!(
|
||||
txn.get_snapshot_data(client_key, snap2.version_id)?
|
||||
.unwrap(),
|
||||
vec![0, 2, 4, 6]
|
||||
);
|
||||
assert_eq!(txn.get_client(client_key)?.unwrap().snapshot, Some(snap2));
|
||||
|
||||
// check that mismatched version is detected
|
||||
assert!(txn.get_snapshot_data(client_key, Uuid::new_v4()).is_err());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue