Skip to content

Rust implementation of Facebook's DataLoader using futures and tokio-core.

License

Notifications You must be signed in to change notification settings

uptimeventures/dataloader-rs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dataloader

Build Status Crates.io Coverage Status

Rust implementation of Facebook's DataLoader using futures and tokio-core.

Documentation

Features

  • Batching load requests
  • Caching load results

Usage

Add to your Cargo.toml

[dependencies]
futures = "0.1"
dataloader = "0.5.1"

Add to your crate

extern crate futures;
extern crate dataloader;

use dataloader::{Loader, BatchFn, BatchFuture};
use futures::Future;
use futures::future::ok;

struct Batcher;
impl BatchFn<i32, i32> for Batcher {
    type Error = ();

    fn load(&self, keys: &[i32]) -> BatchFuture<i32, Self::Error> {
        println!("load batch {:?}", keys);
        ok(keys.into_iter().map(|v| v * 10).collect()).boxed()
    }
}

fn main() {
    let loader = Loader::new(Batcher);
    println!("\n -- Using Loader --");
    {
        let v1 = loader.load(3).and_then(|v| loader.load_many(vec![v, v + 5, v + 10]));
        let v2 = loader.load(4).and_then(|v| loader.load_many(vec![v, v + 5, v + 10]));
        let all = v1.join(v2);
        let output = all.wait().unwrap();
        let expected = (vec![300, 350, 400], vec![400, 450, 500]);
        assert_eq!(expected, output);
    }

    let ld = loader.cached();
    println!("\n -- Using Cached Loader --");
    {
        let v1 = ld.load(3).and_then(|v| ld.load_many(vec![v, v + 5, v + 10]));
        let v2 = ld.load(4).and_then(|v| ld.load_many(vec![v, v + 5, v + 10]));
        let all = v1.join(v2);
        let output = all.wait().unwrap();
        let expected = (vec![300, 350, 400], vec![400, 450, 500]);
        assert_eq!(expected, output);
    }
}

About

Rust implementation of Facebook's DataLoader using futures and tokio-core.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%