Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add document:upsert #38

Merged
merged 18 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,5 @@ tasks.withType<Jar> {
)
)
}
from(configurations.compileClasspath.get().map { if (it.isDirectory()) it else zipTree(it) })
}
10 changes: 9 additions & 1 deletion doc/1/controllers/document/m-update/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ public CompletableFuture<ConcurrentHashMap<String, ArrayList<Object>>> mUpdate(
String index,
String collection,
ArrayList<ConcurrentHashMap<String, Object>> documents,
UpdateOptions options)
Boolean waitForRefresh)
throws NotConnectedException, InternalException

public CompletableFuture<ConcurrentHashMap<String, ArrayList<Object>>> mUpdate(
String index,
String collection,
ArrayList<ConcurrentHashMap<String, Object>> documents,
Boolean waitForRefresh,
Integer retryOnConflict)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad indentation

throws NotConnectedException, InternalException
```

Expand Down
27 changes: 22 additions & 5 deletions doc/1/controllers/document/update-by-query/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,35 @@ An empty or null query will match all documents in the collection.


```java
public CompletableFuture<ConcurrentHashMap<String, ArrayList<Object>>> updateByQuery(
public CompletableFuture<ConcurrentHashMap<String, ArrayList<Object>>> updateByQuery(
String index,
String collection,
ConcurrentHashMap<String, Object> searchQuery,
ConcurrentHashMap<String, Object> changes) throws NotConnectedException, InternalException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad indentation


public CompletableFuture<ConcurrentHashMap<String, ArrayList<Object>>> updateByQuery(
String index,
String collection,
ConcurrentHashMap<String, Object> searchQuery,
ConcurrentHashMap<String, Object> changes,
Boolean waitForRefresh) throws NotConnectedException, InternalException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad indentation


public CompletableFuture<ConcurrentHashMap<String, ArrayList<Object>>> updateByQuery(
String index,
String collection,
ConcurrentHashMap<String, Object> searchQuery,
ConcurrentHashMap<String, Object> changes) throws NotConnectedException, InternalException

public CompletableFuture<ConcurrentHashMap<String, ArrayList<Object>>> updateByQuery(
ConcurrentHashMap<String, Object> changes,
Boolean waitForRefresh,
Integer retryOnConflict) throws NotConnectedException, InternalException

public CompletableFuture<ConcurrentHashMap<String, ArrayList<Object>>> updateByQuery(
String index,
String collection,
ConcurrentHashMap<String, Object> searchQuery,
ConcurrentHashMap<String, Object> changes,
UpdateOptions options) throws NotConnectedException, InternalException
Boolean waitForRefresh,
Integer retryOnConflict,
Boolean source) throws NotConnectedException, InternalException
```

| Argument | Type | Description |
Expand Down
138 changes: 138 additions & 0 deletions doc/1/controllers/document/upsert/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
---
code: true
type: page
title: upsert
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing description

Comment on lines +3 to +4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing description

---

# upsert

<SinceBadge version="Kuzzle 2.8.0"/>
<SinceBadge version="auto-version" />

Applies partial changes to a document. If the document doesn't already exist, a new document is created.

:::: tabs
::: tab Java

```java
public CompletableFuture<ConcurrentHashMap<String, Object>> upsert(
String index,
String collection,
ConcurrentHashMap<String, Object> changes) throws NotConnectedException, InternalException

public CompletableFuture<ConcurrentHashMap<String, Object>> upsert(
String index,
String collection,
ConcurrentHashMap<String, Object> changes,
ConcurrentHashMap<String, Object> defaults) throws NotConnectedException, InternalException

public CompletableFuture<ConcurrentHashMap<String, Object>> upsert(
String index,
String collection,
ConcurrentHashMap<String, Object> changes,
ConcurrentHashMap<String, Object> defaults,
Boolean waitForRefresh) throws NotConnectedException, InternalException

public CompletableFuture<ConcurrentHashMap<String, Object>> upsert(
String index,
String collection,
ConcurrentHashMap<String, Object> changes,
ConcurrentHashMap<String, Object> defaults,
Boolean waitForRefresh,
Integer retryOnConflict) throws NotConnectedException, InternalException

public CompletableFuture<ConcurrentHashMap<String, Object>> upsert(
String index,
String collection,
ConcurrentHashMap<String, Object> changes,
ConcurrentHashMap<String, Object> defaults,
Boolean waitForRefresh,
Integer retryOnConflict,
Boolean source) throws NotConnectedException, InternalException
```

| Argument | Type | Description |
| ------------ | ----------------- | ----------------------------------------- |
| `index` | <pre>String</pre> | Index name |
| `collection` | <pre>String</pre> | Collection name |
| `id` | <pre>String</pre> | Document ID |
| `changes` | <pre>ConcurrentHashMap<String, Object></pre> | Partial content of the document to update |

### Options

Additional query options

| Options | Type<br/>(default) | Description |
| ----------------- | ------------------------------- | ---------------------------------------------------------------------------------- |
| `defaults` | <pre>ConcurrentHashMap<String, Object><br/>(`{}`) | Fields to add to the document if it gets created |
| `waitForRefresh` | <pre>Boolean</pre><br/>(`""`) | If set to `true`, waits for the change to be reflected for `search` (up to 1s) |
| `retryOnConflict` | <pre>Integer</pre><br/>(`10`) | The number of times the database layer should retry in case of version conflict |
| `source` | <pre>Boolean</pre><br/>(`false`)| If true, returns the updated document inside the response


## Returns

A `ConcurrentHashMap<String, Object>` with the following properties:

| Property | Type | Description |
|------------- |--------------------------------------------- |--------------------------------- |
| `_source` | <pre>ConcurrentHashMap<String, Object></pre> | Updated document (if `source` option set to true) |
| `_id` | <pre>String</pre> | ID of the updated document |
| `_version` | <pre>Integer</pre> | Version of the document in the persistent data storage |
| `created` | <pre>Boolean</pre>

## Usage

<<< ./snippets/upsert-java.java

:::
::: tab Kotlin

```kotlin
fun upsert(
index: String,
collection: String,
id: String,
changes: ConcurrentHashMap<String, Any?>,
defaults: ConcurrentHashMap<String, Any?>,
waitForRefresh: Boolean? = null,
retryOnConflict: Int? = null,
source: Boolean? = null): CompletableFuture<ConcurrentHashMap<String, Any?>>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad indentation

