Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for computed fields #8

Closed
chetanmeh opened this issue Feb 9, 2018 · 1 comment
Closed

Add support for computed fields #8

chetanmeh opened this issue Feb 9, 2018 · 1 comment

Comments

@chetanmeh
Copy link
Owner

chetanmeh commented Feb 9, 2018

Some of the CouchDB views use computed fields for query evaluation. For e.g. view for rules computes a root namespace when namespace is a multi segment path

function (doc) {
  var PATHSEP = "/";
  var isRule = function (doc) {  return (doc.trigger !== undefined) };
  if (isRule(doc)) try {
    var ns = doc.namespace.split(PATHSEP);
    var root = ns[0];
    emit([doc.namespace, doc.updated], 1);
    if (root !== doc.namespace) {
      emit([root, doc.updated], 1);
    }
  } catch (e) {}
}

For supporting such cases in non CouchDB store we would introduce a sub document under _computed key which would be used to store such fields. This would allow us to created suitable matching indexed and also perform query against such fields

Required for Query Support #1

chetanmeh added a commit that referenced this issue Feb 9, 2018
Introduce `DocumentHandler` abstraction which generates the computed
sub document depending on input document type
chetanmeh added a commit that referenced this issue Feb 9, 2018
Integrate `DocumentHandler` with `MongoDbStore`. The computed fields
are stored under `_computed` key
@chetanmeh
Copy link
Owner Author

chetanmeh commented Feb 9, 2018

Currently the computed fields are supported for following cases

Whisks

For entities stored in whisk collection following fields are computed

rootns

Root name space. This field is only generated when namespace is a path with more than 1 element

function (doc) {
  var PATHSEP = "/";
  var isRule = function (doc) {  return (doc.trigger !== undefined) };
  if (isRule(doc)) try {
    var ns = doc.namespace.split(PATHSEP);
    var root = ns[0];
    emit([doc.namespace, doc.updated], 1);
    if (root !== doc.namespace) {
      emit([root, doc.updated], 1);
    }
  } catch (e) {}
}

Activations

For activations following fields are computed

nspath

The nspath is computed based on logic in whisks-filters.v2.1.0/activations

function (doc) {
  var PATHSEP = "/";
  var isActivation = function (doc) { return (doc.activationId !== undefined) };
  var summarize = function (doc) {
    var endtime = doc.end !== 0 ? doc.end : undefined;
    return {
        namespace: doc.namespace,
         ...
      }
  };

  var pathFilter = function(doc) {
    for (i = 0; i < doc.annotations.length; i++) {
      var a = doc.annotations[i];
      if (a.key == "path") try {
          var p = a.value.split(PATHSEP);
          if (p.length == 3) {
            return p[1] + PATHSEP + doc.name;
          } else return doc.name;
      } catch (e) {
        return doc.name;
      }
    }
    return doc.name;
  }

  if (isActivation(doc)) try {
    var value = summarize(doc)
    emit([doc.namespace+PATHSEP+pathFilter(doc), doc.start], value);
  } catch (e) {}
}

deleteLogs

This field is computed based on logic in logCleanup/byDateWithLogs i.e. flag would enabled for all such activations which are not sequence

function (doc) {
  if (doc.activationId !== undefined && doc.logs && doc.logs.length > 0) try {
    var deleteLogs = true;
    for (i = 0; i < doc.annotations.length; i++) {
      var a = doc.annotations[i];
      if (a.key == "kind") {
        deleteLogs = a.value != "sequence";
        break;
      }
    }
    if (deleteLogs) {
      emit(doc.start, doc._id);
    }
  } catch (e) {}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant