Skip to content

Commit 8fd1953

Browse files
authored
[DOC-11974] Added 4 new scriptlet pages and updated Eventing Examples page (Jon's feedback in #157) (#161)
* Added Jon's new files to this new PR * Edited existing pages to match style and format of new pages * Added new examples to page and updated formatting and language * Updated nav file with links to new function files * Updated nav page order * More updates * Revert "More updates" This reverts commit faa5a2a. * Deleted nav partial from this PR as I already have the file sitting in a different PR
1 parent 9ecdaea commit 8fd1953

16 files changed

+821
-263
lines changed
Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
= Examples: Using the Eventing Service
2-
:description: This page contains examples of how to use the Eventing Service, using the Couchbase Web Console.
2+
:description: This page contains examples of how to use the Eventing Service with the Couchbase Web Console.
33
:page-edition: Enterprise Edition
44

55
+++ <!-- Couchbase Eventing Examples --> +++
66
[abstract]
77
{description}
88

99
[#examples-step-by-step]
10-
== Step by Step Examples
10+
== Step-by-Step Examples
1111

1212
[#Couchbase-Eventing-Examples]
13-
*Detailed Examples*: These tutorial-like guides are ideal for a novice to learn the basics of the Eventing Service, via complete detailed step by step start-to-finish instructions.
13+
=== Detailed Examples
1414

15-
[cols="1,1,1"]
15+
The following tutorial-like guides have detailed start-to-finish instructions and are ideal for new users to learn the basics of the Eventing Service.
16+
17+
[cols="1,1,1,1"]
1618
|===
1719
| xref:eventing:eventing-example-data-enrichment.adoc[Data Enrichment]
1820
| xref:eventing:eventing-examples-cascade-delete.adoc[Cascade Delete]
@@ -22,18 +24,23 @@
2224
| xref:eventing:eventing-examples-cancel-overwrite-timer.adoc[Cancel or Overwrite Timer]
2325
| xref:eventing:eventing-examples-recurring-timer.adoc[Recurring Timer]
2426
| xref:eventing:eventing-examples-rest-via-curl-get.adoc[External REST via cURL GET]
25-
| xref:eventing:eventing-examples-high-risk.adoc[Risk Assessment]
27+
| xref:eventing:eventing-examples-high-risk.adoc[Risk Assessment]
28+
|
29+
|
30+
|
2631
|===
2732

2833
[#examples-scriptlets]
29-
== Scriptlets or Terse Examples
34+
== Scriptlets
3035

3136
[#Couchbase-Eventing-Scriptlets]
3237
[#examples-scriptlets-kv]
33-
*Basic KV Eventing Functions*: The following Scriptlets are essentially stand alone Eventing Functions examples, and introduce more use cases. Here we assume the reader has a good understanding of the Eventing System and requires little guidance.
38+
=== Basic Accessor Eventing Functions
39+
40+
The following scriptlets are examples of standadlone Eventing Functions.
3441

35-
[#Couchbase-Eventing-Snipets]
36-
[cols="1,1,1"]
42+
[#Couchbase-Eventing-Snippets]
43+
[cols="1,1,1,1"]
3744
|===
3845
| xref:eventing:eventing-handler-basicBucketOps.adoc[basicBucketOps]
3946
| xref:eventing:eventing-handler-curl-get.adoc[basicCurlGet]
@@ -46,23 +53,28 @@
4653
| xref:eventing:eventing-handler-keepLastN.adoc[keepLastN]
4754
| xref:eventing:eventing-handler-docControlledSelfExpiry.adoc[docControlledSelfExpiry]
4855
| xref:eventing:eventing-handler-shippingNotifier.adoc[shippingNotifier]
49-
| xref:eventing:eventing-handler-ConvertBucketToCollections.adoc[ConvertBucketToCollections]
56+
| xref:eventing:eventing-handler-ConvertBucketToCollections.adoc[convertBucketToCollections]
5057
|===
5158

5259
[#examples-scriptlets-n1ql]
53-
*Basic {sqlpp} Eventing Functions*: The following Scriptlets demonstrate using {sqlpp} or the Query Service from within an Eventing Function.
60+
=== Basic {sqlpp} Eventing Functions
5461

55-
[cols="1,1,1"]
62+
The following scriptlets demonstrate how to use {sqlpp} or the Query Service with an Eventing Function.
63+
64+
[cols="1,1,1,1"]
5665
|===
5766
| xref:eventing:eventing-handler-basicN1qlSelectStmt.adoc[basicN1qlSelectStmt]
5867
| xref:eventing:eventing-handler-basicN1qlPreparedSelectStmt.adoc[basicN1qlPreparedSelectStmt]
5968
|
69+
|
6070
|===
6171

6272
[#examples-scriptlets-generic]
63-
*Generic Manipulation Eventing Functions* The following Scriptlets are more advanced use cases which focus on mutating documents without knowledge of the document's schema.
73+
=== Generic Manipulation Eventing Functions
6474

65-
[cols="1,1,1"]
75+
The following scriptlets are examples of advanced use cases that focus on mutating a document without knowing that document's schema.
76+
77+
[cols="1,1,1,1"]
6678
|===
6779
| xref:eventing:eventing-handler-dateToEpochConversion.adoc[dateToEpochConversion]
6880
| xref:eventing:eventing-handler-deepCloneAndModify.adoc[deepCloneAndModify]
@@ -72,13 +84,18 @@
7284
| xref:eventing:eventing-handler-genericFlatten.adoc[genericFlatten]
7385
| xref:eventing:eventing-handler-convertXMLtoJSON.adoc[convertXMLtoJSON]
7486
| xref:eventing:eventing-handler-convertAdvXMLtoJSON.adoc[convertAdvXMLtoJSON]
75-
|
7687
|===
7788

7889
[#examples-scriptlets-advanced-accessors]
79-
*Advanced Accessor Eventing Functions*: The following Scriptlets demonstrate using Advanced Bucket Accessors (introduced in version 6.6.1) which allow the use of CAS, ability to set expirations (or TTLs) and the use of distributed atomic counters to increment or decrement counts.
90+
=== Advanced Accessor Eventing Functions
8091

81-
[cols="1,1,1"]
92+
The following scriptlets demonstrate how to use Advanced Keyspace Accessors, which allow you to:
93+
94+
* Use CAS
95+
* Set expiry (TTL) dates
96+
* Use distributed atomic counters to increment and decrement counts
97+
98+
[cols="1,1,1,1"]
8299
|===
83100
| xref:eventing:eventing-handler-advancedGetOp.adoc[advancedGetOp]
84101
| xref:eventing:eventing-handler-advancedGetOpWithCache.adoc[advancedGetOpWithCache]
@@ -88,30 +105,42 @@
88105
| xref:eventing:eventing-handler-advancedDeleteOp.adoc[advancedDeleteOp]
89106
| xref:eventing:eventing-handler-advancedIncrementOp.adoc[advancedIncrementOp]
90107
| xref:eventing:eventing-handler-advancedDecrementOp.adoc[advancedDecrementOp]
108+
| xref:eventing:eventing-handler-advancedTouchOp.adoc[advancedTouchOp]
91109
| xref:eventing:eventing-handler-advanced-keepLastN.adoc[advancedKeepLastN]
92110
| xref:eventing:eventing-handler-advanced-docControlledSelfExpiry.adoc[advancedDocControlledSelfExpiry]
93111
| xref:eventing:eventing-handler-multiCollectionEventing.adoc[multiCollectionEventing]
112+
| xref:eventing:eventing-handler-advancedSelfRecursion.adoc[advancedSelfRecursion]
113+
| xref:eventing:eventing-handler-advancedMutateInField.adoc[advancedMutateInField]
114+
| xref:eventing:eventing-handler-advancedMutateInArray.adoc[advancedMutateInArray]
94115
|
95116
|===
96117

97118
[#examples-scriptlets-binary-documents]
98-
*Binary Document Support*: The following Scriptlets demonstrate support for binary documents in Eventing. Only a Function with “language compatibility” of 6.6.2 or above in its settings will pass binary documents to the OnUpdate(doc,meta) handler.
119+
=== Binary Document Support
120+
121+
The following scriptlets demonstrate support for binary documents in Eventing.
122+
123+
Your Eventing Function must have a language compatibility setting of Couchbase Server version 6.6.2 or above to pass binary documents in its `OnUpdate(doc,meta)` handler.
99124

100-
[cols="1,1,1"]
125+
[cols="1,1,1,1"]
101126
|===
102127
| xref:eventing:eventing-handler-basicBinaryKV.adoc[basicBinaryKV]
103128
| xref:eventing:eventing-handler-advancedBinaryKV.adoc[advancedBinaryKV]
104129
|
130+
|
105131
|===
106132

107133
[#examples-scriptlets-performance]
108-
*Performance Eventing Functions* The following Scriptlets are performance oriented and/or benchmarks.
134+
=== Performance Eventing Functions
135+
136+
The following scriptlets are examples of performance-oriented or benchmark Eventing Functions.
109137

110-
[cols="1,1,1"]
138+
[cols="1,1,1,1"]
111139
|===
112140
| xref:eventing:eventing-handler-fasterToLocalString.adoc[fasterToLocalString]
113141
|
114142
|
143+
|
115144
|===
116145

117-
+++ <!-- Couchbase Eventing Examples --> +++
146+
+++ <!-- Couchbase Eventing Examples --> +++

modules/eventing/pages/eventing-handler-advanced-docControlledSelfExpiry.adoc

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,46 @@
11
= Function: Advanced Document Controlled Expiry
2-
:description: pass:q[Purge a document automatically based on self-contained start and duration fields.]
2+
:description: pass:q[Purge a document automatically based on the document's self-contained start and duration fields.]
33
:page-edition: Enterprise Edition
44
:tabs:
55

6-
*Goal*: {description}
7-
8-
* This function *advancedDocControlledSelfExpiry* demonstrates self-expiry of a document; for example, a user trial.
9-
* Requires Eventing Storage (or metadata collection) and a "source" collection.
10-
* Needs a Binding of type "bucket alias" (as documented in the Scriptlet).
11-
* When documents are created, they will have no expiration value. This function processes the initial mutation to calculate and set the proper TTL.
12-
* In Couchbase, when using a simple integer expiry value (as opposed to a proper date or time object), the expiration can be specified in two ways:
13-
** As an offset from the current time. If the absolute value of the expiry is less than 30 days (60 * 60 * 24 * 30 seconds), it is considered an offset.
14-
** As an absolute Unix time stamp. If the value is greater than 30 days (60 * 60 * 24 * 30 seconds), it is considered an absolute time stamp.
15-
** As described in xref:learn:data/expiration.adoc[Expiration], if a "Bucket Max Time-To-Live" is set (specified in seconds), it is an enforced hard upper limit. As such, any subsequent document mutation (by {sqlpp}, Eventing, or any Couchbase SDK) will result in the document having its expiration adjusted and set to the bucket’s maximum TTL if the operation has:
16-
*** No TTL.
17-
*** A TTL of zero.
18-
*** A TTL greater than the bucket TTL.
19-
* As we are using Advanced Bucket Accessors setting document expirations (or TTLs) we use a JavaScript Data object.
20-
* Will operate on any document with type == "trial_customers".
21-
* Will ignore any doc with a non-zero TTL
22-
* This is different than setting a TTL on a bucket or a collection which will typically update (or extend) the TTL of a document on each mutation.
6+
{description}
7+
8+
The `advancedDocControlledSelfExpiry` function:
9+
10+
* Demonstrates the self-expiry of a document (for example, a user trial)
11+
* Requires Eventing Storage (or a metadata collection) and a source collection
12+
* Requires a binding of type `bucket alias`
13+
* Processes the initial mutation to calculate and set the TTL of a newly-created document
14+
* Uses a JavaScript data object to set document expiration
15+
* Operates on any document where `type == "trial_custoimers"`
16+
* Ignores any document with a TTL that is not zero
17+
18+
When you use a simple integer instead of a proper date or time object for your document's expiration value, the expiration value is specified in one of the following ways:
19+
20+
* As an offset from the current time if the value is less than 30 days (60 * 60 * 24 * 30 seconds).
21+
* As an absolute Unix time stamp if the value is greater than 30 days (60 * 60 * 24 * 30 seconds).
22+
23+
If a `Bucket Max Time-to-Live` is set and specified in seconds, it's enforced as a hard upper limit.
24+
Any subsequent document mutation, whether by {sqlpp}, Eventing, or a Couchbase SDK, results in the document having its expiration adjusted and set to the bucket's maximum TTL if the operation has:
25+
26+
* No TTL
27+
* A TTL of zero
28+
* A TTL greater than the bucket's TTL
2329
2430
[{tabs}]
2531
====
2632
advancedDocControlledSelfExpiry::
2733
+
2834
--
29-
Two variants of this function are available - a 6.6 version that relies on {sqlpp} and a 6.6.1+/7.0.0+ version (*this Function*) that directly sets the expiration.
30-
You can completely avoid _N1QL(...)_ and use _couchbase.replace(bucket_binding, meta, doc)_ as the advancedDocControlledSelfExpiry variant is much faster.
35+
There are two variants of this function available: a xref:eventing-handler-docControlledSelfExpiry.adoc[Couchbase Server version 6.6 that relies on {sqlpp}], and a Couchbase Server version 6.6.1+/7.0.0+ that directly sets the expiration.
3136

32-
* xref:eventing-handler-docControlledSelfExpiry.adoc[docControlledSelfExpiry (indirect TTL via {sqlpp})]
33-
* <<advancedDocControlledSelfExpiry,advancedDocControlledSelfExpiry (direct TTL)>>
37+
You can improve your function's performance by avoiding N1QL() and using `couchbase.replace(bucket_binding, meta, doc)` instead.
3438

35-
[#advancedDocControlledSelfExpiry]
36-
advancedDocControlledSelfExpiry (direct TTL)
39+
The following example directly sets the expiration.
3740

3841
[source,javascript]
3942
----
40-
// To run configure the settings for this Function, advancedDocControlledSelfExpiry, as follows:
43+
// Configure the settings for the advancedDocControlledSelfExpiry function as follows:
4144
//
4245
// Version 7.1+
4346
// "Function Scope"
@@ -61,44 +64,44 @@ advancedDocControlledSelfExpiry (direct TTL)
6164
// "bucket alias", "src_col", "source", "read and write"
6265
6366
function OnUpdate(doc, meta) {
64-
// Filter items that don't have been updated
67+
// Filter items that have not been updated
6568
if (meta.expiration !== 0) {
6669
log(meta.id, "IGNORE expiration "+meta.expiration+" !== 0 or "+
6770
new Date(meta.expiration).toString());
6871
return;
6972
}
7073
71-
// Optional filter to a specic field like 'type'
74+
// Optional filter to a specific field like 'type'
7275
if (doc.type !== 'trial_customers') return;
7376
74-
// Our expiry is based on a JavaScript date parsable field, it must exist
77+
// The expiry is based on a JavaScript date parsable field
7578
if (!doc.trialStartDate || !doc.trialDurationDays) return;
7679
77-
// Convert the doc's field timeStamp and convert to unix epoch time (in ms.).
80+
// Convert the doc field timeStamp to Unix epoch time in milliseconds
7881
var docTimeStampMs = Date.parse(doc.trialStartDate);
7982
8083
var keepDocForMs = doc.trialDurationDays * 1000 * 60 * 60 * 24 ;
81-
var nowMs = Date.now(); // get current unix time (in ms.).
84+
var nowMs = Date.now(); // Get current Unix time in milliseconds
8285
83-
// Archive if we have kept it for too long no need to set the expiration
86+
// Archive if it has been kept for too long; you do not need to set an expiration
8487
if( nowMs >= (docTimeStampMs + keepDocForMs) ) {
8588
86-
// Delete the document form the source collection via the map alias
89+
// Delete the document from the source collection through the map alias
8790
delete src_col[meta.id];
8891
8992
log(meta.id, "DELETE from src_col to dst_bkt alias as our expiration " +
9093
new Date(docTimeStampMs + keepDocForMs).toString()) + " is already past";
9194
} else {
9295
var key = meta.id;
93-
//set the meta.expiration=ttlMs
96+
// Set the meta.expiration=ttlMs
9497
var ttlMs = docTimeStampMs + keepDocForMs;
9598
9699
if (ttlMs !== 0) {
97100
log(meta.id, "UPDATE expiration "+meta.expiration+" === 0 set to "+
98101
ttlMs+" or " + new Date(ttlMs).toString());
99102
// Advanced Bucket Accessors use JavaScript Date objects
100103
var expiryDate = new Date(ttlMs);
101-
// This is 4X to 5X faster than using N1QL(...) and no need to worry about recursion.
104+
// This is 4X to 5X faster than using N1QL(...) and you do not need to worry about recursion
102105
var res = couchbase.replace(src_col,{"id":meta.id,"expiry_date":expiryDate},doc);
103106
if (!res.success) {
104107
log(meta.id,'Setting TTL to',expiryDate,'failed',res);
@@ -109,13 +112,13 @@ function OnUpdate(doc, meta) {
109112
----
110113
--
111114

112-
Input Data/Mutation::
115+
Input data::
113116
+
114117
--
118+
Create a test set of 4 documents using the Query Editor to insert the data items.
119+
You do not need an Index.
115120

116-
We want to create a test set of four (4) documents, use the Query Editor to insert the the data items (you do not need an index).
117-
118-
Note, if the today is past 08-25-2021 (MM-DD-YYYY) just change the `trialStartDate` for the last two records to at least 90 days from now.
121+
If today's date is past 08-25-2021 (MM-DD-YYYY), you can change the `trialStartDate` for the last two records to at least 90 days from today.
119122

120123
[source,sqlpp]
121124
----
@@ -154,7 +157,7 @@ Note, if the today is past 08-25-2021 (MM-DD-YYYY) just change the `trialStartDa
154157
----
155158
--
156159

157-
Output Data/Mutation::
160+
Output data::
158161
+
159162
--
160163
[source,json]
@@ -179,7 +182,7 @@ NEW/OUTPUT: KEY trial_customers::3
179182
"type": "trial_customers"
180183
}
181184

182-
We end up with two (2) of the four documents (obviously you may need to adjust the {sqlpp} INSERT in a few months as all the document would be immediately deleted).
185+
Returns 2 of the 4 documnents.
183186

184187
* "trial_customers::0" was deleted
185188
* "trial_customers::1" was deleted
@@ -188,4 +191,4 @@ We end up with two (2) of the four documents (obviously you may need to adjust t
188191
189192
----
190193
--
191-
====
194+
====

0 commit comments

Comments
 (0)