Multi query

Union of multiple query results.

YDN-DB provides method for running multiple query in parallel, allowing for executing IN query in SQL syntax.

Consider the following query, SELECT * FROM article WHERE topics IN ("object-relational mapper", "open source") LIMIT 10

Create iterator each for all element of IN condition clause.

var iter_0 = ydn.db.IndexIterator.where('article', 'topics', '=', 'object-relational mapper');
var iter_1 = ydn.db.IndexIterator.where('article', 'topics', '=', 'open source');

Create multi query function, which keep resulting primary keys and advance cursor(s) on each cursor iterator in such a way that, cursor(s) is advance for next primary key.

var keys = [];
var multiQuery = function(sec_keys, pri_keys) {
  var advance = [];
  var cmp = ydn.db.cmp(pri_keys[0], pri_keys[1]);
  if (cmp == 1) { // pri_keys[0] > pri_keys[1]
    if (keys[keys.length - 1] != pri_keys[1]) {
      keys.push(pri_keys[1]);
    }
    advance[1] = true; // advance iter_1 on step
  } else if (cmp == -1) { // pri_keys[0] < pri_keys[1]
    if (keys[keys.length - 1] != pri_keys[0]) {
      keys.push(pri_keys[0]);
    }
    advance[1] = true; // advance iter_1 on step
  } else { // pri_keys[0] == pri_keys[1]
    if (keys[keys.length - 1] != pri_keys[0]) {
      keys.push(pri_keys[0]);
    }
    advance[0] = true; // advance iter_0 on step
    advance[1] = true; // advance iter_1 on step
  }

  if (keys.length >= 10) {
    return [];
  } else {
    return advance;
  }
};

Open database if not done so. You must load the data as described in overview section.

var db = new ydn.db.Storage('nosql-query');

Here, scanning query is performed using scan method. Notice that after key scanning, function record value are retrieve by values method.

db.scan(multiQuery, [iter_0, iter_1]).done(function() {
  db.values('article', keys).done(function(values) {
    console.log(keys, values);
  })
});

Tip

Pages in this sections include the YDN-DB script, so that you follow the sample code in your browser's developer console to see in action.

We can resume the query from the previous by issue the query again. It is possible to reverse the query by changing the sing inside multiQuery function. These are patterns for pagination in ydn-db query, which does not use offset.

scan method is very general and can be combine with key joining algorithm described previously. It should be notice that multi-query is logical disjunction, OR, while key joining is logical conjunction, AND.

Negation, NOT is implemented by multiple query of open lower bound key range and open upper bound key range query. For example, the query SELECT * FROM article WHERE topics != "open source" is result of multi query of iterators ydn.db.IndexIterator.where('article', 'topics', '>', 'open source') and ydn.db.IndexIterator.where('article', 'topics', '<', 'open source').

By re-writing query into canonical normal form, combination of these queries can be executed. There are no limit in number of query, but only one range query can be in the combination. For more detail about these query see Alfred Fuller’s Next gen queries in Google I/0 2010.

Authors

Kyaw Tun