Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
zigzago committed Aug 14, 2019
1 parent bdb3cad commit 5f89f92
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
11 changes: 7 additions & 4 deletions kmongo-core/src/main/kotlin/org/litote/kmongo/MongoIterables.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,24 @@ private class NullHandlerSequence<T>(val sequence: Sequence<T>) : Sequence<T?> {
}

/**
* Evaluates the iterable given the [sequenceChain] of Sequences.
* Evaluates the current [MongoIterable] given the [expression] of Sequences.
*
* The mongo cursor is closed before returning the result.
*
* Sample:
* ```
* col.find().evaluate {
* filter { it.name != "Joe" }.last()
* //this is a sequence evaluation
* //If the first row has a name like "Fred", only one row is loaded in memory!
* filter { it.name != "Joe" }.first()
* }
* ```
*/
fun <T, R> MongoIterable<T>.evaluate(sequenceChain: Sequence<T>.() -> R): R {
fun <T, R> MongoIterable<T>.evaluate(expression: Sequence<T>.() -> R): R {
@Suppress("UNCHECKED_CAST")
return iterator().run {
use {
sequenceChain(
expression(
NullHandlerSequence(
generateSequence {
if (hasNext()) {
Expand Down
19 changes: 19 additions & 0 deletions kmongo-kdoc/docs/extensions-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,25 @@ KMongo does not fix the "for" issue, but **does solve automatically memory leaks

You have nothing to change in your code - just compile it with the KMongo dependency in the classpath!

### Use Sequences

Unfortunately, The `MongoIterable.asSequence()` extension loads in memory all the documents returned by the query.
This is because it's required to close the Mongo cursor in `asSequence()` method, in order to avoid a memory leak!
KMongo provides an [evaluate](https://litote.org/kmongo/dokka/kmongo/org.litote.kmongo/com.mongodb.client.-mongo-iterable/evaluate.html) method that allows to load only the needed rows in memory,
and also ensures that the cursor is closed:
```kotlin
val notJoe = col.find().evaluate {
//this is a sequence evaluation
//If the first row has a name like "Fred", only one row is loaded in memory!
filter { it.name != "Joe" }.first()
}
//If col.find() returns 1M of documents, they are all loaded in memory!
col.find().asSequence().filter { it.name != "Joe" }.first()
```
## withKMongo
The recommended method, in order to setup KMongo, is to use `KMongo.createClient()`
Expand Down
4 changes: 2 additions & 2 deletions kmongo-kdoc/docs/object-mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ You just have to add the ```kmongo-id``` dependency in the frontend to compile.
<dependency>
<groupId>org.litote.kmongo</groupId>
<artifactId>kmongo-id</artifactId>
<version>3.10.2</version>
<version>3.11.0</version>
</dependency>
```

- or Gradle

```
compile 'org.litote.kmongo:kmongo-id:3.10.2'
compile 'org.litote.kmongo:kmongo-id:3.11.0'
```

#### Id <> Json Jackson serialization
Expand Down
16 changes: 8 additions & 8 deletions kmongo-kdoc/docs/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ If you don't know, start with the sync driver and add this dependency to your pr
<dependency>
<groupId>org.litote.kmongo</groupId>
<artifactId>kmongo</artifactId>
<version>3.10.2</version>
<version>3.11.0</version>
</dependency>
```

- or Gradle

```
compile 'org.litote.kmongo:kmongo:3.10.2'
compile 'org.litote.kmongo:kmongo:3.11.0'
```

And [start coding](#lets-start-coding)
Expand All @@ -37,14 +37,14 @@ For the asynchronous driver, reactive streams style, [Kotlin Coroutines](https:/
<dependency>
<groupId>org.litote.kmongo</groupId>
<artifactId>kmongo-async</artifactId>
<version>3.10.2</version>
<version>3.11.0</version>
</dependency>
```

- or Gradle

```
compile 'org.litote.kmongo:kmongo-async:3.10.2'
compile 'org.litote.kmongo:kmongo-async:3.11.0'
```

#### Kotlin Coroutines
Expand All @@ -55,14 +55,14 @@ compile 'org.litote.kmongo:kmongo-async:3.10.2'
<dependency>
<groupId>org.litote.kmongo</groupId>
<artifactId>kmongo-coroutine</artifactId>
<version>3.10.2</version>
<version>3.11.0</version>
</dependency>
```

- or Gradle

```
compile 'org.litote.kmongo:kmongo-coroutine:3.10.2'
compile 'org.litote.kmongo:kmongo-coroutine:3.11.0'
```

#### RxJava2
Expand All @@ -73,14 +73,14 @@ compile 'org.litote.kmongo:kmongo-coroutine:3.10.2'
<dependency>
<groupId>org.litote.kmongo</groupId>
<artifactId>kmongo-rxjava2</artifactId>
<version>3.10.2</version>
<version>3.11.0</version>
</dependency>
```

- or Gradle

```
compile 'org.litote.kmongo:kmongo-rxjava2:3.10.2'
compile 'org.litote.kmongo:kmongo-rxjava2:3.11.0'
```

## Object Mapping Engine
Expand Down

0 comments on commit 5f89f92

Please sign in to comment.