Skip to content

Commit

Permalink
Upgrade to tantivy 0.7, remove some derives, move to failure crate
Browse files Browse the repository at this point in the history
  • Loading branch information
hntd187 committed Sep 16, 2018
1 parent 1f0b227 commit 6f6e92b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 50 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ serde_derive = "1.0"
serde_json = "1.0"
lazy_static = "1.1"
futures = "0.1"
tantivy = "0.6"
tantivy = "0.7"
config = "0.9.0"
log = "0.4"
pretty_env_logger = "0.2"
quick-error = "1.2"
failure = "0.1.2"
crossbeam-channel = "0.2"
capnp = "0.9"

Expand Down
2 changes: 1 addition & 1 deletion src/handlers/bulk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl Handler for BulkHandler {
let resp = create_response(&state, StatusCode::Created, None);
future::ok((state, resp))
}
Err(ref e) => handle_error(state, e),
Err(e) => handle_error(state, &Error::IOError(e.to_string())),
});
Box::new(response)
}
Expand Down
12 changes: 6 additions & 6 deletions src/handlers/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct IndexHandler {
catalog: Arc<RwLock<IndexCatalog>>,
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize)]
pub struct DocsAffected {
docs_affected: u32,
}
Expand Down Expand Up @@ -291,11 +291,11 @@ mod tests {
let body = r#"{ "test_text": "document" }"#;

let response = test_server
.delete("http://localhost/test_index")
.with_body(body)
.with_header(ContentType(mime::APPLICATION_JSON))
.perform()
.unwrap();
.delete("http://localhost/test_index")
.with_body(body)
.with_header(ContentType(mime::APPLICATION_JSON))
.perform()
.unwrap();

assert_eq!(response.status(), StatusCode::BadRequest);
}
Expand Down
3 changes: 1 addition & 2 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use hyper::{Body, Response, StatusCode};
use mime::{self, Mime};
use serde::Serialize;
use serde_json;
use std::error::Error;
use std::sync::Arc;

#[derive(Deserialize, StateData, StaticResponseExtender)]
Expand Down Expand Up @@ -66,7 +65,7 @@ where T: Serialize {
type FutureError = FutureResult<(State, Response<Body>), (State, HandlerError)>;

fn handle_error<T>(state: State, err: &T) -> FutureError
where T: Error + Sized {
where T: failure::Fail + Sized {
let err = serde_json::to_vec(&ErrorResponse::new(&format!("{}", err))).unwrap();
let resp = create_response(&state, StatusCode::BadRequest, Some((err, mime::APPLICATION_JSON)));
future::ok((state, resp))
Expand Down
12 changes: 6 additions & 6 deletions src/handlers/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Search {
}
}

#[derive(Deserialize, Debug, Clone, PartialEq)]
#[derive(Deserialize, Clone, PartialEq, Debug)]
#[serde(untagged)]
pub enum Queries {
TermQuery { term: HashMap<String, Value> },
Expand Down Expand Up @@ -67,7 +67,7 @@ impl SearchHandler {
Ok(s) => s,
Err(ref e) => return handle_error(state, e),
};
info!("Query: {:#?}", search);
info!("Query: {:?}", search);
let docs = match self.catalog.read().unwrap().search_index(&index.index, &search) {
Ok(v) => v,
Err(ref e) => return handle_error(state, e),
Expand Down Expand Up @@ -102,19 +102,19 @@ pub mod tests {
use index::tests::*;
use serde_json;

#[derive(Deserialize, Debug)]
#[derive(Deserialize)]
pub struct TestResults {
pub hits: i32,
pub docs: Vec<TestDoc>,
}

#[derive(Deserialize, Debug)]
#[derive(Deserialize)]
pub struct TestDoc {
pub score: f32,
pub doc: TestSchema,
}

#[derive(Deserialize, Debug)]
#[derive(Deserialize)]
pub struct TestSchema {
pub test_text: Vec<String>,
pub test_i64: Vec<i64>,
Expand Down Expand Up @@ -222,7 +222,7 @@ pub mod tests {

assert_eq!(req.status(), StatusCode::BadRequest);
assert_eq!(
r#"{"reason":"Query Parse Error: Syntax error in query"}"#,
r#"{"reason":"Query Parse Error: invalid digit found in string"}"#,
req.read_utf8_body().unwrap()
)
}
Expand Down
8 changes: 4 additions & 4 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ impl IndexCatalog {
};

let scored_docs: Vec<ScoredDoc> = collector
.score_docs()
.iter()
.top_docs()
.into_iter()
.map(|(score, doc)| {
let d = searcher.doc(&doc).expect("Doc not found in segment");
ScoredDoc::new(*score, schema.to_named_doc(&d))
let d = searcher.doc(doc).expect("Doc not found in segment");
ScoredDoc::new(score, schema.to_named_doc(&d))
}).collect();

Ok(SearchResults::new(scored_docs))
Expand Down
51 changes: 22 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extern crate serde_json;
extern crate log;

#[macro_use]
extern crate quick_error;
extern crate failure;
extern crate futures;

#[cfg_attr(test, macro_use)]
Expand All @@ -27,38 +27,31 @@ extern crate crossbeam_channel;
extern crate pretty_env_logger;

use tantivy::query::QueryParserError;
use tantivy::ErrorKind;
use tantivy::Error as TError;

quick_error! {
#[derive(Debug)]
pub enum Error {
IOError(err: String) {
display("IO Error: {}", err)
}
UnknownIndexField(err: String) {
display("Unknown Field: '{}' queried", err)
}
UnknownIndex(err: String) {
display("Unknown Index: '{}' queried", err)
}
QueryError(err: String) {
display("Query Parse Error: {}", err)
}
}
#[derive(Debug, Fail)]
pub enum Error {
#[fail(display = "IO Error: {}", _0)]
IOError(String),
#[fail(display = "Unknown Field: '{}' queried", _0)]
UnknownIndexField(String),
#[fail(display = "Unknown Index: '{}' queried", _0)]
UnknownIndex(String),
#[fail(display = "Query Parse Error: {}", _0)]
QueryError(String),
}

impl From<tantivy::Error> for Error {
impl From<TError> for Error {
fn from(err: tantivy::Error) -> Error {
match err.0 {
ErrorKind::CorruptedFile(p) | ErrorKind::PathDoesNotExist(p) | ErrorKind::FileAlreadyExists(p) => {
Error::IOError(format!("{:?}", p))
}
ErrorKind::IOError(e) => Error::IOError(e.to_string()),
ErrorKind::SchemaError(e) => Error::UnknownIndex(e.to_string()),
ErrorKind::Msg(e) | ErrorKind::InvalidArgument(e) => Error::IOError(e),
ErrorKind::Poisoned => Error::IOError("Poisoned".to_string()),
ErrorKind::ErrorInThread(e) => Error::IOError(e),
ErrorKind::FastFieldError(_) => Error::IOError("Fast Field Error".to_string()),
match err {
TError::CorruptedFile(p) | TError::PathDoesNotExist(p) | TError::FileAlreadyExists(p) => Error::IOError(format!("{:?}", p)),
TError::IOError(e) => Error::IOError(e.to_string()),
TError::SchemaError(e) => Error::UnknownIndex(e.to_string()),
TError::InvalidArgument(e) => Error::IOError(e),
TError::Poisoned => Error::IOError("Poisoned".to_string()),
TError::ErrorInThread(e) => Error::IOError(e),
TError::LockFailure(e) => Error::IOError(format!("Failed to acquire lock: {:?}", e)),
TError::FastFieldError(_) => Error::IOError("Fast Field Error".to_string()),
}
}
}
Expand Down

0 comments on commit 6f6e92b

Please sign in to comment.