Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/engagingspaces/vertx-dataloader
Efficient, asynchronous batching and caching in clustered environments, port of Facebook DataLoader
https://github.com/engagingspaces/vertx-dataloader
batch dataloader facebook-dataloader graphql java-8 vertx vertx-dataloader
Last synced: 2 months ago
JSON representation
Efficient, asynchronous batching and caching in clustered environments, port of Facebook DataLoader
- Host: GitHub
- URL: https://github.com/engagingspaces/vertx-dataloader
- Owner: engagingspaces
- License: apache-2.0
- Created: 2016-08-17T20:26:24.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-08-06T07:12:46.000Z (over 7 years ago)
- Last Synced: 2024-10-09T20:42:48.219Z (2 months ago)
- Topics: batch, dataloader, facebook-dataloader, graphql, java-8, vertx, vertx-dataloader
- Language: Java
- Homepage:
- Size: 103 KB
- Stars: 71
- Watchers: 10
- Forks: 10
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-graphql - vertx-dataloader - Port of Facebook DataLoader for efficient, asynchronous batching and caching in clustered GraphQL environments (Libraries / Java Libraries)
- awesome-graphql - vertx-dataloader - Port of Facebook DataLoader for efficient, asynchronous batching and caching in clustered GraphQL environments (Libraries / Java Libraries)
- vertx-awesome - Vert.x Dataloader - Java port of Facebook Dataloader for Vert.x. Efficient batching and caching for your data layer. (Utilities)
README
# Vert.x `DataLoader`
[![Build Status](https://travis-ci.org/engagingspaces/vertx-dataloader.svg?branch=master)](https://travis-ci.org/engagingspaces/vertx-dataloader/)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/ec906aa3a12147e28b69b93e3a9d9bf7)](https://www.codacy.com/app/engagingspaces/vertx-dataloader?utm_source=github.com&utm_medium=referral&utm_content=engagingspaces/vertx-dataloader&utm_campaign=Badge_Grade)
[![Apache licensed](https://img.shields.io/hexpm/l/plug.svg?maxAge=2592000)](https://github.com/engagingspaces/vertx-dataloader/blob/master/LICENSE)
[ ![Download](https://api.bintray.com/packages/engagingspaces/maven/vertx-dataloader/images/download.svg) ](https://bintray.com/engagingspaces/maven/vertx-dataloader/_latestVersion)**Note**: A pure java 8 (non-Vert.x) port of this project is now an official [graphql-java](https://github.com/graphql-java/awesome-graphql-java#batch-loading) project: [java-dataloader](https://github.com/graphql-java/java-dataloader)
![vertx-dataloader-concepts](https://cloud.githubusercontent.com/assets/5111931/17837825/f5748bfc-67bd-11e6-9c7a-d407bb92c3d9.png)
This small and simple utility library is a port of [Facebook DataLoader](https://github.com/facebook/dataloader)
to Java 8 for use with [Vert.x](http://vertx.io). It can serve as integral part of your application's data layer to provide a
consistent API over various back-ends and reduce message communication overhead through batching and caching.An important use case for `DataLoader` is improving the efficiency of GraphQL query execution, but there are
many other use cases where you can benefit from using this utility.Most of the code is ported directly from Facebook's reference implementation, with one IMPORTANT adaptation to make
it work for Java 8 and Vert.x. ([more on this below](manual-dispatching)).But before reading on, be sure to take a short dive into the
[original documentation](https://github.com/facebook/dataloader/blob/master/README.md) provided by Lee Byron (@leebyron)
and Nicholas Schrock (@schrockn) from [Facebook](https://www.facebook.com/), the creators of the original data loader.## Table of contents
- [Features](#features)
- [Differences to reference implementation](#differences-to-reference-implementation)
- [Manual dispatching](#manual-dispatching)
- [Let's get started!](#lets-get-started)
- [Installing](#installing)
- [Building](#building)
- [Project plans](#project-plans)
- [Current releases](#current-releases)
- [Known issues](#known-issues)
- [Future ideas](#future-ideas)
- [Other information sources](#other-information-sources)
- [Contributing](#contributing)
- [Acknowledgements](#acknowledgements)
- [Licensing](#licensing)## Features
Vert.x `DataLoader` is a feature-complete port of the Facebook reference implementation with [one major difference](#manual-dispatching). These features are:
- Simple, intuitive API, using generics and fluent coding
- Define batch load function with lambda expression
- Schedule a load request in queue for batching
- Add load requests from anywhere in code
- Request returns [`Future`](http://vertx.io/docs/apidocs/io/vertx/core/Future.html) of requested value
- Can create multiple requests at once, returns [`CompositeFuture`](http://vertx.io/docs/apidocs/io/vertx/core/CompositeFuture.html)
- Caches load requests, so data is only fetched once
- Can clear individual cache keys, so data is fetched on next batch queue dispatch
- Can prime the cache with key/values, to avoid data being fetched needlessly
- Can configure cache key function with lambda expression to extract cache key from complex data loader key types
- Dispatch load request queue after batch is prepared, also returns [`CompositeFuture`](http://vertx.io/docs/apidocs/io/vertx/core/CompositeFuture.html)
- Individual batch futures complete / resolve as batch is processed
- `CompositeFuture`s results are ordered according to insertion order of load requests
- Deals with partial errors when a batch future fails
- Can disable batching and/or caching in configuration
- Can supply your own [`CacheMap`](https://github.com/engagingspaces/vertx-dataloader/blob/master/src/main/java/io/engagingspaces/vertx/dataloader/CacheMap.java) implementations
- Has very high test coverage (see [Acknowledgements](#acknowlegdements))## Differences to reference implementation
### Manual dispatching
The original data loader was written in Javascript for NodeJS. NodeJS is single-threaded in nature, but simulates
asynchronous logic by invoking functions on separate threads in an event loop, as explained
[in this post](http://stackoverflow.com/a/19823583/3455094) on StackOverflow.[Vert.x](http://vertx.io) on the other hand also uses an event loop ([that you should not block!!](http://vertx.io/docs/vertx-core/java/#golden_rule)), but comes
with actor-like [`Verticle`](http://vertx.io/docs/vertx-core/java/#_verticles)s and a
distributed [`EventBus`](http://vertx.io/docs/vertx-core/java/#event_bus) that make it inherently asynchronous, and non-blocking.Now in NodeJS generates so-call 'ticks' in which queued functions are dispatched for execution, and Facebook `DataLoader` uses
the `nextTick()` function in NodeJS to _automatically_ dequeue load requests and send them to the batch execution function for processing.And here there is an **IMPORTANT DIFFERENCE** compared to how _this_ data loader operates!!
In NodeJS the batch preparation will not affect the asynchronous processing behaviour in any way. It will just prepare
batches in 'spare time' as it were.This is different in Vert.x as you will actually _delay_ the execution of your load requests, until the moment where you make a call
to `dataLoader.dispatch()` in comparison to when you would just handle futures directly.Does this make Java `DataLoader` any less useful than the reference implementation? I would argue this is not the case,
and there are also gains to this different mode of operation:- In contrast to the NodeJS implementation _you_ as developer are in full control of when batches are dispatched
- You can attach any logic that determines when a dispatch takes place
- You still retain all other features, full caching support and batching (e.g. to optimize message bus traffic, GraphQL query execution time, etc.)However, with batch execution control comes responsibility! If you forget to make the call to `dispatch()` then the futures
in the load request queue will never be batched, and thus _will never complete_! So be careful when crafting your loader designs.## Let's get started!
### Installing
Gradle users configure the `vertx-dataloader` dependency in `build.gradle`:
```
repositories {
maven {
jcenter()
}
}dependencies {
compile 'io.engagingspaces:vertx-dataloader:1.0.0'
}
```### Building
To build from source use the Gradle wrapper:
```
./gradlew clean build
```Or when using Maven add the following repository to your `pom.xml`:
```
false
central
bintray
http://jcenter.bintray.com
```
And add the dependency to `vertx-dataloader`:
```
io.engagingspaces
vertx-dataloader
1.0.0
pom```
### Using
Please take a look at the example project [vertx-graphql-example](https://github.com/bmsantos/vertx-graphql-example)
created by [Bruno Santos](https://github.com/bmsantos).## Project plans
### Current releases
- `1.0.0` Initial release
### Known issues
- Tests on job queue ordering need refactoring to Futures, one test currently omitted
### Future ideas
- `CompletableFuture` implementation
## Other information sources
- [Facebook DataLoader Github repo](https://github.com/facebook/dataloader)
- [Facebook DataLoader code walkthrough on YouTube](https://youtu.be/OQTnXNCDywA)
- [Using DataLoader and GraphQL to batch requests](https://github.com/gajus/gajus.com-blog/blob/master/posts/using-dataloader-to-batch-requests/index.md)## Contributing
All your feedback and help to improve this project is very welcome. Please create issues for your bugs, ideas and
enhancement requests, or better yet, contribute directly by creating a PR.When reporting an issue, please add a detailed instruction, and if possible a code snippet or test that can be used
as a reproducer of your problem.When creating a pull request, please adhere to the Vert.x coding style where possible, and create tests with your
code so it keeps providing an excellent test coverage level. PR's without tests may not be accepted unless they only
deal with minor changes.## Acknowledgements
This library is entirely inspired by the great works of [Lee Byron](https://github.com/leebyron) and
[Nicholas Schrock](https://github.com/schrockn) from [Facebook](https://www.facebook.com/) whom I like to thank, and
especially @leebyron for taking the time and effort to provide 100% coverage on the codebase. A set of tests which
I also ported.## Licensing
This project [vertx-dataloader](https://github.com/engagingspaces/vertx-dataloader) is licensed under the
[Apache Commons v2.0](https://github.com/engagingspaces/vertx-dataloader/LICENSE) license.Copyright © 2016 Arnold Schrijver and other
[contributors](https://github.com/engagingspaces/vertx-dataloader/graphs/contributors)