Add RemoteServer to the taskchampion crate

This commit is contained in:
Dustin J. Mitchell 2020-11-27 19:47:50 -05:00
parent d46f20e75a
commit 2a37f090a5
10 changed files with 322 additions and 29 deletions

View file

@ -2,7 +2,7 @@ use crate::api::{
failure_to_ise, ServerState, HISTORY_SEGMENT_CONTENT_TYPE, PARENT_VERSION_ID_HEADER,
VERSION_ID_HEADER,
};
use crate::server::{add_version, AddVersionResult, ClientId, VersionId};
use crate::server::{add_version, AddVersionResult, ClientId, VersionId, NO_VERSION_ID};
use actix_web::{error, post, web, HttpMessage, HttpRequest, HttpResponse, Result};
use futures::StreamExt;
@ -51,10 +51,15 @@ pub(crate) async fn service(
// in transit.
let mut txn = server_state.txn().map_err(failure_to_ise)?;
let client = txn
.get_client(client_id)
.map_err(failure_to_ise)?
.ok_or_else(|| error::ErrorNotFound("no such client"))?;
// get, or create, the client
let client = match txn.get_client(client_id).map_err(failure_to_ise)? {
Some(client) => client,
None => {
txn.new_client(client_id, NO_VERSION_ID)
.map_err(failure_to_ise)?;
txn.get_client(client_id).map_err(failure_to_ise)?.unwrap()
}
};
let result = add_version(txn, client_id, client, parent_version_id, body.to_vec())
.map_err(failure_to_ise)?;
@ -86,8 +91,7 @@ mod test {
// set up the storage contents..
{
let mut txn = server_box.txn().unwrap();
txn.set_client_latest_version_id(client_id, Uuid::nil())
.unwrap();
txn.new_client(client_id, Uuid::nil()).unwrap();
}
let server_state = ServerState::new(server_box);
@ -123,8 +127,7 @@ mod test {
// set up the storage contents..
{
let mut txn = server_box.txn().unwrap();
txn.set_client_latest_version_id(client_id, version_id)
.unwrap();
txn.new_client(client_id, version_id).unwrap();
}
let server_state = ServerState::new(server_box);

View file

@ -57,8 +57,7 @@ mod test {
// set up the storage contents..
{
let mut txn = server_box.txn().unwrap();
txn.set_client_latest_version_id(client_id, Uuid::new_v4())
.unwrap();
txn.new_client(client_id, Uuid::new_v4()).unwrap();
txn.add_version(client_id, version_id, parent_version_id, b"abcd".to_vec())
.unwrap();
}
@ -119,8 +118,7 @@ mod test {
// create the client, but not the version
{
let mut txn = server_box.txn().unwrap();
txn.set_client_latest_version_id(client_id, Uuid::new_v4())
.unwrap();
txn.new_client(client_id, Uuid::new_v4()).unwrap();
}
let server_state = ServerState::new(server_box);
let mut app = test::init_service(App::new().service(app_scope(server_state))).await;

View file

@ -149,14 +149,15 @@ mod test {
let client_id = Uuid::new_v4();
let parent_version_id = Uuid::new_v4();
let history_segment = b"abcd".to_vec();
let client = Client {
latest_version_id: if latest_version_id_nil {
Uuid::nil()
} else {
parent_version_id
},
let latest_version_id = if latest_version_id_nil {
Uuid::nil()
} else {
parent_version_id
};
txn.new_client(client_id, latest_version_id)?;
let client = txn.get_client(client_id)?.unwrap();
let result = add_version(
txn,
client_id,

View file

@ -1,5 +1,5 @@
use super::{Client, Storage, StorageTxn, Uuid, Version};
use failure::Fallible;
use failure::{format_err, Fallible};
use std::collections::HashMap;
use std::sync::{Mutex, MutexGuard};
@ -38,19 +38,26 @@ impl<'a> StorageTxn for InnerTxn<'a> {
Ok(self.0.clients.get(&client_id).cloned())
}
fn new_client(&mut self, client_id: Uuid, latest_version_id: Uuid) -> Fallible<()> {
if let Some(_) = self.0.clients.get(&client_id) {
return Err(format_err!("Client {} already exists", client_id));
}
self.0
.clients
.insert(client_id, Client { latest_version_id });
Ok(())
}
fn set_client_latest_version_id(
&mut self,
client_id: Uuid,
latest_version_id: Uuid,
) -> Fallible<()> {
if let Some(client) = self.0.clients.get_mut(&client_id) {
client.latest_version_id = latest_version_id;
Ok(client.latest_version_id = latest_version_id)
} else {
self.0
.clients
.insert(client_id, Client { latest_version_id });
Err(format_err!("Client {} does not exist", client_id))
}
Ok(())
}
fn get_version_by_parent(

View file

@ -20,7 +20,10 @@ pub(crate) trait StorageTxn {
/// Get information about the given client
fn get_client(&mut self, client_id: Uuid) -> Fallible<Option<Client>>;
/// Set the client's latest_version_id (creating the client if necessary)
/// Create a new client with the given latest_version_id
fn new_client(&mut self, client_id: Uuid, latest_version_id: Uuid) -> Fallible<()>;
/// Set the client's latest_version_id
fn set_client_latest_version_id(
&mut self,
client_id: Uuid,