Function: Advanced INCREMENT operation

    +

    Goal: Perform the Advanced INCREMENT operation where Eventing interacts with the Data service.

    • This function advancedIncrementOp merely demonstrates the Advanced INCREMENT operation.

    • Requires a metadata bucket, a source bucket.

    • Needs a Binding of type Bucket Alias (as documented in the Scriptlet).

    • Will operate on any mutation and count the mutations subject to DCP dedup.

    • For more information refer to Advanced INCREMENT operation in the detailed documentation.

    • advancedIncrementOp

    • Input Data/Mutation

    • Output Data

    • Output Log

    // To run need a Binding in this Function's Settings as follows:
    // 1. Bucket Alias
    // 2. src_bkt
    // 3. source
    // 4. read+write
    function OnUpdate(doc, meta) {
        if (!meta.id.startsWith("inc:")) return;
        // log('input meta', meta);
        // log('input doc ', doc);
    
        // if doc.count doesn't exist it will be created
        var ctr_meta = {"id": "my_atomic_counter:1" };
        var result = couchbase.increment(src_bkt,ctr_meta);
        if (result.success) {
            log('success adv. increment: result',result);
        } else {
            log('failure adv. increment: id',ctr_meta.id,'result',result);
        }
    }
    UPSERT INTO `source` (KEY,VALUE)
      VALUES ( "inc:1",  {"somekey":"someValue"} ),
      VALUES ( "inc:2",  {"somekey":"someValue"} ),
      VALUES ( "inc:3",  {"somekey":"someValue"} ),
      VALUES ( "inc:4",  {"somekey":"someValue"} ),
      VALUES ( "inc:5",  {"somekey":"someValue"} ),
      VALUES ( "inc:6",  {"somekey":"someValue"} ),
      VALUES ( "inc:7",  {"somekey":"someValue"} ),
      VALUES ( "inc:8",  {"somekey":"someValue"} ),
      VALUES ( "inc:9",  {"somekey":"someValue"} ),
      VALUES ( "inc:10", {"somekey":"someValue"} );

    We insert ten (10) documents and count the mutations

    KEY: my_atomic_counter:1
    
    {
      "count": 10
    }
    2021-01-08T12:30:59.910-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":1},"meta":{"id":"my_atomic_counter:1","cas":"1610137859908960256"},"success":true}
    
    2021-01-08T12:30:59.910-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":2},"meta":{"id":"my_atomic_counter:1","cas":"1610137859909222400"},"success":true}
    
    2021-01-08T12:30:59.911-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":3},"meta":{"id":"my_atomic_counter:1","cas":"1610137859911385088"},"success":true}
    
    2021-01-08T12:30:59.931-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":4},"meta":{"id":"my_atomic_counter:1","cas":"1610137859929997312"},"success":true}
    
    2021-01-08T12:30:59.931-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":5},"meta":{"id":"my_atomic_counter:1","cas":"1610137859930193920"},"success":true}
    
    2021-01-08T12:30:59.932-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":6},"meta":{"id":"my_atomic_counter:1","cas":"1610137859932160000"},"success":true}
    
    2021-01-08T12:30:59.932-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":7},"meta":{"id":"my_atomic_counter:1","cas":"1610137859932356608"},"success":true}
    
    2021-01-08T12:30:59.948-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":8},"meta":{"id":"my_atomic_counter:1","cas":"1610137859946381312"},"success":true}
    
    2021-01-08T12:30:59.948-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":9},"meta":{"id":"my_atomic_counter:1","cas":"1610137859946708992"},"success":true}
    
    2021-01-08T12:30:59.948-08:00 [INFO] "success adv. increment: result"
    {"doc":{"count":10},"meta":{"id":"my_atomic_counter:1","cas":"1610137859948412928"},"success":true}