```

| Argument | Type | Description |
| ------------ | ----------------- | ----------------------------------------- |
| `index` | <pre>String</pre> | Index name |
| `collection` | <pre>String</pre> | Collection name |
| `id` | <pre>String</pre> | Document ID |
| `changes` | <pre>ConcurrentHashMap<String, Any?></pre> | Partial content of the document to update |

### Options

Additional query options

| Options | Type<br/>(default) | Description |
| ----------------- | ------------------------------- | ---------------------------------------------------------------------------------- |
| `defaults` | <pre>ConcurrentHashMap<String, Any?><br/>(`{}`) | Fields to add to the document if it gets created |
| `waitForRefresh` | <pre>Boolean</pre><br/>(`""`) | If set to `true`, waits for the change to be reflected for `search` (up to 1s) |
| `retryOnConflict` | <pre>Integer</pre><br/>(`10`) | The number of times the database layer should retry in case of version conflict |
| `source` | <pre>Boolean</pre><br/>(`false`)| If true, returns the updated document inside the response


## Returns

A `ConcurrentHashMap<String, Any?>` object, with the following properties:

| Property | Type | Description |
|------------- |--------------------------------------------- |--------------------------------- |
| `_source` | <pre>ConcurrentHashMap<String, Any?></pre> | Updated document (if `source` option set to true) |
| `_id` | <pre>String</pre> | ID of the updated document |
| `_version` | <pre>Integer</pre> | Version of the document in the persistent data storage |
| `created` | <pre>Boolean</pre>

## Usage

<<< ./snippets/upsert-kotlin.kt

:::
::::
17 changes: 17 additions & 0 deletions doc/1/controllers/document/upsert/snippets/upsert-java.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ConcurrentHashMap<String, Object> category = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> changes = new ConcurrentHashMap<>();
category.put("category", "suv");
changes.put("changes", category);

ConcurrentHashMap<String, Object> result = kuzzle
.getDocumentController()
.upsert("nyc-open-data", "yellow-taxi", "some-id", changes)
.get();

/*
{
created=true
_id="some_id",
version=1
}
*/
10 changes: 10 additions & 0 deletions doc/1/controllers/document/upsert/snippets/upsert-java.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: java-document#upsert
description: Applies a partial update to an existing document.
hooks:
before: |
curl -XDELETE kuzzle:7512/nyc-open-data
curl -XPOST kuzzle:7512/nyc-open-data/_create
curl -XPUT kuzzle:7512/nyc-open-data/yellow-taxi
after:
template: print-result
expected: "{created=true, _id=some-id, _version=1}"
15 changes: 15 additions & 0 deletions doc/1/controllers/document/upsert/snippets/upsert-kotlin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
val category: ConcurrentHashMap<String, Any?> =
ConcurrentHashMap<String, Any?>().apply {
put("category", "suv")
}

val changes: ConcurrentHashMap<String, Any?> =
ConcurrentHashMap<String, Any?>().apply {
put("changes", category)
}

val result: ConcurrentHashMap<String, Any?> =
kuzzle
.documentController
.upsert("nyc-open-data", "yellow-taxi", "some-id", changes)
.get()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing indentation

10 changes: 10 additions & 0 deletions doc/1/controllers/document/upsert/snippets/upsert-kotlin.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: kotlin-document#upsert
description: Applies a partial update to an existing document.
hooks:
before: |
curl -XDELETE kuzzle:7512/nyc-open-data
curl -XPOST kuzzle:7512/nyc-open-data/_create
curl -XPUT kuzzle:7512/nyc-open-data/yellow-taxi
after:
template: print-result
expected: "{created=true, _id=some-id, _version=1}"
34 changes: 34 additions & 0 deletions src/main/kotlin/io/kuzzle/sdk/controllers/DocumentController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,40 @@ class DocumentController(kuzzle: Kuzzle) : BaseController(kuzzle) {
.thenApplyAsync { response -> response.result as ConcurrentHashMap<String, Any?> }
}

@JvmOverloads
fun upsert(
index: String,
collection: String,
id: String,
changes: ConcurrentHashMap<String, Any?>,
defaults: ConcurrentHashMap<String, Any?>? = null,
waitForRefresh: Boolean? = null,
retryOnConflict: Int? = null,
source: Boolean? = null): CompletableFuture<ConcurrentHashMap<String, Any?>> {
val query = KuzzleMap().apply {
put("index", index)
put("collection", collection)
put("controller", "document")
put("action", "upsert")
put("body", KuzzleMap().apply {
put("changes", changes)
put("defaults", defaults)
})
put("_id", id)
put("source", source)
put("retryOnConflict", retryOnConflict)
put("waitForRefresh", waitForRefresh)
}

if (defaults != null) {

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless code


return kuzzle
.query(query)
.thenApplyAsync { response -> response.result as ConcurrentHashMap<String, Any?> }
}

@JvmOverloads
fun updateByQuery(
index: String,
Expand Down