Skip to content

Commit 051f782

Browse files
author
Martynas Sudintas
committed
batch is now only calling flush at the end
1 parent 96b706f commit 051f782

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

Controller/RestController.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function postAction(RestRequest $restRequest, $id = null)
5151
$types = $repository->getTypes();
5252
$data['_id'] = $id;
5353
$repository->getManager()->getConnection()->bulk('create', reset($types), $data);
54-
$repository->getManager()->commit();
54+
$repository->getManager()->getConnection()->commit(!$this->isBatch());
5555

5656
return $this->renderRest(
5757
null,
@@ -110,7 +110,7 @@ public function putAction(RestRequest $restRequest, $id = null)
110110
$types = $repository->getTypes();
111111
$data['_id'] = $id;
112112
$repository->getManager()->getConnection()->bulk('index', reset($types), $data);
113-
$repository->getManager()->commit();
113+
$repository->getManager()->getConnection()->commit(!$this->isBatch());
114114

115115
return $this->renderRest(
116116
null,
@@ -129,14 +129,22 @@ public function deleteAction(RestRequest $restRequest, $id = null)
129129
}
130130

131131
$connection = $restRequest->getRepository()->getManager()->getConnection();
132+
$types = $restRequest->getRepository()->getTypes();
133+
132134
try {
133-
$connection->delete(
134-
[
135-
'id' => $id,
136-
'type' => $restRequest->getRepository()->getTypes(),
137-
'index' => $connection->getIndexName(),
138-
]
139-
);
135+
if ($this->isBatch()) {
136+
$types = $restRequest->getRepository()->getTypes();
137+
$connection->bulk('delete', reset($types), ['_id' => $id]);
138+
$connection->commit(false);
139+
} else {
140+
$connection->delete(
141+
[
142+
'id' => $id,
143+
'type' => $types,
144+
'index' => $connection->getIndexName(),
145+
]
146+
);
147+
}
140148
} catch (Missing404Exception $e) {
141149
return $this->renderRest(
142150
['message' => $this->trans('response.error.not_found', ['%id%' => $id])],

Request/BatchProcessor.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function handle(RestRequest $restRequest)
6565
}
6666

6767
$out = [];
68+
$indexes = [];
6869
$proxy = RestRequestProxy::initialize($restRequest);
6970
$currentMethod = $this->getRouter()->getContext()->getMethod();
7071

@@ -85,8 +86,18 @@ public function handle(RestRequest $restRequest)
8586
$this->prepareProxy($proxy, $action['body'], $options);
8687

8788
$out[] = call_user_func_array([$this->getController($id), $method], [$proxy, $options['id']]);
89+
$indexes[$proxy->getRepository()->getManager()->getConnection()->getIndexName()] = null;
8890
}
8991

92+
if (!empty($indexes)) {
93+
$proxy
94+
->getRepository()
95+
->getManager()
96+
->getConnection()
97+
->getClient()
98+
->indices()
99+
->flush(['index' => implode(',', array_keys($indexes))]);
100+
}
90101
$this->getRouter()->getContext()->setMethod($currentMethod);
91102

92103
return $out;

0 commit comments

Comments
 (0)