Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server: Allow choice of storage backend with feature #60

Merged
merged 1 commit into from Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion server/Cargo.toml
Expand Up @@ -10,10 +10,15 @@ SPARQL server based on Oxigraph
"""
edition = "2018"

[features]
sled = ["oxigraph/sled"]
rocksdb = ["oxigraph/rocksdb"]
default = ["rocksdb"]

[dependencies]
argh = "0.1"
async-std = { version = "1", features = ["attributes"] }
async-h1 = "2"
http-types = "2"
oxigraph = { version = "0.1", path="../lib", features = ["rocksdb", "http_client"] }
oxigraph = { version = "0.1", path="../lib", features = ["http_client"] }
url = "2"
25 changes: 13 additions & 12 deletions server/src/main.rs
Expand Up @@ -19,11 +19,15 @@ use http_types::{headers, Body, Error, Method, Mime, Request, Response, Result,
use oxigraph::io::{DatasetFormat, GraphFormat};
use oxigraph::model::{GraphName, NamedNode};
use oxigraph::sparql::{Query, QueryOptions, QueryResults, QueryResultsFormat, Update};
use oxigraph::RocksDbStore;
use std::io::BufReader;
use std::str::FromStr;
use url::form_urlencoded;

#[cfg(feature = "rocksdb")]
use oxigraph::RocksDbStore as Store;
#[cfg(all(feature = "sled", not(feature = "rocksdb")))]
use oxigraph::SledStore as Store;

const MAX_SPARQL_BODY_SIZE: u64 = 1_048_576;
const HTML_ROOT_PAGE: &str = include_str!("../templates/query.html");
const LOGO: &str = include_str!("../../logo.svg");
Expand All @@ -44,7 +48,7 @@ struct Args {
#[async_std::main]
pub async fn main() -> Result<()> {
let args: Args = argh::from_env();
let store = RocksDbStore::open(args.file)?;
let store = Store::open(args.file)?;

println!("Listening for requests at http://{}", &args.bind);
http_server(&args.bind, move |request| {
Expand All @@ -53,7 +57,7 @@ pub async fn main() -> Result<()> {
.await
}

async fn handle_request(request: Request, store: RocksDbStore) -> Result<Response> {
async fn handle_request(request: Request, store: Store) -> Result<Response> {
let mut response = match (request.url().path(), request.method()) {
("/", Method::Get) => {
let mut response = Response::new(StatusCode::Ok);
Expand Down Expand Up @@ -187,7 +191,7 @@ fn simple_response(status: StatusCode, body: impl Into<Body>) -> Response {
}

async fn evaluate_urlencoded_sparql_query(
store: RocksDbStore,
store: Store,
encoded: Vec<u8>,
request: Request,
) -> Result<Response> {
Expand Down Expand Up @@ -218,7 +222,7 @@ async fn evaluate_urlencoded_sparql_query(
}

async fn evaluate_sparql_query(
store: RocksDbStore,
store: Store,
query: String,
default_graph_uris: Vec<String>,
named_graph_uris: Vec<String>,
Expand Down Expand Up @@ -286,10 +290,7 @@ async fn evaluate_sparql_query(
.await
}

async fn evaluate_urlencoded_sparql_update(
store: RocksDbStore,
encoded: Vec<u8>,
) -> Result<Response> {
async fn evaluate_urlencoded_sparql_update(store: Store, encoded: Vec<u8>) -> Result<Response> {
let mut update = None;
let mut default_graph_uris = Vec::new();
let mut named_graph_uris = Vec::new();
Expand Down Expand Up @@ -317,7 +318,7 @@ async fn evaluate_urlencoded_sparql_update(
}

async fn evaluate_sparql_update(
store: RocksDbStore,
store: Store,
update: String,
default_graph_uris: Vec<String>,
named_graph_uris: Vec<String>,
Expand Down Expand Up @@ -438,10 +439,10 @@ impl<R: Read + Unpin> std::io::Read for SyncAsyncReader<R> {

#[cfg(test)]
mod tests {
use super::Store;
use crate::handle_request;
use async_std::task::block_on;
use http_types::{Method, Request, StatusCode, Url};
use oxigraph::RocksDbStore;
use std::collections::hash_map::DefaultHasher;
use std::env::temp_dir;
use std::fs::remove_dir_all;
Expand Down Expand Up @@ -570,7 +571,7 @@ mod tests {
format!("{:?}", request).hash(&mut s);
path.push(&s.finish().to_string());

let store = RocksDbStore::open(&path).unwrap();
let store = Store::open(&path).unwrap();
let (code, message) = match block_on(handle_request(request, store)) {
Ok(r) => (r.status(), "".to_string()),
Err(e) => (e.status(), e.to_string()),
Expand Down