Skip to content

Commit

Permalink
server: allow storage backend choice with feature
Browse files Browse the repository at this point in the history
Implement rocksdb and sled features for the oxigraph_server crate.
  • Loading branch information
theduke committed Sep 18, 2020
1 parent 55f1152 commit 60b7049
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
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 @@ -441,7 +442,7 @@ mod tests {
use crate::handle_request;
use async_std::task::block_on;
use http_types::{Method, Request, StatusCode, Url};
use oxigraph::RocksDbStore;
use oxigraph::Store;
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

0 comments on commit 60b7049

Please sign in to comment.