Skip to content

Batch statements

Flavian Alexandru edited this page May 30, 2016 · 4 revisions

Build Status Coverage Status Maven Central Bintray

Phantom also brrings in support for batch statements. To use them, see IterateeBigTest.scala. Before you read further, you should remember batch statements are not used to improve performance.

Read the official docs for more details, but in short batches guarantee atomicity and they are about 30% slower on average than parallel writes at least, as they require more round trips. If you think you're optimising performance with batches, you might need to find alternative means.

We have tested with 10,000 statements per batch, and 1000 batches processed simultaneously. Before you run the test, beware that it takes ~40 minutes.

Batches use lazy iterators and daisy chain them to offer thread safe behaviour. They are not memory intensive and you can expect consistent processing speed even with 1 000 000 statements per batch.

Batches are immutable and adding a new record will result in a new Batch, just like most things Scala, so be careful to chain the calls.

phantom also supports COUNTER batch updates and UNLOGGED batch updates.

import com.websudos.phantom.dsl._

Batch.logged
    .add(ExampleRecord.update.where(_.id eqs someId).modify(_.name setTo "blabla"))
    .add(ExampleRecord.update.where(_.id eqs someOtherId).modify(_.name setTo "blabla2"))
    .future()

back to top

import com.websudos.phantom.dsl._

Batch.counter
    .add(ExampleRecord.update.where(_.id eqs someId).modify(_.someCounter increment 500L))
    .add(ExampleRecord.update.where(_.id eqs someOtherId).modify(_.someCounter decrement 300L))
    .future()
import com.websudos.phantom.dsl._

Batch.unlogged
    .add(ExampleRecord.update.where(_.id eqs someId).modify(_.name setTo "blabla"))
    .add(ExampleRecord.update.where(_.id eqs someOtherId).modify(_.name setTo "blabla2"))
    .future()