What data in a view is stored vs computed at execution?

What data in a view is stored vs computed at execution?

Hi,

I’m new to couchbase, and haven’t been able to find an answer to this in the documentation or on Google.

I’ve been told that views in couchbase are (or can be depending on configuration) updated when the underlying documents changes.

I’m wondering which parts of the view are stored and which parts are computed when the view is accessed.
From observation it seems that:
- the output of the map function is stored (might be stored depending on configuration?),
- the output of the reduce function is always computed when the view is accessed.

Consider the following examples to help illustrate what I’m trying to understand.

Generate timestamp in map

Map

function (doc, meta) {
  emit(doc.userId, (new Date()).getTime() / 1000);
}

Reduce

function(key, values, rereduce){
  var maxTimestamp = values[0];
  
  for(var i = 1; i < values.length; i++){
    var timestamp = values[i];
    if(timestamp > maxTimestamp)
      maxTimestamp = timestamp;
  }
  
  return maxTimestamp;
}

Execution (with grouping enabled):

"01e7d672-988a-4616-8916-70bb931bef2a"      1521210374.796
"0320b22b-56b8-4a31-aa27-eee7cd5dde64"      1521210374.743
"037b5ebf-85cc-4faf-8032-bac2b741975f"      1521210374.745
"043193dd-adf5-4cd0-ba3a-3d07c5f9f6d8"      1521210374.755
"0b5a9587-a747-456b-919c-58663b67862d"      1521210416.456

The output all had the same timestamp when the view was first created. Documents for user 0b5a9587-a747-456b-919c-58663b67862d had been updated after creating the view, and the above output reflects that.

Generate timestamp in reduce

Map

function (doc, meta) {
  emit(doc.userId, null);
}

Reduce

function(key, values, rereduce){
  if(!rereduce){
    return (new Date()).getTime() / 1000;
  }else{
    return Math.max(values);
  }
}

Execution (grouping enabled):

"01e7d672-988a-4616-8916-70bb931bef2a"      1521215404.078
"0320b22b-56b8-4a31-aa27-eee7cd5dde64"      1521215404.079
"037b5ebf-85cc-4faf-8032-bac2b741975f"      1521215404.079
"043193dd-adf5-4cd0-ba3a-3d07c5f9f6d8"      1521215404.079
"0b5a9587-a747-456b-919c-58663b67862d"      1521215404.079
"0e59f6a5-9a7b-4495-825f-331b9de6a7f1"      1521215404.079

… and again…

"01e7d672-988a-4616-8916-70bb931bef2a"      1521215462.945
"0320b22b-56b8-4a31-aa27-eee7cd5dde64"      1521215462.945
"037b5ebf-85cc-4faf-8032-bac2b741975f"      1521215462.945
"043193dd-adf5-4cd0-ba3a-3d07c5f9f6d8"      1521215462.945
"0b5a9587-a747-456b-919c-58663b67862d"      1521215462.945

The timestamp appears to be generated every time the view is accessed/queried.

I was hoping someone could provide some details into what’s going on here.

Here is a link to some documentation on how the MapReduce works, and how it updates.
https://developer.couchbase.com/documentation/server/5.1/architecture/incremental-map-reduce-views.html

When you are querying the views in your examples, are you viewing it using the Couchbase web UI or via an SDK? What are you setting for the “stale” parameter?