Function: Advanced DELETE operation

    +

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

    • This function advancedDeleteOp merely demonstrates the Advanced DELETE operation.

    • Requires Eventing Storage (or metadata collection) and a "source" collection.

    • Needs a Binding of type "bucket alias" (as documented in the Scriptlet).

    • Will operate on any mutation where doc.type === "control_adv_delete".

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

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

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

    • advancedDeleteOp

    • Input Data/Mutation

    • Output Data/Log

    // To run configure the settings for this Function, advancedDeleteOp, as follows:
    //
    // Version 7.0+
    //   "Listen to Location"
    //     bulk.data.source
    //   "Eventing Storage"
    //     rr100.eventing.metadata
    //   Binding(s)
    //    1. "binding type", "alias name...", "bucket.scope.collection", "Access"
    //       "bucket alias", "src_col",       "bulk.data.source",        "read and write"
    //
    // Version 6.6.1
    //   "Source Bucket"
    //     source
    //   "MetaData Bucket"
    //     metadata
    //   Binding(s)
    //    1. "binding type", "alias name...", "bucket",     "Access"
    //       "bucket alias", "src_col",       "source",     "read and write"
    
    function OnUpdate(doc, meta) {
        if (!meta.id.startsWith("control_adv_delete")) return;
    
        log('input meta', meta);
        log('input doc ', doc);
    
        // Setup, make sure we have our doc to "delete", ignore any errors
        couchbase.insert(src_col,{"id":"test_adv_delete:" + 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_delete:" + doc.ins_id};
        }
        if (doc.mode && doc.mode === "bad_cas") {
            // Here we pass a non-matching CAS it will always fail
            new_meta = {"id":"test_adv_delete:" + 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_col,{"id":"test_adv_delete:" + doc.ins_id});
            if (tmp_r.success) {
                // Here we use the current CAS just read via couchbase.get(...)
                new_meta = {"id":"test_adv_delete:" + doc.ins_id, "cas": tmp_r.meta.cas};
            } else {
                log('Cannot delete due to no such key',"test_adv_delete:" + doc.ins_id);
                return;
            }
        }
        if (doc.mode && doc.mode === "no_key") {
            // Remove (delete with a basic bucket op) so that we have: no such key
            delete src_col["test_adv_delete:" + doc.ins_id]
            new_meta = {"id":"test_adv_delete:" + doc.ins_id};
        }
        var result = couchbase.delete(src_col,new_meta);
        if (result.success) {
            log('success adv. delete: result',result);
        } else {
            log('failure adv. delete: id',new_meta.id,'result',result);
        }
    }
    Mutation #1
    
    INPUT: KEY control_adv_delete::1
    
    {
      "id": 1,
      "type": "control_adv_delete",
      "ins_id": 10,
      "mode": "no_cas"
    }
    
    Mutation #2
    
    INPUT: KEY control_adv_delete::2
    
    {
      "id": 2,
      "type": "control_adv_delete",
      "ins_id": 10,
      "mode": "bad_cas"
    }
    
    Mutation #3
    
    INPUT: KEY control_adv_delete::3
    
    {
      "id": 3,
      "type": "control_adv_delete",
      "ins_id": 10,
      "mode": "no_key"
    }
    
    Mutation #4
    
    INPUT: KEY control_adv_delete::4
    
    {
      "id": 4,
      "type": "control_adv_delete",
      "ins_id": 10,
      "mode": "good_cas"
    }

    We do four (4) deletion attempts the second fails due to a CAS missmatch and the third fails due to no such key.

    Logs from Mutation #1
    
    2021-01-08T11:45:02.897-08:00 [INFO] "input meta"
    {
        "cas": "1610134800219308032",
        "id": "control_adv_delete::1",
        "expiration": 0,
        "flags": 33554438,
        "vb": 221,
        "seq": 1
    }
    2021-01-08T11:45:02.898-08:00 [INFO] "input doc "
    {
        "id": 1,
        "type": "control_adv_delete",
        "ins_id": 10,
        "mode": "no_cas"
    }
    2021-01-08T11:45:02.899-08:00 [INFO] "success adv. delete: result"
    {
        "meta": {
            "id": "test_adv_delete:10",
            "cas": "1610135102898962432"
        },
        "success": true
    }
    
    Logs from Mutation #2
    
    2021-01-08T11:46:11.225-08:00 [INFO] "input meta"
    {
        "cas": "1610135171148152832",
        "id": "control_adv_delete::2",
        "expiration": 0,
        "flags": 33554438,
        "vb": 468,
        "seq": 3
    }
    2021-01-08T11:46:11.225-08:00 [INFO] "input doc "
    {
        "id": 2,
        "type": "control_adv_delete",
        "ins_id": 10,
        "mode": "bad_cas"
    }
    2021-01-08T11:46:11.228-08:00 [INFO] "failure adv. delete: id" "test_adv_delete: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-08T11:52:51.520-08:00 [INFO] "input meta"
    {
        "cas": "1610135571485425664",
        "id": "control_adv_delete::3",
        "expiration": 0,
        "flags": 33554438,
        "vb": 723,
        "seq": 5
    }
    2021-01-08T11:52:51.520-08:00 [INFO] "input doc "
    {
        "id": 3,
        "type": "control_adv_delete",
        "ins_id": 10,
        "mode": "no_key"
    }
    2021-01-08T11:52:51.522-08:00 [INFO] "failure adv. delete: id" "test_adv_delete:10" "result"
    {
        "error": {
            "code": 272,
            "name": "LCB_KEY_ENOENT",
            "desc": "The document key does not exist on the server",
            "key_not_found": true
        },
        "success": false
    }
    
    Logs from Mutation #4
    
    2021-01-08T11:53:36.070-08:00 [INFO] "input meta"
    {
        "cas": "1610135616063602688",
        "id": "control_adv_delete::4",
        "expiration": 0,
        "flags": 33554438,
        "vb": 183,
        "seq": 3
    }
    2021-01-08T11:53:36.070-08:00 [INFO] "input doc "
    {
        "id": 4,
        "type": "control_adv_delete",
        "ins_id": 10,
        "mode": "good_cas"
    }
    2021-01-08T11:53:36.074-08:00 [INFO] "success adv. delete: result"
    {
        "meta": {
            "id": "test_adv_delete:10",
            "cas": "1610135616073760768"
        },
        "success": true
    }