Function: Advanced REPLACE operation

    +

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

    • This function advancedReplaceOp merely demonstrates the Advanced REPLACE 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 where doc.type === "control_adv_replace".

    • Always tries to insert the test document, any insert error will be ignored.

    • There are 3 modes of operation: no_cas, bad_cas, and good_cas.

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

    • advancedReplaceOp

    • 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("control_adv_replace")) return;
    
        log('input meta', meta);
        log('input doc ', doc);
    
        // Setup, make sure we have our doc to "replace", ignore any errors
        couchbase.insert(src_bkt,{"id":"test_adv_replace:" + doc.ins_id},{"a:": 1});
    
        var new_meta;
        if (doc.mode && doc.mode === "no_cas") {
            // Here we pass no CAS it will always succeed
            new_meta = {"id":"test_adv_replace:" + doc.ins_id};
            // optional set an expiry 60 seconds in the future
            // new_meta.expiry_date = new Date(Date.now() + 60 * 1000);
        }
        if (doc.mode && doc.mode === "bad_cas") {
            // Here we pass a non-matching CAS it will always fail
            new_meta = {"id":"test_adv_replace:" + doc.ins_id, "cas":"1111111111111111111"};
        }
        if (doc.mode && doc.mode === "good_cas") {
            // Here we will pass the matching or current CAS it will succeed
            var tmp_r = couchbase.get(src_bkt,{"id":"test_adv_replace:" + doc.ins_id});
            if (tmp_r.success) {
                // Here we use the current CAS just read via couchbase.get(...)
                new_meta = {"id":"test_adv_replace:" + doc.ins_id, "cas": tmp_r.meta.cas};
            } else {
                log('Cannot replace due to no such key',"test_adv_replace:" + doc.ins_id);
                return;
            }
        }
        var new_doc = {id: doc.ins_id, type: "test_adv_replace", random: Math.random()};
        var result = couchbase.replace(src_bkt,new_meta,new_doc);
        if (result.success) {
            log('success adv. replace: result',result);
        } else {
            log('failure adv. replace: id',new_meta.id,'result',result);
        }
    }
    Mutation #1
    
    INPUT: KEY control_adv_replace::1
    
    {
      "id": 1,
      "type": "control_adv_replace",
      "ins_id": 10,
      "mode": "no_cas"
    }
    
    Mutation #2
    
    INPUT: KEY control_adv_replace::2
    
    {
      "id": 2,
      "type": "control_adv_replace",
      "ins_id": 10,
      "mode": "bad_cas"
    }
    
    Mutation #3
    
    INPUT: KEY control_adv_replace::3
    
    {
      "id": 3,
      "type": "control_adv_replace",
      "ins_id": 10,
      "mode": "good_cas"
    }

    We try to replace three (3) documents the first and last replacements succeed but the second, fails because of a CAS missmatch.

    KEY: test_adv_replace:10
    
    {
      "id": 10,
      "type": "test_adv_replace",
      "random": 0.40223830137164085
    }
    Logs from Mutation #1
    
    2021-01-08T10:58:39.846-08:00 [INFO] "input meta"
    {
        "cas": "1610131902064820224",
        "id": "control_adv_replace::1",
        "expiration": 0,
        "flags": 33554438,
        "vb": 417,
        "seq": 4
    }
    2021-01-08T10:58:39.846-08:00 [INFO] "input doc "
    {
        "id": 1,
        "type": "control_adv_replace",
        "ins_id": 10,
        "mode": "no_cas"
    }
    2021-01-08T10:58:39.847-08:00 [INFO] "success adv. replace: result"
    {
        "meta": {
            "id": "test_adv_replace:10",
            "cas": "1610132319847055360"
        },
        "success": true
    }
    
    Logs from Mutation #2
    
    2021-01-08T10:59:04.151-08:00 [INFO] "input meta"
    {
        "cas": "1610132344113397760",
        "id": "control_adv_replace::2",
        "expiration": 0,
        "flags": 33554438,
        "vb": 168,
        "seq": 3
    }
    2021-01-08T10:59:04.151-08:00 [INFO] "input doc "
    {
        "id": 2,
        "type": "control_adv_replace",
        "ins_id": 10,
        "mode": "bad_cas"
    }
    2021-01-08T10:59:04.154-08:00 [INFO] "failure adv. replace: id" "test_adv_replace:10" "result"
    {
        "error": {
            "code": 272,
            "name": "LCB_KEY_EEXISTS",
            "desc": "The document key exists with a CAS value different than specified",
            "cas_mismatch": true
        },
        "success": false
    }
    
    Logs from Mutation #3
    
    2021-01-08T10:59:35.692-08:00 [INFO] "input meta"
    {
        "cas": "1610132375634706432",
        "id": "control_adv_replace::3",
        "expiration": 0,
        "flags": 33554438,
        "vb": 943,
        "seq": 3
    }
    2021-01-08T10:59:35.692-08:00 [INFO] "input doc "
    {
        "id": 3,
        "type": "control_adv_replace",
        "ins_id": 10,
        "mode": "good_cas"
    }
    2021-01-08T10:59:35.696-08:00 [INFO] "success adv. replace: result"
    {
        "meta": {
            "id": "test_adv_replace:10",
            "cas": "1610132375695589376"
        },
        "success": true
    }