1
votes

I'm trying to write a Rocket / Juniper / Rust based GraphQL Server using PickleDB - an in-memory key/value store.

The pickle db is created / loaded at the start and given to rocket to manage:

fn rocket() -> Rocket {
    let pickle_path = var_os(String::from("PICKLE_PATH")).unwrap_or(OsString::from("pickle.db"));
    let pickle_db_dump_policy = PickleDbDumpPolicy::PeriodicDump(Duration::from_secs(120));
    let pickle_serialization_method = SerializationMethod::Bin;

    let pickle_db: PickleDb = match Path::new(&pickle_path).exists() {
        false => PickleDb::new(pickle_path, pickle_db_dump_policy, pickle_serialization_method),
        true => PickleDb::load(pickle_path, pickle_db_dump_policy, pickle_serialization_method).unwrap(),
    };

    rocket::ignite()
        .manage(Schema::new(Query, Mutation))
        .manage(pickle_db)
        .mount(
            "/",
            routes![graphiql, get_graphql_handler, post_graphql_handler],
        )
}

And I want to retrieve the PickleDb instance from the Rocket State in my Guard:

pub struct Context {
    pickle_db: PickleDb,
}

impl juniper::Context for Context {}

impl<'a, 'r> FromRequest<'a, 'r> for Context {
    type Error = ();

    fn from_request(_request: &'a Request<'r>) -> request::Outcome<Context, ()> {
        let pickle_db = _request.guard::<State<PickleDb>>()?.inner();
        Outcome::Success(Context { pickle_db })
    }
}

This does not work because the State only gives me a reference:

26 |         Outcome::Success(Context { pickle_db })
   |                                    ^^^^^^^^^ expected struct `pickledb::pickledb::PickleDb`, found `&pickledb::pickledb::PickleDb`

When I change my Context struct to contain a reference I get lifetime issues which I'm not yet familiar with:

15 |     pickle_db: &PickleDb,
   |                ^ expected named lifetime parameter

I tried using 'static which does make rust quite unhappy and I tried to use the request lifetime (?) 'r of the FromRequest, but that does not really work either...

How do I get this to work? As I'm quite new in rust, is this the right way to do things?

1

1 Answers

0
votes

I finally have a solution, although the need for unsafe indicates it is sub-optimal :)

#![allow(unsafe_code)]
use pickledb::{PickleDb, PickleDbDumpPolicy, SerializationMethod};
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::env;
use std::path::Path;
use std::time::Duration;

pub static mut PICKLE_DB: Option<PickleDb> = None;

pub fn cache_init() {
    let pickle_path = env::var(String::from("PICKLE_PATH")).unwrap_or(String::from("pickle.db"));
    let pickle_db_dump_policy = PickleDbDumpPolicy::PeriodicDump(Duration::from_secs(120));
    let pickle_serialization_method = SerializationMethod::Json;
    let pickle_db = match Path::new(&pickle_path).exists() {
        false => PickleDb::new(
            pickle_path,
            pickle_db_dump_policy,
            pickle_serialization_method,
        ),
        true => PickleDb::load(
            pickle_path,
            pickle_db_dump_policy,
            pickle_serialization_method,
        )
        .unwrap(),
    };
    unsafe {
        PICKLE_DB = Some(pickle_db);
    }
}

pub fn cache_get<V>(key: &str) -> Option<V>
where
    V: DeserializeOwned + std::fmt::Debug,
{
    unsafe {
        let pickle_db = PICKLE_DB
            .as_ref()
            .expect("cache uninitialized - call cache_init()");
        pickle_db.get::<V>(key)
    }
}

pub fn cache_set<V>(key: &str, value: &V) -> Result<(), pickledb::error::Error>
where
    V: Serialize,
{
    unsafe {
        let pickle_db = PICKLE_DB
            .as_mut()
            .expect("cache uninitialized - call cache_init()");
        pickle_db.set::<V>(key, value)?;
        Ok(())
    }
}

This can be simply imported and used as expected, but I think I'll run into issues when the load gets to high...