Skip to content

Commit 815e96e

Browse files
author
Matthijs van Otterdijk
committed
add an endpoint to assign the index of one commit to that of another
1 parent 363566a commit 815e96e

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

src/server.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use std::{
2626
future,
2727
io::{self, ErrorKind},
2828
};
29+
use thiserror::Error;
2930
use tokio::sync::Mutex;
3031
use tokio::{io::AsyncBufReadExt, sync::RwLock};
3132
use tokio_stream::{wrappers::LinesStream, Stream};
@@ -71,6 +72,11 @@ enum ResourceSpec {
7172
commit: String,
7273
previous: Option<String>,
7374
},
75+
AssignIndex {
76+
domain: String,
77+
source_commit: String,
78+
target_commit: String,
79+
},
7480
CheckTask {
7581
task_id: String,
7682
},
@@ -107,6 +113,7 @@ fn query_map(uri: &Uri) -> HashMap<String, String> {
107113
fn uri_to_spec(uri: &Uri) -> Result<ResourceSpec, SpecParseError> {
108114
lazy_static! {
109115
static ref RE_INDEX: Regex = Regex::new(r"^/index(/?)$").unwrap();
116+
static ref RE_ASSIGN: Regex = Regex::new(r"^/assign(/?)$").unwrap();
110117
static ref RE_CHECK: Regex = Regex::new(r"^/check(/?)$").unwrap();
111118
static ref RE_SEARCH: Regex = Regex::new(r"^/search(/?)$").unwrap();
112119
static ref RE_SIMILAR: Regex = Regex::new(r"^/similar(/?)$").unwrap();
@@ -127,6 +134,21 @@ fn uri_to_spec(uri: &Uri) -> Result<ResourceSpec, SpecParseError> {
127134
}),
128135
_ => Err(SpecParseError::NoCommitIdOrDomain),
129136
}
137+
} else if RE_ASSIGN.is_match(path) {
138+
let query = query_map(uri);
139+
let domain = query.get("domain").map(|v| v.to_string());
140+
let source_commit = query.get("source_commit").map(|v| v.to_string());
141+
let target_commit = query.get("target_commit").map(|v| v.to_string());
142+
match (domain, source_commit, target_commit) {
143+
(Some(domain), Some(source_commit), Some(target_commit)) => {
144+
Ok(ResourceSpec::AssignIndex {
145+
domain,
146+
source_commit,
147+
target_commit,
148+
})
149+
}
150+
_ => Err(SpecParseError::NoCommitIdOrDomain),
151+
}
130152
} else if RE_CHECK.is_match(path) {
131153
let query = query_map(uri);
132154
if let Some(task_id) = query.get("task_id") {
@@ -360,6 +382,34 @@ impl Service {
360382
});
361383
}
362384

385+
async fn assign_index(
386+
self: Arc<Self>,
387+
domain: String,
388+
source_commit: String,
389+
target_commit: String,
390+
) -> Result<(), AssignIndexError> {
391+
let source_name = create_index_name(&domain, &source_commit);
392+
let target_name = create_index_name(&domain, &target_commit);
393+
394+
if self.get_index(&target_name).await.is_some() {
395+
return Err(AssignIndexError::TargetCommitAlreadyHasIndex);
396+
}
397+
if let Some(index) = self.get_index(&source_name).await {
398+
let mut indexes = self.indexes.write().await;
399+
indexes.insert(target_name.clone(), index.clone());
400+
401+
std::mem::drop(indexes);
402+
tokio::task::block_in_place(move || {
403+
let path = self.path.clone();
404+
serialize_index(path, &target_name, (*index).clone()).unwrap();
405+
});
406+
407+
Ok(())
408+
} else {
409+
Err(AssignIndexError::SourceCommitNotFound)
410+
}
411+
}
412+
363413
async fn process_operation_chunks(
364414
self: &Arc<Self>,
365415
mut opstream: futures::stream::Chunks<
@@ -407,6 +457,22 @@ impl Service {
407457
self.start_indexing(domain, commit, previous, task_id.clone());
408458
Ok(Response::builder().body(task_id.into()).unwrap())
409459
}
460+
Ok(ResourceSpec::AssignIndex {
461+
domain,
462+
source_commit,
463+
target_commit,
464+
}) => {
465+
let result = self
466+
.assign_index(domain, source_commit, target_commit)
467+
.await;
468+
match result {
469+
Ok(()) => Ok(Response::builder().status(204).body(Body::empty()).unwrap()),
470+
Err(e) => Ok(Response::builder()
471+
.status(400)
472+
.body(e.to_string().into())
473+
.unwrap()),
474+
}
475+
}
410476
Ok(ResourceSpec::CheckTask { task_id }) => {
411477
if let Some(state) = self.get_task_status(&task_id).await {
412478
Ok(Response::builder()
@@ -517,6 +583,16 @@ impl Service {
517583
}
518584
}
519585

586+
#[derive(Debug, Error)]
587+
enum AssignIndexError {
588+
#[error("io error: {0}")]
589+
Io(#[from] io::Error),
590+
#[error("source commit not found")]
591+
SourceCommitNotFound,
592+
#[error("target commit already has an index")]
593+
TargetCommitAlreadyHasIndex,
594+
}
595+
520596
pub async fn serve<P: Into<PathBuf>>(
521597
directory: P,
522598
port: u16,

0 commit comments

Comments
 (0)