https://github.com/timboudreau/mongo-promises
Promise-based wrappers for MongoDB's async Java API
https://github.com/timboudreau/mongo-promises
async guice java mongodb promises
Last synced: 5 months ago
JSON representation
Promise-based wrappers for MongoDB's async Java API
- Host: GitHub
- URL: https://github.com/timboudreau/mongo-promises
- Owner: timboudreau
- Created: 2015-09-19T22:25:33.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2023-05-02T06:31:08.000Z (over 2 years ago)
- Last Synced: 2025-04-05T19:23:44.252Z (6 months ago)
- Topics: async, guice, java, mongodb, promises
- Language: Java
- Size: 98.6 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
MongoDB Async Promises
======================This library provides wrappers around MongoDB's asynchronous Java API to make programming
to it easier - particularly chaining together chunks of work that should happen in sequence,
asynchronously.Read [the Javadoc](http://timboudreau.com/builds/job/mastfrog-parent/lastSuccessfulBuild/artifact/mongo-promises/target/apidocs/index.html)
It uses the [async promises](https://github.com/timboudreau/async-promises) library under the
hood, and also provides builder classes for things like updates, which have fairly fussy and
non-typesafe syntax in MongoDB.To use, simply create a `CollectionPromises` over a native MongoDB `MongoCollection`,
and use the instance methods on that.The point is to provide a clean abstraction for chaining together asynchronous database operations.
Example
-------```java
AsyncPromise promise = p.find().descendingSortBy("ix").withBatchSize(20).find(new FindReceiver>(){int total = 0;
@Override
public void withResults(List obj, Trigger trigger, PromiseContext context) throws Exception {
total += obj.size();
System.out.println("GET ONE BATCH " + obj.size() + " TOTAL " + total);
all.addAll(obj);
trigger.trigger(true, null);
}
}).then(new SimpleLogic(){@Override
public void run(Void data, Trigger next) throws Exception {
nextWasRun.set(true);
next.trigger(null, null);
}
}).then(new Document(), p.count().maxTime(10, TimeUnit.SECONDS).count());promise.start(new Document(), new Trigger(){
@Override
public void trigger(Long count, Throwable thrown) {
countHolder.set(count);
waitForAllResults.countDown();
}
});
```Similar code using JDK 8's lambads:
```java
AsyncPromise promise = p.query().equal("name","foo").build()
.descendingSortBy("ix").withBatchSize(20).find(
(List obj, Trigger trigger, PromiseContext context)-> {
all.addAll(obj);
trigger.trigger(true, null);
}).then((Void data, Trigger next) -> {
nextWasRun.set(true);
next.trigger(null, null);
}).then(new Document(), p.count().maxTime(10, TimeUnit.SECONDS).count())
.start();
```Builders
--------A number of builder classes are included to make things easy - in each case, they return an
AsyncPromise you can start to execute the operation or chain it together with other operations.For example, querying can use a nice `QueryBuilder` (which lets you specify things like $in, $gt,
partial or complete subdocument matches, etc.):```java
CollectionPromises p = new CollectionPromises<>(someCollection);
p.query().equal("skidoo", 23).embedded("foo").embedded("bar").equal("meaning", 42)
.build()
.build()
.elemMatch("array").equal("name", "Joe").greaterThan("age", 18).lessThan("age", 65).build()
.in("city", "Boston", "New York", "Shutesbury").build()```
results in a MongoDB query as follows:
```
{
skidoo : 23,
foo.bar.meaning : 42,
logins : {
country : USA
},
city : {
$in : [Boston, New York, Shutesbury]
},
people : {
$elemMatch : {
name : Joe,
age : {
$gt : 18, $lt : 65
}
}
}
}
```This then drops you into a `FindBuilder` which lets you configure cursor attributes like limit
and sort order, which in turn builds a promise you can run.