server: allow storage backend choice with feature

Implement rocksdb and sled features for the oxigraph_server crate.
pull/60/head
Christoph Herzog 4 years ago
parent 55f1152f35
commit 7e09b105ac
  1. 7
      server/Cargo.toml
  2. 25
      server/src/main.rs

@ -10,10 +10,15 @@ SPARQL server based on Oxigraph
""" """
edition = "2018" edition = "2018"
[features]
sled = ["oxigraph/sled"]
rocksdb = ["oxigraph/rocksdb"]
default = ["rocksdb"]
[dependencies] [dependencies]
argh = "0.1" argh = "0.1"
async-std = { version = "1", features = ["attributes"] } async-std = { version = "1", features = ["attributes"] }
async-h1 = "2" async-h1 = "2"
http-types = "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" url = "2"

@ -19,11 +19,15 @@ use http_types::{headers, Body, Error, Method, Mime, Request, Response, Result,
use oxigraph::io::{DatasetFormat, GraphFormat}; use oxigraph::io::{DatasetFormat, GraphFormat};
use oxigraph::model::{GraphName, NamedNode}; use oxigraph::model::{GraphName, NamedNode};
use oxigraph::sparql::{Query, QueryOptions, QueryResults, QueryResultsFormat, Update}; use oxigraph::sparql::{Query, QueryOptions, QueryResults, QueryResultsFormat, Update};
use oxigraph::RocksDbStore;
use std::io::BufReader; use std::io::BufReader;
use std::str::FromStr; use std::str::FromStr;
use url::form_urlencoded; 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 MAX_SPARQL_BODY_SIZE: u64 = 1_048_576;
const HTML_ROOT_PAGE: &str = include_str!("../templates/query.html"); const HTML_ROOT_PAGE: &str = include_str!("../templates/query.html");
const LOGO: &str = include_str!("../../logo.svg"); const LOGO: &str = include_str!("../../logo.svg");
@ -44,7 +48,7 @@ struct Args {
#[async_std::main] #[async_std::main]
pub async fn main() -> Result<()> { pub async fn main() -> Result<()> {
let args: Args = argh::from_env(); 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); println!("Listening for requests at http://{}", &args.bind);
http_server(&args.bind, move |request| { http_server(&args.bind, move |request| {
@ -53,7 +57,7 @@ pub async fn main() -> Result<()> {
.await .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()) { let mut response = match (request.url().path(), request.method()) {
("/", Method::Get) => { ("/", Method::Get) => {
let mut response = Response::new(StatusCode::Ok); let mut response = Response::new(StatusCode::Ok);
@ -187,7 +191,7 @@ fn simple_response(status: StatusCode, body: impl Into<Body>) -> Response {
} }
async fn evaluate_urlencoded_sparql_query( async fn evaluate_urlencoded_sparql_query(
store: RocksDbStore, store: Store,
encoded: Vec<u8>, encoded: Vec<u8>,
request: Request, request: Request,
) -> Result<Response> { ) -> Result<Response> {
@ -218,7 +222,7 @@ async fn evaluate_urlencoded_sparql_query(
} }
async fn evaluate_sparql_query( async fn evaluate_sparql_query(
store: RocksDbStore, store: Store,
query: String, query: String,
default_graph_uris: Vec<String>, default_graph_uris: Vec<String>,
named_graph_uris: Vec<String>, named_graph_uris: Vec<String>,
@ -286,10 +290,7 @@ async fn evaluate_sparql_query(
.await .await
} }
async fn evaluate_urlencoded_sparql_update( async fn evaluate_urlencoded_sparql_update(store: Store, encoded: Vec<u8>) -> Result<Response> {
store: RocksDbStore,
encoded: Vec<u8>,
) -> Result<Response> {
let mut update = None; let mut update = None;
let mut default_graph_uris = Vec::new(); let mut default_graph_uris = Vec::new();
let mut named_graph_uris = Vec::new(); let mut named_graph_uris = Vec::new();
@ -317,7 +318,7 @@ async fn evaluate_urlencoded_sparql_update(
} }
async fn evaluate_sparql_update( async fn evaluate_sparql_update(
store: RocksDbStore, store: Store,
update: String, update: String,
default_graph_uris: Vec<String>, default_graph_uris: Vec<String>,
named_graph_uris: Vec<String>, named_graph_uris: Vec<String>,
@ -438,10 +439,10 @@ impl<R: Read + Unpin> std::io::Read for SyncAsyncReader<R> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::Store;
use crate::handle_request; use crate::handle_request;
use async_std::task::block_on; use async_std::task::block_on;
use http_types::{Method, Request, StatusCode, Url}; use http_types::{Method, Request, StatusCode, Url};
use oxigraph::RocksDbStore;
use std::collections::hash_map::DefaultHasher; use std::collections::hash_map::DefaultHasher;
use std::env::temp_dir; use std::env::temp_dir;
use std::fs::remove_dir_all; use std::fs::remove_dir_all;
@ -570,7 +571,7 @@ mod tests {
format!("{:?}", request).hash(&mut s); format!("{:?}", request).hash(&mut s);
path.push(&s.finish().to_string()); 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)) { let (code, message) = match block_on(handle_request(request, store)) {
Ok(r) => (r.status(), "".to_string()), Ok(r) => (r.status(), "".to_string()),
Err(e) => (e.status(), e.to_string()), Err(e) => (e.status(), e.to_string()),

Loading…
Cancel
Save