Skip to content

Commit ab766b0

Browse files
authored
fix: batch index write operations (#636)
1 parent cc048b0 commit ab766b0

File tree

11 files changed

+146
-43
lines changed

11 files changed

+146
-43
lines changed

.bsp/sbt.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/main/scala/algolia/AlgoliaSyncHelper.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ case class AlgoliaSyncHelper(client: AlgoliaClient) {
4848
def deleteByQuery[T <: ObjectID: Manifest](index: String, query: Query)(
4949
implicit duration: Duration,
5050
executor: ExecutionContext
51-
): Future[Iterator[TasksMultipleIndex]] = {
51+
): Future[Iterator[TasksSingleIndex]] = {
5252
val res = browse[T](index, query).map { seq =>
5353
client.execute {
5454
delete from index objectIds seq.map(_.objectID)

src/main/scala/algolia/definitions/BatchDefinition.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import org.json4s.JsonAST.JValue
3232
import org.json4s.native.Serialization._
3333
import org.json4s.{Extraction, Formats}
3434

35+
@deprecated("Send one batch per index. Use IndexingBatchDefinition instead.")
3536
case class BatchDefinition(
3637
definitions: Iterable[Definition],
3738
requestOptions: Option[RequestOptions] = None
@@ -68,16 +69,16 @@ case class BatchDefinition(
6869
Iterable(UpdateObjectOperation(addObjectId(obj, objectId), Some(index)))
6970

7071
case ClearIndexDefinition(index, _) =>
71-
Iterable(ClearIndexOperation(index))
72+
Iterable(ClearIndexOperation(Some(index)))
7273

7374
case DeleteObjectDefinition(Some(index), Some(oid), _) =>
74-
Iterable(DeleteObjectOperation(index, oid))
75+
Iterable(DeleteObjectOperation(Some(index), oid))
7576

7677
case SafeDeleteObjectDefinition(op, _) =>
77-
Iterable(DeleteObjectOperation(op.index, op.objectID))
78+
Iterable(DeleteObjectOperation(Some(op.index), op.objectID))
7879

7980
case DeleteIndexDefinition(index, _) =>
80-
Iterable(DeleteIndexOperation(index))
81+
Iterable(DeleteIndexOperation(Some(index)))
8182

8283
case PartialUpdateObjectOperationDefinition(
8384
operation,

src/main/scala/algolia/definitions/DeleteDefinition.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,13 @@ case class DeleteObjectDefinition(
4949
def objectId(objectId: String): DeleteObjectDefinition =
5050
copy(oid = Some(objectId))
5151

52-
def objectIds(objectIds: Iterable[String]): BatchDefinition =
53-
BatchDefinition(objectIds.map { oid =>
54-
DeleteObjectDefinition(index, Some(oid))
55-
})
52+
def objectIds(objectIds: Iterable[String]): IndexingBatchDefinition =
53+
IndexingBatchDefinition(
54+
index.get,
55+
objectIds.map { oid =>
56+
DeleteObjectDefinition(oid = Some(oid))
57+
}
58+
)
5659

5760
def by(query: Query): DeleteByDefinition =
5861
DeleteByDefinition(index, query, requestOptions)

src/main/scala/algolia/definitions/IndexingBatchDefinition.scala

Lines changed: 99 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@
2626
package algolia.definitions
2727

2828
import algolia.http.{HttpPayload, POST}
29-
import algolia.inputs.{
30-
AddObjectOperation,
31-
BatchOperations,
32-
UpdateObjectOperation
33-
}
29+
import algolia.inputs._
3430
import algolia.objects.RequestOptions
35-
import org.json4s.Formats
31+
import org.json4s.JsonAST.JValue
3632
import org.json4s.native.Serialization._
33+
import org.json4s.{Extraction, Formats}
3734

3835
case class IndexingBatchDefinition(
3936
index: String,
@@ -51,16 +48,7 @@ case class IndexingBatchDefinition(
5148
copy(requestOptions = Some(requestOptions))
5249

5350
override private[algolia] def build(): HttpPayload = {
54-
val operations = definitions.map {
55-
case IndexingDefinition(_, None, Some(obj), _) =>
56-
hasObjectId(obj) match {
57-
case (true, o) => UpdateObjectOperation(o)
58-
case (false, o) => AddObjectOperation(o)
59-
}
60-
61-
case IndexingDefinition(_, Some(objectId), Some(obj), _) =>
62-
UpdateObjectOperation(addObjectId(obj, objectId))
63-
}
51+
val operations = definitions.flatMap(transform)
6452

6553
HttpPayload(
6654
POST,
@@ -70,4 +58,99 @@ case class IndexingBatchDefinition(
7058
requestOptions = requestOptions
7159
)
7260
}
61+
62+
private def transform(
63+
definition: Definition
64+
): Iterable[BatchOperation[JValue]] = {
65+
definition match {
66+
case IndexingDefinition(_, None, Some(obj), _) =>
67+
hasObjectId(obj) match {
68+
case (true, o) => Iterable(UpdateObjectOperation(o))
69+
case (false, o) => Iterable(AddObjectOperation(o))
70+
}
71+
72+
case IndexingDefinition(_, Some(objectId), Some(obj), _) =>
73+
Iterable(UpdateObjectOperation(addObjectId(obj, objectId)))
74+
75+
case ClearIndexDefinition(_, _) =>
76+
Iterable(ClearIndexOperation())
77+
78+
case DeleteObjectDefinition(_, Some(oid), _) =>
79+
Iterable(DeleteObjectOperation(objectID = oid))
80+
81+
case SafeDeleteObjectDefinition(op, _) =>
82+
Iterable(DeleteObjectOperation(objectID = op.objectID))
83+
84+
case DeleteIndexDefinition(_, _) =>
85+
Iterable(DeleteIndexOperation())
86+
87+
case PartialUpdateObjectOperationDefinition(
88+
operation,
89+
index,
90+
Some(objectId),
91+
Some(attribute),
92+
value,
93+
true,
94+
_
95+
) =>
96+
val body = Map(
97+
"objectID" -> objectId,
98+
attribute -> PartialUpdateObject(operation.name, value)
99+
)
100+
Iterable(
101+
PartialUpdateObjectOperation(Extraction.decompose(body))
102+
)
103+
104+
case PartialUpdateObjectOperationDefinition(
105+
operation,
106+
index,
107+
Some(objectId),
108+
Some(attribute),
109+
value,
110+
false,
111+
_
112+
) =>
113+
val body = Map(
114+
"objectID" -> objectId,
115+
attribute -> PartialUpdateObject(operation.name, value)
116+
)
117+
Iterable(
118+
PartialUpdateObjectNoCreateOperation(Extraction.decompose(body))
119+
)
120+
121+
case PartialUpdateObjectDefinition(
122+
_,
123+
Some(objectId),
124+
Some(attribute),
125+
value,
126+
_
127+
) =>
128+
val body = Map(
129+
"objectID" -> objectId,
130+
attribute -> value
131+
)
132+
Iterable(
133+
PartialUpdateObjectOperation(Extraction.decompose(body))
134+
)
135+
136+
case IndexingBatchDefinition(_, defs, _) =>
137+
defs.flatMap(transform)
138+
139+
case PartialUpdateOneObjectDefinition(
140+
_,
141+
Some(obj),
142+
createIfNotExists,
143+
_
144+
) =>
145+
if (createIfNotExists) {
146+
Iterable(
147+
PartialUpdateObjectOperation(Extraction.decompose(obj))
148+
)
149+
} else {
150+
Iterable(
151+
PartialUpdateObjectNoCreateOperation(Extraction.decompose(obj))
152+
)
153+
}
154+
}
155+
}
73156
}

src/main/scala/algolia/definitions/PartialUpdateObjectOperationDefinition.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,9 @@ case class PartialUpdateOneObjectDefinition(
208208
def `object`[T <: ObjectID](obj: T): PartialUpdateOneObjectDefinition =
209209
copy(`object` = Some(obj))
210210

211-
def objects[T <: ObjectID](objects: Iterable[T]): BatchDefinition =
212-
BatchDefinition(
211+
def objects[T <: ObjectID](objects: Iterable[T]): IndexingBatchDefinition =
212+
IndexingBatchDefinition(
213+
index,
213214
objects.map(o =>
214215
PartialUpdateOneObjectDefinition(
215216
index,

src/main/scala/algolia/dsl/BatchDsl.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ trait BatchDsl {
3636

3737
implicit val formats: Formats
3838

39+
@deprecated("Send one batch per index. Use indexBatch instead")
3940
def batch(batches: Iterable[Definition]): BatchDefinition = {
4041
BatchDefinition(batches)
4142
}
4243

44+
@deprecated("Send one batch per index, Use indexBatch instead")
4345
def batch(batches: Definition*): BatchDefinition = {
4446
BatchDefinition(batches)
4547
}

src/main/scala/algolia/dsl/IndexingBatchDsl.scala

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,31 @@
2525

2626
package algolia.dsl
2727

28-
import algolia.definitions.IndexingBatchDefinition
28+
import algolia.definitions.{Definition, IndexingBatchDefinition}
2929
import algolia.responses.TasksSingleIndex
3030
import algolia.{AlgoliaClient, Executable}
31+
import org.json4s.Formats
3132

3233
import scala.concurrent.{ExecutionContext, Future}
3334

3435
trait IndexingBatchDsl {
3536

37+
implicit val formats: Formats
38+
39+
def indexBatch(
40+
index: String,
41+
batches: Iterable[Definition]
42+
): IndexingBatchDefinition = {
43+
IndexingBatchDefinition(index, batches)
44+
}
45+
46+
def indexBatch(
47+
index: String,
48+
batches: Definition*
49+
): IndexingBatchDefinition = {
50+
IndexingBatchDefinition(index, batches)
51+
}
52+
3653
implicit object IndexingBatchDefinitionExecutable
3754
extends Executable[IndexingBatchDefinition, TasksSingleIndex] {
3855
override def apply(client: AlgoliaClient, batch: IndexingBatchDefinition)(

src/main/scala/algolia/inputs/BatchOperations.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ case class PartialUpdateObjectNoCreateOperation[T <: AnyRef](
5858
) extends BatchOperation[T]
5959

6060
case class DeleteObjectOperation[T <: AnyRef](
61-
indexName: String,
61+
indexName: Option[String] = None,
6262
objectID: String,
6363
action: String = "deleteObject"
6464
) extends BatchOperation[T]
6565

6666
case class ClearIndexOperation[T <: AnyRef](
67-
indexName: String,
67+
indexName: Option[String] = None,
6868
action: String = "clear"
6969
) extends BatchOperation[T]
7070

7171
case class DeleteIndexOperation[T <: AnyRef](
72-
indexName: String,
72+
indexName: Option[String] = None,
7373
action: String = "delete"
7474
) extends BatchOperation[T]

src/test/scala/algolia/dsl/DeleteObjectTest.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
package algolia.dsl
2727

2828
import algolia.AlgoliaDsl._
29-
import algolia.{AlgoliaTest, inputs}
29+
import algolia.AlgoliaTest
3030
import algolia.http.{DELETE, HttpPayload, POST}
3131
import algolia.inputs.SafeDeleteObjectOperation
3232

@@ -61,11 +61,9 @@ class DeleteObjectTest extends AlgoliaTest {
6161
| {
6262
| "requests":[
6363
| {
64-
| "indexName":"toto",
6564
| "objectID":"1",
6665
| "action":"deleteObject"
6766
| },{
68-
| "indexName":"toto",
6967
| "objectID":"2",
7068
| "action":"deleteObject"
7169
| }
@@ -76,14 +74,13 @@ class DeleteObjectTest extends AlgoliaTest {
7674
(delete from "toto" objectIds Seq("1", "2")).build() should be(
7775
HttpPayload(
7876
POST,
79-
List("1", "indexes", "*", "batch"),
77+
List("1", "indexes", "toto", "batch"),
8078
body = Some(body),
8179
isSearch = false,
8280
requestOptions = None
8381
)
8482
)
8583
}
86-
8784
}
8885
}
8986

0 commit comments

Comments
 (0)