javaastyanax

Astynax MutationBatch


I need to understand the execution of MutationBatch in case of astyanax

Code

  MutationBatch m = keyspace.prepareMutationBatch();
    int batch = 10;
    int count = 0;
    for(int i = 0; i<100 ; i++ ){
      count++;
      m.withRow(CF_STANDARD1, rowKey)
        .putColumn("Column1", "X", null)
        .putColumn("Column2", "X", null);

      m.withRow(CF_STANDARD1, rowKey2)
        .putColumn("Column1", "Y", null);
     if(count == batch){
      try {
         OperationResult<Void> result = m.execute();
          count = 0;
       } catch (ConnectionException e) {
        LOG.error(e);
       } 
     }

    }

Now what I am confused is, should I create new MutationBatch after every call to m.execute() or I can reuse one created earlier.


Solution

  • If the execute() was successful, you should be able to re-user the MutationBatch.

    The API doc says " If successful, all the mutations are cleared and new mutations may be created" (https://netflix.github.io/astyanax/javadoc/com/netflix/astyanax/MutationBatch.html)

    To be sure you can call discardMutations() before re-using it.